Skip to content

Commit c783258

Browse files
committed
[clang][tooling] Fix name range-selector to handle TypeLocs correctly.
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. Differential Revision: https://reviews.llvm.org/D158771
1 parent 50b1276 commit c783258

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

clang/lib/Tooling/Transformer/RangeSelector.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,11 @@ RangeSelector transformer::name(std::string ID) {
232232
if (const auto *T = Node.get<TypeLoc>()) {
233233
TypeLoc Loc = *T;
234234
auto ET = Loc.getAs<ElaboratedTypeLoc>();
235-
if (!ET.isNull()) {
235+
if (!ET.isNull())
236236
Loc = ET.getNamedTypeLoc();
237-
}
237+
if (auto SpecLoc = Loc.getAs<TemplateSpecializationTypeLoc>();
238+
!SpecLoc.isNull())
239+
return CharSourceRange::getTokenRange(SpecLoc.getTemplateNameLoc());
238240
return CharSourceRange::getTokenRange(Loc.getSourceRange());
239241
}
240242
return typeError(ID, Node.getNodeKind(),

clang/unittests/Tooling/RangeSelectorTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,22 @@ TEST(RangeSelectorTest, NameOpTypeLoc) {
486486
EXPECT_THAT_EXPECTED(select(name(CtorTy), MatchC), HasValue("Foo"));
487487
}
488488

489+
TEST(RangeSelectorTest, NameOpTemplateSpecializationTypeLoc) {
490+
StringRef Code = R"cc(
491+
namespace ns {
492+
template <typename T>
493+
struct Foo {};
494+
} // namespace ns
495+
496+
ns::Foo<int> a;
497+
)cc";
498+
const char *Loc = "tyloc";
499+
// Matches declaration of `a`.
500+
TestMatch MatchA =
501+
matchCode(Code, varDecl(hasName("a"), hasTypeLoc(typeLoc().bind(Loc))));
502+
EXPECT_THAT_EXPECTED(select(name(Loc), MatchA), HasValue("Foo"));
503+
}
504+
489505
TEST(RangeSelectorTest, NameOpErrors) {
490506
EXPECT_THAT_EXPECTED(selectFromTrivial(name("unbound_id")),
491507
Failed<StringError>(withUnboundNodeMessage()));

0 commit comments

Comments
 (0)