Skip to content

Commit a1036e5

Browse files
committedOct 28, 2015
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
1 parent f6bd010 commit a1036e5

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed
 

‎clang/lib/Format/UnwrappedLineFormatter.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,14 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
812812
AdditionalIndent);
813813
const AnnotatedLine *PreviousLine = nullptr;
814814
const AnnotatedLine *NextLine = nullptr;
815+
bool PreviousLineFormatted = false;
815816
for (const AnnotatedLine *Line =
816817
Joiner.getNextMergedLine(DryRun, IndentTracker);
817818
Line; Line = NextLine) {
818819
const AnnotatedLine &TheLine = *Line;
819820
unsigned Indent = IndentTracker.getIndent();
820-
bool FixIndentation =
821-
FixBadIndentation && (Indent != TheLine.First->OriginalColumn);
821+
bool FixIndentation = (FixBadIndentation || PreviousLineFormatted) &&
822+
Indent != TheLine.First->OriginalColumn;
822823
bool ShouldFormat = TheLine.Affected || FixIndentation;
823824
// We cannot format this line; if the reason is that the line had a
824825
// parsing error, remember that.
@@ -845,6 +846,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
845846
else
846847
Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
847848
.formatLine(TheLine, Indent, DryRun);
849+
PreviousLineFormatted = true;
848850
} else {
849851
// If no token in the current line is affected, we still need to format
850852
// affected children.
@@ -875,6 +877,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
875877
Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
876878
}
877879
NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
880+
PreviousLineFormatted = false;
878881
}
879882
if (!DryRun)
880883
markFinalized(TheLine.First);

‎clang/test/Format/adjust-indent.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -lines=2:2 \
2+
// RUN: | FileCheck -strict-whitespace %s
3+
4+
void f() {
5+
// CHECK: void f() {
6+
int i;
7+
// CHECK: {{^ int\ i;}}
8+
int j;
9+
// CHECK: {{^ int\ j;}}
10+
}

‎clang/test/Format/line-ranges.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// CHECK: {{^int\ \*i;$}}
55
int*i;
66

7-
// CHECK: {{^\ \ int\ \ \*\ \ i;$}}
8-
int * i;
7+
// CHECK: {{^int\ \ \*\ \ i;$}}
8+
int * i;
99

10-
// CHECK: {{^\ \ int\ \*i;$}}
11-
int * i;
10+
// CHECK: {{^int\ \*i;$}}
11+
int * i;

‎clang/test/Format/ranges.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \
33
// RUN: | FileCheck -strict-whitespace %s
44
// CHECK: {{^int\ \*i;$}}
5-
int*i;
5+
int*i;
66

7-
// CHECK: {{^\ \ int\ \ \*\ \ i;$}}
8-
int * i;
7+
// CHECK: {{^int\ \ \*\ \ i;$}}
8+
int * i;
99

10-
// CHECK: {{^\ \ int\ \*i;$}}
11-
int * i;
10+
// CHECK: {{^int\ \*i;$}}
11+
int * i;

‎clang/unittests/Format/FormatTestSelective.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
4545
}
4646

4747
TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
48-
EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0));
49-
EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0));
48+
EXPECT_EQ("{int b;\n"
49+
" int a;\n"
50+
"}",
51+
format("{int b;\n int a;}", 8, 0));
52+
EXPECT_EQ("{\n"
53+
" int b;\n"
54+
" int a;}",
55+
format("{int b;\n int a;}", 7, 0));
5056

5157
Style.ColumnLimit = 12;
5258
EXPECT_EQ("#define A \\\n"
@@ -84,11 +90,11 @@ TEST_F(FormatTestSelective, ReformatsMovedLines) {
8490
"template <typename T> T *getFETokenInfo() const {\n"
8591
" return static_cast<T *>(FETokenInfo);\n"
8692
"}\n"
87-
" int a; // <- Should not be formatted",
93+
"int a; // <- Should not be formatted",
8894
format(
8995
"template<typename T>\n"
9096
"T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
91-
" int a; // <- Should not be formatted",
97+
"int a; // <- Should not be formatted",
9298
9, 5));
9399
}
94100

@@ -142,12 +148,12 @@ TEST_F(FormatTestSelective, FormatsCommentsLocally) {
142148
" // is\n"
143149
" // a\n"
144150
"\n"
145-
" // This is unrelated",
151+
"//This is unrelated",
146152
format("int a; // This\n"
147153
" // is\n"
148154
" // a\n"
149155
"\n"
150-
" // This is unrelated",
156+
"//This is unrelated",
151157
0, 0));
152158
EXPECT_EQ("int a;\n"
153159
"// This is\n"
@@ -310,13 +316,17 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
310316
EXPECT_EQ("{\n"
311317
"{\n"
312318
" a;\n"
313-
"b;\n"
319+
" b;\n"
320+
" c;\n"
321+
" d;\n"
314322
"}\n"
315323
"}",
316324
format("{\n"
317325
"{\n"
318326
" a;\n"
319-
"b;\n"
327+
" b;\n"
328+
" c;\n"
329+
" d;\n"
320330
"}\n"
321331
"}",
322332
9, 2));

0 commit comments

Comments
 (0)
Please sign in to comment.