Skip to content

Conversation

@tJener
Copy link
Contributor

@tJener tJener commented Nov 27, 2025

A followup to 4099121.

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.

…invocation

A followup to 4099121.

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.
@tJener tJener requested review from legrosbuffle and ymand November 27, 2025 17:39
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Nov 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2025

@llvm/pr-subscribers-clang

Author: Eric Li (tJener)

Changes

A followup to 4099121.

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 --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;

@tJener tJener merged commit 73d1602 into llvm:main Nov 28, 2025
12 checks passed
@tJener tJener deleted the macros branch November 28, 2025 16:32
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.

3 participants