36
36
#include < cstddef>
37
37
#include < optional>
38
38
#include < string>
39
+ #include < unordered_set>
39
40
#include < utility>
40
41
#include < vector>
41
42
@@ -136,7 +137,8 @@ class IdDynMatcher : public DynMatcherInterface {
136
137
bool dynMatches (const DynTypedNode &DynNode, ASTMatchFinder *Finder,
137
138
BoundNodesTreeBuilder *Builder) const override {
138
139
bool Result = InnerMatcher->dynMatches (DynNode, Finder, Builder);
139
- if (Result) Builder->setBinding (ID, DynNode);
140
+ if (Result)
141
+ Builder->setBinding (ID, DynNode);
140
142
return Result;
141
143
}
142
144
@@ -354,7 +356,8 @@ bool DynTypedMatcher::canConvertTo(ASTNodeKind To) const {
354
356
auto TypeKind = ASTNodeKind::getFromNodeKind<Type>();
355
357
// / Mimic the implicit conversions of Matcher<>.
356
358
// / - From Matcher<Type> to Matcher<QualType>
357
- if (From.isSame (TypeKind) && To.isSame (QualKind)) return true ;
359
+ if (From.isSame (TypeKind) && To.isSame (QualKind))
360
+ return true ;
358
361
// / - From Matcher<Base> to Matcher<Derived>
359
362
return From.isBaseOf (To);
360
363
}
@@ -440,8 +443,8 @@ optionallyVariadicOperator(const DynTypedNode &DynNode, ASTMatchFinder *Finder,
440
443
return true ;
441
444
}
442
445
443
- inline static
444
- std::vector<std::string> vectorFromRefs (ArrayRef<const StringRef *> NameRefs) {
446
+ inline static std::vector<std::string>
447
+ vectorFromRefs (ArrayRef<const StringRef *> NameRefs) {
445
448
std::vector<std::string> Names;
446
449
Names.reserve (NameRefs.size ());
447
450
for (auto *Name : NameRefs)
@@ -454,8 +457,8 @@ Matcher<NamedDecl> hasAnyNameFunc(ArrayRef<const StringRef *> NameRefs) {
454
457
new internal::HasNameMatcher (vectorFromRefs (NameRefs)));
455
458
}
456
459
457
- Matcher<ObjCMessageExpr> hasAnySelectorFunc (
458
- ArrayRef<const StringRef *> NameRefs) {
460
+ Matcher<ObjCMessageExpr>
461
+ hasAnySelectorFunc ( ArrayRef<const StringRef *> NameRefs) {
459
462
return hasAnySelectorMatcher (vectorFromRefs (NameRefs));
460
463
}
461
464
@@ -697,27 +700,61 @@ static bool isTokenAtLoc(const SourceManager &SM, const LangOptions &LangOpts,
697
700
return !Invalid && Text == TokenText;
698
701
}
699
702
700
- std::optional<SourceLocation>
701
- getExpansionLocOfMacro (StringRef MacroName, SourceLocation Loc,
702
- const ASTContext &Context) {
703
+ namespace {
704
+ struct SourceLocationHash {
705
+ std::size_t operator ()(const SourceLocation &Loc) const {
706
+ return Loc.getHashValue ();
707
+ }
708
+ };
709
+
710
+ struct SourceLocationEqual {
711
+ bool operator ()(const SourceLocation &LHS, const SourceLocation &RHS) const {
712
+ return LHS == RHS;
713
+ }
714
+ };
715
+
716
+ } // namespace
717
+
718
+ static std::optional<SourceLocation> getExpansionLocOfMacroRecursive (
719
+ StringRef MacroName, SourceLocation Loc, const ASTContext &Context,
720
+ std::unordered_set<SourceLocation, SourceLocationHash, SourceLocationEqual>
721
+ &CheckedLocations) {
703
722
auto &SM = Context.getSourceManager ();
704
723
const LangOptions &LangOpts = Context.getLangOpts ();
705
724
while (Loc.isMacroID ()) {
725
+ if (CheckedLocations.count (Loc)) {
726
+ return std::nullopt;
727
+ }
728
+ CheckedLocations.insert (Loc);
706
729
SrcMgr::ExpansionInfo Expansion =
707
730
SM.getSLocEntry (SM.getFileID (Loc)).getExpansion ();
708
- if (Expansion.isMacroArgExpansion ())
731
+ if (Expansion.isMacroArgExpansion ()) {
709
732
// Check macro argument for an expansion of the given macro. For example,
710
733
// `F(G(3))`, where `MacroName` is `G`.
711
- if (std::optional<SourceLocation> ArgLoc = getExpansionLocOfMacro (
712
- MacroName, Expansion.getSpellingLoc (), Context))
734
+ if (std::optional<SourceLocation> ArgLoc =
735
+ getExpansionLocOfMacroRecursive (MacroName,
736
+ Expansion.getSpellingLoc (),
737
+ Context, CheckedLocations)) {
713
738
return ArgLoc;
739
+ }
740
+ }
714
741
Loc = Expansion.getExpansionLocStart ();
715
- if (isTokenAtLoc (SM, LangOpts, MacroName, Loc))
742
+ if (isTokenAtLoc (SM, LangOpts, MacroName, Loc)) {
716
743
return Loc;
744
+ }
717
745
}
718
746
return std::nullopt;
719
747
}
720
748
749
+ std::optional<SourceLocation>
750
+ getExpansionLocOfMacro (StringRef MacroName, SourceLocation Loc,
751
+ const ASTContext &Context) {
752
+ std::unordered_set<SourceLocation, SourceLocationHash, SourceLocationEqual>
753
+ CheckedLocations;
754
+ return getExpansionLocOfMacroRecursive (MacroName, Loc, Context,
755
+ CheckedLocations);
756
+ }
757
+
721
758
std::shared_ptr<llvm::Regex> createAndVerifyRegex (StringRef Regex,
722
759
llvm::Regex::RegexFlags Flags,
723
760
StringRef MatcherID) {
@@ -744,7 +781,8 @@ const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> typeAliasDecl;
744
781
const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasTemplateDecl>
745
782
typeAliasTemplateDecl;
746
783
const internal::VariadicAllOfMatcher<Decl> decl;
747
- const internal::VariadicDynCastAllOfMatcher<Decl, DecompositionDecl> decompositionDecl;
784
+ const internal::VariadicDynCastAllOfMatcher<Decl, DecompositionDecl>
785
+ decompositionDecl;
748
786
const internal::VariadicDynCastAllOfMatcher<Decl, BindingDecl> bindingDecl;
749
787
const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl>
750
788
linkageSpecDecl;
@@ -845,8 +883,7 @@ const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl>
845
883
objcCategoryImplDecl;
846
884
const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl>
847
885
objcMethodDecl;
848
- const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl>
849
- blockDecl;
886
+ const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl> blockDecl;
850
887
const internal::VariadicDynCastAllOfMatcher<Decl, ObjCIvarDecl> objcIvarDecl;
851
888
const internal::VariadicDynCastAllOfMatcher<Decl, ObjCPropertyDecl>
852
889
objcPropertyDecl;
@@ -907,7 +944,8 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, CXXRewrittenBinaryOperator>
907
944
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFoldExpr> cxxFoldExpr;
908
945
const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
909
946
const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr;
910
- const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr> objcIvarRefExpr;
947
+ const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr>
948
+ objcIvarRefExpr;
911
949
const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr;
912
950
const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
913
951
const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
@@ -937,13 +975,15 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
937
975
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr>
938
976
cxxBoolLiteral;
939
977
const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral> stringLiteral;
940
- const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCStringLiteral> objcStringLiteral;
978
+ const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCStringLiteral>
979
+ objcStringLiteral;
941
980
const internal::VariadicDynCastAllOfMatcher<Stmt, CharacterLiteral>
942
981
characterLiteral;
943
982
const internal::VariadicDynCastAllOfMatcher<Stmt, IntegerLiteral>
944
983
integerLiteral;
945
984
const internal::VariadicDynCastAllOfMatcher<Stmt, FloatingLiteral> floatLiteral;
946
- const internal::VariadicDynCastAllOfMatcher<Stmt, ImaginaryLiteral> imaginaryLiteral;
985
+ const internal::VariadicDynCastAllOfMatcher<Stmt, ImaginaryLiteral>
986
+ imaginaryLiteral;
947
987
const internal::VariadicDynCastAllOfMatcher<Stmt, FixedPointLiteral>
948
988
fixedPointLiteral;
949
989
const internal::VariadicDynCastAllOfMatcher<Stmt, UserDefinedLiteral>
@@ -955,12 +995,10 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNullPtrLiteralExpr>
955
995
const internal::VariadicDynCastAllOfMatcher<Stmt, ChooseExpr> chooseExpr;
956
996
const internal::VariadicDynCastAllOfMatcher<Stmt, ConvertVectorExpr>
957
997
convertVectorExpr;
958
- const internal::VariadicDynCastAllOfMatcher<Stmt, CoawaitExpr>
959
- coawaitExpr;
998
+ const internal::VariadicDynCastAllOfMatcher<Stmt, CoawaitExpr> coawaitExpr;
960
999
const internal::VariadicDynCastAllOfMatcher<Stmt, DependentCoawaitExpr>
961
1000
dependentCoawaitExpr;
962
- const internal::VariadicDynCastAllOfMatcher<Stmt, CoyieldExpr>
963
- coyieldExpr;
1001
+ const internal::VariadicDynCastAllOfMatcher<Stmt, CoyieldExpr> coyieldExpr;
964
1002
const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr> gnuNullExpr;
965
1003
const internal::VariadicDynCastAllOfMatcher<Stmt, GenericSelectionExpr>
966
1004
genericSelectionExpr;
0 commit comments