@@ -738,11 +738,8 @@ class VariableTypeCollector : public SourceEntityWalker {
738
738
SourceManager &SM;
739
739
unsigned int BufferId;
740
740
741
- // / The start offset of the range in which variable types are to be collected.
742
- Optional<unsigned > TotalOffset;
743
-
744
- // / The length of the range in which variable types are to be collected.
745
- Optional<unsigned > TotalLength;
741
+ // / The range in which variable types are to be collected.
742
+ SourceRange TotalRange;
746
743
747
744
// / The output vector for VariableTypeInfos emitted during traversal.
748
745
std::vector<VariableTypeInfo> &Results;
@@ -768,26 +765,31 @@ class VariableTypeCollector : public SourceEntityWalker {
768
765
return {TypeOffsets[PrintedType], PrintedType.size ()};
769
766
}
770
767
768
+ // / Checks whether the given range overlaps the total range in which we
769
+ // / collect variable types.
770
+ bool overlapsTotalRange (SourceRange Range) {
771
+ return TotalRange.isInvalid () || Range.overlaps (TotalRange);
772
+ }
773
+
771
774
public:
772
- VariableTypeCollector (SourceFile &SF, Optional<unsigned > Offset,
773
- Optional<unsigned > Length,
775
+ VariableTypeCollector (SourceFile &SF, SourceRange Range,
774
776
std::vector<VariableTypeInfo> &Results,
775
777
llvm::raw_ostream &OS)
776
778
: SM(SF.getASTContext().SourceMgr), BufferId(*SF.getBufferID()),
777
- TotalOffset (Offset), TotalLength(Length ), Results(Results), OS(OS) {}
779
+ TotalRange (Range ), Results(Results), OS(OS) {}
778
780
779
- bool walkToDeclPre (Decl *D, CharSourceRange Range ) override {
780
- if (Range .isInvalid ()) {
781
+ bool walkToDeclPre (Decl *D, CharSourceRange DeclRange ) override {
782
+ if (DeclRange .isInvalid ()) {
781
783
return false ;
782
784
}
783
- unsigned Offset = SM.getLocOffsetInBuffer (Range.getStart (), BufferId);
784
- unsigned Length = Range.getByteLength ();
785
785
// Skip this declaration and its subtree if outside the range
786
- if (TotalOffset.hasValue () && TotalLength.hasValue () &&
787
- (Offset < *TotalOffset || Offset >= (*TotalOffset + *TotalLength))) {
786
+ if (!overlapsTotalRange (D->getSourceRange ())) {
788
787
return false ;
789
788
}
790
789
if (auto VD = dyn_cast<VarDecl>(D)) {
790
+ unsigned VarOffset =
791
+ SM.getLocOffsetInBuffer (DeclRange.getStart (), BufferId);
792
+ unsigned VarLength = DeclRange.getByteLength ();
791
793
// Print the type to a temporary buffer
792
794
SmallString<64 > Buffer;
793
795
{
@@ -802,21 +804,36 @@ class VariableTypeCollector : public SourceEntityWalker {
802
804
bool HasExplicitType =
803
805
VD->getTypeReprOrParentPatternTypeRepr () != nullptr ;
804
806
// Add the type information to the result list.
805
- Results.emplace_back (Offset, Length , HasExplicitType, Ty.first );
807
+ Results.emplace_back (VarOffset, VarLength , HasExplicitType, Ty.first );
806
808
}
807
809
return true ;
808
810
}
811
+
812
+ bool walkToStmtPre (Stmt *S) override {
813
+ // Skip this statement and its subtree if outside the range
814
+ return overlapsTotalRange (S->getSourceRange ());
815
+ }
816
+
817
+ bool walkToExprPre (Expr *E) override {
818
+ // Skip this expression and its subtree if outside the range
819
+ return overlapsTotalRange (E->getSourceRange ());
820
+ }
821
+
822
+ bool walkToPatternPre (Pattern *P) override {
823
+ // Skip this pattern and its subtree if outside the range
824
+ return overlapsTotalRange (P->getSourceRange ());
825
+ }
809
826
};
810
827
811
828
VariableTypeInfo::VariableTypeInfo (uint32_t Offset, uint32_t Length,
812
829
bool HasExplicitType, uint32_t TypeOffset)
813
830
: Offset(Offset), Length(Length), HasExplicitType(HasExplicitType),
814
831
TypeOffset(TypeOffset) {}
815
832
816
- void swift::collectVariableType (
817
- SourceFile &SF, Optional< unsigned > Offset, Optional< unsigned > Length ,
818
- std::vector<VariableTypeInfo> &Scratch, llvm::raw_ostream &OS) {
819
- VariableTypeCollector Walker (SF, Offset, Length , Scratch, OS);
833
+ void swift::collectVariableType (SourceFile &SF, SourceRange Range,
834
+ std::vector<VariableTypeInfo> &Scratch ,
835
+ llvm::raw_ostream &OS) {
836
+ VariableTypeCollector Walker (SF, Range , Scratch, OS);
820
837
Walker.walk (SF);
821
838
}
822
839
0 commit comments