summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-10-20 17:28:32 +0000
committerDaniel Jasper <djasper@google.com>2013-10-20 17:28:32 +0000
commitdcd5da1fd9352473a8b08c982780f6b14994316f (patch)
treebae21e60bc5bdfc1e3c86e88cb1b53ead5f4af3a /clang/lib/Format
parent2d0cd4978737bdde886d70cb82d5ce8fb385bfa2 (diff)
downloadbcm5719-llvm-dcd5da1fd9352473a8b08c982780f6b14994316f.tar.gz
bcm5719-llvm-dcd5da1fd9352473a8b08c982780f6b14994316f.zip
clang-format: Fix formatting of nested blocks after comment.
Before: DEBUG({ // Comment that used to confuse clang-format. fdafas(); }); Before: DEBUG({ // Comments are now fine. fdafas(); }); This fixed llvm.org/PR17619. llvm-svn: 193051
Diffstat (limited to 'clang/lib/Format')
-rw-r--r--clang/lib/Format/Format.cpp21
1 files changed, 11 insertions, 10 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 90c3e9e96d5..79244b19937 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -541,9 +541,10 @@ private:
/// break or don't break.
bool formatChildren(LineState &State, bool NewLine, bool DryRun,
unsigned &Penalty) {
- const FormatToken &LBrace = *State.NextToken->Previous;
- if (LBrace.isNot(tok::l_brace) || LBrace.BlockKind != BK_Block ||
- LBrace.Children.size() == 0)
+ const FormatToken &Previous = *State.NextToken->Previous;
+ const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
+ if (!LBrace || LBrace->isNot(tok::l_brace) ||
+ LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
// The previous token does not open a block. Nothing to do. We don't
// assert so that we can simply call this function for all tokens.
return true;
@@ -551,8 +552,8 @@ private:
if (NewLine) {
unsigned ParentIndent = State.Stack.back().Indent;
for (SmallVector<AnnotatedLine *, 1>::const_iterator
- I = LBrace.Children.begin(),
- E = LBrace.Children.end();
+ I = Previous.Children.begin(),
+ E = Previous.Children.end();
I != E; ++I) {
unsigned Indent =
ParentIndent + ((*I)->Level - Line.Level - 1) * Style.IndentWidth;
@@ -570,24 +571,24 @@ private:
return true;
}
- if (LBrace.Children.size() > 1)
+ if (Previous.Children.size() > 1)
return false; // Cannot merge multiple statements into a single line.
// We can't put the closing "}" on a line with a trailing comment.
- if (LBrace.Children[0]->Last->isTrailingComment())
+ if (Previous.Children[0]->Last->isTrailingComment())
return false;
if (!DryRun) {
Whitespaces->replaceWhitespace(
- *LBrace.Children[0]->First,
+ *Previous.Children[0]->First,
/*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
/*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
- *LBrace.Children[0]);
+ *Previous.Children[0]);
Penalty += Formatter.format(State.Column + 1, DryRun);
}
- State.Column += 1 + LBrace.Children[0]->Last->TotalLength;
+ State.Column += 1 + Previous.Children[0]->Last->TotalLength;
return true;
}
OpenPOWER on IntegriCloud