Author: Sedenion Date: 2023-05-23T19:40:24-07:00 New Revision: 72ab89e3197cc1bee3b9774edb504690e3e43ed0
URL: https://github.com/llvm/llvm-project/commit/72ab89e3197cc1bee3b9774edb504690e3e43ed0 DIFF: https://github.com/llvm/llvm-project/commit/72ab89e3197cc1bee3b9774edb504690e3e43ed0.diff LOG: [clang-format] Fix indentation for selective formatting The problem was that the LevelIndentTracker remembered the indentation level of previous deeper levels when leaving a scope. Afterwards, when it entered again a deeper level, it blindly reused the the previous indentation level. In case of the --lines option configured such that the previous deeper level was not formatted, that previous level was whatever happened to be there in the source code. The formatter simply believed it. This is fixed by letting the LevelIndentTracker forget the previous deeper levels when stepping out of them (=> change in LevelIndentTracker::nextLine()). Note that this used to be the case until LLVM 14.0.6, but was changed in https://github.com/llvm/llvm-project/issues/56352 to fix a crash. Our commit here essentially reverts that crash fix. It seemed to have been incorrect. The proper fix is to set the AnnotedLine::Level of joined lines correctly (=> change in LineJoiner::join()). See https://github.com/llvm/llvm-project/issues/59178#issuecomment-1542637781 for some more details. Fixes #58464. Fixes #59178. Differential Revision: https://reviews.llvm.org/D151047 Added: Modified: clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTestSelective.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 33be74dfe1b9f..0a2a4391d21c8 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -74,6 +74,12 @@ class LevelIndentTracker { : Line.Level * PPIndentWidth; Indent += AdditionalIndent; } else { + // When going to lower levels, forget previous higher levels so that we + // recompute future higher levels. But don't forget them if we enter a PP + // directive, since these do not terminate a code block. + if (!Line.InPPDirective) + IndentForLevel.resize(Line.Level + 1); + Indent = getIndent(Line.Level); } if (static_cast<int>(Indent) + Offset >= 0) @@ -910,6 +916,7 @@ class LineJoiner { Tok->TotalLength += LengthA; A.Last = Tok; } + A.Level = B.Level; } const FormatStyle &Style; diff --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp index 86ed7aba1913d..96e42dd3a2d18 100644 --- a/clang/unittests/Format/FormatTestSelective.cpp +++ b/clang/unittests/Format/FormatTestSelective.cpp @@ -528,6 +528,26 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { format(" int a;\n" "void ffffff() {}", 11, 0)); + + // https://github.com/llvm/llvm-project/issues/59178 + Style = getMozillaStyle(); + EXPECT_EQ("int a()\n" + "{\n" + "return 0;\n" + "}\n" + "int b()\n" + "{\n" + " return 42;\n" + "}", + format("int a()\n" + "{\n" + "return 0;\n" + "}\n" + "int b()\n" + "{\n" + "return 42;\n" // Format this line only + "}", + 32, 0)); } TEST_F(FormatTestSelective, UnderstandsTabs) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits