<div dir="ltr"><div class="gmail_default" style>On Thu, Jan 10, 2013 at 5:23 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br></div><div class="gmail_extra">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Thu, Jan 10, 2013 at 3:52 AM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> wrote:<br>
> Author: klimek<br>
> Date: Thu Jan 10 05:52:21 2013<br>
> New Revision: 172058<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=172058&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=172058&view=rev</a><br>
> Log:<br>
> Fix layout of blocks inside statements.<br>
><br>
> Previously, we would not indent:<br>
> SOME_MACRO({<br>
> int i;<br>
> });<br>
> correctly. This is fixed by adding the trailing }); to the unwrapped<br>
> line starting with SOME_MACRO({, so the formatter can correctly match<br>
> the braces and indent accordingly.<br>
<br>
</div>This is similar to some of the complexities of lambda syntax - perhaps<br>
you could add some test cases for that, too (though, of course, there<br>
may be bugs in that because it has other syntactic complications)<br></blockquote><div><br></div><div style>Due to there currently being very little lambda code, we're so far not prioritizing it.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
><br>
> Also fixes incorrect parsing of initializer lists, like:<br>
> int a[] = { 1 };<br>
><br>
> Modified:<br>
> cfe/trunk/lib/Format/Format.cpp<br>
> cfe/trunk/lib/Format/UnwrappedLineParser.cpp<br>
> cfe/trunk/lib/Format/UnwrappedLineParser.h<br>
> cfe/trunk/unittests/Format/FormatTest.cpp<br>
><br>
> Modified: cfe/trunk/lib/Format/Format.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172058&r1=172057&r2=172058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172058&r1=172057&r2=172058&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/Format/Format.cpp (original)<br>
> +++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 05:52:21 2013<br>
> @@ -279,7 +279,9 @@<br>
><br>
> if (Newline) {<br>
> unsigned WhitespaceStartColumn = State.Column;<br>
> - if (Previous.is(tok::l_brace)) {<br>
> + if (Current.is(tok::r_brace)) {<br>
> + State.Column = Line.Level * 2;<br>
> + } else if (Previous.is(tok::l_brace)) {<br>
> // FIXME: This does not work with nested static initializers.<br>
> // Implement a better handling for static initializers and similar<br>
> // constructs.<br>
><br>
> Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=172058&r1=172057&r2=172058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=172058&r1=172057&r2=172058&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)<br>
> +++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Jan 10 05:52:21 2013<br>
> @@ -76,6 +76,40 @@<br>
> FormatToken Token;<br>
> };<br>
><br>
> +class ScopedLineState {<br>
> +public:<br>
> + ScopedLineState(UnwrappedLineParser &Parser) : Parser(Parser) {<br>
> + PreBlockLine = Parser.Line.take();<br>
> + Parser.Line.reset(new UnwrappedLine(*PreBlockLine));<br>
> + assert(Parser.LastInCurrentLine == NULL ||<br>
> + Parser.LastInCurrentLine->Children.empty());<br>
> + PreBlockLastToken = Parser.LastInCurrentLine;<br>
> + PreBlockRootTokenInitialized = Parser.RootTokenInitialized;<br>
> + Parser.RootTokenInitialized = false;<br>
> + Parser.LastInCurrentLine = NULL;<br>
> + }<br>
> +<br>
> + ~ScopedLineState() {<br>
> + if (Parser.RootTokenInitialized) {<br>
> + Parser.addUnwrappedLine();<br>
> + }<br>
> + assert(!Parser.RootTokenInitialized);<br>
> + Parser.Line.reset(PreBlockLine);<br>
> + Parser.RootTokenInitialized = PreBlockRootTokenInitialized;<br>
> + Parser.LastInCurrentLine = PreBlockLastToken;<br>
> + assert(Parser.LastInCurrentLine == NULL ||<br>
> + Parser.LastInCurrentLine->Children.empty());<br>
> + Parser.MustBreakBeforeNextToken = true;<br>
> + }<br>
> +<br>
> +private:<br>
> + UnwrappedLineParser &Parser;<br>
> +<br>
> + UnwrappedLine *PreBlockLine;<br>
> + FormatToken* PreBlockLastToken;<br>
> + bool PreBlockRootTokenInitialized;<br>
> +};<br>
> +<br>
> UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,<br>
> FormatTokenSource &Tokens,<br>
> UnwrappedLineConsumer &Callback)<br>
> @@ -204,6 +238,7 @@<br>
> }<br>
><br>
> void UnwrappedLineParser::parseStructuralElement() {<br>
> + assert(!<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace));<br>
> parseComments();<br>
><br>
> int TokenNumber = 0;<br>
> @@ -289,6 +324,10 @@<br>
> parseParens();<br>
> break;<br>
> case tok::l_brace:<br>
> + // A block outside of parentheses must be the last part of a<br>
> + // structural element.<br>
> + // FIXME: Figure out cases where this is not true, and add projections for<br>
> + // them (the one we know is missing are lambdas).<br>
> parseBlock();<br>
> addUnwrappedLine();<br>
> return;<br>
> @@ -301,9 +340,9 @@<br>
> break;<br>
> case tok::equal:<br>
> nextToken();<br>
> - // Skip initializers as they will be formatted by a later step.<br>
> - if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace))<br>
> - nextToken();<br>
> + if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace)) {<br>
> + parseBracedList();<br>
> + }<br>
> break;<br>
> default:<br>
> nextToken();<br>
> @@ -312,6 +351,24 @@<br>
> } while (!eof());<br>
> }<br>
><br>
> +void UnwrappedLineParser::parseBracedList() {<br>
> + nextToken();<br>
> +<br>
> + do {<br>
> + switch (FormatTok.Tok.getKind()) {<br>
> + case tok::l_brace:<br>
> + parseBracedList();<br>
> + break;<br>
> + case tok::r_brace:<br>
> + nextToken();<br>
> + return;<br>
> + default:<br>
> + nextToken();<br>
> + break;<br>
> + }<br>
> + } while (!eof());<br>
> +}<br>
> +<br>
> void UnwrappedLineParser::parseParens() {<br>
> assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_paren) && "'(' expected.");<br>
> nextToken();<br>
> @@ -323,6 +380,15 @@<br>
> case tok::r_paren:<br>
> nextToken();<br>
> return;<br>
> + case tok::l_brace:<br>
> + {<br>
> + nextToken();<br>
> + ScopedLineState LineState(*this);<br>
> + Line->Level += 1;<br>
> + parseLevel(/*HasOpeningBrace=*/true);<br>
> + Line->Level -= 1;<br>
> + }<br>
> + break;<br>
> default:<br>
> nextToken();<br>
> break;<br>
> @@ -626,22 +692,8 @@<br>
> while (!Line->InPPDirective && <a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::hash) &&<br>
> ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||<br>
> FormatTok.IsFirst)) {<br>
> - UnwrappedLine* StoredLine = Line.take();<br>
> - Line.reset(new UnwrappedLine(*StoredLine));<br>
> - assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());<br>
> - FormatToken *StoredLastInCurrentLine = LastInCurrentLine;<br>
> - bool PreviousInitialized = RootTokenInitialized;<br>
> - RootTokenInitialized = false;<br>
> - LastInCurrentLine = NULL;<br>
> -<br>
> + ScopedLineState BlockState(*this);<br>
> parsePPDirective();<br>
> -<br>
> - assert(!RootTokenInitialized);<br>
> - Line.reset(StoredLine);<br>
> - RootTokenInitialized = PreviousInitialized;<br>
> - LastInCurrentLine = StoredLastInCurrentLine;<br>
> - assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());<br>
> - MustBreakBeforeNextToken = true;<br>
> }<br>
> }<br>
><br>
><br>
> Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=172058&r1=172057&r2=172058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=172058&r1=172057&r2=172058&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)<br>
> +++ cfe/trunk/lib/Format/UnwrappedLineParser.h Thu Jan 10 05:52:21 2013<br>
> @@ -131,6 +131,7 @@<br>
> void parsePPUnknown();<br>
> void parseComments();<br>
> void parseStructuralElement();<br>
> + void parseBracedList();<br>
> void parseParens();<br>
> void parseIfThenElse();<br>
> void parseForOrWhileLoop();<br>
> @@ -163,6 +164,8 @@<br>
> const FormatStyle &Style;<br>
> FormatTokenSource *Tokens;<br>
> UnwrappedLineConsumer &Callback;<br>
> +<br>
> + friend class ScopedLineState;<br>
> };<br>
><br>
> } // end namespace format<br>
><br>
> Modified: cfe/trunk/unittests/Format/FormatTest.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172058&r1=172057&r2=172058&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172058&r1=172057&r2=172058&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/unittests/Format/FormatTest.cpp (original)<br>
> +++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 10 05:52:21 2013<br>
> @@ -615,6 +615,30 @@<br>
> " trailing);", getLLVMStyleWithColumns(69));<br>
> }<br>
><br>
> +TEST_F(FormatTest, LayoutBlockInsideParens) {<br>
> + EXPECT_EQ("functionCall({\n"<br>
> + " int i;\n"<br>
> + "});", format(" functionCall ( {int i;} );"));<br>
> +}<br>
> +<br>
> +TEST_F(FormatTest, LayoutBlockInsideStatement) {<br>
> + EXPECT_EQ("SOME_MACRO {\n"<br>
> + " int i;\n"<br>
> + "}\n"<br>
> + "int i;", format(" SOME_MACRO {int i;} int i;"));<br>
> +}<br>
> +<br>
> +TEST_F(FormatTest, LayoutNestedBlocks) {<br>
> + verifyFormat("void AddOsStrings(unsigned bitmask) {\n"<br>
> + " struct s {\n"<br>
> + " int i;\n"<br>
> + " };\n"<br>
> + " s kBitsToOs[] = { { 10 } };\n"<br>
> + " for (int i = 0; i < 10; ++i)\n"<br>
> + " return;\n"<br>
> + "}");<br>
> +}<br>
> +<br>
> //===----------------------------------------------------------------------===//<br>
> // Line break tests.<br>
> //===----------------------------------------------------------------------===//<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br></div></div>