diff --git a/clang/include/clang/Tooling/NodeIntrospection.h b/clang/include/clang/Tooling/NodeIntrospection.h --- a/clang/include/clang/Tooling/NodeIntrospection.h +++ b/clang/include/clang/Tooling/NodeIntrospection.h @@ -23,6 +23,10 @@ class Stmt; class Decl; +class CXXCtorInitializer; +class NestedNameSpecifierLoc; +class TemplateArgumentLoc; +class CXXBaseSpecifier; namespace tooling { @@ -80,6 +84,10 @@ namespace NodeIntrospection { NodeLocationAccessors GetLocations(clang::Stmt const *Object); NodeLocationAccessors GetLocations(clang::Decl const *Object); +NodeLocationAccessors GetLocations(clang::CXXCtorInitializer const *Object); +NodeLocationAccessors GetLocations(clang::NestedNameSpecifierLoc const *); +NodeLocationAccessors GetLocations(clang::TemplateArgumentLoc const *); +NodeLocationAccessors GetLocations(clang::CXXBaseSpecifier const *); NodeLocationAccessors GetLocations(clang::DynTypedNode const &Node); } // namespace NodeIntrospection } // namespace tooling diff --git a/clang/lib/Tooling/CMakeLists.txt b/clang/lib/Tooling/CMakeLists.txt --- a/clang/lib/Tooling/CMakeLists.txt +++ b/clang/lib/Tooling/CMakeLists.txt @@ -41,6 +41,22 @@ NodeLocationAccessors NodeIntrospection::GetLocations(clang::Decl const *) { return {}; } +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::CXXCtorInitializer const *) { + return {}; +} +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::NestedNameSpecifierLoc const*) { + return {}; +} +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::TemplateArgumentLoc const*) { + return {}; +} +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::CXXBaseSpecifier const*) { + return {}; +} NodeLocationAccessors NodeIntrospection::GetLocations(clang::DynTypedNode const &) { return {}; diff --git a/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp b/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp --- a/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp +++ b/clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp @@ -26,7 +26,11 @@ isDefinition(), isSameOrDerivedFrom( // TODO: Extend this with other clades - namedDecl(hasAnyName("clang::Stmt", "clang::Decl")) + namedDecl(hasAnyName("clang::Stmt", "clang::Decl", + "clang::CXXCtorInitializer", + "clang::NestedNameSpecifierLoc", + "clang::TemplateArgumentLoc", + "clang::CXXBaseSpecifier")) .bind("nodeClade")), optionally(isDerivedFrom(cxxRecordDecl().bind("derivedFrom")))) .bind("className"), @@ -116,22 +120,30 @@ InnerMatcher...); }; - auto BoundNodesVec = - match(findAll(publicAccessor(ofClass(equalsNode(ASTClass)), - returns(asString(TypeString))) - .bind("classMethod")), - *ASTClass, *Result.Context); + auto BoundNodesVec = match( + findAll( + publicAccessor( + ofClass(cxxRecordDecl( + equalsNode(ASTClass), + optionally(isDerivedFrom( + cxxRecordDecl(hasAnyName("clang::Stmt", "clang::Decl")) + .bind("stmtOrDeclBase"))))), + returns(asString(TypeString))) + .bind("classMethod")), + *ASTClass, *Result.Context); std::vector Methods; for (const auto &BN : BoundNodesVec) { + const auto *StmtOrDeclBase = + BN.getNodeAs("stmtOrDeclBase"); if (const auto *Node = BN.getNodeAs("classMethod")) { // Only record the getBeginLoc etc on Stmt etc, because it will call // more-derived implementations pseudo-virtually. - if ((ASTClass->getName() != "Stmt" && ASTClass->getName() != "Decl") && + if (StmtOrDeclBase && (Node->getName() == "getBeginLoc" || Node->getName() == "getEndLoc" || - Node->getName() == "getSourceRange")) { + Node->getName() == "getSourceRange")) continue; - } + // Only record the getExprLoc on Expr, because it will call // more-derived implementations pseudo-virtually. if (ASTClass->getName() != "Expr" && Node->getName() == "getExprLoc") { diff --git a/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py b/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py --- a/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py +++ b/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py @@ -180,6 +180,22 @@ NodeLocationAccessors NodeIntrospection::GetLocations(clang::Decl const *) { return {}; } +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::CXXCtorInitializer const *) { + return {}; +} +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::NestedNameSpecifierLoc const*) { + return {}; +} +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::TemplateArgumentLoc const*) { + return {}; +} +NodeLocationAccessors NodeIntrospection::GetLocations( + clang::CXXBaseSpecifier const*) { + return {}; +} NodeLocationAccessors NodeIntrospection::GetLocations(clang::DynTypedNode const &) { return {}; diff --git a/clang/unittests/Introspection/IntrospectionTest.cpp b/clang/unittests/Introspection/IntrospectionTest.cpp --- a/clang/unittests/Introspection/IntrospectionTest.cpp +++ b/clang/unittests/Introspection/IntrospectionTest.cpp @@ -30,7 +30,10 @@ std::map FormatExpected(const MapType &Accessors) { std::map Result; - llvm::transform(Accessors, + llvm::transform(llvm::make_filter_range(Accessors, + [](const auto &Accessor) { + return Accessor.first.isValid(); + }), std::inserter(Result, Result.end()), [](const auto &Accessor) { return std::make_pair( @@ -126,11 +129,9 @@ UnorderedElementsAre( STRING_LOCATION_PAIR(MethodDecl, getBeginLoc()), STRING_LOCATION_PAIR(MethodDecl, getBodyRBrace()), - STRING_LOCATION_PAIR(MethodDecl, getEllipsisLoc()), STRING_LOCATION_PAIR(MethodDecl, getInnerLocStart()), STRING_LOCATION_PAIR(MethodDecl, getLocation()), STRING_LOCATION_PAIR(MethodDecl, getOuterLocStart()), - STRING_LOCATION_PAIR(MethodDecl, getPointOfInstantiation()), STRING_LOCATION_PAIR(MethodDecl, getTypeSpecEndLoc()), STRING_LOCATION_PAIR(MethodDecl, getTypeSpecStartLoc()), STRING_LOCATION_PAIR(MethodDecl, getEndLoc()))); @@ -145,3 +146,741 @@ STRING_LOCATION_PAIR(MethodDecl, getReturnTypeSourceRange()), STRING_LOCATION_PAIR(MethodDecl, getSourceRange()))); } + +TEST(Introspection, SourceLocations_NNS) { + auto AST = + buildASTFromCode(R"cpp( +namespace ns +{ + struct A { + void foo(); +}; +} +void ns::A::foo() {} +)cpp", + "foo.cpp", std::make_shared()); + auto &Ctx = AST->getASTContext(); + auto &TU = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(nestedNameSpecifierLoc().bind("nns"))), TU, Ctx); + + EXPECT_EQ(BoundNodes.size(), 1u); + + const auto *NNS = BoundNodes[0].getNodeAs("nns"); + + auto Result = NodeIntrospection::GetLocations(NNS); + + if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { + return; + } + + auto ExpectedLocations = + FormatExpected(Result.LocationAccessors); + + EXPECT_THAT( + ExpectedLocations, + UnorderedElementsAre(STRING_LOCATION_PAIR(NNS, getBeginLoc()), + STRING_LOCATION_PAIR(NNS, getEndLoc()), + STRING_LOCATION_PAIR(NNS, getLocalBeginLoc()), + STRING_LOCATION_PAIR(NNS, getLocalEndLoc()))); + + auto ExpectedRanges = FormatExpected(Result.RangeAccessors); + + EXPECT_THAT( + ExpectedRanges, + UnorderedElementsAre(STRING_LOCATION_PAIR(NNS, getLocalSourceRange()), + STRING_LOCATION_PAIR(NNS, getSourceRange()))); +} + +TEST(Introspection, SourceLocations_TA_Type) { + auto AST = + buildASTFromCode(R"cpp( +template + struct A { + void foo(); +}; + +void foo() +{ + A a; +} +)cpp", + "foo.cpp", std::make_shared()); + auto &Ctx = AST->getASTContext(); + auto &TU = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU, Ctx); + + EXPECT_EQ(BoundNodes.size(), 1u); + + const auto *TA = BoundNodes[0].getNodeAs("ta"); + + auto Result = NodeIntrospection::GetLocations(TA); + + if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { + return; + } + + auto ExpectedLocations = + FormatExpected(Result.LocationAccessors); + + EXPECT_THAT(ExpectedLocations, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getLocation()))); + + auto ExpectedRanges = FormatExpected(Result.RangeAccessors); + + EXPECT_THAT(ExpectedRanges, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getSourceRange()))); +} + +TEST(Introspection, SourceLocations_TA_Decl) { + auto AST = + buildASTFromCode(R"cpp( +template +void test2() {} +void doNothing() {} +void test() { + test2(); +} +)cpp", + "foo.cpp", std::make_shared()); + auto &Ctx = AST->getASTContext(); + auto &TU = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU, Ctx); + + EXPECT_EQ(BoundNodes.size(), 1u); + + const auto *TA = BoundNodes[0].getNodeAs("ta"); + + auto Result = NodeIntrospection::GetLocations(TA); + + if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { + return; + } + + auto ExpectedLocations = + FormatExpected(Result.LocationAccessors); + + EXPECT_THAT(ExpectedLocations, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getLocation()))); + + auto ExpectedRanges = FormatExpected(Result.RangeAccessors); + + EXPECT_THAT(ExpectedRanges, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getSourceRange()))); +} + +TEST(Introspection, SourceLocations_TA_Nullptr) { + auto AST = + buildASTFromCode(R"cpp( +template +void test2() {} +void doNothing() {} +void test() { + test2(); +} +)cpp", + "foo.cpp", std::make_shared()); + auto &Ctx = AST->getASTContext(); + auto &TU = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU, Ctx); + + EXPECT_EQ(BoundNodes.size(), 1u); + + const auto *TA = BoundNodes[0].getNodeAs("ta"); + + auto Result = NodeIntrospection::GetLocations(TA); + + if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { + return; + } + + auto ExpectedLocations = + FormatExpected(Result.LocationAccessors); + + EXPECT_THAT(ExpectedLocations, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getLocation()))); + + auto ExpectedRanges = FormatExpected(Result.RangeAccessors); + + EXPECT_THAT(ExpectedRanges, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getSourceRange()))); +} + +TEST(Introspection, SourceLocations_TA_Integral) { + auto AST = + buildASTFromCode(R"cpp( +template +void test2() {} +void test() { + test2<42>(); +} +)cpp", + "foo.cpp", std::make_shared()); + auto &Ctx = AST->getASTContext(); + auto &TU = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU, Ctx); + + EXPECT_EQ(BoundNodes.size(), 1u); + + const auto *TA = BoundNodes[0].getNodeAs("ta"); + + auto Result = NodeIntrospection::GetLocations(TA); + + if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { + return; + } + + auto ExpectedLocations = + FormatExpected(Result.LocationAccessors); + + EXPECT_THAT(ExpectedLocations, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getLocation()))); + + auto ExpectedRanges = FormatExpected(Result.RangeAccessors); + + EXPECT_THAT(ExpectedRanges, + UnorderedElementsAre(STRING_LOCATION_PAIR(TA, getSourceRange()))); +} + +TEST(Introspection, SourceLocations_TA_Template) { + auto AST = + buildASTFromCode(R"cpp( +template class A; +template