[clang] [clang-format] Fix qualifier ordering for lines after PP directives (PR #160731)

Ruoyu Zhong via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 25 09:23:32 PDT 2025


https://github.com/ZhongRuoyu created https://github.com/llvm/llvm-project/pull/160731

Lines appearing after preprocessor conditional blocks (like `#endif`) were not having their qualifiers reordered by `QualifierOrder`, while lines inside the conditional blocks were processed correctly.

The issue was that tokens on lines following preprocessor directives have `MustBreakBefore` = `true`. The qualifier alignment logic was breaking immediately upon encountering any token with `MustBreakBefore` = `true`, preventing analysis of the entire line.

The fix allows processing to continue when `MustBreakBefore` = `true` on the first token of a line, since this is expected behavior (the token legitimately starts a new line). Only tokens with `MustBreakBefore` = `true` that appear mid-line will cause the analysis loop to break.

Fixes https://github.com/llvm/llvm-project/issues/160487.

CC @owenca.


>From 2c8e4b75a4711940f0fd49c23b5208a678689d04 Mon Sep 17 00:00:00 2001
From: Ruoyu Zhong <zhongruoyu at outlook.com>
Date: Fri, 26 Sep 2025 00:19:53 +0800
Subject: [PATCH] [clang-format] Fix qualifier ordering for lines after PP
 directives

Lines appearing after preprocessor conditional blocks (like #endif) were
not having their qualifiers reordered by QualifierOrder, while lines
inside the conditional blocks were processed correctly.

The issue was that tokens on lines following preprocessor directives
have MustBreakBefore = true. The qualifier alignment logic was breaking
immediately upon encountering any token with MustBreakBefore = true,
preventing analysis of the entire line.

The fix allows processing to continue when MustBreakBefore = true on the
first token of a line, since this is expected behavior (the token
legitimately starts a new line). Only tokens with MustBreakBefore = true
that appear mid-line will cause the analysis loop to break.

Fixes https://github.com/llvm/llvm-project/issues/160487.

Signed-off-by: Ruoyu Zhong <zhongruoyu at outlook.com>
---
 clang/lib/Format/QualifierAlignmentFixer.cpp  |  2 +-
 clang/unittests/Format/QualifierFixerTest.cpp | 35 +++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 441a37a4902b7..043d957611b19 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -571,7 +571,7 @@ void LeftRightQualifierAlignmentFixer::fixQualifierAlignment(
 
     for (const auto *Tok = First; Tok && Tok != Last && Tok->Next;
          Tok = Tok->Next) {
-      if (Tok->MustBreakBefore)
+      if (Tok->MustBreakBefore && Tok != First)
         break;
       if (Tok->is(tok::comment))
         continue;
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index f42f2e307f713..be5b5d7118184 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1195,6 +1195,41 @@ TEST_F(QualifierFixerTest, QualifiersBrokenUpByPPDirectives) {
                Style);
 }
 
+TEST_F(QualifierFixerTest, QualifierOrderingAfterPreprocessorDirectives) {
+  auto Style = getLLVMStyle();
+  Style.QualifierAlignment = FormatStyle::QAS_Custom;
+  Style.QualifierOrder = {"static", "inline", "const", "type"};
+
+  verifyFormat("#if 1\n"
+               "void foo(const int par);\n"
+               "const int var1;\n"
+               "#endif\n"
+               "\n"
+               "const int var2;\n"
+               "const int var3;\n",
+               "#if 1\n"
+               "void foo(int const par);\n"
+               "int const var1;\n"
+               "#endif\n"
+               "\n"
+               "int const var2;\n"
+               "int const var3;\n",
+               Style);
+  verifyFormat("#if defined(FOO)\n"
+               "static const int x = 1;\n"
+               "#else\n"
+               "static const int x = 2;\n"
+               "#endif\n"
+               "static const int y = 3;",
+               "#if defined(FOO)\n"
+               "const static int x = 1;\n"
+               "#else\n"
+               "const static int x = 2;\n"
+               "#endif\n"
+               "const static int y = 3;",
+               Style);
+}
+
 TEST_F(QualifierFixerTest, UnsignedQualifier) {
 
   FormatStyle Style = getLLVMStyle();



More information about the cfe-commits mailing list