From b05a81debb3e717d7b644ec7f136462a68bd6ec8 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 9 May 2014 13:11:16 +0000 Subject: [PATCH] clang-format: Fix bug introduced by r208392. Also run clang-format over clang-format's files. llvm-svn: 208409 --- clang/lib/Format/BreakableToken.cpp | 8 +-- clang/lib/Format/ContinuationIndenter.cpp | 13 ++-- clang/lib/Format/Format.cpp | 72 +++++++++++------------ clang/lib/Format/FormatToken.cpp | 13 ++-- clang/lib/Format/FormatToken.h | 4 +- clang/lib/Format/TokenAnnotator.cpp | 26 ++++---- clang/lib/Format/UnwrappedLineParser.cpp | 20 +++---- clang/lib/Format/UnwrappedLineParser.h | 2 +- clang/lib/Format/WhitespaceManager.cpp | 5 +- 9 files changed, 74 insertions(+), 89 deletions(-) diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index 93cca6663400..1bea0e565131 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -82,11 +82,9 @@ static BreakableToken::Split getCommentSplit(StringRef Text, return BreakableToken::Split(StringRef::npos, 0); } -static BreakableToken::Split getStringSplit(StringRef Text, - unsigned UsedColumns, - unsigned ColumnLimit, - unsigned TabWidth, - encoding::Encoding Encoding) { +static BreakableToken::Split +getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit, + unsigned TabWidth, encoding::Encoding Encoding) { // FIXME: Reduce unit test case. if (Text.empty()) return BreakableToken::Split(StringRef::npos, 0); diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 5c2bdc521719..77d981b8f5ed 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -98,8 +98,8 @@ bool ContinuationIndenter::canBreak(const LineState &State) { // The opening "{" of a braced list has to be on the same line as the first // element if it is nested in another braced init list or function call. if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && - Previous.Type != TT_DictLiteral && - Previous.BlockKind == BK_BracedInit && Previous.Previous && + Previous.Type != TT_DictLiteral && Previous.BlockKind == BK_BracedInit && + Previous.Previous && Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) return false; // This prevents breaks like: @@ -427,8 +427,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, !PreviousNonComment->isOneOf(tok::comma, tok::semi) && PreviousNonComment->Type != TT_TemplateCloser && PreviousNonComment->Type != TT_BinaryOperator && - Current.Type != TT_BinaryOperator && - !PreviousNonComment->opensScope()) + Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope()) State.Stack.back().BreakBeforeParameter = true; // If we break after { or the [ of an array initializer, we should also break @@ -462,8 +461,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { const FormatToken *NextNonComment = Previous.getNextNonComment(); if (!NextNonComment) NextNonComment = &Current; - if (NextNonComment->is(tok::l_brace) && - NextNonComment->BlockKind == BK_Block) + if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block) return Current.NestingLevel == 0 ? State.FirstIndent : State.Stack.back().Indent; if (Current.isOneOf(tok::r_brace, tok::r_square)) { @@ -832,8 +830,7 @@ unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, return 0; } -static bool getRawStringLiteralPrefixPostfix(StringRef Text, - StringRef &Prefix, +static bool getRawStringLiteralPrefixPostfix(StringRef Text, StringRef &Prefix, StringRef &Postfix) { if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") || Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") || diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 4a66d7aa9563..dcbc9502652d 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -212,7 +212,7 @@ template <> struct MappingTraits { Style.SpaceBeforeAssignmentOperators); IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); IO.mapOptional("CommentPragmas", Style.CommentPragmas); - IO.mapOptional("ForEachMacros", Style.ForEachMacros); + IO.mapOptional("ForEachMacros", Style.ForEachMacros); // For backward compatibility. if (!IO.outputting()) { @@ -240,7 +240,7 @@ template <> struct DocumentListTraits > { if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { Template = Seq[0]; } else { - Template = *((const FormatStyle*)IO.getContext()); + Template = *((const FormatStyle *)IO.getContext()); Template.Language = FormatStyle::LK_None; } Seq.resize(Index + 1, Template); @@ -1188,7 +1188,7 @@ public: FirstInLineIndex(0) { Lex.SetKeepWhitespaceMode(true); - for (const std::string& ForEachMacro : Style.ForEachMacros) + for (const std::string &ForEachMacro : Style.ForEachMacros) ForEachMacros.push_back(&IdentTable.get(ForEachMacro)); std::sort(ForEachMacros.begin(), ForEachMacros.end()); } @@ -1218,10 +1218,10 @@ private: if (tryMergeJSRegexLiteral()) return; - static tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal}; - static tok::TokenKind JSNotIdentity[] = {tok::exclaimequal, tok::equal}; - static tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater, - tok::greaterequal}; + static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal }; + static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal }; + static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater, + tok::greaterequal }; // FIXME: We probably need to change token type to mimic operator with the // correct priority. if (tryMergeTokens(JSIdentity)) @@ -1261,30 +1261,30 @@ private: // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by // a division. bool tryMergeJSRegexLiteral() { - if (Tokens.size() < 2 || Tokens.back()->isNot(tok::slash) || - Tokens[Tokens.size() - 2]->is(tok::unknown)) - return false; - unsigned TokenCount = 0; - unsigned LastColumn = Tokens.back()->OriginalColumn; - for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) { - ++TokenCount; - if (I[0]->is(tok::slash) && I + 1 != E && - (I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace, - tok::exclaim, tok::l_square, tok::colon, tok::comma, - tok::question, tok::kw_return) || - I[1]->isBinaryOperator())) { - Tokens.resize(Tokens.size() - TokenCount); - Tokens.back()->Tok.setKind(tok::unknown); - Tokens.back()->Type = TT_RegexLiteral; - Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn; - return true; - } - - // There can't be a newline inside a regex literal. - if (I[0]->NewlinesBefore > 0) - return false; - } + if (Tokens.size() < 2 || Tokens.back()->isNot(tok::slash) || + Tokens[Tokens.size() - 2]->is(tok::unknown)) return false; + unsigned TokenCount = 0; + unsigned LastColumn = Tokens.back()->OriginalColumn; + for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) { + ++TokenCount; + if (I[0]->is(tok::slash) && I + 1 != E && + (I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace, + tok::exclaim, tok::l_square, tok::colon, tok::comma, + tok::question, tok::kw_return) || + I[1]->isBinaryOperator())) { + Tokens.resize(Tokens.size() - TokenCount); + Tokens.back()->Tok.setKind(tok::unknown); + Tokens.back()->Type = TT_RegexLiteral; + Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn; + return true; + } + + // There can't be a newline inside a regex literal. + if (I[0]->NewlinesBefore > 0) + return false; + } + return false; } bool tryMerge_TMacro() { @@ -1534,7 +1534,7 @@ private: // Index (in 'Tokens') of the last token that starts a new line. unsigned FirstInLineIndex; SmallVector Tokens; - SmallVector ForEachMacros; + SmallVector ForEachMacros; void readRawToken(FormatToken &Tok) { Lex.LexFromRawLexer(Tok.Tok); @@ -1713,12 +1713,10 @@ private: bool LineMoved = PreviousLine && PreviousLine->Affected && Line->First->NewlinesBefore == 0; - bool IsContinuedComment = Line->First->is(tok::comment) && - Line->First->Next == nullptr && - Line->First->NewlinesBefore < 2 && - PreviousLine && - PreviousLine->Affected && - PreviousLine->Last->is(tok::comment); + bool IsContinuedComment = + Line->First->is(tok::comment) && Line->First->Next == nullptr && + Line->First->NewlinesBefore < 2 && PreviousLine && + PreviousLine->Affected && PreviousLine->Last->is(tok::comment); if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || IsContinuedComment) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 5bc5acaebea1..c91d25f46de1 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -64,16 +64,15 @@ unsigned CommaSeparatedList::formatAfterToken(LineState &State, // Ensure that we start on the opening brace. const FormatToken *LBrace = State.NextToken->Previous->Previous; - if (LBrace->isNot(tok::l_brace) || - LBrace->BlockKind == BK_Block || + if (LBrace->isNot(tok::l_brace) || LBrace->BlockKind == BK_Block || LBrace->Type == TT_DictLiteral || LBrace->Next->Type == TT_DesignatedInitializerPeriod) return 0; // Calculate the number of code points we have to format this list. As the // first token is already placed, we have to subtract it. - unsigned RemainingCodePoints = Style.ColumnLimit - State.Column + - State.NextToken->Previous->ColumnWidth; + unsigned RemainingCodePoints = + Style.ColumnLimit - State.Column + State.NextToken->Previous->ColumnWidth; // Find the best ColumnFormat, i.e. the best number of columns to use. const ColumnFormat *Format = getColumnFormat(RemainingCodePoints); @@ -186,8 +185,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { // If this doesn't have a nested list, we require at least 6 elements in order // create a column layout. If it has a nested list, column layout ensures one // list element per line. - if (HasNestedBracedList || Commas.size() < 5 || - Token->NestingLevel != 0) + if (HasNestedBracedList || Commas.size() < 5 || Token->NestingLevel != 0) return; // We can never place more than ColumnLimit / 3 items in a row (because of the @@ -209,8 +207,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { HasRowWithSufficientColumns = true; unsigned length = (Column == Columns - 1) ? EndOfLineItemLength[i] : ItemLengths[i]; - Format.ColumnSizes[Column] = - std::max(Format.ColumnSizes[Column], length); + Format.ColumnSizes[Column] = std::max(Format.ColumnSizes[Column], length); ++Column; } // If all rows are terminated early (e.g. by trailing comments), we don't diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index a0214631844a..f2bba324e67e 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -455,7 +455,9 @@ public: bool DryRun) override; /// \brief Adds \p Token as the next comma to the \c CommaSeparated list. - void CommaFound(const FormatToken *Token) override { Commas.push_back(Token);} + void CommaFound(const FormatToken *Token) override { + Commas.push_back(Token); + } private: /// \brief A struct that holds information on how to format a given list with diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 2d864f25ae7a..85e907d77387 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -226,9 +226,8 @@ private: bool StartsObjCMethodExpr = Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare && CurrentToken->isNot(tok::l_brace) && - (!Parent || - Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, - tok::kw_return, tok::kw_throw) || + (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, + tok::kw_return, tok::kw_throw) || Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn || Parent->Type == TT_CastRParen || getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); @@ -389,8 +388,7 @@ private: Contexts.back().FirstObjCSelectorName = Tok->Previous; } else if (Contexts.back().ColonIsForRangeExpr) { Tok->Type = TT_RangeBasedForLoopColon; - } else if (CurrentToken && - CurrentToken->is(tok::numeric_constant)) { + } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { Tok->Type = TT_BitFieldColon; } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) { Tok->Type = TT_InheritanceColon; @@ -604,7 +602,8 @@ public: private: void resetTokenMetadata(FormatToken *Token) { - if (!Token) return; + if (!Token) + return; // Reset token type in case we have already looked at it and then // recovered from an error (e.g. failure to find the matching >). @@ -867,9 +866,8 @@ private: if (Prev && Tok.Next && Tok.Next->Next) { bool NextIsUnary = Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star); - IsCast = - NextIsUnary && - Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant); + IsCast = NextIsUnary && Tok.Next->Next->isOneOf( + tok::identifier, tok::numeric_constant); } for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) { @@ -964,7 +962,6 @@ private: return TT_UnaryOperator; } - SmallVector Contexts; const FormatStyle &Style; @@ -1331,8 +1328,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, // Use a slightly higher penalty after ")" so that annotations like // "const override" are kept together. bool is_short_annotation = Right.TokenText.size() < 10; - return (Left.is(tok::r_paren) ? 100 : 120) + - (is_short_annotation ? 50 : 0); + return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0); } // In for-loops, prefer breaking at ',' and ';'. @@ -1529,8 +1525,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return false; if (Tok.is(tok::colon)) return !Line.First->isOneOf(tok::kw_case, tok::kw_default) && - Tok.getNextNonComment() && - Tok.Type != TT_ObjCMethodExpr && !Tok.Previous->is(tok::question) && + Tok.getNextNonComment() && Tok.Type != TT_ObjCMethodExpr && + !Tok.Previous->is(tok::question) && (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals); if (Tok.Previous->Type == TT_UnaryOperator || Tok.Previous->Type == TT_CastRParen) @@ -1737,7 +1733,7 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) llvm::errs() << Tok->FakeLParens[i] << "/"; llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; - if (Tok->Next) + if (!Tok->Next) assert(Tok == Line.Last); Tok = Tok->Next; } diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index b9b071bca8d3..e2613fbc171c 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -164,9 +164,7 @@ public: ++LineLevel; } } - ~CompoundStatementIndenter() { - LineLevel = OldLineLevel; - } + ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } private: unsigned &LineLevel; @@ -207,9 +205,10 @@ private: UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, ArrayRef Tokens, UnwrappedLineConsumer &Callback) - : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), - CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(nullptr), - Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {} + : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), + CurrentLines(&Lines), StructuralError(false), Style(Style), + Tokens(nullptr), Callback(Callback), AllTokens(Tokens), + PPBranchLevel(-1) {} void UnwrappedLineParser::reset() { PPBranchLevel = -1; @@ -1279,9 +1278,8 @@ void UnwrappedLineParser::parseEnum() { nextToken(); } // Eat up enum class ... - if (FormatTok->Tok.is(tok::kw_class) || - FormatTok->Tok.is(tok::kw_struct)) - nextToken(); + 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(); @@ -1476,7 +1474,7 @@ void UnwrappedLineParser::addUnwrappedLine() { bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } -bool UnwrappedLineParser::isOnNewLine(const FormatToken& FormatTok) { +bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && FormatTok.NewlinesBefore > 0; } @@ -1531,7 +1529,7 @@ void UnwrappedLineParser::readToken() { conditionalCompilationStart(/*Unreachable=*/false); } else if (FormatTok->Type == TT_ConflictAlternative) { conditionalCompilationAlternative(); - } else if(FormatTok->Type == TT_ConflictEnd) { + } else if (FormatTok->Type == TT_ConflictEnd) { conditionalCompilationEnd(); } FormatTok = Tokens->getNextToken(); diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 58099c9f5110..c9182e9d71d3 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -119,7 +119,7 @@ private: void conditionalCompilationAlternative(); void conditionalCompilationEnd(); - bool isOnNewLine(const FormatToken& FormatTok); + bool isOnNewLine(const FormatToken &FormatTok); // FIXME: We are constantly running into bugs where Line.Level is incorrectly // subtracted from beyond 0. Introduce a method to subtract from Line.Level diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 2906b17d7b04..47b94de4c6f7 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -18,9 +18,8 @@ namespace clang { namespace format { -bool -WhitespaceManager::Change::IsBeforeInFile::operator()(const Change &C1, - const Change &C2) const { +bool WhitespaceManager::Change::IsBeforeInFile:: +operator()(const Change &C1, const Change &C2) const { return SourceMgr.isBeforeInTranslationUnit( C1.OriginalWhitespaceRange.getBegin(), C2.OriginalWhitespaceRange.getBegin());