clang-format: When a line is formatted, also format subsequence lines if their indent is off.

Summary: This is especially important so that if a change is solely inserting a block around a few statements, clang-format-diff.py will still clean up and add indentation to the inner parts.

Reviewers: klimek

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D14105

llvm-svn: 251474
This commit is contained in:
Daniel Jasper 2015-10-28 01:08:22 +00:00
parent f6bd01097a
commit a1036e5d08
5 changed files with 42 additions and 19 deletions

View File

@ -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);

View File

@ -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;}}
}

View File

@ -4,8 +4,8 @@
// CHECK: {{^int\ \*i;$}}
int*i;
// CHECK: {{^\ \ int\ \ \*\ \ i;$}}
// CHECK: {{^int\ \ \*\ \ i;$}}
int * i;
// CHECK: {{^\ \ int\ \*i;$}}
// CHECK: {{^int\ \*i;$}}
int * i;

View File

@ -4,8 +4,8 @@
// CHECK: {{^int\ \*i;$}}
int*i;
// CHECK: {{^\ \ int\ \ \*\ \ i;$}}
// CHECK: {{^int\ \ \*\ \ i;$}}
int * i;
// CHECK: {{^\ \ int\ \*i;$}}
// CHECK: {{^int\ \*i;$}}
int * i;

View File

@ -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"
@ -311,12 +317,16 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
"{\n"
" a;\n"
" b;\n"
" c;\n"
" d;\n"
"}\n"
"}",
format("{\n"
"{\n"
" a;\n"
" b;\n"
" c;\n"
" d;\n"
"}\n"
"}",
9, 2));