[PATCH] D99231: [AST] Add introspection support for more base nodes
This revision was automatically updated to reflect the committed changes. Closed by commit rGf347f0e0b869: [AST] Add introspection support for more base nodes (authored by stephenkelly). Changed prior to commit: https://reviews.llvm.org/D99231?vs=336813=337537#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 Files: clang/include/clang/Tooling/NodeIntrospection.h clang/lib/Tooling/CMakeLists.txt clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py clang/unittests/Introspection/IntrospectionTest.cpp Index: clang/unittests/Introspection/IntrospectionTest.cpp === --- clang/unittests/Introspection/IntrospectionTest.cpp +++ clang/unittests/Introspection/IntrospectionTest.cpp @@ -30,7 +30,10 @@ std::map FormatExpected(const MapType ) { std::map Result; - llvm::transform(Accessors, + llvm::transform(llvm::make_filter_range(Accessors, + [](const auto ) { +return Accessor.first.isValid(); + }), std::inserter(Result, Result.end()), [](const auto ) { 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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); +
[PATCH] D99231: [AST] Add introspection support for more base nodes
aaron.ballman added inline comments. Comment at: clang/include/clang/AST/DeclCXX.h:2270 SourceLocation getEllipsisLoc() const { -assert(isPackExpansion() && "Initializer is not a pack expansion"); +if (!isPackExpansion()) + return {}; steveire wrote: > njames93 wrote: > > steveire wrote: > > > njames93 wrote: > > > > I'm not sure about this change, but I'm guessing there's not a nice way > > > > to predicate these kinds of things > > > Yep. > > @rsmith @aaron.ballman Any issues with changing the contract of these > > functions to not assert for the purpose of this feature? > This was really just missing from > https://github.com/llvm/llvm-project/commit/54272e5b , as in the commit > message. I'm fine with such a change, thanks! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire added inline comments. Comment at: clang/include/clang/AST/DeclCXX.h:2270 SourceLocation getEllipsisLoc() const { -assert(isPackExpansion() && "Initializer is not a pack expansion"); +if (!isPackExpansion()) + return {}; njames93 wrote: > steveire wrote: > > njames93 wrote: > > > I'm not sure about this change, but I'm guessing there's not a nice way > > > to predicate these kinds of things > > Yep. > @rsmith @aaron.ballman Any issues with changing the contract of these > functions to not assert for the purpose of this feature? This was really just missing from https://github.com/llvm/llvm-project/commit/54272e5b , as in the commit message. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D99231: [AST] Add introspection support for more base nodes
njames93 added subscribers: aaron.ballman, rsmith. njames93 added inline comments. Comment at: clang/include/clang/AST/DeclCXX.h:2270 SourceLocation getEllipsisLoc() const { -assert(isPackExpansion() && "Initializer is not a pack expansion"); +if (!isPackExpansion()) + return {}; steveire wrote: > njames93 wrote: > > I'm not sure about this change, but I'm guessing there's not a nice way to > > predicate these kinds of things > Yep. @rsmith @aaron.ballman Any issues with changing the contract of these functions to not assert for the purpose of this feature? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire updated this revision to Diff 336813. steveire added a comment. Update Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 Files: clang/include/clang/AST/DeclCXX.h clang/include/clang/Tooling/NodeIntrospection.h clang/lib/Tooling/CMakeLists.txt clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py clang/unittests/Introspection/IntrospectionTest.cpp Index: clang/unittests/Introspection/IntrospectionTest.cpp === --- clang/unittests/Introspection/IntrospectionTest.cpp +++ clang/unittests/Introspection/IntrospectionTest.cpp @@ -30,7 +30,10 @@ std::map FormatExpected(const MapType ) { std::map Result; - llvm::transform(Accessors, + llvm::transform(llvm::make_filter_range(Accessors, + [](const auto ) { +return Accessor.first.isValid(); + }), std::inserter(Result, Result.end()), [](const auto ) { 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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU,
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire added inline comments. Comment at: clang/include/clang/AST/DeclCXX.h:2270 SourceLocation getEllipsisLoc() const { -assert(isPackExpansion() && "Initializer is not a pack expansion"); +if (!isPackExpansion()) + return {}; njames93 wrote: > I'm not sure about this change, but I'm guessing there's not a nice way to > predicate these kinds of things Yep. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire updated this revision to Diff 336804. steveire added a comment. Update Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 Files: clang/include/clang/AST/DeclCXX.h clang/include/clang/Tooling/NodeIntrospection.h clang/lib/Tooling/CMakeLists.txt clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py clang/unittests/Introspection/IntrospectionTest.cpp Index: clang/unittests/Introspection/IntrospectionTest.cpp === --- clang/unittests/Introspection/IntrospectionTest.cpp +++ clang/unittests/Introspection/IntrospectionTest.cpp @@ -30,7 +30,10 @@ std::map FormatExpected(const MapType ) { std::map Result; - llvm::transform(Accessors, + llvm::transform(llvm::make_filter_range(Accessors, + [](const auto ) { +return Accessor.first.isValid(); + }), std::inserter(Result, Result.end()), [](const auto ) { 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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( + decl(hasDescendant(templateArgumentLoc().bind("ta"))), TU,
[PATCH] D99231: [AST] Add introspection support for more base nodes
njames93 added inline comments. Comment at: clang/include/clang/AST/DeclCXX.h:2270 SourceLocation getEllipsisLoc() const { -assert(isPackExpansion() && "Initializer is not a pack expansion"); +if (!isPackExpansion()) + return {}; I'm not sure about this change, but I'm guessing there's not a nice way to predicate these kinds of things Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire updated this revision to Diff 336651. steveire added a comment. Add locations for CXXBaseSpecifier Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 Files: clang/include/clang/AST/DeclCXX.h clang/include/clang/Tooling/NodeIntrospection.h clang/lib/Tooling/CMakeLists.txt clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py clang/unittests/Introspection/IntrospectionTest.cpp Index: clang/unittests/Introspection/IntrospectionTest.cpp === --- clang/unittests/Introspection/IntrospectionTest.cpp +++ clang/unittests/Introspection/IntrospectionTest.cpp @@ -30,7 +30,10 @@ std::map FormatExpected(const MapType ) { std::map Result; - llvm::transform(Accessors, + llvm::transform(llvm::make_filter_range(Accessors, + [](const auto ) { +return Accessor.first.isValid(); + }), std::inserter(Result, Result.end()), [](const auto ) { 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,737 @@ 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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *Ctx.getTranslationUnitDecl(); + + auto BoundNodes = ast_matchers::match( +
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire added a comment. @njames93 ping? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D99231/new/ https://reviews.llvm.org/D99231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D99231: [AST] Add introspection support for more base nodes
steveire created this revision. steveire added a reviewer: njames93. Herald added a subscriber: mgorny. steveire requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. Replace an assert for a CXXCtorInitializer SourceLocation with a condition, as was done for other locations in commit 54272e5b (NFC: Replace asserts with if() in SourceLocation accessors, 2019-01-07). Fix the logic of detecting pseudo-virtual getBeginLoc etc on Stmt and Decl subclasses. Adjust the test infrastructure to filter out invalid source locations. This makes the tests more clear about which nodes have which locations. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D99231 Files: clang/include/clang/AST/DeclCXX.h clang/include/clang/Tooling/NodeIntrospection.h clang/lib/Tooling/CMakeLists.txt clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py clang/unittests/Introspection/IntrospectionTest.cpp Index: clang/unittests/Introspection/IntrospectionTest.cpp === --- clang/unittests/Introspection/IntrospectionTest.cpp +++ clang/unittests/Introspection/IntrospectionTest.cpp @@ -30,7 +30,10 @@ std::map FormatExpected(const MapType ) { std::map Result; - llvm::transform(Accessors, + llvm::transform(llvm::make_filter_range(Accessors, + [](const auto ) { +return Accessor.first.isValid(); + }), std::inserter(Result, Result.end()), [](const auto ) { 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,538 @@ 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 = AST->getASTContext(); + auto = *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 = AST->getASTContext(); + auto = *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, +