diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-12-12 09:49:52 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-12-12 09:49:52 +0000 |
commit | 3a33f0292bcadc2eaf980ec7581dbe260f0e092b (patch) | |
tree | 05922d09cc643457235fb3203c2d84f3cd999fc2 /clang/lib/Format/UnwrappedLineParser.cpp | |
parent | a92e2311bb1345c60f6c90180028b8adf252897c (diff) | |
download | bcm5719-llvm-3a33f0292bcadc2eaf980ec7581dbe260f0e092b.tar.gz bcm5719-llvm-3a33f0292bcadc2eaf980ec7581dbe260f0e092b.zip |
Implemented GNU-style formatting for compound statements.
Summary:
Added BraceBreakingStyle::BS_GNU. I'm not sure about the correctness of
static initializer formatting, but compound statements should be fine.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2372
llvm-svn: 197138
Diffstat (limited to 'clang/lib/Format/UnwrappedLineParser.cpp')
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 1eb0ae17007..089bd570b5c 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -152,6 +152,27 @@ private: SmallVectorImpl<UnwrappedLine> *OriginalLines; }; +class CompoundStatementIndenter { +public: + CompoundStatementIndenter(UnwrappedLineParser *Parser, + const FormatStyle &Style, unsigned &LineLevel) + : LineLevel(LineLevel), OldLineLevel(LineLevel) { + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) { + Parser->addUnwrappedLine(); + } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) { + Parser->addUnwrappedLine(); + ++LineLevel; + } + } + ~CompoundStatementIndenter() { + LineLevel = OldLineLevel; + } + +private: + unsigned &LineLevel; + unsigned OldLineLevel; +}; + namespace { class IndexedTokenSource : public FormatTokenSource { @@ -677,9 +698,7 @@ void UnwrappedLineParser::parseStructuralElement() { // structural element. // FIXME: Figure out cases where this is not true, and add projections // for them (the one we know is missing are lambdas). - if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || - Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup || - Style.BreakBeforeBraces == FormatStyle::BS_Allman) + if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) addUnwrappedLine(); FormatTok->Type = TT_FunctionLBrace; parseBlock(/*MustBeDeclaration=*/false); @@ -938,13 +957,14 @@ void UnwrappedLineParser::parseIfThenElse() { parseParens(); bool NeedsUnwrappedLine = false; if (FormatTok->Tok.is(tok::l_brace)) { - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) - addUnwrappedLine(); + CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || + Style.BreakBeforeBraces == FormatStyle::BS_GNU) { addUnwrappedLine(); - else + } else { NeedsUnwrappedLine = true; + } } else { addUnwrappedLine(); ++Line->Level; @@ -954,8 +974,7 @@ void UnwrappedLineParser::parseIfThenElse() { if (FormatTok->Tok.is(tok::kw_else)) { nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) - addUnwrappedLine(); + CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else if (FormatTok->Tok.is(tok::kw_if)) { @@ -978,7 +997,8 @@ void UnwrappedLineParser::parseNamespace() { nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || - Style.BreakBeforeBraces == FormatStyle::BS_Allman) + Style.BreakBeforeBraces == FormatStyle::BS_Allman || + Style.BreakBeforeBraces == FormatStyle::BS_GNU) addUnwrappedLine(); bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || @@ -1001,8 +1021,7 @@ void UnwrappedLineParser::parseForOrWhileLoop() { if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) - addUnwrappedLine(); + CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else { @@ -1017,9 +1036,10 @@ void UnwrappedLineParser::parseDoWhile() { assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) - addUnwrappedLine(); + CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); + if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) + addUnwrappedLine(); } else { addUnwrappedLine(); ++Line->Level; @@ -1043,17 +1063,20 @@ void UnwrappedLineParser::parseLabel() { if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) --Line->Level; if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) - addUnwrappedLine(); + CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); if (FormatTok->Tok.is(tok::kw_break)) { - // "break;" after "}" on its own line only for BS_Allman - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) + // "break;" after "}" on its own line only for BS_Allman and BS_GNU + if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || + Style.BreakBeforeBraces == FormatStyle::BS_GNU) { addUnwrappedLine(); + } parseStructuralElement(); } + addUnwrappedLine(); + } else { + addUnwrappedLine(); } - addUnwrappedLine(); Line->Level = OldLineLevel; } @@ -1072,8 +1095,7 @@ void UnwrappedLineParser::parseSwitch() { if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { - if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) - addUnwrappedLine(); + CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else { @@ -1164,7 +1186,8 @@ void UnwrappedLineParser::parseRecord() { } if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || - Style.BreakBeforeBraces == FormatStyle::BS_Allman) + Style.BreakBeforeBraces == FormatStyle::BS_Allman || + Style.BreakBeforeBraces == FormatStyle::BS_GNU) addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true, |