steveire updated this revision to Diff 321991.
steveire added a comment.

Update


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96114/new/

https://reviews.llvm.org/D96114

Files:
  clang/include/clang/AST/RecursiveASTVisitor.h
  clang/lib/AST/ParentMapContext.cpp
  clang/lib/ASTMatchers/ASTMatchFinder.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -3551,6 +3551,14 @@
                            forFunction(functionDecl(hasName("func13"))))))),
       langCxx20OrLater()));
 
+  EXPECT_TRUE(
+      matches(Code,
+              traverse(TK_IgnoreUnlessSpelledInSource,
+                       parmVarDecl(hasName("d"),
+                                   hasParent(lambdaExpr(forFunction(
+                                       functionDecl(hasName("func13"))))))),
+              langCxx20OrLater()));
+
   EXPECT_TRUE(matches(Code,
                       traverse(TK_IgnoreUnlessSpelledInSource,
                                compoundStmt(hasParent(lambdaExpr(forFunction(
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2068,6 +2068,41 @@
                  Constructor1Arg));
 }
 
+TEST(ASTMatchersTest, has_parmVarDecl) {
+
+  auto Code = R"cpp(
+void f(int ii)
+{
+
+}
+)cpp";
+
+  EXPECT_TRUE(matches(
+      Code,
+      traverse(TK_AsIs,
+               functionDecl(hasName("f"),
+                            has(typeLoc(has(parmVarDecl(hasName("ii"))))))),
+      langCxx20OrLater()));
+  EXPECT_TRUE(matches(
+      Code,
+      traverse(TK_IgnoreUnlessSpelledInSource,
+               functionDecl(hasName("f"), has(parmVarDecl(hasName("ii"))))),
+      langCxx20OrLater()));
+
+  EXPECT_TRUE(
+      matches(Code,
+              traverse(TK_AsIs, parmVarDecl(hasName("ii"),
+                                            hasParent(typeLoc(hasParent(
+                                                functionDecl(hasName("f"))))))),
+              langCxx20OrLater()));
+  EXPECT_TRUE(
+      matches(Code,
+              traverse(TK_IgnoreUnlessSpelledInSource,
+                       parmVarDecl(hasName("ii"),
+                                   hasParent(functionDecl(hasName("f"))))),
+              langCxx20OrLater()));
+}
+
 TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {
 
   // Member functions:
Index: clang/lib/ASTMatchers/ASTMatchFinder.cpp
===================================================================
--- clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -143,6 +143,17 @@
     return Matches;
   }
 
+  bool TraverseFunctionHelper(FunctionDecl *FD) {
+    if (Finder->isTraversalIgnoringImplicitNodes()) {
+      for (auto *P : FD->parameters()) {
+        if (!TraverseDecl(P))
+          return false;
+      }
+    }
+    return RecursiveASTVisitor<MatchChildASTVisitor>::TraverseFunctionHelper(
+        FD);
+  }
+
   // The following are overriding methods from the base visitor class.
   // They are public only to allow CRTP to work. They are *not *part
   // of the public API of this class.
Index: clang/lib/AST/ParentMapContext.cpp
===================================================================
--- clang/lib/AST/ParentMapContext.cpp
+++ clang/lib/AST/ParentMapContext.cpp
@@ -345,7 +345,7 @@
 class ParentMapContext::ParentMap::ASTVisitor
     : public RecursiveASTVisitor<ASTVisitor> {
 public:
-  ASTVisitor(ParentMap &Map) : Map(Map) {}
+  ASTVisitor(ParentMap &Map, ASTContext &Ctx) : Map(Map), Ctx(Ctx) {}
 
 private:
   friend class RecursiveASTVisitor<ASTVisitor>;
@@ -422,6 +422,19 @@
         &Map.PointerParents);
   }
   bool TraverseTypeLoc(TypeLoc TypeLocNode) {
+    if (Ctx.getParentMapContext().getTraversalKind() ==
+        TK_IgnoreUnlessSpelledInSource) {
+      if (auto FPTL = TypeLocNode.getAs<FunctionProtoTypeLoc>()) {
+        for (unsigned I = 0, E = FPTL.getNumParams(); I != E; ++I) {
+          if (auto *P = FPTL.getParam(I)) {
+            if (!TraverseDecl(P))
+              return false;
+          }
+        }
+        return true;
+      }
+    }
+
     return TraverseNode(
         TypeLocNode, DynTypedNode::create(TypeLocNode),
         [&] { return VisitorBase::TraverseTypeLoc(TypeLocNode); },
@@ -446,11 +459,12 @@
   }
 
   ParentMap &Map;
+  ASTContext &Ctx;
   llvm::SmallVector<DynTypedNode, 16> ParentStack;
 };
 
 ParentMapContext::ParentMap::ParentMap(ASTContext &Ctx) {
-  ASTVisitor(*this).TraverseAST(Ctx);
+  ASTVisitor(*this, Ctx).TraverseAST(Ctx);
 }
 
 DynTypedNodeList ParentMapContext::getParents(const DynTypedNode &Node) {
Index: clang/include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- clang/include/clang/AST/RecursiveASTVisitor.h
+++ clang/include/clang/AST/RecursiveASTVisitor.h
@@ -473,6 +473,8 @@
 
   bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue);
 
+  bool TraverseFunctionHelper(FunctionDecl *D);
+
 private:
   // These are helper methods used by more than one Traverse* method.
   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
@@ -488,7 +490,6 @@
   bool TraverseCXXRecordHelper(CXXRecordDecl *D);
   bool TraverseDeclaratorHelper(DeclaratorDecl *D);
   bool TraverseDeclContextHelper(DeclContext *DC);
-  bool TraverseFunctionHelper(FunctionDecl *D);
   bool TraverseVarHelper(VarDecl *D);
   bool TraverseOMPExecutableDirective(OMPExecutableDirective *S);
   bool TraverseOMPLoopDirective(OMPLoopDirective *S);
@@ -2079,28 +2080,28 @@
   // We skip decls_begin/decls_end, which are already covered by
   // TraverseFunctionHelper().
   ShouldVisitChildren = false;
-  ReturnValue = TraverseFunctionHelper(D);
+  ReturnValue = getDerived().TraverseFunctionHelper(D);
 })
 
 DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, {
   // We skip decls_begin/decls_end, which are already covered by
   // TraverseFunctionHelper().
   ShouldVisitChildren = false;
-  ReturnValue = TraverseFunctionHelper(D);
+  ReturnValue = getDerived().TraverseFunctionHelper(D);
 })
 
 DEF_TRAVERSE_DECL(CXXMethodDecl, {
   // We skip decls_begin/decls_end, which are already covered by
   // TraverseFunctionHelper().
   ShouldVisitChildren = false;
-  ReturnValue = TraverseFunctionHelper(D);
+  ReturnValue = getDerived().TraverseFunctionHelper(D);
 })
 
 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
   // We skip decls_begin/decls_end, which are already covered by
   // TraverseFunctionHelper().
   ShouldVisitChildren = false;
-  ReturnValue = TraverseFunctionHelper(D);
+  ReturnValue = getDerived().TraverseFunctionHelper(D);
 })
 
 // CXXConversionDecl is the declaration of a type conversion operator.
@@ -2109,14 +2110,14 @@
   // We skip decls_begin/decls_end, which are already covered by
   // TraverseFunctionHelper().
   ShouldVisitChildren = false;
-  ReturnValue = TraverseFunctionHelper(D);
+  ReturnValue = getDerived().TraverseFunctionHelper(D);
 })
 
 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
   // We skip decls_begin/decls_end, which are already covered by
   // TraverseFunctionHelper().
   ShouldVisitChildren = false;
-  ReturnValue = TraverseFunctionHelper(D);
+  ReturnValue = getDerived().TraverseFunctionHelper(D);
 })
 
 template <typename Derived>
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to