diff options
Diffstat (limited to 'lib/Format/UnwrappedLineParser.cpp')
-rw-r--r-- | lib/Format/UnwrappedLineParser.cpp | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 6ad4329..ea1ca39 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -251,7 +251,6 @@ void UnwrappedLineParser::parse() { assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); } } while (!PPLevelBranchIndex.empty()); - } void UnwrappedLineParser::parseFile() { @@ -774,7 +773,15 @@ void UnwrappedLineParser::parseStructuralElement() { parseBracedList(); break; case tok::kw_enum: + // parseEnum falls through and does not yet add an unwrapped line as an + // enum definition can start a structural element. parseEnum(); + // This does not apply for Java and JavaScript. + if (Style.Language == FormatStyle::LK_Java || + Style.Language == FormatStyle::LK_JavaScript) { + addUnwrappedLine(); + return; + } break; case tok::kw_typedef: nextToken(); @@ -785,9 +792,15 @@ void UnwrappedLineParser::parseStructuralElement() { case tok::kw_struct: case tok::kw_union: case tok::kw_class: + // parseRecord falls through and does not yet add an unwrapped line as a + // record declaration or definition can start a structural element. parseRecord(); - // A record declaration or definition is always the start of a structural - // element. + // This does not apply for Java and JavaScript. + if (Style.Language == FormatStyle::LK_Java || + Style.Language == FormatStyle::LK_JavaScript) { + addUnwrappedLine(); + return; + } break; case tok::period: nextToken(); @@ -848,6 +861,7 @@ void UnwrappedLineParser::parseStructuralElement() { Style.Language == FormatStyle::LK_Java) && FormatTok->is(Keywords.kw_interface)) { parseRecord(); + addUnwrappedLine(); break; } @@ -872,8 +886,7 @@ void UnwrappedLineParser::parseStructuralElement() { ? FormatTok->NewlinesBefore > 0 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; - if (FollowedByNewline && - (Text.size() >= 5 || FunctionLike) && + if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { addUnwrappedLine(); return; @@ -1033,7 +1046,7 @@ void UnwrappedLineParser::tryToParseJSFunction() { if (FormatTok->is(tok::l_brace)) tryToParseBracedList(); else - while(FormatTok->isNot(tok::l_brace) && !eof()) + while (FormatTok->isNot(tok::l_brace) && !eof()) nextToken(); } @@ -1066,7 +1079,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { nextToken(); // Fat arrows can be followed by simple expressions or by child blocks // in curly braces. - if (FormatTok->is(tok::l_brace)){ + if (FormatTok->is(tok::l_brace)) { parseChildBlock(); continue; } @@ -1475,6 +1488,7 @@ void UnwrappedLineParser::parseEnum() { // Eat up enum class ... if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) nextToken(); + while (FormatTok->Tok.getIdentifierInfo() || FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, tok::greater, tok::comma, tok::question)) { @@ -1482,8 +1496,14 @@ void UnwrappedLineParser::parseEnum() { // We can have macros or attributes in between 'enum' and the enum name. if (FormatTok->is(tok::l_paren)) parseParens(); - if (FormatTok->is(tok::identifier)) + if (FormatTok->is(tok::identifier)) { nextToken(); + // If there are two identifiers in a row, this is likely an elaborate + // return type. In Java, this can be "implements", etc. + if (Style.Language == FormatStyle::LK_Cpp && + FormatTok->is(tok::identifier)) + return; + } } // Just a declaration or something is wrong. @@ -1505,8 +1525,8 @@ void UnwrappedLineParser::parseEnum() { addUnwrappedLine(); } - // We fall through to parsing a structural element afterwards, so that in - // enum A {} n, m; + // There is no addUnwrappedLine() here so that we fall through to parsing a + // structural element afterwards. Thus, in "enum A {} n, m;", // "} n, m;" will end up in one unwrapped line. } @@ -1576,7 +1596,6 @@ void UnwrappedLineParser::parseRecord() { const FormatToken &InitialToken = *FormatTok; nextToken(); - // The actual identifier can be a nested name specifier, and in macros // it is often token-pasted. while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, @@ -1623,13 +1642,9 @@ void UnwrappedLineParser::parseRecord() { parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, /*MunchSemi=*/false); } - // We fall through to parsing a structural element afterwards, so - // class A {} n, m; - // will end up in one unwrapped line. - // This does not apply for Java and JavaScript. - if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) - addUnwrappedLine(); + // There is no addUnwrappedLine() here so that we fall through to parsing a + // structural element afterwards. Thus, in "class A {} n, m;", + // "} n, m;" will end up in one unwrapped line. } void UnwrappedLineParser::parseObjCProtocolList() { @@ -1722,7 +1737,8 @@ void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { return; } - if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_var)) + if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum, + Keywords.kw_var)) return; // Fall through to parsing the corresponding structure. if (FormatTok->is(tok::l_brace)) { |