diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index 8225a0375b4f154b146fe8e1e63e05959013f350..fcf45c9286f9a9c2b4dce96ea069a8474e5939d1 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -812,13 +812,14 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, AdditionalIndent); const AnnotatedLine *PreviousLine = nullptr; const AnnotatedLine *NextLine = nullptr; + bool PreviousLineFormatted = false; for (const AnnotatedLine *Line = Joiner.getNextMergedLine(DryRun, IndentTracker); Line; Line = NextLine) { const AnnotatedLine &TheLine = *Line; unsigned Indent = IndentTracker.getIndent(); - bool FixIndentation = - FixBadIndentation && (Indent != TheLine.First->OriginalColumn); + bool FixIndentation = (FixBadIndentation || PreviousLineFormatted) && + Indent != TheLine.First->OriginalColumn; bool ShouldFormat = TheLine.Affected || FixIndentation; // We cannot format this line; if the reason is that the line had a // parsing error, remember that. @@ -845,6 +846,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, else Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this) .formatLine(TheLine, Indent, DryRun); + PreviousLineFormatted = true; } else { // If no token in the current line is affected, we still need to format // affected children. @@ -875,6 +877,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); } NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker); + PreviousLineFormatted = false; } if (!DryRun) markFinalized(TheLine.First); diff --git a/test/Format/adjust-indent.cpp b/test/Format/adjust-indent.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5565dc00b62d0b4f6ccd920e5610b3d94e001194 --- /dev/null +++ b/test/Format/adjust-indent.cpp @@ -0,0 +1,10 @@ +// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -lines=2:2 \ +// RUN: | FileCheck -strict-whitespace %s + +void f() { +// CHECK: void f() { +int i; +// CHECK: {{^ int\ i;}} + int j; +// CHECK: {{^ int\ j;}} +} diff --git a/test/Format/line-ranges.cpp b/test/Format/line-ranges.cpp index 9099022b428c30b27b7520edb36ee202f8ed6e27..e81e9624344d014d7a4facf5ae3fc72d6eb525c2 100644 --- a/test/Format/line-ranges.cpp +++ b/test/Format/line-ranges.cpp @@ -4,8 +4,8 @@ // CHECK: {{^int\ \*i;$}} int*i; -// CHECK: {{^\ \ int\ \ \*\ \ i;$}} - int * i; +// CHECK: {{^int\ \ \*\ \ i;$}} +int * i; -// CHECK: {{^\ \ int\ \*i;$}} - int * i; +// CHECK: {{^int\ \*i;$}} +int * i; diff --git a/test/Format/ranges.cpp b/test/Format/ranges.cpp index ace528a9166ce57b8f21cdf315fafaf33def9b2c..66b984e037b3c5f8be5d60761add86d2da4b9197 100644 --- a/test/Format/ranges.cpp +++ b/test/Format/ranges.cpp @@ -2,10 +2,10 @@ // RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \ // RUN: | FileCheck -strict-whitespace %s // CHECK: {{^int\ \*i;$}} - int*i; +int*i; -// CHECK: {{^\ \ int\ \ \*\ \ i;$}} - int * i; +// CHECK: {{^int\ \ \*\ \ i;$}} +int * i; -// CHECK: {{^\ \ int\ \*i;$}} - int * i; +// CHECK: {{^int\ \*i;$}} +int * i; diff --git a/unittests/Format/FormatTestSelective.cpp b/unittests/Format/FormatTestSelective.cpp index 8d2cb5a2fcbd9c530be338b535d3b1f0e34d56cf..a28dfd32e1ca72c654b64bab7d32c11a5928de8b 100644 --- a/unittests/Format/FormatTestSelective.cpp +++ b/unittests/Format/FormatTestSelective.cpp @@ -45,8 +45,14 @@ TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) { } TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) { - EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0)); - EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0)); + EXPECT_EQ("{int b;\n" + " int a;\n" + "}", + format("{int b;\n int a;}", 8, 0)); + EXPECT_EQ("{\n" + " int b;\n" + " int a;}", + format("{int b;\n int a;}", 7, 0)); Style.ColumnLimit = 12; EXPECT_EQ("#define A \\\n" @@ -84,11 +90,11 @@ TEST_F(FormatTestSelective, ReformatsMovedLines) { "template <typename T> T *getFETokenInfo() const {\n" " return static_cast<T *>(FETokenInfo);\n" "}\n" - " int a; // <- Should not be formatted", + "int a; // <- Should not be formatted", format( "template<typename T>\n" "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" - " int a; // <- Should not be formatted", + "int a; // <- Should not be formatted", 9, 5)); } @@ -142,12 +148,12 @@ TEST_F(FormatTestSelective, FormatsCommentsLocally) { " // is\n" " // a\n" "\n" - " // This is unrelated", + "//This is unrelated", format("int a; // This\n" " // is\n" " // a\n" "\n" - " // This is unrelated", + "//This is unrelated", 0, 0)); EXPECT_EQ("int a;\n" "// This is\n" @@ -310,13 +316,17 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { EXPECT_EQ("{\n" "{\n" " a;\n" - "b;\n" + " b;\n" + " c;\n" + " d;\n" "}\n" "}", format("{\n" "{\n" " a;\n" - "b;\n" + " b;\n" + " c;\n" + " d;\n" "}\n" "}", 9, 2));