diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index d9ff99d3cb86..7c04ce09dda9 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -99,19 +99,38 @@ void UnwrappedLineParser::parseComment() { } void UnwrappedLineParser::parseStatement() { - if (FormatTok.Tok.is(tok::kw_public) || FormatTok.Tok.is(tok::kw_protected) || - FormatTok.Tok.is(tok::kw_private)) { + switch (FormatTok.Tok.getKind()) { + case tok::kw_public: + case tok::kw_protected: + case tok::kw_private: parseAccessSpecifier(); return; - } - if (FormatTok.Tok.is(tok::kw_enum)) { - parseEnum(); + case tok::kw_if: + parseIfThenElse(); return; + case tok::kw_do: + parseDoWhile(); + return; + case tok::kw_switch: + parseSwitch(); + return; + case tok::kw_default: + nextToken(); + parseLabel(); + return; + case tok::kw_case: + parseCaseLabel(); + return; + default: + break; } int TokenNumber = 0; do { ++TokenNumber; switch (FormatTok.Tok.getKind()) { + case tok::kw_enum: + parseEnum(); + return; case tok::semi: nextToken(); addUnwrappedLine(); @@ -123,32 +142,16 @@ void UnwrappedLineParser::parseStatement() { parseBlock(); addUnwrappedLine(); return; - case tok::kw_if: - parseIfThenElse(); - return; - case tok::kw_do: - parseDoWhile(); - return; - case tok::kw_switch: - parseSwitch(); - return; - case tok::kw_default: - nextToken(); - parseLabel(); - return; - case tok::kw_case: - parseCaseLabel(); - return; - case tok::raw_identifier: - nextToken(); - break; - default: + case tok::identifier: nextToken(); if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { parseLabel(); return; } break; + default: + nextToken(); + break; } } while (!eof()); } @@ -265,12 +268,35 @@ void UnwrappedLineParser::parseAccessSpecifier() { } void UnwrappedLineParser::parseEnum() { + bool HasContents = false; do { - nextToken(); - if (FormatTok.Tok.is(tok::semi)) { + switch (FormatTok.Tok.getKind()) { + case tok::l_brace: + nextToken(); + addUnwrappedLine(); + ++Line.Level; + break; + case tok::l_paren: + parseParens(); + break; + case tok::comma: + nextToken(); + addUnwrappedLine(); + break; + case tok::r_brace: + if (HasContents) + addUnwrappedLine(); + --Line.Level; + nextToken(); + break; + case tok::semi: nextToken(); addUnwrappedLine(); return; + default: + HasContents = true; + nextToken(); + break; } } while (!eof()); } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f64ea4acd675..e21367bb315d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -244,6 +244,21 @@ TEST_F(FormatTest, DoWhile) { "while (something());"); } +TEST_F(FormatTest, Enum) { + verifyFormat("enum {\n" + " Zero,\n" + " One = 1,\n" + " Two = One + 1,\n" + " Three = (One + Two),\n" + " Four = (Zero && (One ^ Two)) | (One << Two),\n" + " Five = (One, Two, Three, Four, 5)\n" + "};"); + verifyFormat("enum Enum {\n" + "};"); + verifyFormat("enum {\n" + "};"); +} + TEST_F(FormatTest, BreaksDesireably) { verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"