Skip to content

[clang-format] Finalize children after formatting them #73753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,12 @@ class LineJoiner {
};

static void markFinalized(FormatToken *Tok) {
if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
tok::pp_else, tok::pp_endif)) {
Tok = Tok->Next;
}
for (; Tok; Tok = Tok->Next) {
if (Tok->MacroCtx && Tok->MacroCtx->Role == MR_ExpandedArg) {
// In the first pass we format all macro arguments in the expanded token
Expand Down Expand Up @@ -1060,6 +1066,8 @@ class LineFormatter {
}
Penalty +=
formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun);
if (!DryRun)
markFinalized(Child->First);

State.Column += 1 + Child->Last->TotalLength;
return true;
Expand Down Expand Up @@ -1429,16 +1437,8 @@ unsigned UnwrappedLineFormatter::format(
NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
RangeMinLevel = UINT_MAX;
}
if (!DryRun) {
auto *Tok = TheLine.First;
if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
tok::pp_else, tok::pp_endif)) {
Tok = Tok->Next;
}
markFinalized(Tok);
}
if (!DryRun)
markFinalized(TheLine.First);
}
PenaltyCache[CacheKey] = Penalty;
return Penalty;
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6361,6 +6361,13 @@ TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
" int quux = 4;\n"
"}",
Style);
verifyFormat("auto foo = [] { return; };\n"
"#if FOO\n"
"#else\n"
"count = bar;\n"
"mbid = bid;\n"
"#endif",
Style);

// Test with a mix of #if and #else blocks.
verifyFormat("void f1() {\n"
Expand Down