diff options
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 15 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Format/FormatToken.h | 1 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 47 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.h | 5 |
5 files changed, 53 insertions, 19 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 39baef69306..683bb69eacd 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -203,10 +203,12 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { !Current.isTrailingComment()) return true; - if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && - State.Line->MightBeFunctionDecl && - State.Stack.back().BreakBeforeParameter && Current.NestingLevel == 0) + // If the return type spans multiple lines, wrap before the function name. + if ((Current.Type == TT_FunctionDeclarationName || + Current.is(tok::kw_operator)) && + State.Stack.back().BreakBeforeParameter) return true; + if (startsSegmentOfBuilderTypeCall(Current) && (State.Stack.back().CallContinuation != 0 || (State.Stack.back().BreakBeforeParameter && @@ -518,11 +520,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { return State.Stack.back().VariablePos; if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration || PreviousNonComment->Type == TT_AttributeParen)) || - ((NextNonComment->Type == TT_StartOfName || - NextNonComment->is(tok::kw_operator)) && - Current.NestingLevel == 0 && - (!Style.IndentFunctionDeclarationAfterType || - State.Line->StartsDefinition))) + NextNonComment->is(tok::kw_operator) || + NextNonComment->Type == TT_FunctionDeclarationName) return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent); if (NextNonComment->Type == TT_SelectorName) { if (!State.Stack.back().ObjCSelectorNameFound) { diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 2ac5c9da0f9..a65f2e2ba2a 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -216,8 +216,6 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("TabWidth", Style.TabWidth); IO.mapOptional("UseTab", Style.UseTab); IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); - IO.mapOptional("IndentFunctionDeclarationAfterType", - Style.IndentFunctionDeclarationAfterType); IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); @@ -328,7 +326,6 @@ FormatStyle getLLVMStyle() { LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); LLVMStyle.IndentCaseLabels = false; - LLVMStyle.IndentFunctionDeclarationAfterType = false; LLVMStyle.IndentWidth = 2; LLVMStyle.TabWidth = 8; LLVMStyle.MaxEmptyLinesToKeep = 1; @@ -373,7 +370,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; GoogleStyle.DerivePointerAlignment = true; GoogleStyle.IndentCaseLabels = true; - GoogleStyle.IndentFunctionDeclarationAfterType = true; GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; GoogleStyle.ObjCSpaceAfterProperty = false; GoogleStyle.ObjCSpaceBeforeProtocolList = false; diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 26fa061767b..d83804e2e04 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -40,6 +40,7 @@ enum TokenType { TT_CtorInitializerComma, TT_DesignatedInitializerPeriod, TT_DictLiteral, + TT_FunctionDeclarationName, TT_FunctionLBrace, TT_FunctionTypeLParen, TT_ImplicitStringLiteral, diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index f68883da05b..56aa384e744 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -325,8 +325,6 @@ private: return false; } } - // No closing "}" found, this probably starts a definition. - Line.StartsDefinition = true; return true; } @@ -1201,6 +1199,43 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { Line.First->CanBreakBefore = Line.First->MustBreakBefore; } +// This function heuristically determines whether 'Current' starts the name of a +// function declaration. +static bool isFunctionDeclarationName(const FormatToken &Current) { + if (Current.Type != TT_StartOfName || + Current.NestingLevel != 0 || + Current.Previous->Type == TT_StartOfName) + return false; + const FormatToken *Next = Current.Next; + for (; Next; Next = Next->Next) { + if (Next->Type == TT_TemplateOpener) { + Next = Next->MatchingParen; + } else if (Next->is(tok::coloncolon)) { + Next = Next->Next; + if (!Next || !Next->is(tok::identifier)) + return false; + } else if (Next->is(tok::l_paren)) { + break; + } else { + return false; + } + } + if (!Next) + return false; + assert(Next->is(tok::l_paren)); + if (Next->Next == Next->MatchingParen) + return true; + for (const FormatToken *Tok = Next->Next; Tok != Next->MatchingParen; + Tok = Tok->Next) { + if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() || + Tok->Type == TT_PointerOrReference || Tok->Type == TT_StartOfName) + return true; + if (Tok->isOneOf(tok::l_brace, tok::string_literal) || Tok->Tok.isLiteral()) + return false; + } + return false; +} + void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), E = Line.Children.end(); @@ -1215,6 +1250,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { FormatToken *Current = Line.First->Next; bool InFunctionDecl = Line.MightBeFunctionDecl; while (Current) { + if (isFunctionDeclarationName(*Current)) + Current->Type = TT_FunctionDeclarationName; if (Current->Type == TT_LineComment) { if (Current->Previous->BlockKind == BK_BracedInit && Current->Previous->opensScope()) @@ -1320,7 +1357,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Right.Type != TT_ObjCMethodExpr && Right.Type != TT_LambdaLSquare) return 500; } - if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) { + if (Right.Type == TT_StartOfName || + Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator)) { if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) return 3; if (Left.Type == TT_StartOfName) @@ -1674,7 +1712,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return false; if (Left.Tok.getObjCKeywordID() == tok::objc_interface) return false; - if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) + if (Right.Type == TT_StartOfName || + Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator)) return true; if (Right.isTrailingComment()) // We rely on MustBreakBefore being set correctly here as we should not diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 0df70a0d02c..36de010fc94 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -41,8 +41,8 @@ public: : First(Line.Tokens.front().Tok), Level(Line.Level), InPPDirective(Line.InPPDirective), MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), - StartsDefinition(false), Affected(false), - LeadingEmptyLinesAffected(false), ChildrenAffected(false) { + Affected(false), LeadingEmptyLinesAffected(false), + ChildrenAffected(false) { assert(!Line.Tokens.empty()); // Calculate Next and Previous for all tokens. Note that we must overwrite @@ -86,7 +86,6 @@ public: bool InPPDirective; bool MustBeDeclaration; bool MightBeFunctionDecl; - bool StartsDefinition; /// \c True if this line should be formatted, i.e. intersects directly or /// indirectly with one of the input ranges. |