[clang] [clang-format] Fix InsertBraces inserting braces around directives (PR #224947)
Andre Sun via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 19 21:51:21 PDT 2026
https://github.com/and2049 updated https://github.com/llvm/llvm-project/pull/224947
>From d033af06999bc4bd9d085a147c1fb3076abf13c2 Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Sun, 20 Sep 2026 13:07:59 -0400
Subject: [PATCH 1/2] [clang-format] Fix InsertBraces inserting braces around
directives
Fixes #224942.
---
clang/lib/Format/UnwrappedLineParser.cpp | 21 ++++++++-
clang/unittests/Format/BracesInserterTest.cpp | 45 +++++++++++++++++++
2 files changed, 64 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 2d2fe729fbc2f..86445cc02cfa6 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2910,6 +2910,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
}
addUnwrappedLine();
+ const size_t BodyStart = CurrentLines->size();
++Line->Level;
++Line->UnbracedBodyLevel;
parseStructuralElement();
@@ -2917,15 +2918,31 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
if (Tok) {
assert(!Line->InPPDirective);
+ FormatToken *const LBraceTok = Tok;
Tok = nullptr;
- for (const auto &L : llvm::reverse(*CurrentLines)) {
+ size_t BodyEnd = CurrentLines->size();
+ while (BodyEnd > 0) {
+ const auto &L = (*CurrentLines)[--BodyEnd];
if (!L.InPPDirective && getLastNonComment(L)) {
Tok = L.Tokens.back().Tok;
break;
}
}
assert(Tok);
- ++Tok->BraceCount;
+ // Don't insert the braces if they would enclose preprocessor directives.
+ bool EnclosesPPDirective = false;
+ for (size_t I = BodyStart; I <= BodyEnd && !EnclosesPPDirective; ++I) {
+ const auto &L = (*CurrentLines)[I];
+ EnclosesPPDirective =
+ L.InPPDirective ||
+ llvm::any_of(L.Tokens, [](const UnwrappedLineNode &Node) {
+ return Node.Tok->FirstAfterPPLine;
+ });
+ }
+ if (EnclosesPPDirective)
+ LBraceTok->BraceCount = 0;
+ else
+ ++Tok->BraceCount;
}
if (CheckEOF && eof())
diff --git a/clang/unittests/Format/BracesInserterTest.cpp b/clang/unittests/Format/BracesInserterTest.cpp
index 572e53e595e37..11c7c17b9aeb4 100644
--- a/clang/unittests/Format/BracesInserterTest.cpp
+++ b/clang/unittests/Format/BracesInserterTest.cpp
@@ -195,6 +195,51 @@ TEST_F(BracesInserterTest, InsertBraces) {
"#endif",
Style);
+ verifyFormat("if (a)\n"
+ " if (b)\n"
+ "#ifdef A\n"
+ " f();\n"
+ "#else\n"
+ " g();\n"
+ "#endif",
+ Style);
+
+ verifyFormat("while (a)\n"
+ " if (b)\n"
+ " f(\n"
+ "#ifdef A\n"
+ " 1);\n"
+ "#else\n"
+ " 2);\n"
+ "#endif",
+ Style);
+
+ verifyFormat("if (a)\n"
+ " f(\n"
+ "#ifdef A\n"
+ " 1\n"
+ "#else\n"
+ " 2\n"
+ "#endif\n"
+ " );",
+ Style);
+
+ verifyFormat("#if A\n"
+ "if (a) {\n"
+ " f();\n"
+ "}\n"
+ "#else\n"
+ "if (b) {\n"
+ " g();\n"
+ "}\n"
+ "#endif",
+ "#if A\n"
+ "if (a) f();\n"
+ "#else\n"
+ "if (b) g();\n"
+ "#endif",
+ Style);
+
verifyFormat("#if 0\n"
"#elif 1\n"
"#endif\n"
>From dac050d55bc832eca82346639b76ec8f9578e4cf Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Tue, 20 Oct 2026 00:48:40 -0400
Subject: [PATCH 2/2] Address feedback
---
clang/lib/Format/UnwrappedLineParser.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 86445cc02cfa6..4a9c6233d0e3b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2910,7 +2910,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
}
addUnwrappedLine();
- const size_t BodyStart = CurrentLines->size();
+ const auto BodyStart = CurrentLines->size();
++Line->Level;
++Line->UnbracedBodyLevel;
parseStructuralElement();
@@ -2920,7 +2920,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
assert(!Line->InPPDirective);
FormatToken *const LBraceTok = Tok;
Tok = nullptr;
- size_t BodyEnd = CurrentLines->size();
+ auto BodyEnd = CurrentLines->size();
while (BodyEnd > 0) {
const auto &L = (*CurrentLines)[--BodyEnd];
if (!L.InPPDirective && getLastNonComment(L)) {
@@ -2932,10 +2932,10 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
// Don't insert the braces if they would enclose preprocessor directives.
bool EnclosesPPDirective = false;
for (size_t I = BodyStart; I <= BodyEnd && !EnclosesPPDirective; ++I) {
- const auto &L = (*CurrentLines)[I];
+ const auto &BodyLine = (*CurrentLines)[I];
EnclosesPPDirective =
- L.InPPDirective ||
- llvm::any_of(L.Tokens, [](const UnwrappedLineNode &Node) {
+ BodyLine.InPPDirective ||
+ llvm::any_of(BodyLine.Tokens, [](const UnwrappedLineNode &Node) {
return Node.Tok->FirstAfterPPLine;
});
}
More information about the cfe-commits
mailing list