steveire created this revision.
steveire added a reviewer: njames93.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The test code has lots of interesting locations which are not yet
introspected, but those will come later:

http://ce.steveire.com/z/3T90hR


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98775

Files:
  clang/include/clang/Tooling/NodeIntrospection.h
  clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
  clang/lib/Tooling/EmptyNodeIntrospection.inc.in
  clang/unittests/Introspection/IntrospectionTest.cpp

Index: clang/unittests/Introspection/IntrospectionTest.cpp
===================================================================
--- clang/unittests/Introspection/IntrospectionTest.cpp
+++ clang/unittests/Introspection/IntrospectionTest.cpp
@@ -42,7 +42,7 @@
 
 #define STRING_LOCATION_PAIR(INSTANCE, LOC) Pair(#LOC, INSTANCE->LOC)
 
-TEST(Introspection, SourceLocations) {
+TEST(Introspection, SourceLocations_Stmt) {
   auto AST = buildASTFromCode("void foo() {} void bar() { foo(); }", "foo.cpp",
                               std::make_shared<PCHContainerOperations>());
   auto &Ctx = AST->getASTContext();
@@ -80,3 +80,78 @@
               UnorderedElementsAre(
                   STRING_LOCATION_PAIR(FooCall, getSourceRange())));
 }
+
+TEST(Introspection, SourceLocations_Decl) {
+  auto AST =
+      buildASTFromCode(R"cpp(
+namespace ns1
+{
+namespace ns2
+{
+  template<typename T, typename U>
+  struct Foo
+  {
+  };
+  template<typename T, typename U>
+  struct Bar
+  {
+    struct Nested {
+      template<typename A, typename B>
+      Foo<A, B> method(int i, bool b) const noexcept;
+    };
+  };
+}
+}
+
+template<typename T, typename U>
+template<typename A, typename B>
+ns1::ns2::Foo<A, B> ns1::ns2::Bar<T, U>::Nested::method(int i, bool b) const noexcept(true)
+{
+
+}
+)cpp",
+                       "foo.cpp", std::make_shared<PCHContainerOperations>());
+  auto &Ctx = AST->getASTContext();
+  auto &TU = *Ctx.getTranslationUnitDecl();
+
+  auto BoundNodes = ast_matchers::match(
+      decl(hasDescendant(
+          cxxMethodDecl(hasName("method"), isDefinition()).bind("method"))),
+      TU, Ctx);
+
+  EXPECT_EQ(BoundNodes.size(), 1u);
+
+  auto *MethodDecl = BoundNodes[0].getNodeAs<CXXMethodDecl>("method");
+
+  auto Result = NodeIntrospection::GetLocations(MethodDecl);
+
+  if (Result.LocationAccessors.empty() && Result.RangeAccessors.empty()) {
+    return;
+  }
+
+  auto ExpectedLocations =
+      FormatExpected<SourceLocation>(Result.LocationAccessors);
+
+  EXPECT_THAT(ExpectedLocations,
+              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())));
+
+  auto ExpectedRanges = FormatExpected<SourceRange>(Result.RangeAccessors);
+
+  EXPECT_THAT(
+      ExpectedRanges,
+      UnorderedElementsAre(
+          STRING_LOCATION_PAIR(MethodDecl, getExceptionSpecSourceRange()),
+          STRING_LOCATION_PAIR(MethodDecl, getParametersSourceRange()),
+          STRING_LOCATION_PAIR(MethodDecl, getReturnTypeSourceRange()),
+          STRING_LOCATION_PAIR(MethodDecl, getSourceRange())));
+}
Index: clang/lib/Tooling/EmptyNodeIntrospection.inc.in
===================================================================
--- clang/lib/Tooling/EmptyNodeIntrospection.inc.in
+++ clang/lib/Tooling/EmptyNodeIntrospection.inc.in
@@ -12,6 +12,9 @@
 NodeLocationAccessors NodeIntrospection::GetLocations(clang::Stmt const *) {
   return {};
 }
+NodeLocationAccessors NodeIntrospection::GetLocations(clang::Decl const *) {
+  return {};
+}
 NodeLocationAccessors
 NodeIntrospection::GetLocations(clang::DynTypedNode const &) {
   return {};
Index: clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
===================================================================
--- clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
+++ clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.cpp
@@ -26,7 +26,8 @@
           isDefinition(),
           isSameOrDerivedFrom(
               // TODO: Extend this with other clades
-              namedDecl(hasName("clang::Stmt")).bind("nodeClade")),
+              namedDecl(hasAnyName("clang::Stmt", "clang::Decl"))
+                  .bind("nodeClade")),
           optionally(isDerivedFrom(cxxRecordDecl().bind("derivedFrom"))))
           .bind("className"),
       this);
Index: clang/include/clang/Tooling/NodeIntrospection.h
===================================================================
--- clang/include/clang/Tooling/NodeIntrospection.h
+++ clang/include/clang/Tooling/NodeIntrospection.h
@@ -78,6 +78,7 @@
 
 namespace NodeIntrospection {
 NodeLocationAccessors GetLocations(clang::Stmt const *Object);
+NodeLocationAccessors GetLocations(clang::Decl const *Object);
 NodeLocationAccessors GetLocations(clang::DynTypedNode const &Node);
 } // namespace NodeIntrospection
 } // namespace tooling
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to