[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 06:40:41 PDT 2023
ymandel updated this revision to Diff 553455.
ymandel marked 2 inline comments as done.
ymandel added a comment.
address comments
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.553455.patch
Type: text/x-patch
Size: 1715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230825/2dcd5fc3/attachment.bin>
More information about the cfe-commits
mailing list