[clang] f019e5f - [AST][Introspection] Add a check to detect if introspection is supported.

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 15 14:21:49 PDT 2021


Author: Nathan James
Date: 2021-04-15T22:21:41+01:00
New Revision: f019e5f73ed732b374e376f5ddbba5d1f67dca0c

URL: https://github.com/llvm/llvm-project/commit/f019e5f73ed732b374e376f5ddbba5d1f67dca0c
DIFF: https://github.com/llvm/llvm-project/commit/f019e5f73ed732b374e376f5ddbba5d1f67dca0c.diff

LOG: [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

Added: 
    

Modified: 
    clang/include/clang/Tooling/NodeIntrospection.h
    clang/lib/Tooling/CMakeLists.txt
    clang/lib/Tooling/DumpTool/generate_cxx_src_locs.py
    clang/unittests/Introspection/IntrospectionTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/NodeIntrospection.h b/clang/include/clang/Tooling/NodeIntrospection.h
index 9147c7db6c27..5489a67efa22 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 6d70c8976f03..0da3dbd0b927 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 0adebeb3b3bf..b0953df19203 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 4a684f26a624..880068c43b6e 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<PCHContainerOperations>());
   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<SourceLocation>(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<A, B> ns1::ns2::Bar<T, U>::Nested::method(int i, bool b) const
 
   auto Result = NodeIntrospection::GetLocations(MethodDecl);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -148,6 +143,8 @@ ns1::ns2::Foo<A, B> ns1::ns2::Bar<T, U>::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<SourceLocation>(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<typename T>
@@ -219,10 +214,6 @@ void foo()
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -236,6 +227,8 @@ void foo()
 }
 
 TEST(Introspection, SourceLocations_TA_Decl) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<void(*Ty)()>
@@ -258,10 +251,6 @@ void test() {
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -275,6 +264,8 @@ void test() {
 }
 
 TEST(Introspection, SourceLocations_TA_Nullptr) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<void(*Ty)()>
@@ -297,10 +288,6 @@ void test() {
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -314,6 +301,8 @@ void test() {
 }
 
 TEST(Introspection, SourceLocations_TA_Integral) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<int>
@@ -335,10 +324,6 @@ void test() {
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -352,6 +337,8 @@ void test() {
 }
 
 TEST(Introspection, SourceLocations_TA_Template) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<typename T> class A;
@@ -374,10 +361,6 @@ void bar()
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -393,6 +376,8 @@ void bar()
 }
 
 TEST(Introspection, SourceLocations_TA_TemplateExpansion) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<template<typename> class ...> class B { };
@@ -414,10 +399,6 @@ template<template<typename> class ...> class B { };
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -434,6 +415,8 @@ template<template<typename> class ...> class B { };
 }
 
 TEST(Introspection, SourceLocations_TA_Expression) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<int, int = 0> class testExpr;
@@ -452,10 +435,6 @@ template<int I> class testExpr<I> { };
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -469,6 +448,8 @@ template<int I> class testExpr<I> { };
 }
 
 TEST(Introspection, SourceLocations_TA_Pack) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<typename... T> class A {};
@@ -491,10 +472,6 @@ void foo()
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(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<SourceLocation>(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<SourceLocation>(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<SourceLocation>(Result.LocationAccessors);
 
@@ -637,6 +608,8 @@ struct C {
 }
 
 TEST(Introspection, SourceLocations_CXXCtorInitializer_pack) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<typename... T>
@@ -664,10 +637,6 @@ struct D : Templ<T...> {
 
   auto Result = NodeIntrospection::GetLocations(CtorInit);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -686,6 +655,8 @@ struct D : Templ<T...> {
 }
 
 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<SourceLocation>(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<SourceLocation>(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<SourceLocation>(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<typename T, typename U>
@@ -825,10 +790,6 @@ class B : A<int, bool> {};
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -844,6 +805,8 @@ class B : A<int, bool> {};
 }
 
 TEST(Introspection, SourceLocations_CXXBaseSpecifier_pack) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<typename... T>
@@ -866,10 +829,6 @@ struct Templ : T... {
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 


        


More information about the cfe-commits mailing list