https://github.com/luoliwoshang created 
https://github.com/llvm/llvm-project/pull/184555

Fixes #184425

When parsing an alias declaration like:

```c++
using A = X<int>>;
```

clang splits `>>` into two `>` tokens while parsing templates. In this 
token-split case, `Lexer::getLocForEndOfToken` was advancing past the 
already-correct expansion end location, which moved the fix-it insertion point 
too far right (at `;` instead of before the second `>`).

This patch detects token-split expansion ranges and returns the expansion end 
location directly after `isAtEndOfMacroExpansion`, avoiding the extra 
token-length advance.

Also adds a regression test in 
`clang/test/Parser/cxx-alias-decl-split-angle-fixit.cpp` that checks the fix-it 
insertion location is `{4:17-4:17}` for the example above.

Local validation:
- `tools/clang/lib/Lex/CMakeFiles/obj.clangLex.dir/Lexer.cpp.o` builds 
successfully
- full `clang` build/test still in progress


From 13312f0cd5defe71ebd3bf8659a5d3a72c81afef Mon Sep 17 00:00:00 2001
From: luoliwoshang <[email protected]>
Date: Wed, 4 Mar 2026 15:01:37 +0800
Subject: [PATCH] [clang] Fix alias-decl fix-it location with token-split
 expansion

---
 clang/lib/Lex/Lexer.cpp                                | 9 +++++++++
 clang/test/Parser/cxx-alias-decl-split-angle-fixit.cpp | 7 +++++++
 2 files changed, 16 insertions(+)
 create mode 100644 clang/test/Parser/cxx-alias-decl-split-angle-fixit.cpp

diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index cbf0c77232db7..f9f9a57330ac3 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -862,8 +862,17 @@ SourceLocation Lexer::getLocForEndOfToken(SourceLocation 
Loc, unsigned Offset,
     return {};
 
   if (Loc.isMacroID()) {
+    // Token-split expansion ranges (for example, when splitting '>>' into two
+    // '>' tokens while parsing templates) are character ranges, so the
+    // expansion end location already points just past the split token.
+    const bool IsTokenSplitRange =
+        !SM.getSLocEntry(SM.getFileID(Loc))
+             .getExpansion()
+             .isExpansionTokenRange();
     if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
       return {}; // Points inside the macro expansion.
+    if (IsTokenSplitRange)
+      return Loc;
   }
 
   unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
diff --git a/clang/test/Parser/cxx-alias-decl-split-angle-fixit.cpp 
b/clang/test/Parser/cxx-alias-decl-split-angle-fixit.cpp
new file mode 100644
index 0000000000000..fc683431a958a
--- /dev/null
+++ b/clang/test/Parser/cxx-alias-decl-split-angle-fixit.cpp
@@ -0,0 +1,7 @@
+// RUN: not %clang_cc1 -fsyntax-only -std=c++11 -fdiagnostics-parseable-fixits 
%s 2>&1 | FileCheck %s
+
+template <typename> struct X {};
+using A = X<int>>;
+
+// CHECK: error: expected ';' after alias declaration
+// CHECK: fix-it:"{{.*}}":{4:17-4:17}:";"

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to