Changeset View
Changeset View
Standalone View
Standalone View
clang/lib/Format/TokenAnnotator.cpp
Show First 20 Lines • Show All 337 Lines • ▼ Show 20 Lines | while (CurrentToken) { | ||||
if (Left->Previous && Left->Previous->is(TT_JavaAnnotation)) | if (Left->Previous && Left->Previous->is(TT_JavaAnnotation)) | ||||
CurrentToken->setType(TT_JavaAnnotation); | CurrentToken->setType(TT_JavaAnnotation); | ||||
if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation)) | if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation)) | ||||
CurrentToken->setType(TT_LeadingJavaAnnotation); | CurrentToken->setType(TT_LeadingJavaAnnotation); | ||||
if (Left->Previous && Left->Previous->is(TT_AttributeSquare)) | if (Left->Previous && Left->Previous->is(TT_AttributeSquare)) | ||||
CurrentToken->setType(TT_AttributeSquare); | CurrentToken->setType(TT_AttributeSquare); | ||||
if (!HasMultipleLines) | if (!HasMultipleLines) | ||||
Left->PackingKind = PPK_Inconclusive; | Left->setPackingKind(PPK_Inconclusive); | ||||
else if (HasMultipleParametersOnALine) | else if (HasMultipleParametersOnALine) | ||||
Left->PackingKind = PPK_BinPacked; | Left->setPackingKind(PPK_BinPacked); | ||||
else | else | ||||
Left->PackingKind = PPK_OnePerLine; | Left->setPackingKind(PPK_OnePerLine); | ||||
next(); | next(); | ||||
return true; | return true; | ||||
} | } | ||||
if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) | if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) | ||||
return false; | return false; | ||||
if (CurrentToken->is(tok::l_brace)) | if (CurrentToken->is(tok::l_brace)) | ||||
▲ Show 20 Lines • Show All 340 Lines • ▼ Show 20 Lines | if (CurrentToken) { | ||||
Left->ParentBracket = Contexts.back().ContextKind; | Left->ParentBracket = Contexts.back().ContextKind; | ||||
if (Contexts.back().CaretFound) | if (Contexts.back().CaretFound) | ||||
Left->setType(TT_ObjCBlockLBrace); | Left->setType(TT_ObjCBlockLBrace); | ||||
Contexts.back().CaretFound = false; | Contexts.back().CaretFound = false; | ||||
ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); | ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); | ||||
Contexts.back().ColonIsDictLiteral = true; | Contexts.back().ColonIsDictLiteral = true; | ||||
if (Left->BlockKind == BK_BracedInit) | if (Left->is(BK_BracedInit)) | ||||
Contexts.back().IsExpression = true; | Contexts.back().IsExpression = true; | ||||
if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && | if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && | ||||
Left->Previous->is(TT_JsTypeColon)) | Left->Previous->is(TT_JsTypeColon)) | ||||
Contexts.back().IsExpression = false; | Contexts.back().IsExpression = false; | ||||
while (CurrentToken) { | while (CurrentToken) { | ||||
if (CurrentToken->is(tok::r_brace)) { | if (CurrentToken->is(tok::r_brace)) { | ||||
Left->MatchingParen = CurrentToken; | Left->MatchingParen = CurrentToken; | ||||
Show All 30 Lines | bool parseBrace() { | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
void updateParameterCount(FormatToken *Left, FormatToken *Current) { | void updateParameterCount(FormatToken *Left, FormatToken *Current) { | ||||
// For ObjC methods, the number of parameters is calculated differently as | // For ObjC methods, the number of parameters is calculated differently as | ||||
// method declarations have a different structure (the parameters are not | // method declarations have a different structure (the parameters are not | ||||
// inside a bracket scope). | // inside a bracket scope). | ||||
if (Current->is(tok::l_brace) && Current->BlockKind == BK_Block) | if (Current->is(tok::l_brace) && Current->is(BK_Block)) | ||||
++Left->BlockParameterCount; | ++Left->BlockParameterCount; | ||||
if (Current->is(tok::comma)) { | if (Current->is(tok::comma)) { | ||||
++Left->ParameterCount; | ++Left->ParameterCount; | ||||
if (!Left->Role) | if (!Left->Role) | ||||
Left->Role.reset(new CommaSeparatedList(Style)); | Left->Role.reset(new CommaSeparatedList(Style)); | ||||
Left->Role->CommaFound(Current); | Left->Role->CommaFound(Current); | ||||
} else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { | } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { | ||||
Left->ParameterCount = 1; | Left->ParameterCount = 1; | ||||
▲ Show 20 Lines • Show All 1,652 Lines • ▼ Show 20 Lines | Line.First->TotalLength = | ||||
Line.First->IsMultiline ? Style.ColumnLimit | Line.First->IsMultiline ? Style.ColumnLimit | ||||
: Line.FirstStartColumn + Line.First->ColumnWidth; | : Line.FirstStartColumn + Line.First->ColumnWidth; | ||||
FormatToken *Current = Line.First->Next; | FormatToken *Current = Line.First->Next; | ||||
bool InFunctionDecl = Line.MightBeFunctionDecl; | bool InFunctionDecl = Line.MightBeFunctionDecl; | ||||
while (Current) { | while (Current) { | ||||
if (isFunctionDeclarationName(*Current, Line)) | if (isFunctionDeclarationName(*Current, Line)) | ||||
Current->setType(TT_FunctionDeclarationName); | Current->setType(TT_FunctionDeclarationName); | ||||
if (Current->is(TT_LineComment)) { | if (Current->is(TT_LineComment)) { | ||||
if (Current->Previous->BlockKind == BK_BracedInit && | if (Current->Previous->is(BK_BracedInit) && | ||||
Current->Previous->opensScope()) | Current->Previous->opensScope()) | ||||
Current->SpacesRequiredBefore = | Current->SpacesRequiredBefore = | ||||
(Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1; | (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1; | ||||
else | else | ||||
Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; | Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; | ||||
// If we find a trailing comment, iterate backwards to determine whether | // If we find a trailing comment, iterate backwards to determine whether | ||||
// it seems to relate to a specific parameter. If so, break before that | // it seems to relate to a specific parameter. If so, break before that | ||||
▲ Show 20 Lines • Show All 318 Lines • ▼ Show 20 Lines | bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, | ||||
if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && | if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && | ||||
Left.Tok.getObjCKeywordID() == tok::objc_property) | Left.Tok.getObjCKeywordID() == tok::objc_property) | ||||
return true; | return true; | ||||
if (Right.is(tok::hashhash)) | if (Right.is(tok::hashhash)) | ||||
return Left.is(tok::hash); | return Left.is(tok::hash); | ||||
if (Left.isOneOf(tok::hashhash, tok::hash)) | if (Left.isOneOf(tok::hashhash, tok::hash)) | ||||
return Right.is(tok::hash); | return Right.is(tok::hash); | ||||
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || | if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || | ||||
(Left.is(tok::l_brace) && Left.BlockKind != BK_Block && | (Left.is(tok::l_brace) && Left.isNot(BK_Block) && | ||||
Right.is(tok::r_brace) && Right.BlockKind != BK_Block)) | Right.is(tok::r_brace) && Right.isNot(BK_Block))) | ||||
return Style.SpaceInEmptyParentheses; | return Style.SpaceInEmptyParentheses; | ||||
if (Style.SpacesInConditionalStatement) { | if (Style.SpacesInConditionalStatement) { | ||||
if (Left.is(tok::l_paren) && Left.Previous && | if (Left.is(tok::l_paren) && Left.Previous && | ||||
isKeywordWithCondition(*Left.Previous)) | isKeywordWithCondition(*Left.Previous)) | ||||
return true; | return true; | ||||
if (Right.is(tok::r_paren) && Right.MatchingParen && | if (Right.is(tok::r_paren) && Right.MatchingParen && | ||||
Right.MatchingParen->Previous && | Right.MatchingParen->Previous && | ||||
isKeywordWithCondition(*Right.MatchingParen->Previous)) | isKeywordWithCondition(*Right.MatchingParen->Previous)) | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) && | ||||
(!Left.is(TT_PointerOrReference) || | (!Left.is(TT_PointerOrReference) || | ||||
(Style.PointerAlignment != FormatStyle::PAS_Right && | (Style.PointerAlignment != FormatStyle::PAS_Right && | ||||
!Line.IsMultiVariableDeclStmt))) | !Line.IsMultiVariableDeclStmt))) | ||||
return true; | return true; | ||||
if (Left.is(TT_PointerOrReference)) | if (Left.is(TT_PointerOrReference)) | ||||
return Right.Tok.isLiteral() || Right.is(TT_BlockComment) || | return Right.Tok.isLiteral() || Right.is(TT_BlockComment) || | ||||
(Right.isOneOf(Keywords.kw_override, Keywords.kw_final) && | (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) && | ||||
!Right.is(TT_StartOfName)) || | !Right.is(TT_StartOfName)) || | ||||
(Right.is(tok::l_brace) && Right.BlockKind == BK_Block) || | (Right.is(tok::l_brace) && Right.is(BK_Block)) || | ||||
(!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare, | (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare, | ||||
tok::l_paren) && | tok::l_paren) && | ||||
(Style.PointerAlignment != FormatStyle::PAS_Right && | (Style.PointerAlignment != FormatStyle::PAS_Right && | ||||
!Line.IsMultiVariableDeclStmt) && | !Line.IsMultiVariableDeclStmt) && | ||||
Left.Previous && | Left.Previous && | ||||
!Left.Previous->isOneOf(tok::l_paren, tok::coloncolon, | !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon, | ||||
tok::l_square)); | tok::l_square)); | ||||
// Ensure right pointer alignement with ellipsis e.g. int *...P | // Ensure right pointer alignement with ellipsis e.g. int *...P | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | if (Right.is(tok::l_square) && | ||||
TT_DesignatedInitializerLSquare, | TT_DesignatedInitializerLSquare, | ||||
TT_StructuredBindingLSquare, TT_AttributeSquare) && | TT_StructuredBindingLSquare, TT_AttributeSquare) && | ||||
!Left.isOneOf(tok::numeric_constant, TT_DictLiteral) && | !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) && | ||||
!(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets && | !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets && | ||||
Right.is(TT_ArraySubscriptLSquare))) | Right.is(TT_ArraySubscriptLSquare))) | ||||
return false; | return false; | ||||
if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) | if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) | ||||
return !Left.Children.empty(); // No spaces in "{}". | return !Left.Children.empty(); // No spaces in "{}". | ||||
if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) || | if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) || | ||||
(Right.is(tok::r_brace) && Right.MatchingParen && | (Right.is(tok::r_brace) && Right.MatchingParen && | ||||
Right.MatchingParen->BlockKind != BK_Block)) | Right.MatchingParen->isNot(BK_Block))) | ||||
return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true; | return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true; | ||||
if (Left.is(TT_BlockComment)) | if (Left.is(TT_BlockComment)) | ||||
// No whitespace in x(/*foo=*/1), except for JavaScript. | // No whitespace in x(/*foo=*/1), except for JavaScript. | ||||
return Style.Language == FormatStyle::LK_JavaScript || | return Style.Language == FormatStyle::LK_JavaScript || | ||||
!Left.TokenText.endswith("=*/"); | !Left.TokenText.endswith("=*/"); | ||||
// Space between template and attribute. | // Space between template and attribute. | ||||
// e.g. template <typename T> [[nodiscard]] ... | // e.g. template <typename T> [[nodiscard]] ... | ||||
Show All 27 Lines | if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) | ||||
return false; | return false; | ||||
if (Right.is(TT_UnaryOperator)) | if (Right.is(TT_UnaryOperator)) | ||||
return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && | return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && | ||||
(Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr)); | (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr)); | ||||
if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square, | if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square, | ||||
tok::r_paren) || | tok::r_paren) || | ||||
Left.isSimpleTypeSpecifier()) && | Left.isSimpleTypeSpecifier()) && | ||||
Right.is(tok::l_brace) && Right.getNextNonComment() && | Right.is(tok::l_brace) && Right.getNextNonComment() && | ||||
Right.BlockKind != BK_Block) | Right.isNot(BK_Block)) | ||||
return false; | return false; | ||||
if (Left.is(tok::period) || Right.is(tok::period)) | if (Left.is(tok::period) || Right.is(tok::period)) | ||||
return false; | return false; | ||||
if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") | if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") | ||||
return false; | return false; | ||||
if (Left.is(TT_TemplateCloser) && Left.MatchingParen && | if (Left.is(TT_TemplateCloser) && Left.MatchingParen && | ||||
Left.MatchingParen->Previous && | Left.MatchingParen->Previous && | ||||
(Left.MatchingParen->Previous->is(tok::period) || | (Left.MatchingParen->Previous->is(tok::period) || | ||||
Show All 25 Lines | |||||
bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, | bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, | ||||
const FormatToken &Right) { | const FormatToken &Right) { | ||||
const FormatToken &Left = *Right.Previous; | const FormatToken &Left = *Right.Previous; | ||||
if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo()) | if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo()) | ||||
return true; // Never ever merge two identifiers. | return true; // Never ever merge two identifiers. | ||||
if (Style.isCpp()) { | if (Style.isCpp()) { | ||||
if (Left.is(tok::kw_operator)) | if (Left.is(tok::kw_operator)) | ||||
return Right.is(tok::coloncolon); | return Right.is(tok::coloncolon); | ||||
if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit && | if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) && | ||||
!Left.opensScope() && Style.SpaceBeforeCpp11BracedList) | !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) | ||||
return true; | return true; | ||||
} else if (Style.Language == FormatStyle::LK_Proto || | } else if (Style.Language == FormatStyle::LK_Proto || | ||||
Style.Language == FormatStyle::LK_TextProto) { | Style.Language == FormatStyle::LK_TextProto) { | ||||
if (Right.is(tok::period) && | if (Right.is(tok::period) && | ||||
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, | Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, | ||||
Keywords.kw_repeated, Keywords.kw_extend)) | Keywords.kw_repeated, Keywords.kw_extend)) | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 336 Lines • ▼ Show 20 Lines | if (Right.is(TT_TrailingUnaryOperator)) | ||||
return false; | return false; | ||||
if (Left.is(TT_RegexLiteral)) | if (Left.is(TT_RegexLiteral)) | ||||
return false; | return false; | ||||
return spaceRequiredBetween(Line, Left, Right); | return spaceRequiredBetween(Line, Left, Right); | ||||
} | } | ||||
// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. | // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. | ||||
static bool isAllmanBrace(const FormatToken &Tok) { | static bool isAllmanBrace(const FormatToken &Tok) { | ||||
return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block && | return Tok.is(tok::l_brace) && Tok.is(BK_Block) && | ||||
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral); | !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral); | ||||
} | } | ||||
// Returns 'true' if 'Tok' is an function argument. | // Returns 'true' if 'Tok' is an function argument. | ||||
static bool IsFunctionArgument(const FormatToken &Tok) { | static bool IsFunctionArgument(const FormatToken &Tok) { | ||||
return Tok.MatchingParen && Tok.MatchingParen->Next && | return Tok.MatchingParen && Tok.MatchingParen->Next && | ||||
Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren); | Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren); | ||||
} | } | ||||
Show All 19 Lines | static bool isOneChildWithoutMustBreakBefore(const FormatToken &Tok) { | ||||
while (curElt) { | while (curElt) { | ||||
if (curElt->MustBreakBefore) | if (curElt->MustBreakBefore) | ||||
return false; | return false; | ||||
curElt = curElt->Next; | curElt = curElt->Next; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
static bool isAllmanLambdaBrace(const FormatToken &Tok) { | static bool isAllmanLambdaBrace(const FormatToken &Tok) { | ||||
return (Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block && | return (Tok.is(tok::l_brace) && Tok.is(BK_Block) && | ||||
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral)); | !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral)); | ||||
} | } | ||||
static bool isAllmanBraceIncludedBreakableLambda( | static bool isAllmanBraceIncludedBreakableLambda( | ||||
const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption) { | const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption) { | ||||
if (!isAllmanLambdaBrace(Tok)) | if (!isAllmanLambdaBrace(Tok)) | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, | ||||
// comment, the intention is to insert a line break after it in order to make | // comment, the intention is to insert a line break after it in order to make | ||||
// shuffling around entries easier. Import statements, especially in | // shuffling around entries easier. Import statements, especially in | ||||
// JavaScript, can be an exception to this rule. | // JavaScript, can be an exception to this rule. | ||||
if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) { | if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) { | ||||
const FormatToken *BeforeClosingBrace = nullptr; | const FormatToken *BeforeClosingBrace = nullptr; | ||||
if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || | if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || | ||||
(Style.Language == FormatStyle::LK_JavaScript && | (Style.Language == FormatStyle::LK_JavaScript && | ||||
Left.is(tok::l_paren))) && | Left.is(tok::l_paren))) && | ||||
Left.BlockKind != BK_Block && Left.MatchingParen) | Left.isNot(BK_Block) && Left.MatchingParen) | ||||
BeforeClosingBrace = Left.MatchingParen->Previous; | BeforeClosingBrace = Left.MatchingParen->Previous; | ||||
else if (Right.MatchingParen && | else if (Right.MatchingParen && | ||||
(Right.MatchingParen->isOneOf(tok::l_brace, | (Right.MatchingParen->isOneOf(tok::l_brace, | ||||
TT_ArrayInitializerLSquare) || | TT_ArrayInitializerLSquare) || | ||||
(Style.Language == FormatStyle::LK_JavaScript && | (Style.Language == FormatStyle::LK_JavaScript && | ||||
Right.MatchingParen->is(tok::l_paren)))) | Right.MatchingParen->is(tok::l_paren)))) | ||||
BeforeClosingBrace = &Left; | BeforeClosingBrace = &Left; | ||||
if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || | if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || | ||||
BeforeClosingBrace->isTrailingComment())) | BeforeClosingBrace->isTrailingComment())) | ||||
return true; | return true; | ||||
} | } | ||||
if (Right.is(tok::comment)) | if (Right.is(tok::comment)) | ||||
return Left.BlockKind != BK_BracedInit && | return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) && | ||||
Left.isNot(TT_CtorInitializerColon) && | |||||
(Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); | (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); | ||||
if (Left.isTrailingComment()) | if (Left.isTrailingComment()) | ||||
return true; | return true; | ||||
if (Right.Previous->IsUnterminatedLiteral) | if (Right.Previous->IsUnterminatedLiteral) | ||||
return true; | return true; | ||||
if (Right.is(tok::lessless) && Right.Next && | if (Right.is(tok::lessless) && Right.Next && | ||||
Right.Previous->is(tok::string_literal) && | Right.Previous->is(tok::string_literal) && | ||||
Right.Next->is(tok::string_literal)) | Right.Next->is(tok::string_literal)) | ||||
▲ Show 20 Lines • Show All 292 Lines • ▼ Show 20 Lines | bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, | ||||
if (Left.is(TT_PointerOrReference)) | if (Left.is(TT_PointerOrReference)) | ||||
return false; | return false; | ||||
if (Right.isTrailingComment()) | if (Right.isTrailingComment()) | ||||
// We rely on MustBreakBefore being set correctly here as we should not | // We rely on MustBreakBefore being set correctly here as we should not | ||||
// change the "binding" behavior of a comment. | // change the "binding" behavior of a comment. | ||||
// The first comment in a braced lists is always interpreted as belonging to | // The first comment in a braced lists is always interpreted as belonging to | ||||
// the first list element. Otherwise, it should be placed outside of the | // the first list element. Otherwise, it should be placed outside of the | ||||
// list. | // list. | ||||
return Left.BlockKind == BK_BracedInit || | return Left.is(BK_BracedInit) || | ||||
(Left.is(TT_CtorInitializerColon) && | (Left.is(TT_CtorInitializerColon) && | ||||
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); | Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); | ||||
if (Left.is(tok::question) && Right.is(tok::colon)) | if (Left.is(tok::question) && Right.is(tok::colon)) | ||||
return false; | return false; | ||||
if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) | if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) | ||||
return Style.BreakBeforeTernaryOperators; | return Style.BreakBeforeTernaryOperators; | ||||
if (Left.is(TT_ConditionalExpr) || Left.is(tok::question)) | if (Left.is(TT_ConditionalExpr) || Left.is(tok::question)) | ||||
return !Style.BreakBeforeTernaryOperators; | return !Style.BreakBeforeTernaryOperators; | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser)) | ||||
return false; | return false; | ||||
if (Right.is(tok::r_square) && Right.MatchingParen && | if (Right.is(tok::r_square) && Right.MatchingParen && | ||||
Right.MatchingParen->is(TT_LambdaLSquare)) | Right.MatchingParen->is(TT_LambdaLSquare)) | ||||
return false; | return false; | ||||
// We only break before r_brace if there was a corresponding break before | // We only break before r_brace if there was a corresponding break before | ||||
// the l_brace, which is tracked by BreakBeforeClosingBrace. | // the l_brace, which is tracked by BreakBeforeClosingBrace. | ||||
if (Right.is(tok::r_brace)) | if (Right.is(tok::r_brace)) | ||||
return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block; | return Right.MatchingParen && Right.MatchingParen->is(BK_Block); | ||||
// Allow breaking after a trailing annotation, e.g. after a method | // Allow breaking after a trailing annotation, e.g. after a method | ||||
// declaration. | // declaration. | ||||
if (Left.is(TT_TrailingAnnotation)) | if (Left.is(TT_TrailingAnnotation)) | ||||
return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, | return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, | ||||
tok::less, tok::coloncolon); | tok::less, tok::coloncolon); | ||||
if (Right.is(tok::kw___attribute) || | if (Right.is(tok::kw___attribute) || | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { | ||||
llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n"; | llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n"; | ||||
const FormatToken *Tok = Line.First; | const FormatToken *Tok = Line.First; | ||||
while (Tok) { | while (Tok) { | ||||
llvm::errs() << " M=" << Tok->MustBreakBefore | llvm::errs() << " M=" << Tok->MustBreakBefore | ||||
<< " C=" << Tok->CanBreakBefore | << " C=" << Tok->CanBreakBefore | ||||
<< " T=" << getTokenTypeName(Tok->getType()) | << " T=" << getTokenTypeName(Tok->getType()) | ||||
<< " S=" << Tok->SpacesRequiredBefore | << " S=" << Tok->SpacesRequiredBefore | ||||
<< " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount | << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount | ||||
<< " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty | << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty | ||||
<< " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength | << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength | ||||
<< " PPK=" << Tok->PackingKind << " FakeLParens="; | << " PPK=" << Tok->getPackingKind() << " FakeLParens="; | ||||
for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) | for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) | ||||
llvm::errs() << Tok->FakeLParens[i] << "/"; | llvm::errs() << Tok->FakeLParens[i] << "/"; | ||||
llvm::errs() << " FakeRParens=" << Tok->FakeRParens; | llvm::errs() << " FakeRParens=" << Tok->FakeRParens; | ||||
llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo(); | llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo(); | ||||
llvm::errs() << " Text='" << Tok->TokenText << "'\n"; | llvm::errs() << " Text='" << Tok->TokenText << "'\n"; | ||||
if (!Tok->Next) | if (!Tok->Next) | ||||
assert(Tok == Line.Last); | assert(Tok == Line.Last); | ||||
Tok = Tok->Next; | Tok = Tok->Next; | ||||
} | } | ||||
llvm::errs() << "----\n"; | llvm::errs() << "----\n"; | ||||
} | } | ||||
} // namespace format | } // namespace format | ||||
} // namespace clang | } // namespace clang |