[clang] c783258 - [clang][tooling] Fix `name` range-selector to handle TypeLocs correctly.
Yitzhak Mandelbaum via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 25 07:45:34 PDT 2023
Author: Yitzhak Mandelbaum
Date: 2023-08-25T14:44:43Z
New Revision: c78325815062d62f0e2c68b6e72bb31b60c48260
URL: https://github.com/llvm/llvm-project/commit/c78325815062d62f0e2c68b6e72bb31b60c48260
DIFF: https://github.com/llvm/llvm-project/commit/c78325815062d62f0e2c68b6e72bb31b60c48260.diff
LOG: [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
Added:
Modified:
clang/lib/Tooling/Transformer/RangeSelector.cpp
clang/unittests/Tooling/RangeSelectorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Tooling/Transformer/RangeSelector.cpp b/clang/lib/Tooling/Transformer/RangeSelector.cpp
index 753e89e0e1f330..7370baf010834c 100644
--- a/clang/lib/Tooling/Transformer/RangeSelector.cpp
+++ b/clang/lib/Tooling/Transformer/RangeSelector.cpp
@@ -232,9 +232,11 @@ RangeSelector transformer::name(std::string ID) {
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(),
diff --git a/clang/unittests/Tooling/RangeSelectorTest.cpp b/clang/unittests/Tooling/RangeSelectorTest.cpp
index a7fea004c3d3bf..cbb8e25bb92f24 100644
--- a/clang/unittests/Tooling/RangeSelectorTest.cpp
+++ b/clang/unittests/Tooling/RangeSelectorTest.cpp
@@ -486,6 +486,22 @@ TEST(RangeSelectorTest, NameOpTypeLoc) {
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()));
More information about the cfe-commits
mailing list