[PATCH] D126757: Fix clang RecursiveASTVisitor for definition of XXXTemplateSpecializationDecl

Qingyuan Zheng via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 31 23:35:08 PDT 2022


daiyousei-qz created this revision.
daiyousei-qz added reviewers: nridge, klimek.
Herald added subscribers: usaxena95, kadircet.
Herald added a project: All.
daiyousei-qz requested review of this revision.
Herald added subscribers: cfe-commits, ilya-biryukov.
Herald added a project: clang.

The original discussion starts at this issue: https://github.com/clangd/clangd/issues/1132
where clangd's semantic highlighting is missing for symbols of a template specialization definition. It turns out the visitor didn't traverse the base classes of Class/Var##TemplateSpecializationDecl, i.e. CXXRecordDecl/VarDecl. This patch adds them back as what is done in DEF_TRAVERSE_TMPL_PART_SPEC_DECL.

I'm not sure who should be included as reviewers. Please edit as needed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126757

Files:
  clang/include/clang/AST/RecursiveASTVisitor.h


Index: clang/include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- clang/include/clang/AST/RecursiveASTVisitor.h
+++ clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2021,7 +2021,7 @@
 
 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); })
 
-#define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND)                              \
+#define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND, DECLKIND)                    \
   DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, {                \
     /* For implicit instantiations ("set<int> x;"), we don't want to           \
        recurse at all, since the instatiated template isn't written in         \
@@ -2035,17 +2035,21 @@
       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));                              \
                                                                                \
     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));              \
-    if (!getDerived().shouldVisitTemplateInstantiations() &&                   \
-        D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)      \
+    if (getDerived().shouldVisitTemplateInstantiations() ||                    \
+        D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {    \
+      /* Traverse base definition for explicit specializations */              \
+      TRY_TO(Traverse##DECLKIND##Helper(D));                                   \
+    } else {                                                                   \
       /* Returning from here skips traversing the                              \
          declaration context of the *TemplateSpecializationDecl                \
          (embedded in the DEF_TRAVERSE_DECL() macro)                           \
          which contains the instantiated members of the template. */           \
       return true;                                                             \
+    }                                                                          \
   })
 
-DEF_TRAVERSE_TMPL_SPEC_DECL(Class)
-DEF_TRAVERSE_TMPL_SPEC_DECL(Var)
+DEF_TRAVERSE_TMPL_SPEC_DECL(Class, CXXRecord)
+DEF_TRAVERSE_TMPL_SPEC_DECL(Var, Var)
 
 template <typename Derived>
 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126757.433291.patch
Type: text/x-patch
Size: 2335 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220601/7c9e17b8/attachment.bin>


More information about the cfe-commits mailing list