From e25509f85774529e2fe3e9631ada2200569f7698 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 17 Dec 2012 11:29:41 +0000 Subject: [PATCH] Fix several formatting problems. More specifically: - Improve formatting of static initializers. - Fix formatting of lines comments in enums. - Fix formmating of trailing line comments. llvm-svn: 170316 --- clang/lib/Format/Format.cpp | 9 +++++---- clang/lib/Format/UnwrappedLineParser.cpp | 27 +++++++++++++++------------ clang/lib/Format/UnwrappedLineParser.h | 2 +- clang/unittests/Format/FormatTest.cpp | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e8faa12a..eb077d3 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -734,15 +734,16 @@ private: } bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) { - if (Right.Tok.is(tok::r_paren)) + if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) || + Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater)) return false; if (isBinaryOperator(Left)) return true; if (Right.Tok.is(tok::lessless)) return true; - return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || - Left.Tok.is(tok::semi) || Left.Tok.is(tok::equal) || - Left.Tok.is(tok::ampamp) || Left.Tok.is(tok::pipepipe) || + return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || Left.Tok.is( + tok::semi) || Left.Tok.is(tok::equal) || Left.Tok.is(tok::ampamp) || + Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::l_brace) || (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren)); } diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 8545f33..7e91cb4 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -43,7 +43,8 @@ bool UnwrappedLineParser::parseLevel() { parsePPDirective(); break; case tok::comment: - parseComment(); + nextToken(); + addUnwrappedLine(); break; case tok::l_brace: Error |= parseBlock(); @@ -90,22 +91,16 @@ void UnwrappedLineParser::parsePPDirective() { } } -void UnwrappedLineParser::parseComment() { - while (!eof()) { - nextToken(); - if (FormatTok.NewlinesBefore > 0) { - addUnwrappedLine(); - return; - } - } -} - -void UnwrappedLineParser::parseStatement() { +void UnwrappedLineParser::parseComments() { // Consume leading line comments, e.g. for branches without compounds. while (FormatTok.Tok.is(tok::comment)) { nextToken(); addUnwrappedLine(); } +} + +void UnwrappedLineParser::parseStatement() { + parseComments(); switch (FormatTok.Tok.getKind()) { case tok::kw_namespace: @@ -164,6 +159,12 @@ void UnwrappedLineParser::parseStatement() { return; } break; + case tok::equal: + nextToken(); + // Skip initializers as they will be formatted by a later step. + if (FormatTok.Tok.is(tok::l_brace)) + nextToken(); + break; default: nextToken(); break; @@ -325,6 +326,7 @@ void UnwrappedLineParser::parseEnum() { nextToken(); addUnwrappedLine(); ++Line.Level; + parseComments(); break; case tok::l_paren: parseParens(); @@ -332,6 +334,7 @@ void UnwrappedLineParser::parseEnum() { case tok::comma: nextToken(); addUnwrappedLine(); + parseComments(); break; case tok::r_brace: if (HasContents) diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 908391d..287194f 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -96,7 +96,7 @@ private: bool parseLevel(); bool parseBlock(unsigned AddLevels = 1); void parsePPDirective(); - void parseComment(); + void parseComments(); void parseStatement(); void parseParens(); void parseIfThenElse(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index d70442c..5ce1022 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -245,6 +245,19 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { EXPECT_EQ("int i // This is a fancy variable\n = 5;", format("int i // This is a fancy variable\n= 5;")); + EXPECT_EQ("enum E {\n" + " // comment\n" + " VAL_A, // comment\n" + " VAL_B\n" + "};", + format("enum E{\n// comment\nVAL_A,// comment\nVAL_B};")); + + verifyFormat( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); +} + +TEST_F(FormatTest, UnderstandsMultiLineComments) { verifyFormat("f(/*test=*/ true);"); } @@ -319,6 +332,15 @@ TEST_F(FormatTest, FormatsNamespaces) { "}"); } +TEST_F(FormatTest, StaticInitializers) { + verifyFormat("static SomeClass SC = { 1, 'a' };"); + + // FIXME: Format like enums if the static initializer does not fit on a line. + verifyFormat( + "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); +} + //===----------------------------------------------------------------------===// // Line break tests. //===----------------------------------------------------------------------===// -- 2.7.4