bool UnwrappedLineParser::parseLevel() { bool Error = false; do { switch (FormatTok.Tok.getKind()) { case tok::hash: parsePPDirective(); break; case tok::comment: nextToken(); addUnwrappedLine(); break; case tok::l_brace: Error |= parseBlock(); addUnwrappedLine(); break; case tok::r_brace: // Stray '}' is an error. return true; default: parseStatement(); break; } } while (!eof()); return Error; }
void UnwrappedLineParser::parseIfThenElse() { assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); bool NeedsUnwrappedLine = false; if (FormatTok->Tok.is(tok::l_brace)) { parseBlock(/*MustBeDeclaration=*/ false); NeedsUnwrappedLine = true; } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } if (FormatTok->Tok.is(tok::kw_else)) { nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { parseBlock(/*MustBeDeclaration=*/ false); addUnwrappedLine(); } else if (FormatTok->Tok.is(tok::kw_if)) { parseIfThenElse(); } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } } else if (NeedsUnwrappedLine) { addUnwrappedLine(); } }
void UnwrappedLineParser::parseIfThenElse() { assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); nextToken(); parseParens(); bool NeedsUnwrappedLine = false; if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(); NeedsUnwrappedLine = true; } else { addUnwrappedLine(); ++Line.Level; parseStatement(); --Line.Level; } if (FormatTok.Tok.is(tok::kw_else)) { nextToken(); if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(); addUnwrappedLine(); } else if (FormatTok.Tok.is(tok::kw_if)) { parseIfThenElse(); } else { addUnwrappedLine(); ++Line.Level; parseStatement(); --Line.Level; } } else if (NeedsUnwrappedLine) { addUnwrappedLine(); } }
void UnwrappedLineParser::parseEnum() { bool HasContents = false; do { switch (FormatTok.Tok.getKind()) { case tok::l_brace: nextToken(); addUnwrappedLine(); ++Line.Level; parseComments(); break; case tok::l_paren: parseParens(); break; case tok::comma: nextToken(); addUnwrappedLine(); parseComments(); 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()); }
void UnwrappedLineParser::parseReturn() { nextToken(); do { switch (FormatTok->Tok.getKind()) { case tok::l_brace: parseBracedList(); if (FormatTok->Tok.isNot(tok::semi)) { // Assume missing ';'. addUnwrappedLine(); return; } break; case tok::l_paren: parseParens(); break; case tok::r_brace: // Assume missing ';'. addUnwrappedLine(); return; case tok::semi: nextToken(); addUnwrappedLine(); return; case tok::l_square: tryToParseLambda(); break; default: nextToken(); break; } } while (!eof()); }
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { nextToken(); nextToken(); // interface name // @interface can be followed by either a base class, or a category. if (FormatTok->Tok.is(tok::colon)) { nextToken(); nextToken(); // base class name } else if (FormatTok->Tok.is(tok::l_paren)) // Skip category, if present. parseParens(); if (FormatTok->Tok.is(tok::less)) parseObjCProtocolList(); if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || Style.BreakBeforeBraces == FormatStyle::BS_GNU) addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/true); } // With instance variables, this puts '}' on its own line. Without instance // variables, this ends the @interface line. addUnwrappedLine(); parseObjCUntilAtEnd(); }
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { bool SwitchLabelEncountered = false; do { switch (FormatTok->Tok.getKind()) { case tok::comment: nextToken(); addUnwrappedLine(); break; case tok::l_brace: // FIXME: Add parameter whether this can happen - if this happens, we must // be in a non-declaration context. parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); break; case tok::r_brace: if (HasOpeningBrace) return; StructuralError = true; nextToken(); addUnwrappedLine(); break; case tok::kw_default: case tok::kw_case: if (!SwitchLabelEncountered && (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) ++Line->Level; SwitchLabelEncountered = true; parseStructuralElement(); break; default: parseStructuralElement(); break; } } while (!eof()); }
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { do { switch (FormatTok->Tok.getKind()) { case tok::comment: nextToken(); addUnwrappedLine(); break; case tok::l_brace: // FIXME: Add parameter whether this can happen - if this happens, we must // be in a non-declaration context. parseBlock(/*MustBeDeclaration=*/ false); addUnwrappedLine(); break; case tok::r_brace: if (HasOpeningBrace) return; StructuralError = true; nextToken(); addUnwrappedLine(); break; default: parseStructuralElement(); break; } } while (!eof()); }
void UnwrappedLineParser::parseSwitch() { assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); nextToken(); parseParens(); if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(Style.IndentCaseLabels ? 2 : 1); addUnwrappedLine(); } else { addUnwrappedLine(); Line.Level += (Style.IndentCaseLabels ? 2 : 1); parseStatement(); Line.Level -= (Style.IndentCaseLabels ? 2 : 1); } }
void UnwrappedLineParser::parseForOrWhileLoop() { assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && "'for' or 'while' expected"); nextToken(); parseParens(); if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(); addUnwrappedLine(); } else { addUnwrappedLine(); ++Line.Level; parseStatement(); --Line.Level; } }
void UnwrappedLineParser::parseSwitch() { assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1); addUnwrappedLine(); } else { addUnwrappedLine(); Line->Level += (Style.IndentCaseLabels ? 2 : 1); parseStructuralElement(); Line->Level -= (Style.IndentCaseLabels ? 2 : 1); } }
void UnwrappedLineParser::parseForOrWhileLoop() { assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) && "'for' or 'while' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { parseBlock(/*MustBeDeclaration=*/ false); addUnwrappedLine(); } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } }
void UnwrappedLineParser::parseComments() { // Consume leading line comments, e.g. for branches without compounds. while (FormatTok.Tok.is(tok::comment)) { nextToken(); addUnwrappedLine(); } }
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, bool MunchSemi) { assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); unsigned InitialLevel = Line->Level; nextToken(); addUnwrappedLine(); ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, MustBeDeclaration); if (AddLevel) ++Line->Level; parseLevel(/*HasOpeningBrace=*/true); if (!FormatTok->Tok.is(tok::r_brace)) { Line->Level = InitialLevel; StructuralError = true; return; } nextToken(); // Munch the closing brace. if (MunchSemi && FormatTok->Tok.is(tok::semi)) nextToken(); Line->Level = InitialLevel; }
void UnwrappedLineParser::parseObjCUntilAtEnd() { do { if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { nextToken(); addUnwrappedLine(); break; } if (FormatTok->is(tok::l_brace)) { parseBlock(/*MustBeDeclaration=*/false); // In ObjC interfaces, nothing should be following the "}". addUnwrappedLine(); } else { parseStructuralElement(); } } while (!eof()); }
void UnwrappedLineParser::parseObjCProtocol() { nextToken(); nextToken(); // protocol name if (FormatTok->Tok.is(tok::less)) parseObjCProtocolList(); // Check for protocol declaration. if (FormatTok->Tok.is(tok::semi)) { nextToken(); return addUnwrappedLine(); } addUnwrappedLine(); parseObjCUntilAtEnd(); }
void UnwrappedLineParser::parseEnum() { if (FormatTok->Tok.is(tok::kw_enum)) { // Won't be 'enum' for NS_ENUMs. nextToken(); } // 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)) { nextToken(); // We can have macros or attributes in between 'enum' and the enum name. if (FormatTok->Tok.is(tok::l_paren)) { parseParens(); } if (FormatTok->Tok.is(tok::identifier)) nextToken(); } if (FormatTok->Tok.is(tok::l_brace)) { FormatTok->BlockKind = BK_Block; bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); if (HasError) { if (FormatTok->is(tok::semi)) nextToken(); addUnwrappedLine(); } } // We fall through to parsing a structural element afterwards, so that in // enum A {} n, m; // "} n, m;" will end up in one unwrapped line. }
void UnwrappedLineParser::parseSwitch() { assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } }
void UnwrappedLineParser::parseAccessSpecifier() { nextToken(); // Otherwise, we don't know what it is, and we'd better keep the next token. if (FormatTok->Tok.is(tok::colon)) nextToken(); addUnwrappedLine(); }
void UnwrappedLineParser::parseSwitch() { assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } }
void UnwrappedLineParser::parseNamespace() { assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); nextToken(); if (FormatTok->Tok.is(tok::identifier)) nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/ true, 0); // Munch the semicolon after a namespace. This is more common than one would // think. Puttin the semicolon into its own line is very ugly. if (FormatTok->Tok.is(tok::semi)) nextToken(); addUnwrappedLine(); } // FIXME: Add error handling. }
void UnwrappedLineParser::parseFile() { ScopedDeclarationState DeclarationState( *Line, DeclarationScopeStack, /*MustBeDeclaration=*/ !Line->InPPDirective); parseLevel(/*HasOpeningBrace=*/false); // Make sure to format the remaining tokens. flushComments(true); addUnwrappedLine(); }
void UnwrappedLineParser::parseForOrWhileLoop() { assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) || FormatTok->IsForEachMacro) && "'for', 'while' or foreach macro expected"); nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else { addUnwrappedLine(); ++Line->Level; parseStructuralElement(); --Line->Level; } }
void UnwrappedLineParser::parsePPDirective() { while (!eof()) { nextToken(); if (FormatTok.NewlinesBefore > 0) { addUnwrappedLine(); return; } } }
void UnwrappedLineParser::parseEnum() { nextToken(); if (FormatTok->Tok.is(tok::identifier) || FormatTok->Tok.is(tok::kw___attribute) || FormatTok->Tok.is(tok::kw___declspec)) { nextToken(); // We can have macros or attributes in between 'enum' and the enum name. if (FormatTok->Tok.is(tok::l_paren)) { parseParens(); } if (FormatTok->Tok.is(tok::identifier)) nextToken(); } if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) addUnwrappedLine(); nextToken(); addUnwrappedLine(); ++Line->Level; do { switch (FormatTok->Tok.getKind()) { case tok::l_paren: parseParens(); break; case tok::r_brace: addUnwrappedLine(); nextToken(); --Line->Level; return; case tok::comma: nextToken(); addUnwrappedLine(); break; default: nextToken(); break; } } while (!eof()); } // We fall through to parsing a structural element afterwards, so that in // enum A {} n, m; // "} n, m;" will end up in one unwrapped line. }
void UnwrappedLineParser::parseObjCUntilAtEnd() { do { if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { nextToken(); addUnwrappedLine(); break; } parseStructuralElement(); } while (!eof()); }
void UnwrappedLineParser::parseNamespace() { assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); nextToken(); if (FormatTok.Tok.is(tok::identifier)) nextToken(); if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(0); addUnwrappedLine(); } // FIXME: Add error handling. }
void UnwrappedLineParser::parseAccessSpecifier() { nextToken(); // Understand Qt's slots. if (FormatTok->is(tok::identifier) && (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS")) nextToken(); // Otherwise, we don't know what it is, and we'd better keep the next token. if (FormatTok->Tok.is(tok::colon)) nextToken(); addUnwrappedLine(); }
void UnwrappedLineParser::parseLabel() { if (FormatTok->Tok.isNot(tok::colon)) return; nextToken(); unsigned OldLineLevel = Line->Level; 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(); 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) addUnwrappedLine(); parseStructuralElement(); } } addUnwrappedLine(); Line->Level = OldLineLevel; }
void UnwrappedLineParser::parseDoWhile() { assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); nextToken(); if (FormatTok.Tok.is(tok::l_brace)) { parseBlock(); } else { addUnwrappedLine(); ++Line.Level; parseStatement(); --Line.Level; } // FIXME: Add error handling. if (!FormatTok.Tok.is(tok::kw_while)) { addUnwrappedLine(); return; } nextToken(); parseStatement(); }