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

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 27 20:12:40 PDT 2025


Author: Ruoyu Zhong
Date: 2025-09-27T20:12:36-07:00
New Revision: 9a01561760a2785fbf6d66f86ba2361d36a648f7

URL: https://github.com/llvm/llvm-project/commit/9a01561760a2785fbf6d66f86ba2361d36a648f7
DIFF: https://github.com/llvm/llvm-project/commit/9a01561760a2785fbf6d66f86ba2361d36a648f7.diff

LOG: [clang-format] Fix qualifier ordering for lines after PP directives (#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.

Added: 
    

Modified: 
    clang/lib/Format/QualifierAlignmentFixer.cpp
    clang/unittests/Format/QualifierFixerTest.cpp

Removed: 
    


################################################################################
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..58e64ff368946 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;",
+               "#if 1\n"
+               "void foo(int const par);\n"
+               "int const var1;\n"
+               "#endif\n"
+               "\n"
+               "int const var2;\n"
+               "int const var3;",
+               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