Skip to content

[Clang][Preprocessor] Expand UCNs in macro concatenation #145351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ Bug Fixes in This Version
- Fixed incorrect token location when emitting diagnostics for tokens expanded from macros. (#GH143216)
- Fixed an infinite recursion when checking constexpr destructors. (#GH141789)
- Fixed a crash when a malformed using declaration appears in a ``constexpr`` function. (#GH144264)
- Fixed a bug when use unicode character name in macro concatenation. (#GH145240)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Lex/TokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
const char *ResultTokStrPtr = nullptr;
SourceLocation StartLoc = LHSTok.getLocation();
SourceLocation PasteOpLoc;
bool HasUCNs = false;

auto IsAtEnd = [&TokenStream, &CurIdx] {
return TokenStream.size() == CurIdx;
Expand Down Expand Up @@ -885,6 +886,9 @@ bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,

// Finally, replace LHS with the result, consume the RHS, and iterate.
++CurIdx;

// Set Token::HasUCN flag if LHS or RHS contains any UCNs.
HasUCNs = LHSTok.hasUCN() || RHS.hasUCN() || HasUCNs;
LHSTok = Result;
} while (!IsAtEnd() && TokenStream[CurIdx].is(tok::hashhash));

Expand Down Expand Up @@ -913,6 +917,13 @@ bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
// token pasting re-lexes the result token in raw mode, identifier information
// isn't looked up. As such, if the result is an identifier, look up id info.
if (LHSTok.is(tok::raw_identifier)) {

// If there has any UNCs in concated token, we should mark this token
// with Token::HasUCN flag, then LookUpIdentifierInfo will expand UCNs in
// token.
if (HasUCNs)
LHSTok.setFlag(Token::HasUCN);

// Look up the identifier info for the token. We disabled identifier lookup
// by saying we're skipping contents, so we need to do this manually.
PP.LookUpIdentifierInfo(LHSTok);
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Preprocessor/macro_paste_identifier_ucn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %clang_cc1 -fms-extensions %s -verify
// RUN: %clang_cc1 -E -fms-extensions %s | FileCheck %s
// expected-no-diagnostics

#define CAT(a,b) a##b

char foo\u00b5;
char*p = &CAT(foo, \u00b5);
// CHECK: char fooµ;
// CHECK-NEXT: char*p = &fooµ;
Loading