[clang] [AST] RecursiveASTVisitor: traverse the require clause for partial template specializations. (PR #75795)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 18 06:06:17 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

<details>
<summary>Changes</summary>

This fixes tooling (clangd, include-cleaner) bugs where we miss functionalities on concept AST nodes.

---
Full diff: https://github.com/llvm/llvm-project/pull/75795.diff


2 Files Affected:

- (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+1-6) 
- (modified) clang/unittests/Tooling/RecursiveASTVisitorTests/Concept.cpp (+19) 


``````````diff
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index c501801b95bd95..8f2714e142bbe3 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2036,12 +2036,7 @@ bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND)               \
   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, {         \
     /* The partial specialization. */                                          \
-    if (TemplateParameterList *TPL = D->getTemplateParameters()) {             \
-      for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();   \
-           I != E; ++I) {                                                      \
-        TRY_TO(TraverseDecl(*I));                                              \
-      }                                                                        \
-    }                                                                          \
+    TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));   \
     /* The args that remains unspecialized. */                                 \
     TRY_TO(TraverseTemplateArgumentLocsHelper(                                 \
         D->getTemplateArgsAsWritten()->getTemplateArgs(),                      \
diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTests/Concept.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTests/Concept.cpp
index 594b299b543694..6a8d91672f1d93 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTests/Concept.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTests/Concept.cpp
@@ -86,6 +86,25 @@ TEST(RecursiveASTVisitor, Concepts) {
   EXPECT_EQ(3, Visitor.ConceptRequirementsTraversed);
   EXPECT_EQ(1, Visitor.ConceptReferencesTraversed);
   EXPECT_EQ(1, Visitor.ConceptReferencesVisited);
+
+  Visitor = {};
+  llvm::StringRef Code =
+      R"cpp(
+template<typename T> concept True = false;
+template <typename F> struct Foo {};
+
+template <typename F>
+  requires requires { requires True<F>; }
+struct Foo<F> {};
+
+template <typename F> requires True<F>
+struct Foo<F>  {};
+  )cpp";
+  EXPECT_TRUE(Visitor.runOver(Code, ConceptVisitor::Lang_CXX2a));
+  // Check that the concept references from the partial specializations are
+  // visited.
+  EXPECT_EQ(2, Visitor.ConceptReferencesTraversed);
+  EXPECT_EQ(2, Visitor.ConceptReferencesVisited);
 }
 
 struct VisitDeclOnlyOnce : ExpectedLocationVisitor<VisitDeclOnlyOnce> {

``````````

</details>


https://github.com/llvm/llvm-project/pull/75795


More information about the cfe-commits mailing list