Strange issue with RecursiveASTVisitor, TestTU, and variable templates

Hi folks,

I've run into a strange issue when trying to use a RecursiveASTVisitor on an AST created by TestTU::build(): it seems that top-level variable template declarations are never visited, only their inner VarDecl is.

I posted a patch with a test case illustrating the problem [1].

The problem appears to be related to this call to ASTContext::setTraversalScope() [2]. If I change that to set the traversal scope to the entire translation unit, the variable template declaration is visited correctly.

I did some further digging and found that the proximate cause of the problem is this line in clang's parser code [3]. However, I'm out of my depth here and don't know if this is actually a parser / AST bug, or a bug in how TestTU / ParsedAST are using these APIs.

Any guidance would be appreciated!

Thanks,
Nate

[1] https://reviews.llvm.org/D60954
[2] https://github.com/llvm/llvm-project/blob/bc76bbcaa0553b29f904d45cef9cc2a65d589f50/clang-tools-extra/clangd/ClangdUnit.cpp#L356
[3] https://github.com/llvm/llvm-project/blob/bc76bbcaa0553b29f904d45cef9cc2a65d589f50/clang/lib/Parse/ParseDecl.cpp#L2224

Hi Nathan,

I agree that looks weird, behavior for all templates should be consistent. We can either workaround this in our code or update the clang frontend to pass the template rather than the specialization to HandleTopLevelDecl.
The latter seems cleaner, I’ll try it out and see if anything breaks.

Could you describe the problem you are trying to solve? Maybe there are alternative ways to go about it that don’t involve looking at the template decl.

PS in the meantime you could workaround in your code by visiting VarTemplateSpecializationDecl, and calling getSpecializedTemplateOrPartial().

Could you describe the problem you are trying to solve? Maybe
there are alternative ways to go about it that don't involve looking
at the template decl.

I am working on a protoype implementation of semantic highlighting [1]. To perform semantic highlighting, I need to visit all the tokens we may want to apply a highlighting to, including template parameters. Due to this bug, for a top-level variable template, the template parameter list never gets visited.

Thanks,
Nate

[1] https://github.com/Microsoft/vscode-languageserver-node/pull/367