[llvm-branch-commits] [clang] release/21.x: [clang-format] Fix a bug in OneLineFormatOffRegex (#162961) (PR #163022)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Oct 11 14:49:09 PDT 2025


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/163022

Backport 72d6d6e25a33bdea389002c699734e5ee68fe75a

Requested by: @owenca

>From 9eea440dee0708cdf1c79fcd4e2761f1d41645e2 Mon Sep 17 00:00:00 2001
From: owenca <owenpiano at gmail.com>
Date: Sat, 11 Oct 2025 14:40:26 -0700
Subject: [PATCH] [clang-format] Fix a bug in OneLineFormatOffRegex (#162961)

Fixes #162402

(cherry picked from commit 72d6d6e25a33bdea389002c699734e5ee68fe75a)
---
 clang/lib/Format/FormatTokenLexer.cpp | 13 +++++++------
 clang/unittests/Format/FormatTest.cpp |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 49da3160daf50..165ede2d04b2c 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -93,12 +93,6 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
     auto &Tok = *Tokens.back();
     const auto NewlinesBefore = Tok.NewlinesBefore;
     switch (FormatOff) {
-    case FO_CurrentLine:
-      if (NewlinesBefore == 0)
-        Tok.Finalized = true;
-      else
-        FormatOff = FO_None;
-      break;
     case FO_NextLine:
       if (NewlinesBefore > 1) {
         FormatOff = FO_None;
@@ -107,6 +101,13 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
         FormatOff = FO_CurrentLine;
       }
       break;
+    case FO_CurrentLine:
+      if (NewlinesBefore == 0) {
+        Tok.Finalized = true;
+        break;
+      }
+      FormatOff = FO_None;
+      [[fallthrough]];
     default:
       if (!FormattingDisabled && FormatOffRegex.match(Tok.TokenText)) {
         if (Tok.is(tok::comment) &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 95682f2d8cfd4..bda88115f09f7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25081,6 +25081,11 @@ TEST_F(FormatTest, OneLineFormatOffRegex) {
                " } while (0 )",
                Style);
 
+  Style.OneLineFormatOffRegex = "MACRO_TEST";
+  verifyNoChange(" MACRO_TEST1 ( ) ;\n"
+                 "   MACRO_TEST2( );",
+                 Style);
+
   Style.ColumnLimit = 50;
   Style.OneLineFormatOffRegex = "^LogErrorPrint$";
   verifyFormat(" myproject::LogErrorPrint(logger, \"Don't split me!\");\n"



More information about the llvm-branch-commits mailing list