[clang] be5c7c5 - Widen `name` stencil to support `TypeLoc` nodes.

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Thu May 13 15:23:21 PDT 2021


Author: Weston Carvalho
Date: 2021-05-13T23:23:12+01:00
New Revision: be5c7c5d8230428f024bd656beb48ef8462985ff

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

LOG: Widen `name` stencil to support `TypeLoc` nodes.

Differential Revision: https://reviews.llvm.org/D102185

Added: 
    

Modified: 
    clang/include/clang/Tooling/Transformer/RangeSelector.h
    clang/lib/Tooling/Transformer/RangeSelector.cpp
    clang/unittests/Tooling/RangeSelectorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/Transformer/RangeSelector.h b/clang/include/clang/Tooling/Transformer/RangeSelector.h
index f17fb8c7b5c69..8ff31f7a03428 100644
--- a/clang/include/clang/Tooling/Transformer/RangeSelector.h
+++ b/clang/include/clang/Tooling/Transformer/RangeSelector.h
@@ -73,9 +73,9 @@ RangeSelector statement(std::string ID);
 /// binding in the match result.
 RangeSelector member(std::string ID);
 
-/// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr or \c
-/// CxxCtorInitializer) selects the name's token.  Only selects the final
-/// identifier of a qualified name, but not any qualifiers or template
+/// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr, \c
+/// CxxCtorInitializer, and \c TypeLoc) selects the name's token.  Only selects
+/// the final identifier of a qualified name, but not any qualifiers or template
 /// arguments.  For example, for `::foo::bar::baz` and `::foo::bar::baz<int>`,
 /// it selects only `baz`.
 ///

diff  --git a/clang/lib/Tooling/Transformer/RangeSelector.cpp b/clang/lib/Tooling/Transformer/RangeSelector.cpp
index 0f3138db218af..753e89e0e1f33 100644
--- a/clang/lib/Tooling/Transformer/RangeSelector.cpp
+++ b/clang/lib/Tooling/Transformer/RangeSelector.cpp
@@ -8,6 +8,7 @@
 
 #include "clang/Tooling/Transformer/RangeSelector.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/TypeLoc.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Lex/Lexer.h"
@@ -228,8 +229,16 @@ RangeSelector transformer::name(std::string ID) {
       SourceLocation L = I->getMemberLocation();
       return CharSourceRange::getTokenRange(L, L);
     }
+    if (const auto *T = Node.get<TypeLoc>()) {
+      TypeLoc Loc = *T;
+      auto ET = Loc.getAs<ElaboratedTypeLoc>();
+      if (!ET.isNull()) {
+        Loc = ET.getNamedTypeLoc();
+      }
+      return CharSourceRange::getTokenRange(Loc.getSourceRange());
+    }
     return typeError(ID, Node.getNodeKind(),
-                     "DeclRefExpr, NamedDecl, CXXCtorInitializer");
+                     "DeclRefExpr, NamedDecl, CXXCtorInitializer, TypeLoc");
   };
 }
 

diff  --git a/clang/unittests/Tooling/RangeSelectorTest.cpp b/clang/unittests/Tooling/RangeSelectorTest.cpp
index 0dded6963ee1b..a7fea004c3d3b 100644
--- a/clang/unittests/Tooling/RangeSelectorTest.cpp
+++ b/clang/unittests/Tooling/RangeSelectorTest.cpp
@@ -457,6 +457,35 @@ TEST(RangeSelectorTest, NameOpCtorInitializer) {
   EXPECT_THAT_EXPECTED(select(name(Init), Match), HasValue("field"));
 }
 
+TEST(RangeSelectorTest, NameOpTypeLoc) {
+  StringRef Code = R"cc(
+    namespace ns {
+    struct Foo {
+      Foo();
+      Foo(int);
+      Foo(int, int);
+    };
+    }  // namespace ns
+
+    ns::Foo a;
+    auto b = ns::Foo(3);
+    auto c = ns::Foo(1, 2);
+  )cc";
+  const char *CtorTy = "ctor_ty";
+  // Matches declaration of `a`
+  TestMatch MatchA = matchCode(
+      Code, varDecl(hasName("a"), hasTypeLoc(typeLoc().bind(CtorTy))));
+  EXPECT_THAT_EXPECTED(select(name(CtorTy), MatchA), HasValue("Foo"));
+  // Matches call of Foo(int)
+  TestMatch MatchB = matchCode(
+      Code, cxxFunctionalCastExpr(hasTypeLoc(typeLoc().bind(CtorTy))));
+  EXPECT_THAT_EXPECTED(select(name(CtorTy), MatchB), HasValue("Foo"));
+  // Matches call of Foo(int, int)
+  TestMatch MatchC = matchCode(
+      Code, cxxTemporaryObjectExpr(hasTypeLoc(typeLoc().bind(CtorTy))));
+  EXPECT_THAT_EXPECTED(select(name(CtorTy), MatchC), HasValue("Foo"));
+}
+
 TEST(RangeSelectorTest, NameOpErrors) {
   EXPECT_THAT_EXPECTED(selectFromTrivial(name("unbound_id")),
                        Failed<StringError>(withUnboundNodeMessage()));


        


More information about the cfe-commits mailing list