Author: Owen Pan Date: 2023-11-29T12:56:05-08:00 New Revision: bbae59ae71c73e26809d4ab7da1a80239ebd9381
URL: https://github.com/llvm/llvm-project/commit/bbae59ae71c73e26809d4ab7da1a80239ebd9381 DIFF: https://github.com/llvm/llvm-project/commit/bbae59ae71c73e26809d4ab7da1a80239ebd9381.diff LOG: [clang-format] Finalize children after formatting them (#73753) This would also fix the overlapping replacements below: ``` $ clang-format a( #else #endif ) = []() { )} The new replacement overlaps with an existing replacement. New replacement: <stdin>: 38:+7:" " Existing replacement: <stdin>: 38:+7:" " ``` Fixed #73487. Added: Modified: clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 280485d9a90d1bf..40730cd53529ed3 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -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 @@ -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; @@ -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; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 420afe5992f2a0b..8782cb7c49cad33 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits