https://github.com/harrishancock updated https://github.com/llvm/llvm-project/pull/184891
>From ba3f7c35a6f2d86854bd5b725e5a872a50ee1351 Mon Sep 17 00:00:00 2001 From: Harris Hancock <[email protected]> Date: Thu, 5 Mar 2026 19:12:48 +0000 Subject: [PATCH] [clang-format] Fix Macros configuration not working with try/catch expansions The lexer heuristic tryTransformTryUsageForC() was intended to allow C code to use `try` as an identifier by demoting tok::kw_try to tok::identifier when the following token did not look like the start of a try body. However, when MacroExpander::parseDefinition() lexed a macro like "TRY_MACRO=try", the `try` token in the expansion body was followed by eof, triggering the heuristic. This caused `try` to be demoted to an identifier in the macro definition, so expanded code was never parsed as a try/catch statement. Delete tryTransformTryUsageForC() entirely. The heuristic predates the introduction of LK_C, which makes it unnecessary: `try` is only a non-keyword in C, and LK_C already handles it correctly without any special lexer intervention. The existing FormatTryAsAVariable test cases for `try` as a variable are updated to use LK_C. Assisted-by: Claude (anthropic.com) --- clang/lib/Format/FormatTokenLexer.cpp | 22 ---------------------- clang/lib/Format/FormatTokenLexer.h | 1 - clang/unittests/Format/FormatTest.cpp | 10 ++++++---- 3 files changed, 6 insertions(+), 27 deletions(-) diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 4a087d9e6dc2b..0dc6f776aeca0 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -163,8 +163,6 @@ void FormatTokenLexer::tryMergePreviousTokens() { return; if (tryMergeForEach()) return; - if (Style.isCpp() && tryTransformTryUsageForC()) - return; if ((Style.Language == FormatStyle::LK_Cpp || Style.Language == FormatStyle::LK_ObjC) && @@ -533,26 +531,6 @@ bool FormatTokenLexer::tryMergeForEach() { return true; } -bool FormatTokenLexer::tryTransformTryUsageForC() { - if (Tokens.size() < 2) - return false; - auto &Try = *(Tokens.end() - 2); - if (Try->isNot(tok::kw_try)) - return false; - auto &Next = *(Tokens.end() - 1); - if (Next->isOneOf(tok::l_brace, tok::colon, tok::hash, tok::comment)) - return false; - - if (Tokens.size() > 2) { - auto &At = *(Tokens.end() - 3); - if (At->is(tok::at)) - return false; - } - - Try->Tok.setKind(tok::identifier); - return true; -} - bool FormatTokenLexer::tryMergeLessLess() { // Merge X,less,less,Y into X,lessless,Y unless X or Y is less. if (Tokens.size() < 3) diff --git a/clang/lib/Format/FormatTokenLexer.h b/clang/lib/Format/FormatTokenLexer.h index 57c572af3defd..4141e1434f72f 100644 --- a/clang/lib/Format/FormatTokenLexer.h +++ b/clang/lib/Format/FormatTokenLexer.h @@ -56,7 +56,6 @@ class FormatTokenLexer { bool tryMergeNullishCoalescingEqual(); bool tryTransformCSharpForEach(); bool tryMergeForEach(); - bool tryTransformTryUsageForC(); // Merge the most recently lexed tokens into a single token if their kinds are // correct. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 33af71de398be..1359119454479 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -4896,9 +4896,11 @@ TEST_F(FormatTest, FormatTryCatch) { } TEST_F(FormatTest, FormatTryAsAVariable) { - verifyFormat("int try;"); - verifyFormat("int try, size;"); - verifyFormat("try = foo();"); + auto Style = getLLVMStyle(FormatStyle::LK_C); + verifyFormat("int try;", Style); + verifyFormat("int try, size;", Style); + verifyFormat("try = foo();", Style); + verifyFormat("if (try < size) {\n return true;\n}"); verifyFormat("int catch;"); @@ -4906,7 +4908,7 @@ TEST_F(FormatTest, FormatTryAsAVariable) { verifyFormat("catch = foo();"); verifyFormat("if (catch < size) {\n return true;\n}"); - FormatStyle Style = getLLVMStyle(); + Style.Language = FormatStyle::LK_Cpp; Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterFunction = true; Style.BraceWrapping.BeforeCatch = true; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
