[clang] [clang-format] Keep macro tokens out of enclosing brace lookbehind (PR #247227)

Raiyyan via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 21:43:01 PST 2026


https://github.com/7se7en72025 updated https://github.com/llvm/llvm-project/pull/247227

>From 7a324afbf51bac59aafdb2d7f47cbdcae9435a8a Mon Sep 17 00:00:00 2001
From: 7se7en72025 <f20241312 at pilani.bits-pilani.ac.in>
Date: Mon, 30 Nov 2026 21:54:47 +0530
Subject: [PATCH 1/2] [clang-format] Keep macro tokens out of enclosing brace
 lookbehind

Assisted-by: OpenAI Codex
---
 clang/lib/Format/UnwrappedLineParser.cpp |  9 ++++++++-
 clang/unittests/Format/FormatTest.cpp    | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 28f240553cd63c..93f88d65106915 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -516,13 +516,20 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
   SmallVector<StackEntry, 8> LBraceStack;
   assert(Tok->is(tok::l_brace));
 
+  const FormatToken *BeforeMacro = nullptr;
   do {
     auto *NextTok = Tokens->getNextNonComment();
+    const FormatToken *PreviousTokenForNext = Tok;
+    // A macro replacement token is not adjacent to the surrounding code.
+    if (BeforeMacro && NextTok->HasUnescapedNewline)
+      PreviousTokenForNext = std::exchange(BeforeMacro, nullptr);
 
     if (!Line->InMacroBody && !Style.isTableGen()) {
       // Skip PPDirective lines (except macro definitions) and comments.
       while (NextTok->is(tok::hash)) {
         NextTok = Tokens->getNextToken();
+        if (NextTok->is(tok::pp_define))
+          BeforeMacro = PreviousTokenForNext;
         if (NextTok->isOneOf(tok::pp_not_keyword, tok::pp_define))
           break;
         do {
@@ -663,7 +670,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
       break;
     }
 
-    PrevTok = Tok;
+    PrevTok = PreviousTokenForNext;
     Tok = NextTok;
   } while (Tok->isNot(tok::eof) && !LBraceStack.empty());
 
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index be4936e9a8d95e..97d70281194e0e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6184,6 +6184,23 @@ TEST_F(FormatTest, HashInMacroDefinition) {
                "}");
 }
 
+TEST_F(FormatTest, FunctionBodyContainsOnlyPreprocessorDirectives) {
+  verifyFormat("void f() {\n"
+               "#define FOO 1\n"
+               "}",
+               "void f(){\n"
+               "#define FOO 1\n"
+               "}");
+  verifyFormat("void f() {\n"
+               "#define FOO 1\n"
+               "#define BAR 2\n"
+               "}",
+               "void f(){\n"
+               "#define FOO 1\n"
+               "#define BAR 2\n"
+               "}");
+}
+
 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
   verifyFormat("#define A (x)");
   verifyFormat("#define A(x)");

>From 0f47b7b75b9aa3ac462fce837354836da1b11bbe Mon Sep 17 00:00:00 2001
From: Raiyyan 3 <f20241312 at pilani.bits-pilani.ac.in>
Date: Sun, 6 Dec 2026 11:12:52 +0530
Subject: [PATCH 2/2] [clang-format] Test adjacent strings in directives inside
 functions

Cover a macro whose replacement list ends in two literals. Leaving either literal as the previous token can make the enclosing function braces look like a braced initializer.
---
 clang/unittests/Format/FormatTest.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a1e8660a36a259..13acfbde1ebabc 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6221,6 +6221,16 @@ TEST_F(FormatTest, FunctionBodyContainsOnlyPreprocessorDirectives) {
                "#define FOO 1\n"
                "#define BAR 2\n"
                "}");
+
+  auto Style = getLLVMStyle();
+  Style.BreakAdjacentStringLiterals = false;
+  verifyFormat("void f() {\n"
+               "#define FOO \"first\" \"second\"\n"
+               "}",
+               "void f(){\n"
+               "#define FOO \"first\" \"second\"\n"
+               "}",
+               Style);
 }
 
 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {



More information about the cfe-commits mailing list