[clang] [clang][Tooling] Fix `getFileRange` returning a range spanning macro invocation (PR #169842)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 27 09:40:32 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Eric Li (tJener)
<details>
<summary>Changes</summary>
A followup to 40991215f4aba37fd43b65d96ad0a445dcd041b2.
When the start or end token is inside a macro argument and the other is outside of the macro, we want to reject the range for a similar reason. The range will include half of the macro call, either the closing paren or the macro name and open paren.
---
Full diff: https://github.com/llvm/llvm-project/pull/169842.diff
2 Files Affected:
- (modified) clang/lib/Tooling/Transformer/SourceCode.cpp (+1-6)
- (modified) clang/unittests/Tooling/SourceCodeTest.cpp (+2)
``````````diff
diff --git a/clang/lib/Tooling/Transformer/SourceCode.cpp b/clang/lib/Tooling/Transformer/SourceCode.cpp
index fa9bf3427b8a0..7adceda05ad4f 100644
--- a/clang/lib/Tooling/Transformer/SourceCode.cpp
+++ b/clang/lib/Tooling/Transformer/SourceCode.cpp
@@ -114,12 +114,7 @@ static bool spelledInMacroDefinition(CharSourceRange Range,
return B.isInvalid() || B != E;
}
- if (Range.getBegin().isMacroID())
- return getMacroArgumentSpellingLoc(Range.getBegin(), SM).isInvalid();
- if (Range.getEnd().isMacroID())
- return getMacroArgumentSpellingLoc(Range.getEnd(), SM).isInvalid();
-
- return false;
+ return Range.getBegin().isMacroID() || Range.getEnd().isMacroID();
}
// Returns the expansion char-range of `Loc` if `Loc` is a split token. For
diff --git a/clang/unittests/Tooling/SourceCodeTest.cpp b/clang/unittests/Tooling/SourceCodeTest.cpp
index 2f59ced0ebc83..a998954a6e9ea 100644
--- a/clang/unittests/Tooling/SourceCodeTest.cpp
+++ b/clang/unittests/Tooling/SourceCodeTest.cpp
@@ -511,11 +511,13 @@ TEST(SourceCodeTest, EditInvolvingExpansionIgnoringExpansionShouldFail) {
#define M2(x, y) x ## y
#define M3(x) foobar(x)
#define M4(x, y) x y
+#define M5(x) x
int foobar(int);
int a = M1(foobar);
int b = M2(foo, bar(2));
int c = M3(3);
int d = M4(foobar, (4));
+int e = M5(foobar) (5);
)cpp");
CallsVisitor Visitor;
``````````
</details>
https://github.com/llvm/llvm-project/pull/169842
More information about the cfe-commits
mailing list