[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 22:07:22 PDT 2026
================
@@ -2910,22 +2910,39 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
}
addUnwrappedLine();
+ const size_t BodyStart = CurrentLines->size();
++Line->Level;
++Line->UnbracedBodyLevel;
parseStructuralElement();
--Line->UnbracedBodyLevel;
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) {
----------------
and2049 wrote:
`FirstAfterPPLine` can also apply to the token after a directive in the middle of an unwrapped line, such as on `1` in `f(\n#ifdef A\n 1);` in the new nested while/if test case.
I tried with checking only the first token, which makes that test fail.
https://github.com/llvm/llvm-project/pull/224947
More information about the cfe-commits
mailing list