This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG72ab89e3197c: [clang-format] Fix indentation for selective formatting (authored by Sedeniono, committed by owenpan).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D151047/new/ https://reviews.llvm.org/D151047 Files: clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTestSelective.cpp Index: clang/unittests/Format/FormatTestSelective.cpp =================================================================== --- clang/unittests/Format/FormatTestSelective.cpp +++ clang/unittests/Format/FormatTestSelective.cpp @@ -528,6 +528,26 @@ 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) { Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -74,6 +74,12 @@ : 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 @@ Tok->TotalLength += LengthA; A.Last = Tok; } + A.Level = B.Level; } const FormatStyle &Style;
Index: clang/unittests/Format/FormatTestSelective.cpp =================================================================== --- clang/unittests/Format/FormatTestSelective.cpp +++ clang/unittests/Format/FormatTestSelective.cpp @@ -528,6 +528,26 @@ 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) { Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -74,6 +74,12 @@ : 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 @@ Tok->TotalLength += LengthA; A.Last = Tok; } + A.Level = B.Level; } const FormatStyle &Style;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits