Skip to content
Snippets Groups Projects
Commit 06611196 authored by Daniel Jasper's avatar Daniel Jasper
Browse files

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251474 91177308-0d34-0410-b5e6-96231b3b80d8
parent 68098861
No related branches found
No related tags found
No related merge requests found
...@@ -812,13 +812,14 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, ...@@ -812,13 +812,14 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
AdditionalIndent); AdditionalIndent);
const AnnotatedLine *PreviousLine = nullptr; const AnnotatedLine *PreviousLine = nullptr;
const AnnotatedLine *NextLine = nullptr; const AnnotatedLine *NextLine = nullptr;
bool PreviousLineFormatted = false;
for (const AnnotatedLine *Line = for (const AnnotatedLine *Line =
Joiner.getNextMergedLine(DryRun, IndentTracker); Joiner.getNextMergedLine(DryRun, IndentTracker);
Line; Line = NextLine) { Line; Line = NextLine) {
const AnnotatedLine &TheLine = *Line; const AnnotatedLine &TheLine = *Line;
unsigned Indent = IndentTracker.getIndent(); unsigned Indent = IndentTracker.getIndent();
bool FixIndentation = bool FixIndentation = (FixBadIndentation || PreviousLineFormatted) &&
FixBadIndentation && (Indent != TheLine.First->OriginalColumn); Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation; bool ShouldFormat = TheLine.Affected || FixIndentation;
// We cannot format this line; if the reason is that the line had a // We cannot format this line; if the reason is that the line had a
// parsing error, remember that. // parsing error, remember that.
...@@ -845,6 +846,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, ...@@ -845,6 +846,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
else else
Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this) Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
.formatLine(TheLine, Indent, DryRun); .formatLine(TheLine, Indent, DryRun);
PreviousLineFormatted = true;
} else { } else {
// If no token in the current line is affected, we still need to format // If no token in the current line is affected, we still need to format
// affected children. // affected children.
...@@ -875,6 +877,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, ...@@ -875,6 +877,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
} }
NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker); NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
PreviousLineFormatted = false;
} }
if (!DryRun) if (!DryRun)
markFinalized(TheLine.First); markFinalized(TheLine.First);
......
// 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;}}
}
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
// CHECK: {{^int\ \*i;$}} // CHECK: {{^int\ \*i;$}}
int*i; int*i;
// CHECK: {{^\ \ int\ \ \*\ \ i;$}} // CHECK: {{^int\ \ \*\ \ i;$}}
int * i; int * i;
// CHECK: {{^\ \ int\ \*i;$}} // CHECK: {{^int\ \*i;$}}
int * i; int * i;
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
// RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \ // RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \
// RUN: | FileCheck -strict-whitespace %s // RUN: | FileCheck -strict-whitespace %s
// CHECK: {{^int\ \*i;$}} // CHECK: {{^int\ \*i;$}}
int*i; int*i;
// CHECK: {{^\ \ int\ \ \*\ \ i;$}} // CHECK: {{^int\ \ \*\ \ i;$}}
int * i; int * i;
// CHECK: {{^\ \ int\ \*i;$}} // CHECK: {{^int\ \*i;$}}
int * i; int * i;
...@@ -45,8 +45,14 @@ TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) { ...@@ -45,8 +45,14 @@ TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
} }
TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) { TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0)); EXPECT_EQ("{int b;\n"
EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0)); " 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; Style.ColumnLimit = 12;
EXPECT_EQ("#define A \\\n" EXPECT_EQ("#define A \\\n"
...@@ -84,11 +90,11 @@ TEST_F(FormatTestSelective, ReformatsMovedLines) { ...@@ -84,11 +90,11 @@ TEST_F(FormatTestSelective, ReformatsMovedLines) {
"template <typename T> T *getFETokenInfo() const {\n" "template <typename T> T *getFETokenInfo() const {\n"
" return static_cast<T *>(FETokenInfo);\n" " return static_cast<T *>(FETokenInfo);\n"
"}\n" "}\n"
" int a; // <- Should not be formatted", "int a; // <- Should not be formatted",
format( format(
"template<typename T>\n" "template<typename T>\n"
"T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
" int a; // <- Should not be formatted", "int a; // <- Should not be formatted",
9, 5)); 9, 5));
} }
...@@ -142,12 +148,12 @@ TEST_F(FormatTestSelective, FormatsCommentsLocally) { ...@@ -142,12 +148,12 @@ TEST_F(FormatTestSelective, FormatsCommentsLocally) {
" // is\n" " // is\n"
" // a\n" " // a\n"
"\n" "\n"
" // This is unrelated", "//This is unrelated",
format("int a; // This\n" format("int a; // This\n"
" // is\n" " // is\n"
" // a\n" " // a\n"
"\n" "\n"
" // This is unrelated", "//This is unrelated",
0, 0)); 0, 0));
EXPECT_EQ("int a;\n" EXPECT_EQ("int a;\n"
"// This is\n" "// This is\n"
...@@ -310,13 +316,17 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { ...@@ -310,13 +316,17 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
EXPECT_EQ("{\n" EXPECT_EQ("{\n"
"{\n" "{\n"
" a;\n" " a;\n"
"b;\n" " b;\n"
" c;\n"
" d;\n"
"}\n" "}\n"
"}", "}",
format("{\n" format("{\n"
"{\n" "{\n"
" a;\n" " a;\n"
"b;\n" " b;\n"
" c;\n"
" d;\n"
"}\n" "}\n"
"}", "}",
9, 2)); 9, 2));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment