[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

GitOrigin-RevId: f019e5f73ed732b374e376f5ddbba5d1f67dca0c
diff --git a/include/clang/Tooling/NodeIntrospection.h b/include/clang/Tooling/NodeIntrospection.h
index 9147c7d..5489a67 100644
--- a/include/clang/Tooling/NodeIntrospection.h
+++ b/include/clang/Tooling/NodeIntrospection.h
@@ -85,6 +85,7 @@
 };
 
 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/lib/Tooling/CMakeLists.txt b/lib/Tooling/CMakeLists.txt
index 6d70c89..0da3dbd 100644
--- a/lib/Tooling/CMakeLists.txt
+++ b/lib/Tooling/CMakeLists.txt
@@ -35,6 +35,8 @@
 namespace clang {
 namespace tooling {
 
+bool NodeIntrospection::hasIntrospectionSupport() { return false; }
+
 NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) {
   return {};
 }
diff --git a/lib/Tooling/DumpTool/generate_cxx_src_locs.py b/lib/Tooling/DumpTool/generate_cxx_src_locs.py
index 0adebeb..b0953df 100755
--- a/lib/Tooling/DumpTool/generate_cxx_src_locs.py
+++ b/lib/Tooling/DumpTool/generate_cxx_src_locs.py
@@ -28,6 +28,8 @@
 
 using LocationAndString = SourceLocationMap::value_type;
 using RangeAndString = SourceRangeMap::value_type;
+
+bool NodeIntrospection::hasIntrospectionSupport() { return true; }
 """
 
     def GenerateBaseGetLocationsDeclaration(self, CladeName):
@@ -174,6 +176,8 @@
 namespace clang {
 namespace tooling {
 
+bool NodeIntrospection::hasIntrospectionSupport() { return false; }
+
 NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) {
   return {};
 }
diff --git a/unittests/Introspection/IntrospectionTest.cpp b/unittests/Introspection/IntrospectionTest.cpp
index 4a684f2..880068c 100644
--- a/unittests/Introspection/IntrospectionTest.cpp
+++ b/unittests/Introspection/IntrospectionTest.cpp
@@ -46,6 +46,8 @@
 #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 @@
 
   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_Decl) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 namespace ns1 {
@@ -118,10 +117,6 @@
 
   auto Result = NodeIntrospection::GetLocations(MethodDecl);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -148,6 +143,8 @@
 }
 
 TEST(Introspection, SourceLocations_NNS) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 namespace ns
@@ -171,10 +168,6 @@
 
   auto Result = NodeIntrospection::GetLocations(NNS);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -194,6 +187,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Type) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<typename T>
@@ -219,10 +214,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -236,6 +227,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Decl) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<void(*Ty)()>
@@ -258,10 +251,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -275,6 +264,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Nullptr) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<void(*Ty)()>
@@ -297,10 +288,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -314,6 +301,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Integral) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<int>
@@ -335,10 +324,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -352,6 +337,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Template) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<typename T> class A;
@@ -374,10 +361,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -393,6 +376,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_TemplateExpansion) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<template<typename> class ...> class B { };
@@ -414,10 +399,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -434,6 +415,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Expression) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<int, int = 0> class testExpr;
@@ -452,10 +435,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -469,6 +448,8 @@
 }
 
 TEST(Introspection, SourceLocations_TA_Pack) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<typename... T> class A {};
@@ -491,10 +472,6 @@
 
   auto Result = NodeIntrospection::GetLocations(TA);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -508,6 +485,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXCtorInitializer_base) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 struct A {
@@ -532,10 +511,6 @@
 
   auto Result = NodeIntrospection::GetLocations(CtorInit);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -552,6 +527,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXCtorInitializer_member) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 struct A {
@@ -574,10 +551,6 @@
 
   auto Result = NodeIntrospection::GetLocations(CtorInit);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -595,6 +568,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXCtorInitializer_ctor) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 struct C {
@@ -617,10 +592,6 @@
 
   auto Result = NodeIntrospection::GetLocations(CtorInit);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -637,6 +608,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXCtorInitializer_pack) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<typename... T>
@@ -664,10 +637,6 @@
 
   auto Result = NodeIntrospection::GetLocations(CtorInit);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -686,6 +655,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXBaseSpecifier_plain) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 class A {};
@@ -706,10 +677,6 @@
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -725,6 +692,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXBaseSpecifier_accessspec) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 class A {};
@@ -745,10 +714,6 @@
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -764,6 +729,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXBaseSpecifier_virtual) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 class A {};
@@ -785,10 +752,6 @@
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -804,6 +767,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXBaseSpecifier_template_base) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST =
       buildASTFromCode(R"cpp(
 template<typename T, typename U>
@@ -825,10 +790,6 @@
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);
 
@@ -844,6 +805,8 @@
 }
 
 TEST(Introspection, SourceLocations_CXXBaseSpecifier_pack) {
+  if (!NodeIntrospection::hasIntrospectionSupport())
+    return;
   auto AST = buildASTFromCodeWithArgs(
       R"cpp(
 template<typename... T>
@@ -866,10 +829,6 @@
 
   auto Result = NodeIntrospection::GetLocations(Base);
 
-  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
-    return;
-  }
-
   auto ExpectedLocations =
       FormatExpected<SourceLocation>(Result.LocationAccessors);