Skip to content

Conversation

@yuhaouy
Copy link
Contributor

@yuhaouy yuhaouy commented Oct 22, 2025

Previously, when the text in selected range was different from the decl's name, name returned an invalid range, which could cause crashes if name was nested in other range selectors that assumed always valid ranges. With this change, name returns an Error if it can't get the range.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-clang

Author: Yu Hao (yuhaouy)

Changes

Previously, when the text in selected range was different from the decl's name, name returned an invalid range, which could cause crashes if name was nested in other range selectors that assumed always valid ranges. With this change, name returns an Error if it can't get the range.


Full diff: https://github.com/llvm/llvm-project/pull/164715.diff

1 Files Affected:

  • (modified) clang/lib/Tooling/Transformer/RangeSelector.cpp (+6-2)
diff --git a/clang/lib/Tooling/Transformer/RangeSelector.cpp b/clang/lib/Tooling/Transformer/RangeSelector.cpp
index 171c786bc366f..fbde6e4a2b914 100644
--- a/clang/lib/Tooling/Transformer/RangeSelector.cpp
+++ b/clang/lib/Tooling/Transformer/RangeSelector.cpp
@@ -205,8 +205,12 @@ RangeSelector transformer::name(std::string ID) {
       // `foo<int>` for which this range will be too short.  Doing so will
       // require subcasing `NamedDecl`, because it doesn't provide virtual
       // access to the \c DeclarationNameInfo.
-      if (tooling::getText(R, *Result.Context) != D->getName())
-        return CharSourceRange();
+      StringRef Text = tooling::getText(R, *Result.Context);
+      if (Text != D->getName())
+        return llvm::make_error<StringError>(
+            llvm::errc::not_supported,
+            "Failed to get the name range for the decl. Name is " +
+                D->getName() + ", but range is " + Text);
       return R;
     }
     if (const auto *E = Node.get<DeclRefExpr>()) {

@fmayer fmayer requested a review from ymand October 22, 2025 22:05
@fmayer
Copy link
Contributor

fmayer commented Oct 22, 2025

Looks reasonable

Copy link
Collaborator

@ymand ymand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@ymand
Copy link
Collaborator

ymand commented Oct 23, 2025

Actually -- are there any tests you can add/updated to reflect?

@yuhaouy yuhaouy force-pushed the yuhaouy-fix-name-range-selector branch from bf13452 to 23b4523 Compare October 23, 2025 23:12
@yuhaouy
Copy link
Contributor Author

yuhaouy commented Oct 23, 2025

Actually -- are there any tests you can add/updated to reflect?

Good point. I've only seen name failing for ranges in macro body. name works if the range is in macro arg.
Added tests for both macro body and args.

…nstead of an invalid range.

Previously, when the text in selected range was different from the decl's name, `name` returned an invalid range, which could cause crashes if `name` was nested in other range selectors that  assumed always valid ranges. With this change, `name` returns an `Error` if it can't get the range.
@yuhaouy yuhaouy force-pushed the yuhaouy-fix-name-range-selector branch from 23b4523 to 57dd8a9 Compare October 24, 2025 19:59
@yuhaouy yuhaouy requested a review from ymand October 30, 2025 19:12
@fmayer fmayer enabled auto-merge (squash) October 30, 2025 20:25
@fmayer fmayer merged commit 067e205 into llvm:main Oct 31, 2025
10 checks passed
@github-actions
Copy link

@yuhaouy Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

DEBADRIBASAK pushed a commit to DEBADRIBASAK/llvm-project that referenced this pull request Nov 3, 2025
…nstead of an invalid range. (llvm#164715)

Previously, when the text in selected range was different from the
decl's name, `name` returned an invalid range, which could cause crashes
if `name` was nested in other range selectors that assumed always valid
ranges. With this change, `name` returns an `Error` if it can't get the
range.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants