[PATCH] D158771: [clang][tooling] Fix `name` range-selector to handle TypeLocs correctly.

Yitzhak Mandelbaum via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 25 07:45:47 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc78325815062: [clang][tooling] Fix `name` range-selector to handle TypeLocs correctly. (authored by ymandel).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158771/new/

https://reviews.llvm.org/D158771

Files:
  clang/lib/Tooling/Transformer/RangeSelector.cpp
  clang/unittests/Tooling/RangeSelectorTest.cpp


Index: clang/unittests/Tooling/RangeSelectorTest.cpp
===================================================================
--- clang/unittests/Tooling/RangeSelectorTest.cpp
+++ clang/unittests/Tooling/RangeSelectorTest.cpp
@@ -486,6 +486,22 @@
   EXPECT_THAT_EXPECTED(select(name(CtorTy), MatchC), HasValue("Foo"));
 }
 
+TEST(RangeSelectorTest, NameOpTemplateSpecializationTypeLoc) {
+  StringRef Code = R"cc(
+    namespace ns {
+    template <typename T>
+    struct Foo {};
+    }  // namespace ns
+
+    ns::Foo<int> a;
+  )cc";
+  const char *Loc = "tyloc";
+  // Matches declaration of `a`.
+  TestMatch MatchA =
+      matchCode(Code, varDecl(hasName("a"), hasTypeLoc(typeLoc().bind(Loc))));
+  EXPECT_THAT_EXPECTED(select(name(Loc), MatchA), HasValue("Foo"));
+}
+
 TEST(RangeSelectorTest, NameOpErrors) {
   EXPECT_THAT_EXPECTED(selectFromTrivial(name("unbound_id")),
                        Failed<StringError>(withUnboundNodeMessage()));
Index: clang/lib/Tooling/Transformer/RangeSelector.cpp
===================================================================
--- clang/lib/Tooling/Transformer/RangeSelector.cpp
+++ clang/lib/Tooling/Transformer/RangeSelector.cpp
@@ -232,9 +232,11 @@
     if (const auto *T = Node.get<TypeLoc>()) {
       TypeLoc Loc = *T;
       auto ET = Loc.getAs<ElaboratedTypeLoc>();
-      if (!ET.isNull()) {
+      if (!ET.isNull())
         Loc = ET.getNamedTypeLoc();
-      }
+      if (auto SpecLoc = Loc.getAs<TemplateSpecializationTypeLoc>();
+          !SpecLoc.isNull())
+        return CharSourceRange::getTokenRange(SpecLoc.getTemplateNameLoc());
       return CharSourceRange::getTokenRange(Loc.getSourceRange());
     }
     return typeError(ID, Node.getNodeKind(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158771.553472.patch
Type: text/x-patch
Size: 1715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230825/5933e14e/attachment-0001.bin>


More information about the cfe-commits mailing list