From f019e5f73ed732b374e376f5ddbba5d1f67dca0c Mon Sep 17 00:00:00 2001 From: Nathan James Date: Thu, 15 Apr 2021 22:21:40 +0100 Subject: [PATCH] [AST][Introspection] Add a check to detect if introspection is supported. This could probably be made into a compile time constant, but that would involve generating a second inc file. Reviewed By: steveire Differential Revision: https://reviews.llvm.org/D100530 --- .../include/clang/Tooling/NodeIntrospection.h | 1 + clang/lib/Tooling/CMakeLists.txt | 2 + .../Tooling/DumpTool/generate_cxx_src_locs.py | 4 + .../Introspection/IntrospectionTest.cpp | 121 ++++++------------ 4 files changed, 47 insertions(+), 81 deletions(-) diff --git a/clang/include/clang/Tooling/NodeIntrospection.h b/clang/include/clang/Tooling/NodeIntrospection.h index 9147c7db6c271..5489a67efa22d 100644 --- a/clang/include/clang/Tooling/NodeIntrospection.h +++ b/clang/include/clang/Tooling/NodeIntrospection.h @@ -85,6 +85,7 @@ struct NodeLocationAccessors { }; namespace NodeIntrospection { +bool hasIntrospectionSupport(); NodeLocationAccessors GetLocations(clang::Stmt const *Object); NodeLocationAccessors GetLocations(clang::Decl const *Object); NodeLocationAccessors GetLocations(clang::CXXCtorInitializer const *Object); diff --git a/clang/lib/Tooling/CMakeLists.txt b/clang/lib/Tooling/CMakeLists.txt index 6d70c8976f03b..0da3dbd0b927c 100644 --- a/clang/lib/Tooling/CMakeLists.txt +++ b/clang/lib/Tooling/CMakeLists.txt @@ -35,6 +35,8 @@ if (NOT Python3_EXECUTABLE namespace clang { namespace tooling { +bool NodeIntrospection::hasIntrospectionSupport() { return false; } + NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) { return {}; } diff --git a/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py b/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py index 0adebeb3b3bfa..b0953df192037 100755 --- a/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py +++ b/clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py @@ -28,6 +28,8 @@ def GeneratePrologue(self): using LocationAndString = SourceLocationMap::value_type; using RangeAndString = SourceRangeMap::value_type; + +bool NodeIntrospection::hasIntrospectionSupport() { return true; } """ def GenerateBaseGetLocationsDeclaration(self, CladeName): @@ -174,6 +176,8 @@ def main(): namespace clang { namespace tooling { +bool NodeIntrospection::hasIntrospectionSupport() { return false; } + NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) { return {}; } diff --git a/clang/unittests/Introspection/IntrospectionTest.cpp b/clang/unittests/Introspection/IntrospectionTest.cpp index 4a684f26a6248..880068c43b6e5 100644 --- a/clang/unittests/Introspection/IntrospectionTest.cpp +++ b/clang/unittests/Introspection/IntrospectionTest.cpp @@ -46,6 +46,8 @@ FormatExpected(const MapType &Accessors) { #define STRING_LOCATION_PAIR(INSTANCE, LOC) Pair(#LOC, INSTANCE->LOC) TEST(Introspection, SourceLocations_Stmt) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode("void foo() {} void bar() { foo(); }", "foo.cpp", std::make_shared()); auto &Ctx = AST->getASTContext(); @@ -62,11 +64,6 @@ TEST(Introspection, SourceLocations_Stmt) { auto Result = NodeIntrospection::GetLocations(FooCall); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) - { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -84,6 +81,8 @@ TEST(Introspection, SourceLocations_Stmt) { } TEST(Introspection, SourceLocations_Decl) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( namespace ns1 { @@ -118,10 +117,6 @@ ns1::ns2::Foo ns1::ns2::Bar::Nested::method(int i, bool b) const auto Result = NodeIntrospection::GetLocations(MethodDecl); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -148,6 +143,8 @@ ns1::ns2::Foo ns1::ns2::Bar::Nested::method(int i, bool b) const } TEST(Introspection, SourceLocations_NNS) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( namespace ns @@ -171,10 +168,6 @@ void ns::A::foo() {} auto Result = NodeIntrospection::GetLocations(NNS); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -194,6 +187,8 @@ void ns::A::foo() {} } TEST(Introspection, SourceLocations_TA_Type) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template @@ -219,10 +214,6 @@ void foo() auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -236,6 +227,8 @@ void foo() } TEST(Introspection, SourceLocations_TA_Decl) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template @@ -258,10 +251,6 @@ void test() { auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -275,6 +264,8 @@ void test() { } TEST(Introspection, SourceLocations_TA_Nullptr) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template @@ -297,10 +288,6 @@ void test() { auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -314,6 +301,8 @@ void test() { } TEST(Introspection, SourceLocations_TA_Integral) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template @@ -335,10 +324,6 @@ void test() { auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -352,6 +337,8 @@ void test() { } TEST(Introspection, SourceLocations_TA_Template) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template class A; @@ -374,10 +361,6 @@ void bar() auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -393,6 +376,8 @@ void bar() } TEST(Introspection, SourceLocations_TA_TemplateExpansion) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCodeWithArgs( R"cpp( template class ...> class B { }; @@ -414,10 +399,6 @@ template class ...> class B { }; auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -434,6 +415,8 @@ template class ...> class B { }; } TEST(Introspection, SourceLocations_TA_Expression) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template class testExpr; @@ -452,10 +435,6 @@ template class testExpr { }; auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -469,6 +448,8 @@ template class testExpr { }; } TEST(Introspection, SourceLocations_TA_Pack) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCodeWithArgs( R"cpp( template class A {}; @@ -491,10 +472,6 @@ void foo() auto Result = NodeIntrospection::GetLocations(TA); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -508,6 +485,8 @@ void foo() } TEST(Introspection, SourceLocations_CXXCtorInitializer_base) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( struct A { @@ -532,10 +511,6 @@ struct B : A { auto Result = NodeIntrospection::GetLocations(CtorInit); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -552,6 +527,8 @@ struct B : A { } TEST(Introspection, SourceLocations_CXXCtorInitializer_member) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( struct A { @@ -574,10 +551,6 @@ struct A { auto Result = NodeIntrospection::GetLocations(CtorInit); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -595,6 +568,8 @@ struct A { } TEST(Introspection, SourceLocations_CXXCtorInitializer_ctor) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( struct C { @@ -617,10 +592,6 @@ struct C { auto Result = NodeIntrospection::GetLocations(CtorInit); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -637,6 +608,8 @@ struct C { } TEST(Introspection, SourceLocations_CXXCtorInitializer_pack) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCodeWithArgs( R"cpp( template @@ -664,10 +637,6 @@ struct D : Templ { auto Result = NodeIntrospection::GetLocations(CtorInit); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -686,6 +655,8 @@ struct D : Templ { } TEST(Introspection, SourceLocations_CXXBaseSpecifier_plain) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( class A {}; @@ -706,10 +677,6 @@ class B : A {}; auto Result = NodeIntrospection::GetLocations(Base); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -725,6 +692,8 @@ class B : A {}; } TEST(Introspection, SourceLocations_CXXBaseSpecifier_accessspec) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( class A {}; @@ -745,10 +714,6 @@ class B : public A {}; auto Result = NodeIntrospection::GetLocations(Base); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -764,6 +729,8 @@ class B : public A {}; } TEST(Introspection, SourceLocations_CXXBaseSpecifier_virtual) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( class A {}; @@ -785,10 +752,6 @@ class C : virtual B, A {}; auto Result = NodeIntrospection::GetLocations(Base); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -804,6 +767,8 @@ class C : virtual B, A {}; } TEST(Introspection, SourceLocations_CXXBaseSpecifier_template_base) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCode(R"cpp( template @@ -825,10 +790,6 @@ class B : A {}; auto Result = NodeIntrospection::GetLocations(Base); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors); @@ -844,6 +805,8 @@ class B : A {}; } TEST(Introspection, SourceLocations_CXXBaseSpecifier_pack) { + if (!NodeIntrospection::hasIntrospectionSupport()) + return; auto AST = buildASTFromCodeWithArgs( R"cpp( template @@ -866,10 +829,6 @@ struct Templ : T... { auto Result = NodeIntrospection::GetLocations(Base); - if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) { - return; - } - auto ExpectedLocations = FormatExpected(Result.LocationAccessors);