[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
Thu Aug 24 13:23:14 PDT 2023
ymandel created this revision.
ymandel added reviewers: sammccall, li.zhe.hua.
Herald added a project: All.
ymandel requested review of this revision.
Herald added a project: clang.
Previously, where `name` was applied to a template-specialization TypeLoc, it
returned the entire specialization (name, brackets, args) rather than just the
name identifier. With this change, exactly the name token is selected.
Repository:
rG LLVM Github Monorepo
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 { T x; };
+ } // 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,12 @@
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(),
+ SpecLoc.getTemplateNameLoc());
return CharSourceRange::getTokenRange(Loc.getSourceRange());
}
return typeError(ID, Node.getNodeKind(),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158771.553241.patch
Type: text/x-patch
Size: 1798 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230824/91d1ae02/attachment.bin>
More information about the cfe-commits
mailing list