[clang] [clang][Tooling] Fix `getFileRange` returning a range spanning macro invocation (PR #169842)
Eric Li via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 27 09:40:03 PST 2025
https://github.com/tJener created https://github.com/llvm/llvm-project/pull/169842
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.
>From 0c30e724095f5170e1805fd002b47e10ec006347 Mon Sep 17 00:00:00 2001
From: Eric Li <li.zhe.hua at gmail.com>
Date: Thu, 27 Nov 2025 12:33:34 -0500
Subject: [PATCH] [clang][Tooling] Fix `getFileRange` returning a range
spanning macro invocation
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.
---
clang/lib/Tooling/Transformer/SourceCode.cpp | 7 +------
clang/unittests/Tooling/SourceCodeTest.cpp | 2 ++
2 files changed, 3 insertions(+), 6 deletions(-)
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;
More information about the cfe-commits
mailing list