[clang] [clang-format] Prevent line comment continuation across formatting-disabled boundary (PR #233414)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 24 00:43:30 PDT 2026
https://github.com/akasakariko updated https://github.com/llvm/llvm-project/pull/233414
>From 88782ee1bffe7c7f7a32425c63ad03076b07d19d Mon Sep 17 00:00:00 2001
From: akasakariko <148060537+akasakariko at users.noreply.github.com>
Date: Mon, 19 Oct 2026 10:28:39 +0900
Subject: [PATCH 1/2] [clang-format] Prevent line comment continuation across
formatting-disabled boundary
Fixes #222510
Assisted-by: pi coding agent
---
clang/lib/Format/FormatToken.h | 3 +++
clang/unittests/Format/FormatTest.cpp | 31 +++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 691ecdd24f448f..b3f5aeffac8f5d 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -2128,6 +2128,9 @@ inline bool continuesLineComment(const FormatToken &FormatTok,
const FormatToken *MinColumnToken) {
if (!Previous || !MinColumnToken)
return false;
+ // Do not continue a comment section across a formatting-disabled boundary.
+ if (FormatTok.Finalized != Previous->Finalized)
+ return false;
unsigned MinContinueColumn =
MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d346036448140c..ba95e6f1aa47a0 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22638,6 +22638,37 @@ TEST_F(FormatTest, OneLineFormatOffRegex) {
" tm t; // FormatOff\n"
" int i;",
Style);
+
+ Style = getLLVMStyle();
+ Style.UseTab = FormatStyle::UT_Always;
+ Style.TabWidth = 4;
+ Style.IndentWidth = 4;
+ Style.ReflowComments = FormatStyle::RCS_Never;
+ Style.OneLineFormatOffRegex = "// #if";
+ verifyFormat("void f() {\n"
+ "#if !X\n"
+ "#else\n"
+ "\t{\n"
+ "#if Y\n"
+ "#else // #if Y\n"
+ "\t\t// comment\n"
+ "#endif\n"
+ "\t}\n"
+ "#endif\n"
+ "}",
+ "void f()\n"
+ "{\n"
+ "#if !X\n"
+ "#else\n"
+ " {\n"
+ "#if Y\n"
+ "#else // #if Y\n"
+ " // comment\n"
+ "#endif\n"
+ " }\n"
+ "#endif\n"
+ "}",
+ Style);
}
TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
>From 3d070f0ac6b434476052926601edf8e52144cac1 Mon Sep 17 00:00:00 2001
From: KayanoLiam <Kayano04 at outlook.jp>
Date: Sat, 24 Oct 2026 15:43:16 +0800
Subject: [PATCH 2/2] [clang-format] Simplify formatting stability regression
test
Use the two-argument verifyFormat helper to check the expected code directly
Assisted-by: pi coding agent
---
clang/unittests/Format/FormatTest.cpp | 12 ------------
1 file changed, 12 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ba95e6f1aa47a0..ad792cd4c6ef35 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22656,18 +22656,6 @@ TEST_F(FormatTest, OneLineFormatOffRegex) {
"\t}\n"
"#endif\n"
"}",
- "void f()\n"
- "{\n"
- "#if !X\n"
- "#else\n"
- " {\n"
- "#if Y\n"
- "#else // #if Y\n"
- " // comment\n"
- "#endif\n"
- " }\n"
- "#endif\n"
- "}",
Style);
}
More information about the cfe-commits
mailing list