hokein created this revision. hokein added a reviewer: sammccall. Herald added subscribers: usaxena95, kadircet, arphaman, kristof.beyls. Herald added a project: clang. hokein requested review of this revision.
Given the following VarTemplateDecl AST, VarTemplateDecl col:26 X |-TemplateTypeParmDecl typename depth 0 index 0 `-VarDecl X 'bool' cinit `-CXXBoolLiteralExpr 'bool' true previously, we returned the VarDecl as the top-level decl, which was not correct, the top-level decl should be VarTemplateDecl. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D89098 Files: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp clang/lib/Parse/ParseDecl.cpp Index: clang/lib/Parse/ParseDecl.cpp =================================================================== --- clang/lib/Parse/ParseDecl.cpp +++ clang/lib/Parse/ParseDecl.cpp @@ -2195,6 +2195,7 @@ // Inform the current actions module that we just parsed this declarator. Decl *ThisDecl = nullptr; + VarTemplateDecl *VarTemplateD = nullptr; switch (TemplateInfo.Kind) { case ParsedTemplateInfo::NonTemplate: ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); @@ -2205,10 +2206,12 @@ ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), *TemplateInfo.TemplateParams, D); - if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) + if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) { // Re-direct this decl to refer to the templated decl so that we can // initialize it. ThisDecl = VT->getTemplatedDecl(); + VarTemplateD = VT; + } break; } case ParsedTemplateInfo::ExplicitInstantiation: { @@ -2385,8 +2388,7 @@ } Actions.FinalizeDeclaration(ThisDecl); - - return ThisDecl; + return VarTemplateD ? VarTemplateD : ThisDecl; } /// ParseSpecifierQualifierList Index: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/ParsedASTTests.cpp +++ clang-tools-extra/clangd/unittests/ParsedASTTests.cpp @@ -104,6 +104,14 @@ EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); } +TEST(ParsedASTTest, VarTemplateDecl) { + TestTU TU; + TU.Code = "template <typename> bool X = true;"; + auto AST = TU.build(); + EXPECT_THAT(AST.getLocalTopLevelDecls(), + ElementsAre(AllOf(DeclNamed("X"), WithTemplateArgs("")))); +} + TEST(ParsedASTTest, DoesNotGetIncludedTopDecls) { TestTU TU; TU.HeaderCode = R"cpp(
Index: clang/lib/Parse/ParseDecl.cpp =================================================================== --- clang/lib/Parse/ParseDecl.cpp +++ clang/lib/Parse/ParseDecl.cpp @@ -2195,6 +2195,7 @@ // Inform the current actions module that we just parsed this declarator. Decl *ThisDecl = nullptr; + VarTemplateDecl *VarTemplateD = nullptr; switch (TemplateInfo.Kind) { case ParsedTemplateInfo::NonTemplate: ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); @@ -2205,10 +2206,12 @@ ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), *TemplateInfo.TemplateParams, D); - if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) + if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) { // Re-direct this decl to refer to the templated decl so that we can // initialize it. ThisDecl = VT->getTemplatedDecl(); + VarTemplateD = VT; + } break; } case ParsedTemplateInfo::ExplicitInstantiation: { @@ -2385,8 +2388,7 @@ } Actions.FinalizeDeclaration(ThisDecl); - - return ThisDecl; + return VarTemplateD ? VarTemplateD : ThisDecl; } /// ParseSpecifierQualifierList Index: clang-tools-extra/clangd/unittests/ParsedASTTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/ParsedASTTests.cpp +++ clang-tools-extra/clangd/unittests/ParsedASTTests.cpp @@ -104,6 +104,14 @@ EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); } +TEST(ParsedASTTest, VarTemplateDecl) { + TestTU TU; + TU.Code = "template <typename> bool X = true;"; + auto AST = TU.build(); + EXPECT_THAT(AST.getLocalTopLevelDecls(), + ElementsAre(AllOf(DeclNamed("X"), WithTemplateArgs("")))); +} + TEST(ParsedASTTest, DoesNotGetIncludedTopDecls) { TestTU TU; TU.HeaderCode = R"cpp(
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits