Skip to content

Commit b904d0f

Browse files
committed
Address stylistic suggestions in VariableTypeCollector
Rename VariableTypeCollector::getTypeOffsets to ::getTypeOffset and only return the start offset, since the end offset is no longer needed (the string is null-terminated). Also improve variable naming slightly.
1 parent 1a844ef commit b904d0f

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

include/swift/Sema/IDETypeChecking.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ namespace swift {
225225
};
226226

227227
/// Collect type information for every variable declaration in \c SF
228-
/// within the given range.
228+
/// within the given range into \c VariableTypeInfos.
229229
/// All types will be printed to \c OS and the type offsets of the
230230
/// \c VariableTypeInfos will index into the string that backs this
231231
/// stream.
232232
void collectVariableType(SourceFile &SF, SourceRange Range,
233-
std::vector<VariableTypeInfo> &Scratch,
233+
std::vector<VariableTypeInfo> &VariableTypeInfos,
234234
llvm::raw_ostream &OS);
235235

236236
/// FIXME: All of the below goes away once CallExpr directly stores its

lib/IDE/IDETypeChecking.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ swift::collectExpressionType(SourceFile &SF,
735735
/// declaration.
736736
class VariableTypeCollector : public SourceEntityWalker {
737737
private:
738-
SourceManager &SM;
738+
const SourceManager &SM;
739739
unsigned int BufferId;
740740

741741
/// The range in which variable types are to be collected.
@@ -746,23 +746,23 @@ class VariableTypeCollector : public SourceEntityWalker {
746746

747747
/// We print all types into a single output stream (e.g. into a string buffer)
748748
/// and provide offsets into this string buffer to describe individual types,
749-
/// i.e. `OS` builds a string that contains all null-terminated printed type
749+
/// i.e. \c OS builds a string that contains all null-terminated printed type
750750
/// strings. When referring to one of these types, we can use the offsets at
751-
/// which it starts in the `OS`.
751+
/// which it starts in the \c OS.
752752
llvm::raw_ostream &OS;
753753

754-
/// Map from a printed type to the offset in OS where the type starts.
754+
/// Map from a printed type to the offset in \c OS where the type starts.
755755
llvm::StringMap<uint32_t> TypeOffsets;
756756

757-
/// Returns the start and end offset of this string in `OS`. If `PrintedType`
758-
/// hasn't been printed to `OS` yet, this function will do so.
759-
std::pair<uint32_t, uint32_t> getTypeOffsets(StringRef PrintedType) {
757+
/// Returns the start offset of this string in \c OS. If \c PrintedType
758+
/// hasn't been printed to \c OS yet, this function will do so.
759+
uint32_t getTypeOffset(StringRef PrintedType) {
760760
auto It = TypeOffsets.find(PrintedType);
761761
if (It == TypeOffsets.end()) {
762762
TypeOffsets[PrintedType] = OS.tell();
763763
OS << PrintedType << '\0';
764764
}
765-
return {TypeOffsets[PrintedType], PrintedType.size()};
765+
return TypeOffsets[PrintedType];
766766
}
767767

768768
/// Checks whether the given range overlaps the total range in which we
@@ -772,44 +772,44 @@ class VariableTypeCollector : public SourceEntityWalker {
772772
}
773773

774774
public:
775-
VariableTypeCollector(SourceFile &SF, SourceRange Range,
775+
VariableTypeCollector(const SourceFile &SF, SourceRange Range,
776776
std::vector<VariableTypeInfo> &Results,
777777
llvm::raw_ostream &OS)
778778
: SM(SF.getASTContext().SourceMgr), BufferId(*SF.getBufferID()),
779779
TotalRange(Range), Results(Results), OS(OS) {}
780780

781-
bool walkToDeclPre(Decl *D, CharSourceRange DeclRange) override {
782-
if (DeclRange.isInvalid()) {
783-
return false;
781+
bool walkToDeclPre(Decl *D, CharSourceRange DeclNameRange) override {
782+
if (DeclNameRange.isInvalid()) {
783+
return true;
784784
}
785785
// Skip this declaration and its subtree if outside the range
786786
if (!overlapsTotalRange(D->getSourceRange())) {
787787
return false;
788788
}
789789
if (auto VD = dyn_cast<VarDecl>(D)) {
790790
unsigned VarOffset =
791-
SM.getLocOffsetInBuffer(DeclRange.getStart(), BufferId);
792-
unsigned VarLength = DeclRange.getByteLength();
791+
SM.getLocOffsetInBuffer(DeclNameRange.getStart(), BufferId);
792+
unsigned VarLength = DeclNameRange.getByteLength();
793793
// Print the type to a temporary buffer
794794
SmallString<64> Buffer;
795795
{
796796
llvm::raw_svector_ostream OS(Buffer);
797797
PrintOptions Options;
798798
Options.SynthesizeSugarOnTypes = true;
799799
auto Ty = VD->getType();
800-
// Skip this declaration if the type is an error type.
800+
// Skip this declaration and its children if the type is an error type.
801801
if (Ty->is<ErrorType>()) {
802802
return false;
803803
}
804804
Ty->print(OS, Options);
805805
}
806-
// Transfer the type to `OS` if needed and get the offsets of this string
806+
// Transfer the type to `OS` if needed and get the offset of this string
807807
// in `OS`.
808-
auto TyOffsets = getTypeOffsets(Buffer.str());
808+
auto TyOffset = getTypeOffset(Buffer.str());
809809
bool HasExplicitType =
810810
VD->getTypeReprOrParentPatternTypeRepr() != nullptr;
811811
// Add the type information to the result list.
812-
Results.emplace_back(VarOffset, VarLength, HasExplicitType, TyOffsets.first);
812+
Results.emplace_back(VarOffset, VarLength, HasExplicitType, TyOffset);
813813
}
814814
return true;
815815
}
@@ -835,10 +835,10 @@ VariableTypeInfo::VariableTypeInfo(uint32_t Offset, uint32_t Length,
835835
: Offset(Offset), Length(Length), HasExplicitType(HasExplicitType),
836836
TypeOffset(TypeOffset) {}
837837

838-
void swift::collectVariableType(SourceFile &SF, SourceRange Range,
839-
std::vector<VariableTypeInfo> &Scratch,
840-
llvm::raw_ostream &OS) {
841-
VariableTypeCollector Walker(SF, Range, Scratch, OS);
838+
void swift::collectVariableType(
839+
SourceFile &SF, SourceRange Range,
840+
std::vector<VariableTypeInfo> &VariableTypeInfos, llvm::raw_ostream &OS) {
841+
VariableTypeCollector Walker(SF, Range, VariableTypeInfos, OS);
842842
Walker.walk(SF);
843843
}
844844

0 commit comments

Comments
 (0)