2013-01-30 05:01:14 +08:00
|
|
|
//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements a token annotator, i.e. creates
|
|
|
|
/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TokenAnnotator.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2015-05-06 16:38:24 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2013-04-09 04:33:42 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2014-04-22 06:55:36 +08:00
|
|
|
#define DEBUG_TYPE "format-token-annotator"
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-07-01 12:03:19 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
/// \brief A parser that gathers additional information about tokens.
|
|
|
|
///
|
2013-03-13 00:28:18 +08:00
|
|
|
/// The \c TokenAnnotator tries to match parenthesis and square brakets and
|
2013-01-30 05:01:14 +08:00
|
|
|
/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
|
|
|
|
/// into template parameter lists.
|
|
|
|
class AnnotatingParser {
|
|
|
|
public:
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords)
|
2014-11-24 00:46:28 +08:00
|
|
|
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
|
|
|
|
Keywords(Keywords) {
|
2013-06-26 08:30:14 +08:00
|
|
|
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
|
2014-03-18 19:22:45 +08:00
|
|
|
resetTokenMetadata(CurrentToken);
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
private:
|
2013-01-30 05:01:14 +08:00
|
|
|
bool parseAngle() {
|
2016-02-05 22:17:16 +08:00
|
|
|
if (!CurrentToken || !CurrentToken->Previous)
|
|
|
|
return false;
|
|
|
|
if (NonTemplateLess.count(CurrentToken->Previous))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2016-02-05 22:17:16 +08:00
|
|
|
|
|
|
|
const FormatToken& Previous = *CurrentToken->Previous;
|
|
|
|
if (Previous.Previous) {
|
|
|
|
if (Previous.Previous->Tok.isLiteral())
|
|
|
|
return false;
|
|
|
|
if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
|
|
|
|
(!Previous.Previous->MatchingParen ||
|
|
|
|
!Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Left = CurrentToken->Previous;
|
2015-05-04 15:39:00 +08:00
|
|
|
Left->ParentBracket = Contexts.back().ContextKind;
|
2016-02-11 14:43:01 +08:00
|
|
|
ScopedContextCreator ContextCreator(*this, tok::less, 12);
|
2015-05-06 22:53:50 +08:00
|
|
|
|
|
|
|
// If this angle is in the context of an expression, we need to be more
|
2015-05-07 22:19:59 +08:00
|
|
|
// hesitant to detect it as opening template parameters.
|
2015-05-06 22:53:50 +08:00
|
|
|
bool InExprContext = Contexts.back().IsExpression;
|
|
|
|
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().IsExpression = false;
|
2014-03-27 19:17:36 +08:00
|
|
|
// If there's a template keyword before the opening angle bracket, this is a
|
|
|
|
// template parameter, not an argument.
|
|
|
|
Contexts.back().InTemplateArgument =
|
2014-05-09 16:15:10 +08:00
|
|
|
Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
|
2014-03-27 19:17:36 +08:00
|
|
|
|
2014-10-21 17:57:09 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java &&
|
|
|
|
CurrentToken->is(tok::question))
|
|
|
|
next();
|
|
|
|
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-01-30 05:01:14 +08:00
|
|
|
if (CurrentToken->is(tok::greater)) {
|
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
|
|
|
CurrentToken->Type = TT_TemplateCloser;
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-03 05:52:57 +08:00
|
|
|
if (CurrentToken->is(tok::question) &&
|
2014-11-14 16:22:46 +08:00
|
|
|
Style.Language == FormatStyle::LK_Java) {
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-06 22:53:50 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
|
|
|
|
(CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext))
|
2013-05-15 21:46:48 +08:00
|
|
|
return false;
|
2013-06-02 02:56:00 +08:00
|
|
|
// If a && or || is found and interpreted as a binary operator, this set
|
2013-06-04 00:16:41 +08:00
|
|
|
// of angles is likely part of something like "a < b && c > d". If the
|
2013-06-02 02:56:00 +08:00
|
|
|
// angles are inside an expression, the ||/&& might also be a binary
|
|
|
|
// operator that was misinterpreted because we are parsing template
|
|
|
|
// parameters.
|
|
|
|
// FIXME: This is getting out of hand, write a decent parser.
|
2013-05-29 22:47:47 +08:00
|
|
|
if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
CurrentToken->Previous->is(TT_BinaryOperator) &&
|
2014-07-28 21:19:58 +08:00
|
|
|
Contexts[Contexts.size() - 2].IsExpression &&
|
2015-06-17 17:43:56 +08:00
|
|
|
!Line.startsWith(tok::kw_template))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2013-02-14 23:01:34 +08:00
|
|
|
updateParameterCount(Left, CurrentToken);
|
2013-01-30 05:01:14 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseParens(bool LookForDecls = false) {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!CurrentToken)
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2015-05-04 15:39:00 +08:00
|
|
|
FormatToken *Left = CurrentToken->Previous;
|
|
|
|
Left->ParentBracket = Contexts.back().ContextKind;
|
2013-03-14 21:45:21 +08:00
|
|
|
ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
|
2013-02-06 18:05:46 +08:00
|
|
|
|
|
|
|
// FIXME: This is a bit of a hack. Do better.
|
|
|
|
Contexts.back().ColonIsForRangeExpr =
|
|
|
|
Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
bool StartsObjCMethodExpr = false;
|
|
|
|
if (CurrentToken->is(tok::caret)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
// (^ can start a block type.
|
2013-01-30 05:01:14 +08:00
|
|
|
Left->Type = TT_ObjCBlockLParen;
|
2013-05-29 22:47:47 +08:00
|
|
|
} else if (FormatToken *MaybeSel = Left->Previous) {
|
2013-01-30 05:01:14 +08:00
|
|
|
// @selector( starts a selector.
|
2013-05-29 22:47:47 +08:00
|
|
|
if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
|
|
|
|
MaybeSel->Previous->is(tok::at)) {
|
2013-01-30 05:01:14 +08:00
|
|
|
StartsObjCMethodExpr = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:55:33 +08:00
|
|
|
if (Left->is(TT_OverloadedOperatorLParen)) {
|
|
|
|
Contexts.back().IsExpression = false;
|
|
|
|
} else if (Left->Previous &&
|
2015-08-13 21:43:51 +08:00
|
|
|
(Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
|
|
|
|
tok::kw_if, tok::kw_while, tok::l_paren,
|
|
|
|
tok::comma) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Left->Previous->is(TT_BinaryOperator))) {
|
2013-10-21 02:15:30 +08:00
|
|
|
// static_assert, if and while usually contain expressions.
|
2013-08-02 01:58:23 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2016-03-22 01:57:31 +08:00
|
|
|
} else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
|
|
|
|
Left->Previous->is(Keywords.kw_function)) {
|
|
|
|
Contexts.back().IsExpression = false;
|
2013-10-21 02:15:30 +08:00
|
|
|
} else if (Left->Previous && Left->Previous->is(tok::r_square) &&
|
|
|
|
Left->Previous->MatchingParen &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
|
2013-10-21 02:15:30 +08:00
|
|
|
// This is a parameter list of a lambda expression.
|
|
|
|
Contexts.back().IsExpression = false;
|
2015-05-12 18:20:32 +08:00
|
|
|
} else if (Line.InPPDirective &&
|
2016-01-11 20:55:33 +08:00
|
|
|
(!Left->Previous || !Left->Previous->is(tok::identifier))) {
|
2015-05-12 18:20:32 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2014-03-27 17:43:54 +08:00
|
|
|
} else if (Contexts[Contexts.size() - 2].CaretFound) {
|
2013-12-23 15:29:06 +08:00
|
|
|
// This is the parameter list of an ObjC block.
|
|
|
|
Contexts.back().IsExpression = false;
|
2014-01-29 04:13:43 +08:00
|
|
|
} else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
|
|
|
|
Left->Type = TT_AttributeParen;
|
2015-05-04 17:22:29 +08:00
|
|
|
} else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
|
2014-04-01 20:55:11 +08:00
|
|
|
// The first argument to a foreach macro is a declaration.
|
|
|
|
Contexts.back().IsForEachMacro = true;
|
|
|
|
Contexts.back().IsExpression = false;
|
2014-10-21 15:57:50 +08:00
|
|
|
} else if (Left->Previous && Left->Previous->MatchingParen &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
|
2014-10-21 15:57:50 +08:00
|
|
|
Contexts.back().IsExpression = false;
|
2015-12-28 15:44:25 +08:00
|
|
|
} else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
|
|
|
|
bool IsForOrCatch =
|
|
|
|
Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
|
|
|
|
Contexts.back().IsExpression = !IsForOrCatch;
|
2013-10-21 02:15:30 +08:00
|
|
|
}
|
2013-08-02 01:58:23 +08:00
|
|
|
|
2013-02-06 18:05:46 +08:00
|
|
|
if (StartsObjCMethodExpr) {
|
|
|
|
Contexts.back().ColonIsObjCMethodExpr = true;
|
|
|
|
Left->Type = TT_ObjCMethodExpr;
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2015-12-28 15:44:25 +08:00
|
|
|
bool MightBeFunctionType = CurrentToken->isOneOf(tok::star, tok::amp) &&
|
|
|
|
!Contexts[Contexts.size() - 2].IsExpression;
|
2013-07-10 22:02:49 +08:00
|
|
|
bool HasMultipleLines = false;
|
|
|
|
bool HasMultipleParametersOnALine = false;
|
2015-04-15 15:26:18 +08:00
|
|
|
bool MightBeObjCForRangeLoop =
|
|
|
|
Left->Previous && Left->Previous->is(tok::kw_for);
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-01-30 05:01:14 +08:00
|
|
|
// LookForDecls is set when "if (" has been seen. Check for
|
|
|
|
// 'identifier' '*' 'identifier' followed by not '=' -- this
|
|
|
|
// '*' has to be a binary operator but determineStarAmpUsage() will
|
|
|
|
// categorize it as an unary operator, so set the right type here.
|
2013-05-29 22:47:47 +08:00
|
|
|
if (LookForDecls && CurrentToken->Next) {
|
2013-07-04 22:47:51 +08:00
|
|
|
FormatToken *Prev = CurrentToken->getPreviousNonComment();
|
2013-06-08 00:02:52 +08:00
|
|
|
if (Prev) {
|
2013-07-04 22:47:51 +08:00
|
|
|
FormatToken *PrevPrev = Prev->getPreviousNonComment();
|
2013-06-08 00:02:52 +08:00
|
|
|
FormatToken *Next = CurrentToken->Next;
|
|
|
|
if (PrevPrev && PrevPrev->is(tok::identifier) &&
|
|
|
|
Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
|
|
|
|
CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
|
|
|
|
Prev->Type = TT_BinaryOperator;
|
|
|
|
LookForDecls = false;
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (CurrentToken->Previous->is(TT_PointerOrReference) &&
|
2013-08-12 20:16:34 +08:00
|
|
|
CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
|
|
|
|
tok::coloncolon))
|
|
|
|
MightBeFunctionType = true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (CurrentToken->Previous->is(TT_BinaryOperator))
|
2014-06-30 21:44:47 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2013-01-30 05:01:14 +08:00
|
|
|
if (CurrentToken->is(tok::r_paren)) {
|
2013-05-29 22:47:47 +08:00
|
|
|
if (MightBeFunctionType && CurrentToken->Next &&
|
2013-07-16 19:37:21 +08:00
|
|
|
(CurrentToken->Next->is(tok::l_paren) ||
|
|
|
|
(CurrentToken->Next->is(tok::l_square) &&
|
2015-12-28 15:44:25 +08:00
|
|
|
Line.MustBeDeclaration)))
|
2013-05-28 16:33:00 +08:00
|
|
|
Left->Type = TT_FunctionTypeLParen;
|
2013-01-30 05:01:14 +08:00
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
|
|
|
|
2016-01-07 22:36:11 +08:00
|
|
|
if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
|
|
|
|
Left->Previous && Left->Previous->is(tok::l_paren)) {
|
|
|
|
// Detect the case where macros are used to generate lambdas or
|
|
|
|
// function bodies, e.g.:
|
|
|
|
// auto my_lambda = MARCO((Type *type, int i) { .. body .. });
|
|
|
|
for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
|
|
|
|
if (Tok->is(TT_BinaryOperator) &&
|
|
|
|
Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
|
|
|
|
Tok->Type = TT_PointerOrReference;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 18:07:47 +08:00
|
|
|
if (StartsObjCMethodExpr) {
|
2013-02-06 18:05:46 +08:00
|
|
|
CurrentToken->Type = TT_ObjCMethodExpr;
|
2014-05-09 16:15:10 +08:00
|
|
|
if (Contexts.back().FirstObjCSelectorName) {
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
|
|
|
|
Contexts.back().LongestObjCSelectorName;
|
2013-02-05 18:07:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left->is(TT_AttributeParen))
|
2014-01-29 04:13:43 +08:00
|
|
|
CurrentToken->Type = TT_AttributeParen;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
|
2014-10-21 17:25:39 +08:00
|
|
|
CurrentToken->Type = TT_JavaAnnotation;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
|
2014-11-01 02:23:49 +08:00
|
|
|
CurrentToken->Type = TT_LeadingJavaAnnotation;
|
2014-01-29 04:13:43 +08:00
|
|
|
|
2013-07-10 22:02:49 +08:00
|
|
|
if (!HasMultipleLines)
|
|
|
|
Left->PackingKind = PPK_Inconclusive;
|
|
|
|
else if (HasMultipleParametersOnALine)
|
|
|
|
Left->PackingKind = PPK_BinPacked;
|
|
|
|
else
|
|
|
|
Left->PackingKind = PPK_OnePerLine;
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-13 22:41:29 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2015-04-15 15:26:18 +08:00
|
|
|
|
|
|
|
if (CurrentToken->is(tok::l_brace))
|
2014-01-19 15:46:32 +08:00
|
|
|
Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
|
2013-07-10 22:02:49 +08:00
|
|
|
if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
|
|
|
|
!CurrentToken->Next->HasUnescapedNewline &&
|
|
|
|
!CurrentToken->Next->isTrailingComment())
|
|
|
|
HasMultipleParametersOnALine = true;
|
2014-04-03 17:00:49 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
|
2014-03-11 17:29:46 +08:00
|
|
|
CurrentToken->isSimpleTypeSpecifier())
|
|
|
|
Contexts.back().IsExpression = false;
|
2015-04-15 15:26:18 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::semi, tok::colon))
|
|
|
|
MightBeObjCForRangeLoop = false;
|
|
|
|
if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in))
|
|
|
|
CurrentToken->Type = TT_ObjCForIn;
|
2015-05-13 20:54:30 +08:00
|
|
|
// When we discover a 'new', we set CanBeExpression to 'false' in order to
|
|
|
|
// parse the type correctly. Reset that after a comma.
|
|
|
|
if (CurrentToken->is(tok::comma))
|
|
|
|
Contexts.back().CanBeExpression = true;
|
2015-04-15 15:26:18 +08:00
|
|
|
|
2014-06-03 20:02:45 +08:00
|
|
|
FormatToken *Tok = CurrentToken;
|
2013-01-30 05:01:14 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2014-06-03 20:02:45 +08:00
|
|
|
updateParameterCount(Left, Tok);
|
2013-07-10 22:02:49 +08:00
|
|
|
if (CurrentToken && CurrentToken->HasUnescapedNewline)
|
|
|
|
HasMultipleLines = true;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseSquare() {
|
|
|
|
if (!CurrentToken)
|
|
|
|
return false;
|
|
|
|
|
2013-06-17 21:19:53 +08:00
|
|
|
// A '[' could be an index subscript (after an identifier or after
|
2013-02-10 10:08:05 +08:00
|
|
|
// ')' or ']'), it could be the start of an Objective-C method
|
2015-06-19 09:52:53 +08:00
|
|
|
// expression, or it could the start of an Objective-C array literal.
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Left = CurrentToken->Previous;
|
2015-05-04 15:39:00 +08:00
|
|
|
Left->ParentBracket = Contexts.back().ContextKind;
|
2013-07-04 22:47:51 +08:00
|
|
|
FormatToken *Parent = Left->getPreviousNonComment();
|
2013-01-30 05:01:14 +08:00
|
|
|
bool StartsObjCMethodExpr =
|
2015-05-28 15:21:50 +08:00
|
|
|
Style.Language == FormatStyle::LK_Cpp &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
|
2014-05-09 18:26:08 +08:00
|
|
|
CurrentToken->isNot(tok::l_brace) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(!Parent ||
|
|
|
|
Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
|
|
|
|
tok::kw_return, tok::kw_throw) ||
|
|
|
|
Parent->isUnaryOperator() ||
|
|
|
|
Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
|
2013-05-29 22:47:47 +08:00
|
|
|
getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
|
2013-10-27 01:00:22 +08:00
|
|
|
bool ColonFound = false;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2015-05-28 15:21:50 +08:00
|
|
|
unsigned BindingIncrease = 1;
|
|
|
|
if (Left->is(TT_Unknown)) {
|
|
|
|
if (StartsObjCMethodExpr) {
|
|
|
|
Left->Type = TT_ObjCMethodExpr;
|
2015-05-29 14:19:49 +08:00
|
|
|
} else if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
|
2015-06-02 22:20:08 +08:00
|
|
|
Contexts.back().ContextKind == tok::l_brace &&
|
2015-05-29 14:19:49 +08:00
|
|
|
Parent->isOneOf(tok::l_brace, tok::comma)) {
|
|
|
|
Left->Type = TT_JsComputedPropertyName;
|
2016-01-04 15:27:33 +08:00
|
|
|
} else if (Style.Language == FormatStyle::LK_Proto ||
|
|
|
|
(Parent &&
|
2016-01-04 21:11:41 +08:00
|
|
|
Parent->isOneOf(TT_BinaryOperator, tok::at, tok::comma,
|
|
|
|
tok::l_paren, tok::l_square, tok::question,
|
2016-01-04 23:51:56 +08:00
|
|
|
tok::colon, tok::kw_return,
|
|
|
|
// Should only be relevant to JavaScript:
|
|
|
|
tok::kw_default))) {
|
2015-05-28 15:21:50 +08:00
|
|
|
Left->Type = TT_ArrayInitializerLSquare;
|
|
|
|
} else {
|
|
|
|
BindingIncrease = 10;
|
|
|
|
Left->Type = TT_ArraySubscriptLSquare;
|
|
|
|
}
|
2013-02-06 18:05:46 +08:00
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2015-05-28 15:21:50 +08:00
|
|
|
ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
|
|
|
|
Contexts.back().IsExpression = true;
|
|
|
|
Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
|
|
|
|
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-01-30 05:01:14 +08:00
|
|
|
if (CurrentToken->is(tok::r_square)) {
|
2013-09-05 18:04:31 +08:00
|
|
|
if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Left->is(TT_ObjCMethodExpr)) {
|
2013-02-06 14:20:11 +08:00
|
|
|
// An ObjC method call is rarely followed by an open parenthesis.
|
2013-01-30 05:01:14 +08:00
|
|
|
// FIXME: Do we incorrectly label ":" with this?
|
|
|
|
StartsObjCMethodExpr = false;
|
|
|
|
Left->Type = TT_Unknown;
|
|
|
|
}
|
2014-04-08 21:07:41 +08:00
|
|
|
if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
|
2013-02-06 18:05:46 +08:00
|
|
|
CurrentToken->Type = TT_ObjCMethodExpr;
|
2013-02-06 14:20:11 +08:00
|
|
|
// determineStarAmpUsage() thinks that '*' '[' is allocating an
|
|
|
|
// array of pointers, but if '[' starts a selector then '*' is a
|
|
|
|
// binary operator.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Parent && Parent->is(TT_PointerOrReference))
|
2013-02-07 00:54:35 +08:00
|
|
|
Parent->Type = TT_BinaryOperator;
|
2013-02-04 15:21:18 +08:00
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
2014-05-09 16:15:10 +08:00
|
|
|
if (Contexts.back().FirstObjCSelectorName) {
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
|
|
|
|
Contexts.back().LongestObjCSelectorName;
|
2014-06-03 20:02:45 +08:00
|
|
|
if (Left->BlockParameterCount > 1)
|
2013-12-23 15:29:06 +08:00
|
|
|
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-13 22:41:29 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2014-10-16 16:38:51 +08:00
|
|
|
if (CurrentToken->is(tok::colon)) {
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left->is(TT_ArraySubscriptLSquare)) {
|
2014-10-16 16:38:51 +08:00
|
|
|
Left->Type = TT_ObjCMethodExpr;
|
|
|
|
StartsObjCMethodExpr = true;
|
|
|
|
Contexts.back().ColonIsObjCMethodExpr = true;
|
|
|
|
if (Parent && Parent->is(tok::r_paren))
|
|
|
|
Parent->Type = TT_CastRParen;
|
|
|
|
}
|
2013-10-27 01:00:22 +08:00
|
|
|
ColonFound = true;
|
2014-10-16 16:38:51 +08:00
|
|
|
}
|
2015-05-28 15:21:50 +08:00
|
|
|
if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
|
|
|
|
!ColonFound)
|
2013-10-22 23:30:28 +08:00
|
|
|
Left->Type = TT_ArrayInitializerLSquare;
|
2015-01-10 07:25:06 +08:00
|
|
|
FormatToken *Tok = CurrentToken;
|
2013-01-30 05:01:14 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2014-06-03 20:02:45 +08:00
|
|
|
updateParameterCount(Left, Tok);
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseBrace() {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (CurrentToken) {
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Left = CurrentToken->Previous;
|
2015-05-04 15:39:00 +08:00
|
|
|
Left->ParentBracket = Contexts.back().ContextKind;
|
2014-03-27 17:43:54 +08:00
|
|
|
|
|
|
|
if (Contexts.back().CaretFound)
|
|
|
|
Left->Type = TT_ObjCBlockLBrace;
|
|
|
|
Contexts.back().CaretFound = false;
|
|
|
|
|
2013-10-24 18:31:50 +08:00
|
|
|
ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
|
|
|
|
Contexts.back().ColonIsDictLiteral = true;
|
2014-05-22 17:00:33 +08:00
|
|
|
if (Left->BlockKind == BK_BracedInit)
|
|
|
|
Contexts.back().IsExpression = true;
|
2013-05-26 13:39:26 +08:00
|
|
|
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-05-06 16:27:33 +08:00
|
|
|
if (CurrentToken->is(tok::r_brace)) {
|
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
|
|
|
|
return false;
|
|
|
|
updateParameterCount(Left, CurrentToken);
|
2014-08-15 13:00:35 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
|
|
|
|
FormatToken *Previous = CurrentToken->getPreviousNonComment();
|
2015-10-12 11:19:07 +08:00
|
|
|
if (((CurrentToken->is(tok::colon) &&
|
|
|
|
(!Contexts.back().ColonIsDictLiteral ||
|
|
|
|
Style.Language != FormatStyle::LK_Cpp)) ||
|
2014-08-27 19:53:26 +08:00
|
|
|
Style.Language == FormatStyle::LK_Proto) &&
|
2016-03-01 12:19:47 +08:00
|
|
|
(Previous->Tok.getIdentifierInfo() ||
|
2016-03-03 06:44:03 +08:00
|
|
|
Previous->is(tok::string_literal)))
|
2014-08-15 13:00:35 +08:00
|
|
|
Previous->Type = TT_SelectorName;
|
2015-04-04 15:56:55 +08:00
|
|
|
if (CurrentToken->is(tok::colon) ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript)
|
2014-08-15 13:00:35 +08:00
|
|
|
Left->Type = TT_DictLiteral;
|
2014-06-10 22:44:02 +08:00
|
|
|
}
|
2013-05-06 16:27:33 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-20 20:56:39 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
void updateParameterCount(FormatToken *Left, FormatToken *Current) {
|
2016-02-08 06:17:13 +08:00
|
|
|
if (Current->is(tok::l_brace) && Current->BlockKind == BK_Block)
|
2014-06-03 20:02:45 +08:00
|
|
|
++Left->BlockParameterCount;
|
2013-07-10 22:02:49 +08:00
|
|
|
if (Current->is(tok::comma)) {
|
2013-02-14 23:01:34 +08:00
|
|
|
++Left->ParameterCount;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
if (!Left->Role)
|
|
|
|
Left->Role.reset(new CommaSeparatedList(Style));
|
|
|
|
Left->Role->CommaFound(Current);
|
2013-07-10 22:02:49 +08:00
|
|
|
} else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
|
2013-02-14 23:01:34 +08:00
|
|
|
Left->ParameterCount = 1;
|
2013-07-10 22:02:49 +08:00
|
|
|
}
|
2013-02-14 23:01:34 +08:00
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
|
|
|
|
bool parseConditional() {
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-01-30 05:01:14 +08:00
|
|
|
if (CurrentToken->is(tok::colon)) {
|
|
|
|
CurrentToken->Type = TT_ConditionalExpr;
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseTemplateDeclaration() {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (CurrentToken && CurrentToken->is(tok::less)) {
|
2013-01-30 05:01:14 +08:00
|
|
|
CurrentToken->Type = TT_TemplateOpener;
|
|
|
|
next();
|
|
|
|
if (!parseAngle())
|
|
|
|
return false;
|
2014-05-09 16:15:10 +08:00
|
|
|
if (CurrentToken)
|
2013-05-29 22:47:47 +08:00
|
|
|
CurrentToken->Previous->ClosesTemplateDeclaration = true;
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool consumeToken() {
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Tok = CurrentToken;
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
2013-05-29 22:47:47 +08:00
|
|
|
switch (Tok->Tok.getKind()) {
|
2013-01-30 05:01:14 +08:00
|
|
|
case tok::plus:
|
|
|
|
case tok::minus:
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!Tok->Previous && Line.MustBeDeclaration)
|
2013-01-30 05:01:14 +08:00
|
|
|
Tok->Type = TT_ObjCMethodSpecifier;
|
|
|
|
break;
|
|
|
|
case tok::colon:
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!Tok->Previous)
|
2013-03-18 20:50:26 +08:00
|
|
|
return false;
|
2013-01-30 05:01:14 +08:00
|
|
|
// Colons from ?: are handled in parseConditional().
|
2015-04-13 23:01:40 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
2015-05-05 16:40:32 +08:00
|
|
|
if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
|
|
|
|
(Contexts.size() == 1 && // switch/case labels
|
2015-04-13 23:01:40 +08:00
|
|
|
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
|
2015-05-05 16:40:32 +08:00
|
|
|
Contexts.back().ContextKind == tok::l_paren || // function params
|
|
|
|
Contexts.back().ContextKind == tok::l_square || // array type
|
2015-06-03 16:43:18 +08:00
|
|
|
(Contexts.size() == 1 &&
|
|
|
|
Line.MustBeDeclaration)) { // method/property declaration
|
2015-04-13 23:01:40 +08:00
|
|
|
Tok->Type = TT_JsTypeColon;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 15:28:12 +08:00
|
|
|
if (Contexts.back().ColonIsDictLiteral ||
|
|
|
|
Style.Language == FormatStyle::LK_Proto) {
|
2013-10-24 18:31:50 +08:00
|
|
|
Tok->Type = TT_DictLiteral;
|
2013-02-06 18:05:46 +08:00
|
|
|
} else if (Contexts.back().ColonIsObjCMethodExpr ||
|
2015-06-17 17:43:56 +08:00
|
|
|
Line.startsWith(TT_ObjCMethodSpecifier)) {
|
2013-01-30 05:01:14 +08:00
|
|
|
Tok->Type = TT_ObjCMethodExpr;
|
2014-06-10 22:44:02 +08:00
|
|
|
Tok->Previous->Type = TT_SelectorName;
|
2013-09-10 17:38:25 +08:00
|
|
|
if (Tok->Previous->ColumnWidth >
|
2016-01-04 15:29:07 +08:00
|
|
|
Contexts.back().LongestObjCSelectorName)
|
2013-10-10 21:36:20 +08:00
|
|
|
Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!Contexts.back().FirstObjCSelectorName)
|
2013-05-29 22:47:47 +08:00
|
|
|
Contexts.back().FirstObjCSelectorName = Tok->Previous;
|
2013-02-06 18:05:46 +08:00
|
|
|
} else if (Contexts.back().ColonIsForRangeExpr) {
|
2015-04-13 23:01:40 +08:00
|
|
|
Tok->Type = TT_RangeBasedForLoopColon;
|
2014-05-09 21:11:16 +08:00
|
|
|
} else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
|
2013-10-10 21:36:20 +08:00
|
|
|
Tok->Type = TT_BitFieldColon;
|
2014-05-28 18:09:11 +08:00
|
|
|
} else if (Contexts.size() == 1 &&
|
|
|
|
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
|
2016-03-02 05:41:58 +08:00
|
|
|
if (Tok->Previous->isOneOf(tok::r_paren, tok::kw_noexcept))
|
2015-02-19 01:09:53 +08:00
|
|
|
Tok->Type = TT_CtorInitializerColon;
|
|
|
|
else
|
|
|
|
Tok->Type = TT_InheritanceColon;
|
2014-10-20 20:01:45 +08:00
|
|
|
} else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
|
|
|
|
Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
|
|
|
|
// This handles a special macro in ObjC code where selectors including
|
|
|
|
// the colon are passed as macro arguments.
|
|
|
|
Tok->Type = TT_ObjCMethodExpr;
|
2013-03-14 21:45:21 +08:00
|
|
|
} else if (Contexts.back().ContextKind == tok::l_paren) {
|
2015-04-13 23:01:40 +08:00
|
|
|
Tok->Type = TT_InlineASMColon;
|
2013-02-05 18:07:47 +08:00
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
break;
|
2016-03-22 01:57:31 +08:00
|
|
|
case tok::pipe:
|
|
|
|
case tok::amp:
|
|
|
|
// | and & in declarations/type expressions represent union and
|
|
|
|
// intersection types, respectively.
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
!Contexts.back().IsExpression)
|
|
|
|
Tok->Type = TT_JsTypeOperator;
|
|
|
|
break;
|
2013-01-30 05:01:14 +08:00
|
|
|
case tok::kw_if:
|
|
|
|
case tok::kw_while:
|
2014-05-09 16:15:10 +08:00
|
|
|
if (CurrentToken && CurrentToken->is(tok::l_paren)) {
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
2013-06-26 08:30:14 +08:00
|
|
|
if (!parseParens(/*LookForDecls=*/true))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case tok::kw_for:
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().ColonIsForRangeExpr = true;
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
|
|
|
if (!parseParens())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case tok::l_paren:
|
2015-07-16 03:11:58 +08:00
|
|
|
// When faced with 'operator()()', the kw_operator handler incorrectly
|
|
|
|
// marks the first l_paren as a OverloadedOperatorLParen. Here, we make
|
|
|
|
// the first two parens OverloadedOperators and the second l_paren an
|
|
|
|
// OverloadedOperatorLParen.
|
|
|
|
if (Tok->Previous &&
|
|
|
|
Tok->Previous->is(tok::r_paren) &&
|
|
|
|
Tok->Previous->MatchingParen &&
|
|
|
|
Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
|
|
|
|
Tok->Previous->Type = TT_OverloadedOperator;
|
|
|
|
Tok->Previous->MatchingParen->Type = TT_OverloadedOperator;
|
|
|
|
Tok->Type = TT_OverloadedOperatorLParen;
|
|
|
|
}
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
if (!parseParens())
|
|
|
|
return false;
|
2013-11-08 01:43:07 +08:00
|
|
|
if (Line.MustBeDeclaration && Contexts.size() == 1 &&
|
2015-06-17 17:43:56 +08:00
|
|
|
!Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
|
2015-01-14 17:47:57 +08:00
|
|
|
(!Tok->Previous ||
|
2015-07-06 19:30:34 +08:00
|
|
|
!Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
|
|
|
|
TT_LeadingJavaAnnotation)))
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
Line.MightBeFunctionDecl = true;
|
2013-01-30 05:01:14 +08:00
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
if (!parseSquare())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
if (!parseBrace())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case tok::less:
|
2016-02-05 22:17:16 +08:00
|
|
|
if (parseAngle()) {
|
2013-01-30 05:01:14 +08:00
|
|
|
Tok->Type = TT_TemplateOpener;
|
2015-01-08 16:48:21 +08:00
|
|
|
} else {
|
2013-01-30 05:01:14 +08:00
|
|
|
Tok->Type = TT_BinaryOperator;
|
2015-05-06 16:38:24 +08:00
|
|
|
NonTemplateLess.insert(Tok);
|
2013-01-30 05:01:14 +08:00
|
|
|
CurrentToken = Tok;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case tok::r_paren:
|
|
|
|
case tok::r_square:
|
|
|
|
return false;
|
|
|
|
case tok::r_brace:
|
|
|
|
// Lines can start with '}'.
|
2014-05-09 16:15:10 +08:00
|
|
|
if (Tok->Previous)
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case tok::greater:
|
|
|
|
Tok->Type = TT_BinaryOperator;
|
|
|
|
break;
|
|
|
|
case tok::kw_operator:
|
2013-09-02 17:20:39 +08:00
|
|
|
while (CurrentToken &&
|
|
|
|
!CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
|
2013-03-13 22:41:29 +08:00
|
|
|
if (CurrentToken->isOneOf(tok::star, tok::amp))
|
2013-02-11 16:01:18 +08:00
|
|
|
CurrentToken->Type = TT_PointerOrReference;
|
|
|
|
consumeToken();
|
2016-01-09 23:56:40 +08:00
|
|
|
if (CurrentToken &&
|
|
|
|
CurrentToken->Previous->isOneOf(TT_BinaryOperator, tok::comma))
|
2013-08-28 15:27:35 +08:00
|
|
|
CurrentToken->Previous->Type = TT_OverloadedOperator;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
2013-05-10 15:59:58 +08:00
|
|
|
if (CurrentToken) {
|
2013-02-11 16:01:18 +08:00
|
|
|
CurrentToken->Type = TT_OverloadedOperatorLParen;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (CurrentToken->Previous->is(TT_BinaryOperator))
|
2013-05-29 22:47:47 +08:00
|
|
|
CurrentToken->Previous->Type = TT_OverloadedOperator;
|
2013-05-10 15:59:58 +08:00
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
break;
|
|
|
|
case tok::question:
|
2015-04-13 23:01:40 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next &&
|
2015-06-11 21:29:20 +08:00
|
|
|
Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
|
2015-04-13 23:01:40 +08:00
|
|
|
tok::r_brace)) {
|
2015-05-05 16:40:32 +08:00
|
|
|
// Question marks before semicolons, colons, etc. indicate optional
|
|
|
|
// types (fields, parameters), e.g.
|
|
|
|
// function(x?: string, y?) {...}
|
|
|
|
// class X { y?; }
|
2015-04-13 23:01:40 +08:00
|
|
|
Tok->Type = TT_JsTypeOptionalQuestion;
|
|
|
|
break;
|
|
|
|
}
|
2015-05-05 16:40:32 +08:00
|
|
|
// Declarations cannot be conditional expressions, this can only be part
|
|
|
|
// of a type declaration.
|
|
|
|
if (Line.MustBeDeclaration &&
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript)
|
|
|
|
break;
|
2013-01-30 05:01:14 +08:00
|
|
|
parseConditional();
|
|
|
|
break;
|
|
|
|
case tok::kw_template:
|
|
|
|
parseTemplateDeclaration();
|
|
|
|
break;
|
2013-04-03 21:36:17 +08:00
|
|
|
case tok::comma:
|
2015-04-20 20:54:29 +08:00
|
|
|
if (Contexts.back().InCtorInitializer)
|
|
|
|
Tok->Type = TT_CtorInitializerComma;
|
2015-05-19 19:51:39 +08:00
|
|
|
else if (Contexts.back().FirstStartOfName &&
|
2015-06-17 17:43:56 +08:00
|
|
|
(Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
|
2013-04-03 21:36:17 +08:00
|
|
|
Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
|
2015-03-02 02:55:26 +08:00
|
|
|
Line.IsMultiVariableDeclStmt = true;
|
|
|
|
}
|
2014-04-01 20:55:11 +08:00
|
|
|
if (Contexts.back().IsForEachMacro)
|
|
|
|
Contexts.back().IsExpression = true;
|
2013-04-03 21:36:17 +08:00
|
|
|
break;
|
2013-01-30 05:01:14 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseIncludeDirective() {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (CurrentToken && CurrentToken->is(tok::less)) {
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-05-29 22:47:47 +08:00
|
|
|
if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
|
2013-01-30 05:01:14 +08:00
|
|
|
CurrentToken->Type = TT_ImplicitStringLiteral;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseWarningOrError() {
|
|
|
|
next();
|
|
|
|
// We still want to format the whitespace left of the first token of the
|
|
|
|
// warning or error.
|
|
|
|
next();
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-01-30 05:01:14 +08:00
|
|
|
CurrentToken->Type = TT_ImplicitStringLiteral;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 21:56:49 +08:00
|
|
|
void parsePragma() {
|
|
|
|
next(); // Consume "pragma".
|
2015-04-22 17:45:42 +08:00
|
|
|
if (CurrentToken &&
|
|
|
|
CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
|
|
|
|
bool IsMark = CurrentToken->is(Keywords.kw_mark);
|
2014-01-09 21:56:49 +08:00
|
|
|
next(); // Consume "mark".
|
|
|
|
next(); // Consume first token (so we fix leading whitespace).
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2015-04-22 17:45:42 +08:00
|
|
|
if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
|
|
|
|
CurrentToken->Type = TT_ImplicitStringLiteral;
|
2014-01-09 21:56:49 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 00:46:28 +08:00
|
|
|
LineType parsePreprocessorDirective() {
|
|
|
|
LineType Type = LT_PreprocessorDirective;
|
2013-01-30 05:01:14 +08:00
|
|
|
next();
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!CurrentToken)
|
2014-11-24 00:46:28 +08:00
|
|
|
return Type;
|
2013-10-12 05:43:05 +08:00
|
|
|
if (CurrentToken->Tok.is(tok::numeric_constant)) {
|
|
|
|
CurrentToken->SpacesRequiredBefore = 1;
|
2014-11-24 00:46:28 +08:00
|
|
|
return Type;
|
2013-10-12 05:43:05 +08:00
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
// Hashes in the middle of a line can lead to any strange token
|
|
|
|
// sequence.
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!CurrentToken->Tok.getIdentifierInfo())
|
2014-11-24 00:46:28 +08:00
|
|
|
return Type;
|
2013-05-29 22:47:47 +08:00
|
|
|
switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
|
2013-01-30 05:01:14 +08:00
|
|
|
case tok::pp_include:
|
2015-05-19 19:22:29 +08:00
|
|
|
case tok::pp_include_next:
|
2013-01-30 05:01:14 +08:00
|
|
|
case tok::pp_import:
|
2014-08-13 16:29:18 +08:00
|
|
|
next();
|
2013-01-30 05:01:14 +08:00
|
|
|
parseIncludeDirective();
|
2014-11-24 00:46:28 +08:00
|
|
|
Type = LT_ImportStatement;
|
2013-01-30 05:01:14 +08:00
|
|
|
break;
|
|
|
|
case tok::pp_error:
|
|
|
|
case tok::pp_warning:
|
|
|
|
parseWarningOrError();
|
|
|
|
break;
|
2014-01-09 21:56:49 +08:00
|
|
|
case tok::pp_pragma:
|
|
|
|
parsePragma();
|
|
|
|
break;
|
2013-04-23 21:54:04 +08:00
|
|
|
case tok::pp_if:
|
|
|
|
case tok::pp_elif:
|
2014-01-21 16:56:09 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2013-04-23 21:54:04 +08:00
|
|
|
parseLine();
|
|
|
|
break;
|
2013-01-30 05:01:14 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken)
|
2013-02-05 17:34:14 +08:00
|
|
|
next();
|
2014-11-24 00:46:28 +08:00
|
|
|
return Type;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
public:
|
2013-01-30 05:01:14 +08:00
|
|
|
LineType parseLine() {
|
2015-05-06 16:38:24 +08:00
|
|
|
NonTemplateLess.clear();
|
2015-04-23 17:54:10 +08:00
|
|
|
if (CurrentToken->is(tok::hash))
|
2014-11-24 00:46:28 +08:00
|
|
|
return parsePreprocessorDirective();
|
2014-01-21 16:56:09 +08:00
|
|
|
|
2014-01-18 00:21:39 +08:00
|
|
|
// Directly allow to 'import <string-literal>' to support protocol buffer
|
|
|
|
// definitions (code.google.com/p/protobuf) or missing "#" (either way we
|
|
|
|
// should not break the line).
|
|
|
|
IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
|
2014-11-27 02:03:42 +08:00
|
|
|
if ((Style.Language == FormatStyle::LK_Java &&
|
|
|
|
CurrentToken->is(Keywords.kw_package)) ||
|
|
|
|
(Info && Info->getPPKeywordID() == tok::pp_import &&
|
2014-12-04 16:57:27 +08:00
|
|
|
CurrentToken->Next &&
|
2015-01-14 18:02:49 +08:00
|
|
|
CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
|
|
|
|
tok::kw_static))) {
|
2014-08-13 16:29:18 +08:00
|
|
|
next();
|
2014-01-18 00:21:39 +08:00
|
|
|
parseIncludeDirective();
|
2014-11-24 00:46:28 +08:00
|
|
|
return LT_ImportStatement;
|
2014-08-13 16:29:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this line starts and ends in '<' and '>', respectively, it is likely
|
|
|
|
// part of "#define <a/b.h>".
|
|
|
|
if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
|
|
|
|
parseIncludeDirective();
|
2014-11-24 00:46:28 +08:00
|
|
|
return LT_ImportStatement;
|
2014-08-13 16:29:18 +08:00
|
|
|
}
|
2014-01-18 00:21:39 +08:00
|
|
|
|
2015-04-23 17:54:10 +08:00
|
|
|
// In .proto files, top-level options are very similar to import statements
|
|
|
|
// and should not be line-wrapped.
|
|
|
|
if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
|
|
|
|
CurrentToken->is(Keywords.kw_option)) {
|
|
|
|
next();
|
|
|
|
if (CurrentToken && CurrentToken->is(tok::identifier))
|
|
|
|
return LT_ImportStatement;
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:32:20 +08:00
|
|
|
// import {...} from '...';
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
CurrentToken->is(Keywords.kw_import))
|
|
|
|
return LT_ImportStatement;
|
|
|
|
|
2014-11-24 00:46:28 +08:00
|
|
|
bool KeywordVirtualFound = false;
|
|
|
|
bool ImportStatement = false;
|
2014-05-09 16:15:10 +08:00
|
|
|
while (CurrentToken) {
|
2013-01-30 05:01:14 +08:00
|
|
|
if (CurrentToken->is(tok::kw_virtual))
|
|
|
|
KeywordVirtualFound = true;
|
2016-03-22 22:32:20 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
// export {...} from '...';
|
|
|
|
// An export followed by "from 'some string';" is a re-export from
|
|
|
|
// another module identified by a URI and is treated as a
|
|
|
|
// LT_ImportStatement (i.e. prevent wraps on it for long URIs).
|
|
|
|
// Just "export {...};" or "export class ..." should not be treated as
|
|
|
|
// an import in this sense.
|
|
|
|
if (Line.First->is(tok::kw_export) &&
|
|
|
|
CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
|
|
|
|
CurrentToken->Next->isStringLiteral())
|
|
|
|
ImportStatement = true;
|
|
|
|
if (isClosureImportStatement(*CurrentToken))
|
|
|
|
ImportStatement = true;
|
|
|
|
}
|
2013-01-30 05:01:14 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return LT_Invalid;
|
|
|
|
}
|
|
|
|
if (KeywordVirtualFound)
|
|
|
|
return LT_VirtualFunctionDecl;
|
2014-11-24 00:46:28 +08:00
|
|
|
if (ImportStatement)
|
|
|
|
return LT_ImportStatement;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2015-06-17 17:43:56 +08:00
|
|
|
if (Line.startsWith(TT_ObjCMethodSpecifier)) {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (Contexts.back().FirstObjCSelectorName)
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
|
|
|
|
Contexts.back().LongestObjCSelectorName;
|
2013-02-05 18:07:47 +08:00
|
|
|
return LT_ObjCMethodDecl;
|
|
|
|
}
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
return LT_Other;
|
|
|
|
}
|
|
|
|
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
private:
|
2016-03-22 22:32:20 +08:00
|
|
|
bool isClosureImportStatement(const FormatToken &Tok) {
|
2014-11-24 00:46:28 +08:00
|
|
|
// FIXME: Closure-library specific stuff should not be hard-coded but be
|
|
|
|
// configurable.
|
2016-03-22 22:32:20 +08:00
|
|
|
return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
|
2014-11-24 00:46:28 +08:00
|
|
|
Tok.Next->Next && (Tok.Next->Next->TokenText == "module" ||
|
2015-10-30 03:05:20 +08:00
|
|
|
Tok.Next->Next->TokenText == "provide" ||
|
2014-11-24 00:46:28 +08:00
|
|
|
Tok.Next->Next->TokenText == "require" ||
|
2016-02-22 23:06:53 +08:00
|
|
|
Tok.Next->Next->TokenText == "setTestOnly" ||
|
|
|
|
Tok.Next->Next->TokenText == "forwardDeclare") &&
|
2014-11-27 22:46:03 +08:00
|
|
|
Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
|
2014-11-24 00:46:28 +08:00
|
|
|
}
|
|
|
|
|
2014-03-18 19:22:45 +08:00
|
|
|
void resetTokenMetadata(FormatToken *Token) {
|
2014-05-09 21:11:16 +08:00
|
|
|
if (!Token)
|
|
|
|
return;
|
2014-03-18 19:22:45 +08:00
|
|
|
|
|
|
|
// Reset token type in case we have already looked at it and then
|
|
|
|
// recovered from an error (e.g. failure to find the matching >).
|
2015-05-04 17:22:29 +08:00
|
|
|
if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
|
|
|
|
TT_FunctionLBrace, TT_ImplicitStringLiteral,
|
2015-06-05 21:18:09 +08:00
|
|
|
TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
|
|
|
|
TT_RegexLiteral))
|
2014-03-18 19:22:45 +08:00
|
|
|
CurrentToken->Type = TT_Unknown;
|
2014-07-19 09:06:45 +08:00
|
|
|
CurrentToken->Role.reset();
|
2015-01-22 02:04:02 +08:00
|
|
|
CurrentToken->MatchingParen = nullptr;
|
2014-03-18 19:22:45 +08:00
|
|
|
CurrentToken->FakeLParens.clear();
|
|
|
|
CurrentToken->FakeRParens = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
void next() {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (CurrentToken) {
|
clang-format: Be more conservative about braced list column layout.
Specifically disable it for nested braced lists as it commonly can look
really weird. Eventually, we'll want to become smarter and format some of
the nested lists better.
Before:
SomeStruct my_struct_array = {
{ aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa,
aaaaaaaaaa, aaaaaaaaaa, aaaaaaa, aaa },
{ aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa },
{ aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, a, aaaaaaaaaa,
aaaaaaaaa, aaa },
};
After:
SomeStruct my_struct_array = {
{ aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaa, aaa },
{ aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa },
{ aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa },
};
llvm-svn: 196783
2013-12-09 22:40:19 +08:00
|
|
|
CurrentToken->NestingLevel = Contexts.size() - 1;
|
2014-08-14 18:52:56 +08:00
|
|
|
CurrentToken->BindingStrength = Contexts.back().BindingStrength;
|
2014-11-25 18:05:17 +08:00
|
|
|
modifyContext(*CurrentToken);
|
2014-08-14 18:52:56 +08:00
|
|
|
determineTokenType(*CurrentToken);
|
2013-05-29 22:47:47 +08:00
|
|
|
CurrentToken = CurrentToken->Next;
|
2014-05-08 15:01:45 +08:00
|
|
|
}
|
2013-02-18 20:44:35 +08:00
|
|
|
|
2014-03-18 19:22:45 +08:00
|
|
|
resetTokenMetadata(CurrentToken);
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-06 18:05:46 +08:00
|
|
|
/// \brief A struct to hold information valid in a specific context, e.g.
|
|
|
|
/// a pair of parenthesis.
|
|
|
|
struct Context {
|
2013-03-14 21:45:21 +08:00
|
|
|
Context(tok::TokenKind ContextKind, unsigned BindingStrength,
|
|
|
|
bool IsExpression)
|
|
|
|
: ContextKind(ContextKind), BindingStrength(BindingStrength),
|
2015-04-15 15:26:18 +08:00
|
|
|
IsExpression(IsExpression) {}
|
2013-03-14 21:45:21 +08:00
|
|
|
|
|
|
|
tok::TokenKind ContextKind;
|
2013-02-06 18:05:46 +08:00
|
|
|
unsigned BindingStrength;
|
|
|
|
bool IsExpression;
|
2015-04-15 15:26:18 +08:00
|
|
|
unsigned LongestObjCSelectorName = 0;
|
|
|
|
bool ColonIsForRangeExpr = false;
|
|
|
|
bool ColonIsDictLiteral = false;
|
|
|
|
bool ColonIsObjCMethodExpr = false;
|
|
|
|
FormatToken *FirstObjCSelectorName = nullptr;
|
|
|
|
FormatToken *FirstStartOfName = nullptr;
|
|
|
|
bool CanBeExpression = true;
|
|
|
|
bool InTemplateArgument = false;
|
|
|
|
bool InCtorInitializer = false;
|
|
|
|
bool CaretFound = false;
|
|
|
|
bool IsForEachMacro = false;
|
2013-02-06 18:05:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
|
|
|
|
/// of each instance.
|
|
|
|
struct ScopedContextCreator {
|
|
|
|
AnnotatingParser &P;
|
|
|
|
|
2013-03-14 21:45:21 +08:00
|
|
|
ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
|
|
|
|
unsigned Increase)
|
|
|
|
: P(P) {
|
2013-07-08 22:34:09 +08:00
|
|
|
P.Contexts.push_back(Context(ContextKind,
|
|
|
|
P.Contexts.back().BindingStrength + Increase,
|
|
|
|
P.Contexts.back().IsExpression));
|
2013-02-06 18:05:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedContextCreator() { P.Contexts.pop_back(); }
|
|
|
|
};
|
2013-02-04 15:21:18 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
void modifyContext(const FormatToken &Current) {
|
2013-05-29 22:47:47 +08:00
|
|
|
if (Current.getPrecedence() == prec::Assignment &&
|
2015-12-24 02:01:29 +08:00
|
|
|
!Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
|
2013-05-29 22:47:47 +08:00
|
|
|
(!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2015-06-17 17:43:56 +08:00
|
|
|
if (!Line.startsWith(TT_UnaryOperator)) {
|
2015-01-24 03:04:49 +08:00
|
|
|
for (FormatToken *Previous = Current.Previous;
|
2015-07-22 06:51:00 +08:00
|
|
|
Previous && Previous->Previous &&
|
|
|
|
!Previous->Previous->isOneOf(tok::comma, tok::semi);
|
2015-01-24 03:04:49 +08:00
|
|
|
Previous = Previous->Previous) {
|
|
|
|
if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
|
|
|
|
Previous = Previous->MatchingParen;
|
|
|
|
if (!Previous)
|
|
|
|
break;
|
|
|
|
}
|
2015-03-11 20:59:49 +08:00
|
|
|
if (Previous->opensScope())
|
|
|
|
break;
|
2015-01-24 03:04:49 +08:00
|
|
|
if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
|
2015-02-25 18:30:06 +08:00
|
|
|
Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
|
|
|
|
Previous->Previous && Previous->Previous->isNot(tok::equal))
|
2015-01-24 03:04:49 +08:00
|
|
|
Previous->Type = TT_PointerOrReference;
|
2014-08-13 22:00:41 +08:00
|
|
|
}
|
2013-02-04 15:21:18 +08:00
|
|
|
}
|
2015-05-19 20:29:27 +08:00
|
|
|
} else if (Current.is(tok::lessless) &&
|
|
|
|
(!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
|
|
|
|
Contexts.back().IsExpression = true;
|
2013-12-16 16:36:18 +08:00
|
|
|
} else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
|
2013-02-06 18:05:46 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2014-11-21 22:08:38 +08:00
|
|
|
} else if (Current.is(TT_TrailingReturnArrow)) {
|
|
|
|
Contexts.back().IsExpression = false;
|
2015-09-16 07:48:17 +08:00
|
|
|
} else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
|
2015-06-05 21:18:09 +08:00
|
|
|
Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
|
2013-03-13 22:41:29 +08:00
|
|
|
} else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
|
2013-05-29 22:47:47 +08:00
|
|
|
for (FormatToken *Previous = Current.Previous;
|
2013-03-13 22:41:29 +08:00
|
|
|
Previous && Previous->isOneOf(tok::star, tok::amp);
|
2013-05-29 22:47:47 +08:00
|
|
|
Previous = Previous->Previous)
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
Previous->Type = TT_PointerOrReference;
|
2016-02-01 19:21:07 +08:00
|
|
|
if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
|
|
|
|
Contexts.back().IsExpression = false;
|
2013-05-29 22:47:47 +08:00
|
|
|
} else if (Current.Previous &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Current.Previous->is(TT_CtorInitializerColon)) {
|
2013-02-18 20:44:35 +08:00
|
|
|
Contexts.back().IsExpression = true;
|
2013-07-27 00:56:36 +08:00
|
|
|
Contexts.back().InCtorInitializer = true;
|
2013-03-13 15:49:51 +08:00
|
|
|
} else if (Current.is(tok::kw_new)) {
|
|
|
|
Contexts.back().CanBeExpression = false;
|
2015-06-18 18:59:26 +08:00
|
|
|
} else if (Current.isOneOf(tok::semi, tok::exclaim)) {
|
2013-05-03 22:41:24 +08:00
|
|
|
// This should be the condition or increment in a for-loop.
|
|
|
|
Contexts.back().IsExpression = true;
|
Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.
Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.
Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.
Before:
return sizeof(int * *);
Type **A = static_cast<Type * *>(P);
Now:
return sizeof(int**);
Type **A = static_cast<Type **>(P);
While here, also make all methods of AnnotatingParser except parseLine()
private.
Review URL: http://llvm-reviews.chandlerc.com/D384
llvm-svn: 174975
2013-02-13 00:17:07 +08:00
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
}
|
2013-02-04 15:21:18 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
void determineTokenType(FormatToken &Current) {
|
|
|
|
if (!Current.is(TT_Unknown))
|
|
|
|
// The token type is already known.
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Line.MightBeFunctionDecl can only be true after the parentheses of a
|
|
|
|
// function declaration have been found. In this case, 'Current' is a
|
|
|
|
// trailing token of this declaration and thus cannot be a name.
|
|
|
|
if (Current.is(Keywords.kw_instanceof)) {
|
|
|
|
Current.Type = TT_BinaryOperator;
|
|
|
|
} else if (isStartOfName(Current) &&
|
|
|
|
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
|
|
|
|
Contexts.back().FirstStartOfName = &Current;
|
|
|
|
Current.Type = TT_StartOfName;
|
2015-11-11 10:02:15 +08:00
|
|
|
} else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
|
2014-11-25 18:05:17 +08:00
|
|
|
AutoFound = true;
|
|
|
|
} else if (Current.is(tok::arrow) &&
|
|
|
|
Style.Language == FormatStyle::LK_Java) {
|
|
|
|
Current.Type = TT_LambdaArrow;
|
|
|
|
} else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
|
|
|
|
Current.NestingLevel == 0) {
|
|
|
|
Current.Type = TT_TrailingReturnArrow;
|
|
|
|
} else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
|
|
|
|
Current.Type =
|
|
|
|
determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
|
|
|
|
Contexts.back().IsExpression,
|
|
|
|
Contexts.back().InTemplateArgument);
|
|
|
|
} else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
|
|
|
|
Current.Type = determinePlusMinusCaretUsage(Current);
|
|
|
|
if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
|
|
|
|
Contexts.back().CaretFound = true;
|
|
|
|
} else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
|
|
|
|
Current.Type = determineIncrementUsage(Current);
|
|
|
|
} else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
|
|
|
|
Current.Type = TT_UnaryOperator;
|
|
|
|
} else if (Current.is(tok::question)) {
|
2015-05-05 16:40:32 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
Line.MustBeDeclaration) {
|
|
|
|
// In JavaScript, `interface X { foo?(): bar; }` is an optional method
|
|
|
|
// on the interface, not a ternary expression.
|
|
|
|
Current.Type = TT_JsTypeOptionalQuestion;
|
|
|
|
} else {
|
|
|
|
Current.Type = TT_ConditionalExpr;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.isBinaryOperator() &&
|
|
|
|
(!Current.Previous || Current.Previous->isNot(tok::l_square))) {
|
|
|
|
Current.Type = TT_BinaryOperator;
|
|
|
|
} else if (Current.is(tok::comment)) {
|
2015-05-06 16:58:57 +08:00
|
|
|
if (Current.TokenText.startswith("/*")) {
|
|
|
|
if (Current.TokenText.endswith("*/"))
|
|
|
|
Current.Type = TT_BlockComment;
|
|
|
|
else
|
|
|
|
// The lexer has for some reason determined a comment here. But we
|
|
|
|
// cannot really handle it, if it isn't properly terminated.
|
|
|
|
Current.Tok.setKind(tok::unknown);
|
|
|
|
} else {
|
|
|
|
Current.Type = TT_LineComment;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.is(tok::r_paren)) {
|
|
|
|
if (rParenEndsCast(Current))
|
|
|
|
Current.Type = TT_CastRParen;
|
2015-05-18 21:47:23 +08:00
|
|
|
if (Current.MatchingParen && Current.Next &&
|
|
|
|
!Current.Next->isBinaryOperator() &&
|
2016-04-06 21:58:09 +08:00
|
|
|
!Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
|
|
|
|
tok::period, tok::arrow, tok::coloncolon))
|
2015-05-18 21:47:23 +08:00
|
|
|
if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
|
|
|
|
if (BeforeParen->is(tok::identifier) &&
|
|
|
|
BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
|
|
|
|
(!BeforeParen->Previous ||
|
|
|
|
BeforeParen->Previous->ClosesTemplateDeclaration))
|
|
|
|
Current.Type = TT_FunctionAnnotationRParen;
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.is(tok::at) && Current.Next) {
|
2015-05-11 23:15:48 +08:00
|
|
|
if (Current.Next->isStringLiteral()) {
|
|
|
|
Current.Type = TT_ObjCStringLiteral;
|
|
|
|
} else {
|
|
|
|
switch (Current.Next->Tok.getObjCKeywordID()) {
|
|
|
|
case tok::objc_interface:
|
|
|
|
case tok::objc_implementation:
|
|
|
|
case tok::objc_protocol:
|
|
|
|
Current.Type = TT_ObjCDecl;
|
|
|
|
break;
|
|
|
|
case tok::objc_property:
|
|
|
|
Current.Type = TT_ObjCProperty;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-11-24 23:42:34 +08:00
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.is(tok::period)) {
|
|
|
|
FormatToken *PreviousNoComment = Current.getPreviousNonComment();
|
|
|
|
if (PreviousNoComment &&
|
|
|
|
PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
|
|
|
|
Current.Type = TT_DesignatedInitializerPeriod;
|
2015-01-10 07:25:06 +08:00
|
|
|
else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
|
|
|
|
Current.Previous->isOneOf(TT_JavaAnnotation,
|
|
|
|
TT_LeadingJavaAnnotation)) {
|
|
|
|
Current.Type = Current.Previous->Type;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
|
|
|
|
Current.Previous &&
|
|
|
|
!Current.Previous->isOneOf(tok::equal, tok::at) &&
|
|
|
|
Line.MightBeFunctionDecl && Contexts.size() == 1) {
|
|
|
|
// Line.MightBeFunctionDecl can only be true after the parentheses of a
|
|
|
|
// function declaration have been found.
|
|
|
|
Current.Type = TT_TrailingAnnotation;
|
2015-02-19 01:17:15 +08:00
|
|
|
} else if ((Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript) &&
|
|
|
|
Current.Previous) {
|
2015-01-10 07:25:06 +08:00
|
|
|
if (Current.Previous->is(tok::at) &&
|
|
|
|
Current.isNot(Keywords.kw_interface)) {
|
|
|
|
const FormatToken &AtToken = *Current.Previous;
|
|
|
|
const FormatToken *Previous = AtToken.getPreviousNonComment();
|
|
|
|
if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
|
|
|
|
Current.Type = TT_LeadingJavaAnnotation;
|
|
|
|
else
|
|
|
|
Current.Type = TT_JavaAnnotation;
|
|
|
|
} else if (Current.Previous->is(tok::period) &&
|
|
|
|
Current.Previous->isOneOf(TT_JavaAnnotation,
|
|
|
|
TT_LeadingJavaAnnotation)) {
|
|
|
|
Current.Type = Current.Previous->Type;
|
|
|
|
}
|
2013-02-04 15:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 17:47:29 +08:00
|
|
|
/// \brief Take a guess at whether \p Tok starts a name of a function or
|
|
|
|
/// variable declaration.
|
|
|
|
///
|
|
|
|
/// This is a heuristic based on whether \p Tok is an identifier following
|
|
|
|
/// something that is likely a type.
|
|
|
|
bool isStartOfName(const FormatToken &Tok) {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (Tok.isNot(tok::identifier) || !Tok.Previous)
|
2013-07-02 17:47:29 +08:00
|
|
|
return false;
|
|
|
|
|
2015-07-03 18:12:53 +08:00
|
|
|
if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof))
|
2015-02-19 07:48:37 +08:00
|
|
|
return false;
|
2016-02-01 19:20:47 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
Tok.Previous->is(Keywords.kw_in))
|
|
|
|
return false;
|
2015-01-14 18:00:20 +08:00
|
|
|
|
2013-07-02 17:47:29 +08:00
|
|
|
// Skip "const" as it does not have an influence on whether this is a name.
|
|
|
|
FormatToken *PreviousNotConst = Tok.Previous;
|
2014-05-09 16:15:10 +08:00
|
|
|
while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
|
2013-07-02 17:47:29 +08:00
|
|
|
PreviousNotConst = PreviousNotConst->Previous;
|
|
|
|
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!PreviousNotConst)
|
2013-07-02 17:47:29 +08:00
|
|
|
return false;
|
|
|
|
|
2013-07-08 22:34:09 +08:00
|
|
|
bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
|
|
|
|
PreviousNotConst->Previous &&
|
|
|
|
PreviousNotConst->Previous->is(tok::hash);
|
2013-07-02 17:47:29 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (PreviousNotConst->is(TT_TemplateCloser))
|
2013-08-19 18:16:18 +08:00
|
|
|
return PreviousNotConst && PreviousNotConst->MatchingParen &&
|
|
|
|
PreviousNotConst->MatchingParen->Previous &&
|
2014-11-20 17:48:11 +08:00
|
|
|
PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
|
2013-08-19 18:16:18 +08:00
|
|
|
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
|
|
|
|
|
2014-05-05 21:48:09 +08:00
|
|
|
if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
|
|
|
|
PreviousNotConst->MatchingParen->Previous &&
|
|
|
|
PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
|
|
|
|
return true;
|
|
|
|
|
2015-08-25 21:40:51 +08:00
|
|
|
return (!IsPPKeyword &&
|
|
|
|
PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
PreviousNotConst->is(TT_PointerOrReference) ||
|
2014-01-16 17:11:55 +08:00
|
|
|
PreviousNotConst->isSimpleTypeSpecifier();
|
2013-07-02 17:47:29 +08:00
|
|
|
}
|
|
|
|
|
2014-05-06 17:08:34 +08:00
|
|
|
/// \brief Determine whether ')' is ending a cast.
|
|
|
|
bool rParenEndsCast(const FormatToken &Tok) {
|
2015-11-23 23:55:55 +08:00
|
|
|
// C-style casts are only used in C++ and Java.
|
|
|
|
if (Style.Language != FormatStyle::LK_Cpp &&
|
|
|
|
Style.Language != FormatStyle::LK_Java)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Empty parens aren't casts and there are no casts at the end of the line.
|
|
|
|
if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
|
|
|
|
return false;
|
2015-11-23 23:55:50 +08:00
|
|
|
|
2015-11-23 23:55:55 +08:00
|
|
|
FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
|
2015-11-23 23:55:50 +08:00
|
|
|
if (LeftOfParens) {
|
2015-11-24 03:11:45 +08:00
|
|
|
// If there is an opening parenthesis left of the current parentheses,
|
|
|
|
// look past it as these might be chained casts.
|
|
|
|
if (LeftOfParens->is(tok::r_paren)) {
|
|
|
|
if (!LeftOfParens->MatchingParen ||
|
|
|
|
!LeftOfParens->MatchingParen->Previous)
|
|
|
|
return false;
|
|
|
|
LeftOfParens = LeftOfParens->MatchingParen->Previous;
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:55:55 +08:00
|
|
|
// If there is an identifier (or with a few exceptions a keyword) right
|
|
|
|
// before the parentheses, this is unlikely to be a cast.
|
2015-11-23 23:55:50 +08:00
|
|
|
if (LeftOfParens->Tok.getIdentifierInfo() &&
|
|
|
|
!LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
|
|
|
|
tok::kw_delete))
|
|
|
|
return false;
|
2015-11-23 23:55:55 +08:00
|
|
|
|
|
|
|
// Certain other tokens right before the parentheses are also signals that
|
|
|
|
// this cannot be a cast.
|
|
|
|
if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
|
2015-11-23 23:55:50 +08:00
|
|
|
TT_TemplateCloser))
|
|
|
|
return false;
|
2014-11-26 20:23:10 +08:00
|
|
|
}
|
2015-11-23 23:55:55 +08:00
|
|
|
|
|
|
|
if (Tok.Next->is(tok::question))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// As Java has no function types, a "(" after the ")" likely means that this
|
|
|
|
// is a cast.
|
|
|
|
if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If a (non-string) literal follows, this is likely a cast.
|
|
|
|
if (Tok.Next->isNot(tok::string_literal) &&
|
|
|
|
(Tok.Next->Tok.isLiteral() ||
|
|
|
|
Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Heuristically try to determine whether the parentheses contain a type.
|
2014-11-25 18:05:17 +08:00
|
|
|
bool ParensAreType =
|
|
|
|
!Tok.Previous ||
|
|
|
|
Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
|
|
|
|
Tok.Previous->isSimpleTypeSpecifier();
|
2014-05-06 17:08:34 +08:00
|
|
|
bool ParensCouldEndDecl =
|
2015-10-26 20:08:47 +08:00
|
|
|
Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
|
2015-12-28 15:44:25 +08:00
|
|
|
if (ParensAreType && !ParensCouldEndDecl)
|
2015-11-23 23:55:55 +08:00
|
|
|
return true;
|
2014-05-06 17:08:34 +08:00
|
|
|
|
2015-11-23 23:55:55 +08:00
|
|
|
// At this point, we heuristically assume that there are no casts at the
|
|
|
|
// start of the line. We assume that we have found most cases where there
|
|
|
|
// are by the logic above, e.g. "(void)x;".
|
|
|
|
if (!LeftOfParens)
|
|
|
|
return false;
|
|
|
|
|
2016-04-05 19:46:06 +08:00
|
|
|
// If the following token is an identifier or 'this', this is a cast. All
|
|
|
|
// cases where this can be something else are handled above.
|
|
|
|
if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
|
2015-11-23 23:55:55 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Tok.Next->Next)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the next token after the parenthesis is a unary operator, assume
|
|
|
|
// that this is cast, unless there are unexpected tokens inside the
|
|
|
|
// parenthesis.
|
|
|
|
bool NextIsUnary =
|
|
|
|
Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
|
|
|
|
if (!NextIsUnary || Tok.Next->is(tok::plus) ||
|
|
|
|
!Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
|
|
|
|
return false;
|
|
|
|
// Search for unexpected tokens.
|
|
|
|
for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
|
|
|
|
Prev = Prev->Previous) {
|
|
|
|
if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
|
|
|
|
return false;
|
2014-05-06 17:08:34 +08:00
|
|
|
}
|
2015-11-23 23:55:55 +08:00
|
|
|
return true;
|
2014-05-06 17:08:34 +08:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:21:18 +08:00
|
|
|
/// \brief Return the type of the given token assuming it is * or &.
|
2014-03-27 19:17:36 +08:00
|
|
|
TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
|
|
|
|
bool InTemplateArgument) {
|
2014-09-05 16:53:45 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript)
|
|
|
|
return TT_BinaryOperator;
|
|
|
|
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *PrevToken = Tok.getPreviousNonComment();
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!PrevToken)
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_UnaryOperator;
|
|
|
|
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *NextToken = Tok.getNextNonComment();
|
2015-08-24 22:28:08 +08:00
|
|
|
if (!NextToken ||
|
|
|
|
NextToken->isOneOf(tok::arrow, Keywords.kw_final,
|
|
|
|
Keywords.kw_override) ||
|
2014-11-21 22:08:38 +08:00
|
|
|
(NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
|
2015-08-24 22:28:08 +08:00
|
|
|
return TT_PointerOrReference;
|
2013-02-04 15:21:18 +08:00
|
|
|
|
2014-08-29 20:54:38 +08:00
|
|
|
if (PrevToken->is(tok::coloncolon))
|
2013-03-02 01:13:29 +08:00
|
|
|
return TT_PointerOrReference;
|
|
|
|
|
2013-03-13 22:41:29 +08:00
|
|
|
if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
|
2013-03-14 18:50:25 +08:00
|
|
|
tok::comma, tok::semi, tok::kw_return, tok::colon,
|
2014-11-24 23:42:34 +08:00
|
|
|
tok::equal, tok::kw_delete, tok::kw_sizeof) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
|
|
|
|
TT_UnaryOperator, TT_CastRParen))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_UnaryOperator;
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
|
2013-02-06 14:20:11 +08:00
|
|
|
return TT_PointerOrReference;
|
2015-10-07 09:41:14 +08:00
|
|
|
if (NextToken->is(tok::kw_operator) && !IsExpression)
|
|
|
|
return TT_PointerOrReference;
|
|
|
|
if (NextToken->isOneOf(tok::comma, tok::semi))
|
2014-06-23 15:36:18 +08:00
|
|
|
return TT_PointerOrReference;
|
2013-02-06 14:20:11 +08:00
|
|
|
|
2013-09-10 18:26:38 +08:00
|
|
|
if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
|
|
|
|
PrevToken->MatchingParen->Previous &&
|
2014-06-11 15:35:16 +08:00
|
|
|
PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
|
|
|
|
tok::kw_decltype))
|
2013-09-10 18:26:38 +08:00
|
|
|
return TT_PointerOrReference;
|
|
|
|
|
2014-03-28 17:27:09 +08:00
|
|
|
if (PrevToken->Tok.isLiteral() ||
|
|
|
|
PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
|
2014-10-29 02:28:22 +08:00
|
|
|
tok::kw_false, tok::r_brace) ||
|
2014-03-28 17:27:09 +08:00
|
|
|
NextToken->Tok.isLiteral() ||
|
|
|
|
NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
|
|
|
|
NextToken->isUnaryOperator() ||
|
2014-03-27 19:17:36 +08:00
|
|
|
// If we know we're in a template argument, there are no named
|
|
|
|
// declarations. Thus, having an identifier on the right-hand side
|
|
|
|
// indicates a binary operator.
|
|
|
|
(InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_BinaryOperator;
|
|
|
|
|
2014-10-29 02:11:52 +08:00
|
|
|
// "&&(" is quite unlikely to be two successive unary "&".
|
|
|
|
if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
|
|
|
|
return TT_BinaryOperator;
|
|
|
|
|
2014-06-02 19:54:20 +08:00
|
|
|
// This catches some cases where evaluation order is used as control flow:
|
|
|
|
// aaa && aaa->f();
|
|
|
|
const FormatToken *NextNextToken = NextToken->getNextNonComment();
|
|
|
|
if (NextNextToken && NextNextToken->is(tok::arrow))
|
|
|
|
return TT_BinaryOperator;
|
|
|
|
|
2013-02-04 15:21:18 +08:00
|
|
|
// It is very unlikely that we are going to find a pointer or reference type
|
|
|
|
// definition on the RHS of an assignment.
|
2014-11-24 03:15:35 +08:00
|
|
|
if (IsExpression && !Contexts.back().CaretFound)
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_BinaryOperator;
|
|
|
|
|
|
|
|
return TT_PointerOrReference;
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *PrevToken = Tok.getPreviousNonComment();
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!PrevToken || PrevToken->is(TT_CastRParen))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_UnaryOperator;
|
|
|
|
|
|
|
|
// Use heuristics to recognize unary operators.
|
2013-03-13 22:41:29 +08:00
|
|
|
if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
|
|
|
|
tok::question, tok::colon, tok::kw_return,
|
|
|
|
tok::kw_case, tok::at, tok::l_brace))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_UnaryOperator;
|
|
|
|
|
2013-02-06 00:21:00 +08:00
|
|
|
// There can't be two consecutive binary operators.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (PrevToken->is(TT_BinaryOperator))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_UnaryOperator;
|
|
|
|
|
|
|
|
// Fall back to marking the token as binary operator.
|
|
|
|
return TT_BinaryOperator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
|
2013-05-29 22:47:47 +08:00
|
|
|
TokenType determineIncrementUsage(const FormatToken &Tok) {
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *PrevToken = Tok.getPreviousNonComment();
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!PrevToken || PrevToken->is(TT_CastRParen))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_UnaryOperator;
|
2013-03-13 22:41:29 +08:00
|
|
|
if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
|
2013-02-04 15:21:18 +08:00
|
|
|
return TT_TrailingUnaryOperator;
|
|
|
|
|
|
|
|
return TT_UnaryOperator;
|
|
|
|
}
|
2013-02-06 18:05:46 +08:00
|
|
|
|
|
|
|
SmallVector<Context, 8> Contexts;
|
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
const FormatStyle &Style;
|
2013-02-06 18:05:46 +08:00
|
|
|
AnnotatedLine &Line;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *CurrentToken;
|
2013-07-09 22:36:48 +08:00
|
|
|
bool AutoFound;
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords;
|
2015-05-06 16:38:24 +08:00
|
|
|
|
|
|
|
// Set of "<" tokens that do not open a template parameter list. If parseAngle
|
|
|
|
// determines that a specific token can't be a template opener, it will make
|
|
|
|
// same decision irrespective of the decisions for tokens leading up to it.
|
|
|
|
// Store this information to prevent this from causing exponential runtime.
|
|
|
|
llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
2015-03-09 00:06:46 +08:00
|
|
|
static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
|
|
|
|
static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
|
2013-02-08 23:28:42 +08:00
|
|
|
/// \brief Parses binary expressions by inserting fake parenthesis based on
|
|
|
|
/// operator precedence.
|
|
|
|
class ExpressionParser {
|
|
|
|
public:
|
2014-11-04 20:41:02 +08:00
|
|
|
ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
|
|
|
|
AnnotatedLine &Line)
|
|
|
|
: Style(Style), Keywords(Keywords), Current(Line.First) {}
|
2013-02-08 23:28:42 +08:00
|
|
|
|
|
|
|
/// \brief Parse expressions with the given operatore precedence.
|
2013-02-24 05:01:55 +08:00
|
|
|
void parse(int Precedence = 0) {
|
clang-format: Improve ObjC variadic and binary expression parameters.
Before:
[self aaaaaaaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];
[self aaaaaaaaaaaaaaa:aaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];
After:
[self aaaaaaaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |
aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];
[self aaaaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];
This addresses llvm.org/PR15349 and llvm.org/PR16185.
llvm-svn: 194214
2013-11-08 03:23:49 +08:00
|
|
|
// Skip 'return' and ObjC selector colons as they are not part of a binary
|
|
|
|
// expression.
|
2014-11-25 18:05:17 +08:00
|
|
|
while (Current && (Current->is(tok::kw_return) ||
|
|
|
|
(Current->is(tok::colon) &&
|
|
|
|
Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
|
2013-09-30 16:29:03 +08:00
|
|
|
next();
|
|
|
|
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!Current || Precedence > PrecedenceArrowAndPeriod)
|
2013-08-23 23:14:03 +08:00
|
|
|
return;
|
|
|
|
|
2013-05-31 22:56:12 +08:00
|
|
|
// Conditional expressions need to be parsed separately for proper nesting.
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
if (Precedence == prec::Conditional) {
|
2013-05-31 22:56:12 +08:00
|
|
|
parseConditionalExpr();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-23 23:14:03 +08:00
|
|
|
|
|
|
|
// Parse unary operators, which all have a higher precedence than binary
|
|
|
|
// operators.
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
if (Precedence == PrecedenceUnaryOperator) {
|
2013-08-23 23:14:03 +08:00
|
|
|
parseUnaryOperator();
|
2013-02-08 23:28:42 +08:00
|
|
|
return;
|
2013-08-23 23:14:03 +08:00
|
|
|
}
|
2013-02-08 23:28:42 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Start = Current;
|
2014-05-09 16:15:10 +08:00
|
|
|
FormatToken *LatestOperator = nullptr;
|
2014-04-16 20:26:54 +08:00
|
|
|
unsigned OperatorIndex = 0;
|
2013-02-08 23:28:42 +08:00
|
|
|
|
2013-02-24 05:01:55 +08:00
|
|
|
while (Current) {
|
2013-02-08 23:28:42 +08:00
|
|
|
// Consume operators with higher precedence.
|
2013-04-09 04:33:42 +08:00
|
|
|
parse(Precedence + 1);
|
2013-02-08 23:28:42 +08:00
|
|
|
|
2013-08-23 23:14:03 +08:00
|
|
|
int CurrentPrecedence = getCurrentPrecedence();
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current && Current->is(TT_SelectorName) &&
|
2013-12-23 15:29:06 +08:00
|
|
|
Precedence == CurrentPrecedence) {
|
|
|
|
if (LatestOperator)
|
|
|
|
addFakeParenthesis(Start, prec::Level(Precedence));
|
2013-08-23 23:14:03 +08:00
|
|
|
Start = Current;
|
2013-12-23 15:29:06 +08:00
|
|
|
}
|
2013-02-24 05:01:55 +08:00
|
|
|
|
2013-02-08 23:28:42 +08:00
|
|
|
// At the end of the line or when an operator with higher precedence is
|
|
|
|
// found, insert fake parenthesis and return.
|
2014-10-20 19:12:51 +08:00
|
|
|
if (!Current || (Current->closesScope() && Current->MatchingParen) ||
|
2014-11-14 21:03:40 +08:00
|
|
|
(CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
|
|
|
|
(CurrentPrecedence == prec::Conditional &&
|
|
|
|
Precedence == prec::Assignment && Current->is(tok::colon))) {
|
2014-12-03 21:20:49 +08:00
|
|
|
break;
|
2013-02-08 23:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Consume scopes: (), [], <> and {}
|
2013-04-10 17:49:49 +08:00
|
|
|
if (Current->opensScope()) {
|
|
|
|
while (Current && !Current->closesScope()) {
|
2013-02-08 23:28:42 +08:00
|
|
|
next();
|
|
|
|
parse();
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
// Operator found.
|
2014-04-16 20:26:54 +08:00
|
|
|
if (CurrentPrecedence == Precedence) {
|
2016-01-05 21:03:50 +08:00
|
|
|
if (LatestOperator)
|
|
|
|
LatestOperator->NextOperator = Current;
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
LatestOperator = Current;
|
2014-04-16 20:26:54 +08:00
|
|
|
Current->OperatorIndex = OperatorIndex;
|
|
|
|
++OperatorIndex;
|
|
|
|
}
|
2014-11-12 07:04:51 +08:00
|
|
|
next(/*SkipPastLeadingComments=*/Precedence > 0);
|
2013-02-08 23:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-03 21:20:49 +08:00
|
|
|
|
|
|
|
if (LatestOperator && (Current || Precedence > 0)) {
|
2016-01-05 21:03:50 +08:00
|
|
|
// LatestOperator->LastOperator = true;
|
2014-12-03 21:20:49 +08:00
|
|
|
if (Precedence == PrecedenceArrowAndPeriod) {
|
|
|
|
// Call expressions don't have a binary operator precedence.
|
|
|
|
addFakeParenthesis(Start, prec::Unknown);
|
|
|
|
} else {
|
|
|
|
addFakeParenthesis(Start, prec::Level(Precedence));
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 23:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-23 23:14:03 +08:00
|
|
|
/// \brief Gets the precedence (+1) of the given token for binary operators
|
|
|
|
/// and other tokens that we treat like binary operators.
|
|
|
|
int getCurrentPrecedence() {
|
|
|
|
if (Current) {
|
2014-09-05 16:29:31 +08:00
|
|
|
const FormatToken *NextNonComment = Current->getNextNonComment();
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current->is(TT_ConditionalExpr))
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
return prec::Conditional;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (NextNonComment && NextNonComment->is(tok::colon) &&
|
|
|
|
NextNonComment->is(TT_DictLiteral))
|
2014-09-05 16:29:31 +08:00
|
|
|
return prec::Comma;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Current->is(TT_LambdaArrow))
|
2014-11-21 20:14:12 +08:00
|
|
|
return prec::Comma;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Current->is(TT_JsFatArrow))
|
2015-06-05 16:25:37 +08:00
|
|
|
return prec::Assignment;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName,
|
|
|
|
TT_JsComputedPropertyName) ||
|
|
|
|
(Current->is(tok::comment) && NextNonComment &&
|
|
|
|
NextNonComment->is(TT_SelectorName)))
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
return 0;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Current->is(TT_RangeBasedForLoopColon))
|
2014-02-07 18:09:46 +08:00
|
|
|
return prec::Comma;
|
2015-07-03 18:12:53 +08:00
|
|
|
if ((Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript) &&
|
|
|
|
Current->is(Keywords.kw_instanceof))
|
|
|
|
return prec::Relational;
|
2016-02-01 19:20:47 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
Current->is(Keywords.kw_in))
|
|
|
|
return prec::Relational;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
return Current->getPrecedence();
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Current->isOneOf(tok::period, tok::arrow))
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
return PrecedenceArrowAndPeriod;
|
2015-06-04 01:08:40 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java &&
|
|
|
|
Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
|
|
|
|
Keywords.kw_throws))
|
2014-11-03 03:16:41 +08:00
|
|
|
return 0;
|
2013-08-23 23:14:03 +08:00
|
|
|
}
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
return -1;
|
2013-08-23 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:56:12 +08:00
|
|
|
void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
|
|
|
|
Start->FakeLParens.push_back(Precedence);
|
2013-09-06 16:08:14 +08:00
|
|
|
if (Precedence > prec::Unknown)
|
|
|
|
Start->StartsBinaryExpression = true;
|
|
|
|
if (Current) {
|
2014-11-14 20:31:14 +08:00
|
|
|
FormatToken *Previous = Current->Previous;
|
2014-12-03 22:02:59 +08:00
|
|
|
while (Previous->is(tok::comment) && Previous->Previous)
|
2014-11-14 20:31:14 +08:00
|
|
|
Previous = Previous->Previous;
|
|
|
|
++Previous->FakeRParens;
|
2013-09-06 16:08:14 +08:00
|
|
|
if (Precedence > prec::Unknown)
|
2014-11-14 20:31:14 +08:00
|
|
|
Previous->EndsBinaryExpression = true;
|
2013-09-06 16:08:14 +08:00
|
|
|
}
|
2013-05-31 22:56:12 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:14:03 +08:00
|
|
|
/// \brief Parse unary operator expressions and surround them with fake
|
|
|
|
/// parentheses if appropriate.
|
|
|
|
void parseUnaryOperator() {
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Current || Current->isNot(TT_UnaryOperator)) {
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
parse(PrecedenceArrowAndPeriod);
|
2013-08-23 23:14:03 +08:00
|
|
|
return;
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
}
|
2013-08-23 23:14:03 +08:00
|
|
|
|
|
|
|
FormatToken *Start = Current;
|
|
|
|
next();
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
parseUnaryOperator();
|
2013-08-23 23:14:03 +08:00
|
|
|
|
|
|
|
// The actual precedence doesn't matter.
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
addFakeParenthesis(Start, prec::Unknown);
|
2013-08-23 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:56:12 +08:00
|
|
|
void parseConditionalExpr() {
|
2014-10-16 17:10:11 +08:00
|
|
|
while (Current && Current->isTrailingComment()) {
|
|
|
|
next();
|
|
|
|
}
|
2013-05-31 22:56:12 +08:00
|
|
|
FormatToken *Start = Current;
|
clang-format: Revamp builder-type call formatting.
Previously builder-type calls were only correctly recognized in
top-level calls.
This fixes llvm.org/PR16981.
Before:
someobj->Add((new util::filetools::Handler(dir))->OnEvent1(
NewPermanentCallback(this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
After:
someobj->Add((new util::filetools::Handler(dir))
->OnEvent1(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBA))
->OnEvent2(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBB))
->OnEvent3(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBC))
->OnEvent5(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBD))
->OnEvent6(NewPermanentCallback(
this, &HandlerHolderClass::EventHandlerCBE)));
llvm-svn: 189337
2013-08-27 19:09:05 +08:00
|
|
|
parse(prec::LogicalOr);
|
2013-05-31 22:56:12 +08:00
|
|
|
if (!Current || !Current->is(tok::question))
|
|
|
|
return;
|
|
|
|
next();
|
2014-11-14 21:03:40 +08:00
|
|
|
parse(prec::Assignment);
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Current || Current->isNot(TT_ConditionalExpr))
|
2013-05-31 22:56:12 +08:00
|
|
|
return;
|
|
|
|
next();
|
2014-11-14 21:03:40 +08:00
|
|
|
parse(prec::Assignment);
|
2013-05-31 22:56:12 +08:00
|
|
|
addFakeParenthesis(Start, prec::Conditional);
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:58:30 +08:00
|
|
|
void next(bool SkipPastLeadingComments = true) {
|
2013-06-17 21:19:53 +08:00
|
|
|
if (Current)
|
|
|
|
Current = Current->Next;
|
2014-09-04 22:58:30 +08:00
|
|
|
while (Current &&
|
|
|
|
(Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
|
|
|
|
Current->isTrailingComment())
|
2013-05-29 22:47:47 +08:00
|
|
|
Current = Current->Next;
|
2013-02-08 23:28:42 +08:00
|
|
|
}
|
|
|
|
|
2014-11-03 03:16:41 +08:00
|
|
|
const FormatStyle &Style;
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Current;
|
2013-02-08 23:28:42 +08:00
|
|
|
};
|
|
|
|
|
2013-07-01 12:03:19 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-01-10 07:25:06 +08:00
|
|
|
void TokenAnnotator::setCommentLineLevels(
|
|
|
|
SmallVectorImpl<AnnotatedLine *> &Lines) {
|
2014-05-09 16:15:10 +08:00
|
|
|
const AnnotatedLine *NextNonCommentLine = nullptr;
|
2013-11-06 03:10:03 +08:00
|
|
|
for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
|
|
|
|
E = Lines.rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
|
2014-05-09 16:15:10 +08:00
|
|
|
(*I)->First->Next == nullptr)
|
2013-11-06 03:10:03 +08:00
|
|
|
(*I)->Level = NextNonCommentLine->Level;
|
2013-09-06 15:54:20 +08:00
|
|
|
else
|
2014-05-09 16:15:10 +08:00
|
|
|
NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
|
2013-11-06 03:10:03 +08:00
|
|
|
|
|
|
|
setCommentLineLevels((*I)->Children);
|
2013-09-06 15:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
void TokenAnnotator::annotate(AnnotatedLine &Line) {
|
2013-09-06 15:54:20 +08:00
|
|
|
for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
|
|
|
|
E = Line.Children.end();
|
2013-09-05 17:29:45 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
annotate(**I);
|
|
|
|
}
|
2014-11-04 20:41:02 +08:00
|
|
|
AnnotatingParser Parser(Style, Line, Keywords);
|
2013-01-30 05:01:14 +08:00
|
|
|
Line.Type = Parser.parseLine();
|
|
|
|
if (Line.Type == LT_Invalid)
|
|
|
|
return;
|
|
|
|
|
2014-11-04 20:41:02 +08:00
|
|
|
ExpressionParser ExprParser(Style, Keywords, Line);
|
2013-02-08 23:28:42 +08:00
|
|
|
ExprParser.parse();
|
|
|
|
|
2015-06-17 17:43:56 +08:00
|
|
|
if (Line.startsWith(TT_ObjCMethodSpecifier))
|
2013-01-30 05:01:14 +08:00
|
|
|
Line.Type = LT_ObjCMethodDecl;
|
2015-06-17 17:43:56 +08:00
|
|
|
else if (Line.startsWith(TT_ObjCDecl))
|
2013-01-30 05:01:14 +08:00
|
|
|
Line.Type = LT_ObjCDecl;
|
2015-06-17 17:43:56 +08:00
|
|
|
else if (Line.startsWith(TT_ObjCProperty))
|
2013-01-30 05:01:14 +08:00
|
|
|
Line.Type = LT_ObjCProperty;
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
Line.First->SpacesRequiredBefore = 1;
|
|
|
|
Line.First->CanBreakBefore = Line.First->MustBreakBefore;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:50:33 +08:00
|
|
|
// This function heuristically determines whether 'Current' starts the name of a
|
|
|
|
// function declaration.
|
|
|
|
static bool isFunctionDeclarationName(const FormatToken &Current) {
|
2015-07-16 03:11:58 +08:00
|
|
|
auto skipOperatorName = [](const FormatToken* Next) -> const FormatToken* {
|
|
|
|
for (; Next; Next = Next->Next) {
|
|
|
|
if (Next->is(TT_OverloadedOperatorLParen))
|
|
|
|
return Next;
|
|
|
|
if (Next->is(TT_OverloadedOperator))
|
|
|
|
continue;
|
|
|
|
if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
|
|
|
|
// For 'new[]' and 'delete[]'.
|
|
|
|
if (Next->Next && Next->Next->is(tok::l_square) &&
|
|
|
|
Next->Next->Next && Next->Next->Next->is(tok::r_square))
|
|
|
|
Next = Next->Next->Next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:50:33 +08:00
|
|
|
break;
|
2015-07-16 03:11:58 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
const FormatToken *Next = Current.Next;
|
|
|
|
if (Current.is(tok::kw_operator)) {
|
|
|
|
if (Current.Previous && Current.Previous->is(tok::coloncolon))
|
|
|
|
return false;
|
|
|
|
Next = skipOperatorName(Next);
|
|
|
|
} else {
|
|
|
|
if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
|
2014-07-09 15:50:33 +08:00
|
|
|
return false;
|
2015-07-16 03:11:58 +08:00
|
|
|
for (; Next; Next = Next->Next) {
|
|
|
|
if (Next->is(TT_TemplateOpener)) {
|
|
|
|
Next = Next->MatchingParen;
|
|
|
|
} else if (Next->is(tok::coloncolon)) {
|
|
|
|
Next = Next->Next;
|
|
|
|
if (!Next)
|
|
|
|
return false;
|
|
|
|
if (Next->is(tok::kw_operator)) {
|
|
|
|
Next = skipOperatorName(Next->Next);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!Next->is(tok::identifier))
|
|
|
|
return false;
|
|
|
|
} else if (Next->is(tok::l_paren)) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-09 15:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-16 03:11:58 +08:00
|
|
|
|
|
|
|
if (!Next || !Next->is(tok::l_paren))
|
2014-07-09 15:50:33 +08:00
|
|
|
return false;
|
|
|
|
if (Next->Next == Next->MatchingParen)
|
|
|
|
return true;
|
2015-01-19 18:52:16 +08:00
|
|
|
for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
|
2014-07-09 15:50:33 +08:00
|
|
|
Tok = Tok->Next) {
|
|
|
|
if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
|
2014-07-09 15:50:33 +08:00
|
|
|
return true;
|
2015-04-23 21:58:40 +08:00
|
|
|
if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
|
|
|
|
Tok->Tok.isLiteral())
|
2014-07-09 15:50:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-19 06:20:15 +08:00
|
|
|
bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
|
|
|
|
assert(Line.MightBeFunctionDecl);
|
|
|
|
|
|
|
|
if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
|
|
|
|
Style.AlwaysBreakAfterReturnType ==
|
|
|
|
FormatStyle::RTBS_TopLevelDefinitions) &&
|
|
|
|
Line.Level > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (Style.AlwaysBreakAfterReturnType) {
|
|
|
|
case FormatStyle::RTBS_None:
|
|
|
|
return false;
|
|
|
|
case FormatStyle::RTBS_All:
|
|
|
|
case FormatStyle::RTBS_TopLevel:
|
|
|
|
return true;
|
|
|
|
case FormatStyle::RTBS_AllDefinitions:
|
|
|
|
case FormatStyle::RTBS_TopLevelDefinitions:
|
|
|
|
return Line.mightBeFunctionDefinition();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
|
2014-05-22 16:36:53 +08:00
|
|
|
for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
|
|
|
|
E = Line.Children.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
calculateFormattingInformation(**I);
|
|
|
|
}
|
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
Line.First->TotalLength =
|
|
|
|
Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
|
2013-05-29 22:47:47 +08:00
|
|
|
if (!Line.First->Next)
|
2013-02-06 22:22:40 +08:00
|
|
|
return;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Current = Line.First->Next;
|
2013-11-08 01:52:51 +08:00
|
|
|
bool InFunctionDecl = Line.MightBeFunctionDecl;
|
2014-05-09 16:15:10 +08:00
|
|
|
while (Current) {
|
2014-07-09 15:50:33 +08:00
|
|
|
if (isFunctionDeclarationName(*Current))
|
|
|
|
Current->Type = TT_FunctionDeclarationName;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current->is(TT_LineComment)) {
|
2014-03-10 23:06:25 +08:00
|
|
|
if (Current->Previous->BlockKind == BK_BracedInit &&
|
|
|
|
Current->Previous->opensScope())
|
2013-12-20 05:41:37 +08:00
|
|
|
Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
|
|
|
|
else
|
|
|
|
Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
|
2014-03-21 19:58:45 +08:00
|
|
|
|
|
|
|
// If we find a trailing comment, iterate backwards to determine whether
|
|
|
|
// it seems to relate to a specific parameter. If so, break before that
|
|
|
|
// parameter to avoid changing the comment's meaning. E.g. don't move 'b'
|
|
|
|
// to the previous line in:
|
|
|
|
// SomeFunction(a,
|
|
|
|
// b, // comment
|
|
|
|
// c);
|
2014-03-21 20:15:40 +08:00
|
|
|
if (!Current->HasUnescapedNewline) {
|
2014-03-21 19:58:45 +08:00
|
|
|
for (FormatToken *Parameter = Current->Previous; Parameter;
|
|
|
|
Parameter = Parameter->Previous) {
|
|
|
|
if (Parameter->isOneOf(tok::comment, tok::r_brace))
|
|
|
|
break;
|
|
|
|
if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
|
2014-03-21 19:58:45 +08:00
|
|
|
Parameter->HasUnescapedNewline)
|
|
|
|
Parameter->MustBreakBefore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 15:29:06 +08:00
|
|
|
} else if (Current->SpacesRequiredBefore == 0 &&
|
2014-05-06 22:12:21 +08:00
|
|
|
spaceRequiredBefore(Line, *Current)) {
|
2013-10-12 05:43:05 +08:00
|
|
|
Current->SpacesRequiredBefore = 1;
|
2013-12-20 05:41:37 +08:00
|
|
|
}
|
2013-02-06 22:22:40 +08:00
|
|
|
|
2013-09-17 17:52:48 +08:00
|
|
|
Current->MustBreakBefore =
|
|
|
|
Current->MustBreakBefore || mustBreakBefore(Line, *Current);
|
|
|
|
|
2015-12-19 06:20:15 +08:00
|
|
|
if (!Current->MustBreakBefore && InFunctionDecl &&
|
|
|
|
Current->is(TT_FunctionDeclarationName))
|
|
|
|
Current->MustBreakBefore = mustBreakForReturnType(Line);
|
2014-08-05 20:16:31 +08:00
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
Current->CanBreakBefore =
|
|
|
|
Current->MustBreakBefore || canBreakBefore(Line, *Current);
|
2014-05-22 16:36:53 +08:00
|
|
|
unsigned ChildSize = 0;
|
|
|
|
if (Current->Previous->Children.size() == 1) {
|
|
|
|
FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
|
|
|
|
ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
|
|
|
|
: LastOfChild.TotalLength + 1;
|
|
|
|
}
|
2015-01-10 07:25:06 +08:00
|
|
|
const FormatToken *Prev = Current->Previous;
|
2014-10-28 00:31:46 +08:00
|
|
|
if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
|
|
|
|
(Prev->Children.size() == 1 &&
|
|
|
|
Prev->Children[0]->First->MustBreakBefore) ||
|
2013-09-10 17:38:25 +08:00
|
|
|
Current->IsMultiline)
|
2014-10-28 00:31:46 +08:00
|
|
|
Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
|
2013-02-06 22:22:40 +08:00
|
|
|
else
|
2014-10-28 00:31:46 +08:00
|
|
|
Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
|
|
|
|
ChildSize + Current->SpacesRequiredBefore;
|
2013-11-08 01:52:51 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current->is(TT_CtorInitializerColon))
|
2013-11-08 01:52:51 +08:00
|
|
|
InFunctionDecl = false;
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
// FIXME: Only calculate this if CanBreakBefore is true once static
|
|
|
|
// initializers etc. are sorted out.
|
|
|
|
// FIXME: Move magic numbers to a better place.
|
2013-11-08 01:52:51 +08:00
|
|
|
Current->SplitPenalty = 20 * Current->BindingStrength +
|
|
|
|
splitPenalty(Line, *Current, InFunctionDecl);
|
2013-02-06 22:22:40 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
Current = Current->Next;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
2013-04-09 04:33:42 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
calculateUnbreakableTailLengths(Line);
|
2014-05-09 16:15:10 +08:00
|
|
|
for (Current = Line.First; Current != nullptr; Current = Current->Next) {
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
if (Current->Role)
|
|
|
|
Current->Role->precomputeFormattingInfos(Current);
|
|
|
|
}
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
DEBUG({ printDebugInfo(Line); });
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
|
|
|
|
unsigned UnbreakableTailLength = 0;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Current = Line.Last;
|
2014-05-09 16:15:10 +08:00
|
|
|
while (Current) {
|
2013-05-22 20:51:29 +08:00
|
|
|
Current->UnbreakableTailLength = UnbreakableTailLength;
|
|
|
|
if (Current->CanBreakBefore ||
|
|
|
|
Current->isOneOf(tok::comment, tok::string_literal)) {
|
|
|
|
UnbreakableTailLength = 0;
|
|
|
|
} else {
|
|
|
|
UnbreakableTailLength +=
|
2013-09-10 17:38:25 +08:00
|
|
|
Current->ColumnWidth + Current->SpacesRequiredBefore;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2013-05-29 22:47:47 +08:00
|
|
|
Current = Current->Previous;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
|
2013-11-08 01:52:51 +08:00
|
|
|
const FormatToken &Tok,
|
|
|
|
bool InFunctionDecl) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Left = *Tok.Previous;
|
|
|
|
const FormatToken &Right = Tok;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-05-28 19:30:49 +08:00
|
|
|
if (Left.is(tok::semi))
|
|
|
|
return 0;
|
2014-11-14 18:15:56 +08:00
|
|
|
|
|
|
|
if (Style.Language == FormatStyle::LK_Java) {
|
2014-11-26 20:31:19 +08:00
|
|
|
if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
|
2014-11-14 18:15:56 +08:00
|
|
|
return 1;
|
|
|
|
if (Right.is(Keywords.kw_implements))
|
|
|
|
return 2;
|
|
|
|
if (Left.is(tok::comma) && Left.NestingLevel == 0)
|
|
|
|
return 3;
|
2014-11-27 23:37:42 +08:00
|
|
|
} else if (Style.Language == FormatStyle::LK_JavaScript) {
|
2015-05-08 16:38:52 +08:00
|
|
|
if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
|
2014-11-27 23:37:42 +08:00
|
|
|
return 100;
|
2015-07-10 21:39:26 +08:00
|
|
|
if (Left.is(TT_JsTypeColon))
|
2016-01-08 18:51:24 +08:00
|
|
|
return 35;
|
2014-11-14 18:15:56 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 17:54:30 +08:00
|
|
|
if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Right.Next->is(TT_DictLiteral)))
|
2013-05-28 19:30:49 +08:00
|
|
|
return 1;
|
2014-01-19 17:04:08 +08:00
|
|
|
if (Right.is(tok::l_square)) {
|
2015-12-30 20:23:00 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Proto)
|
2014-01-19 17:04:08 +08:00
|
|
|
return 1;
|
2015-12-30 20:23:00 +08:00
|
|
|
if (Left.is(tok::r_square))
|
2016-03-01 12:19:55 +08:00
|
|
|
return 200;
|
2015-05-13 16:47:16 +08:00
|
|
|
// Slightly prefer formatting local lambda definitions like functions.
|
|
|
|
if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
|
2016-01-04 15:29:40 +08:00
|
|
|
return 35;
|
2015-06-03 16:57:36 +08:00
|
|
|
if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
|
|
|
|
TT_ArrayInitializerLSquare))
|
2014-06-10 21:27:57 +08:00
|
|
|
return 500;
|
2014-01-19 17:04:08 +08:00
|
|
|
}
|
2014-11-14 18:15:56 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
|
|
|
|
Right.is(tok::kw_operator)) {
|
2015-06-17 17:43:56 +08:00
|
|
|
if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
return 3;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_StartOfName))
|
2015-05-15 17:58:11 +08:00
|
|
|
return 110;
|
clang-format: Be more conservative about braced list column layout.
Specifically disable it for nested braced lists as it commonly can look
really weird. Eventually, we'll want to become smarter and format some of
the nested lists better.
Before:
SomeStruct my_struct_array = {
{ aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa,
aaaaaaaaaa, aaaaaaaaaa, aaaaaaa, aaa },
{ aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa },
{ aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, a, aaaaaaaaaa,
aaaaaaaaa, aaa },
};
After:
SomeStruct my_struct_array = {
{ aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaa, aaa },
{ aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa },
{ aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa },
};
llvm-svn: 196783
2013-12-09 22:40:19 +08:00
|
|
|
if (InFunctionDecl && Right.NestingLevel == 0)
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
return Style.PenaltyReturnTypeOnItsOwnLine;
|
2013-08-19 18:16:18 +08:00
|
|
|
return 200;
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
}
|
2015-03-12 23:04:53 +08:00
|
|
|
if (Right.is(TT_PointerOrReference))
|
2015-03-19 17:40:16 +08:00
|
|
|
return 190;
|
2015-06-05 21:18:09 +08:00
|
|
|
if (Right.is(TT_LambdaArrow))
|
2015-03-02 18:35:13 +08:00
|
|
|
return 110;
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Left.is(tok::equal) && Right.is(tok::l_brace))
|
|
|
|
return 150;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_CastRParen))
|
2013-07-05 15:58:34 +08:00
|
|
|
return 100;
|
2014-01-22 16:04:52 +08:00
|
|
|
if (Left.is(tok::coloncolon) ||
|
|
|
|
(Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
|
2013-01-30 05:01:14 +08:00
|
|
|
return 500;
|
2013-04-06 01:22:09 +08:00
|
|
|
if (Left.isOneOf(tok::kw_class, tok::kw_struct))
|
|
|
|
return 5000;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon))
|
2013-02-26 21:18:08 +08:00
|
|
|
return 2;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
if (Right.isMemberAccess()) {
|
2015-12-08 03:50:48 +08:00
|
|
|
// Breaking before the "./->" of a chained call/member access is reasonably
|
|
|
|
// cheap, as formatting those with one call per line is generally
|
|
|
|
// desirable. In particular, it should be cheaper to break before the call
|
|
|
|
// than it is to break inside a call's parameters, which could lead to weird
|
|
|
|
// "hanging" indents. The exception is the very last "./->" to support this
|
|
|
|
// frequent pattern:
|
|
|
|
//
|
|
|
|
// aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
|
|
|
|
// dddddddd);
|
|
|
|
//
|
|
|
|
// which might otherwise be blown up onto many lines. Here, clang-format
|
|
|
|
// won't produce "hanging" indents anyway as there is no other trailing
|
|
|
|
// call.
|
2016-01-05 21:03:50 +08:00
|
|
|
//
|
|
|
|
// Also apply higher penalty is not a call as that might lead to a wrapping
|
|
|
|
// like:
|
|
|
|
//
|
|
|
|
// aaaaaaa
|
|
|
|
// .aaaaaaaaa.bbbbbbbb(cccccccc);
|
|
|
|
return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
|
|
|
|
? 150
|
|
|
|
: 35;
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_TrailingAnnotation) &&
|
2014-06-02 17:52:08 +08:00
|
|
|
(!Right.Next || Right.Next->isNot(tok::l_paren))) {
|
2014-10-11 16:24:56 +08:00
|
|
|
// Moving trailing annotations to the next line is fine for ObjC method
|
|
|
|
// declarations.
|
2015-06-17 17:43:56 +08:00
|
|
|
if (Line.startsWith(TT_ObjCMethodSpecifier))
|
2014-10-11 16:24:56 +08:00
|
|
|
return 10;
|
2014-02-17 15:57:46 +08:00
|
|
|
// Generally, breaking before a trailing annotation is bad unless it is
|
|
|
|
// function-like. It seems to be especially preferable to keep standard
|
|
|
|
// annotations (i.e. "const", "final" and "override") on the same line.
|
2013-12-16 23:01:54 +08:00
|
|
|
// Use a slightly higher penalty after ")" so that annotations like
|
|
|
|
// "const override" are kept together.
|
2014-04-09 18:01:49 +08:00
|
|
|
bool is_short_annotation = Right.TokenText.size() < 10;
|
2014-05-09 21:11:16 +08:00
|
|
|
return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
|
2013-12-16 23:01:54 +08:00
|
|
|
}
|
2013-05-22 16:28:26 +08:00
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
// In for-loops, prefer breaking at ',' and ';'.
|
2015-06-17 17:43:56 +08:00
|
|
|
if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
return 4;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
|
|
|
// In Objective-C method expressions, prefer breaking before "param:" over
|
|
|
|
// breaking after it.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_SelectorName))
|
2013-01-30 05:01:14 +08:00
|
|
|
return 0;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
|
2013-11-23 22:27:27 +08:00
|
|
|
return Line.MightBeFunctionDecl ? 50 : 500;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2015-10-27 20:38:37 +08:00
|
|
|
if (Left.is(tok::l_paren) && InFunctionDecl &&
|
|
|
|
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
|
2013-04-11 22:29:13 +08:00
|
|
|
return 100;
|
2015-07-08 00:09:39 +08:00
|
|
|
if (Left.is(tok::l_paren) && Left.Previous &&
|
|
|
|
Left.Previous->isOneOf(tok::kw_if, tok::kw_for))
|
2015-02-26 17:49:08 +08:00
|
|
|
return 1000;
|
2013-12-27 14:39:56 +08:00
|
|
|
if (Left.is(tok::equal) && InFunctionDecl)
|
|
|
|
return 110;
|
2014-08-08 20:00:13 +08:00
|
|
|
if (Right.is(tok::r_brace))
|
|
|
|
return 1;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_TemplateOpener))
|
2014-08-15 13:00:39 +08:00
|
|
|
return 100;
|
2014-11-19 07:55:27 +08:00
|
|
|
if (Left.opensScope()) {
|
2015-10-27 20:38:37 +08:00
|
|
|
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
|
2014-11-19 07:55:27 +08:00
|
|
|
return 0;
|
2013-10-25 22:29:37 +08:00
|
|
|
return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
|
|
|
|
: 19;
|
2014-11-19 07:55:27 +08:00
|
|
|
}
|
2015-01-14 18:36:31 +08:00
|
|
|
if (Left.is(TT_JavaAnnotation))
|
|
|
|
return 50;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-02-07 05:04:05 +08:00
|
|
|
if (Right.is(tok::lessless)) {
|
2015-05-17 15:27:09 +08:00
|
|
|
if (Left.is(tok::string_literal) &&
|
2016-01-05 21:03:50 +08:00
|
|
|
(Right.NextOperator || Right.OperatorIndex != 1)) {
|
2013-06-05 22:09:10 +08:00
|
|
|
StringRef Content = Left.TokenText;
|
2013-09-29 20:02:57 +08:00
|
|
|
if (Content.startswith("\""))
|
|
|
|
Content = Content.drop_front(1);
|
|
|
|
if (Content.endswith("\""))
|
|
|
|
Content = Content.drop_back(1);
|
|
|
|
Content = Content.trim();
|
2013-03-14 22:00:17 +08:00
|
|
|
if (Content.size() > 1 &&
|
|
|
|
(Content.back() == ':' || Content.back() == '='))
|
2013-07-15 22:33:14 +08:00
|
|
|
return 25;
|
2013-02-07 05:04:05 +08:00
|
|
|
}
|
2013-07-15 23:04:42 +08:00
|
|
|
return 1; // Breaking at a << is really cheap.
|
2013-02-07 05:04:05 +08:00
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_ConditionalExpr))
|
2013-02-26 21:59:14 +08:00
|
|
|
return prec::Conditional;
|
2013-05-29 22:47:47 +08:00
|
|
|
prec::Level Level = Left.getPrecedence();
|
2015-05-06 22:23:38 +08:00
|
|
|
if (Level != prec::Unknown)
|
|
|
|
return Level;
|
|
|
|
Level = Right.getPrecedence();
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Level != prec::Unknown)
|
|
|
|
return Level;
|
2013-03-02 00:48:32 +08:00
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Left,
|
|
|
|
const FormatToken &Right) {
|
2014-05-06 22:12:21 +08:00
|
|
|
if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
|
|
|
|
return true;
|
2014-01-28 23:20:33 +08:00
|
|
|
if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
|
|
|
|
Left.Tok.getObjCKeywordID() == tok::objc_property)
|
|
|
|
return true;
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Right.is(tok::hashhash))
|
|
|
|
return Left.is(tok::hash);
|
2013-03-13 22:41:29 +08:00
|
|
|
if (Left.isOneOf(tok::hashhash, tok::hash))
|
2013-01-30 05:01:14 +08:00
|
|
|
return Right.is(tok::hash);
|
2013-08-20 20:36:34 +08:00
|
|
|
if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
|
|
|
|
return Style.SpaceInEmptyParentheses;
|
|
|
|
if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
|
2015-01-10 07:25:06 +08:00
|
|
|
return (Right.is(TT_CastRParen) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
|
2013-08-20 20:36:34 +08:00
|
|
|
? Style.SpacesInCStyleCastParentheses
|
|
|
|
: Style.SpacesInParentheses;
|
|
|
|
if (Right.isOneOf(tok::semi, tok::comma))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
|
|
|
if (Right.is(tok::less) &&
|
2015-04-16 15:02:19 +08:00
|
|
|
(Left.is(tok::kw_template) ||
|
2013-01-30 05:01:14 +08:00
|
|
|
(Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
|
|
|
|
return true;
|
2013-03-13 22:41:29 +08:00
|
|
|
if (Left.isOneOf(tok::exclaim, tok::tilde))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
|
|
|
if (Left.is(tok::at) &&
|
2013-03-13 22:41:29 +08:00
|
|
|
Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
|
|
|
|
tok::numeric_constant, tok::l_paren, tok::l_brace,
|
|
|
|
tok::kw_true, tok::kw_false))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2016-01-04 15:28:12 +08:00
|
|
|
if (Left.is(tok::colon))
|
|
|
|
return !Left.is(TT_ObjCMethodExpr);
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Left.is(tok::coloncolon))
|
|
|
|
return false;
|
2013-03-13 22:41:29 +08:00
|
|
|
if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2013-07-01 17:47:25 +08:00
|
|
|
if (Right.is(tok::ellipsis))
|
2013-10-21 00:56:16 +08:00
|
|
|
return Left.Tok.isLiteral();
|
2013-09-04 16:20:47 +08:00
|
|
|
if (Left.is(tok::l_square) && Right.is(tok::amp))
|
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_PointerOrReference))
|
2015-08-25 21:40:51 +08:00
|
|
|
return (Left.is(tok::r_paren) && Left.MatchingParen &&
|
|
|
|
(Left.MatchingParen->is(TT_OverloadedOperatorLParen) ||
|
|
|
|
(Left.MatchingParen->Previous &&
|
|
|
|
Left.MatchingParen->Previous->is(TT_FunctionDeclarationName)))) ||
|
2015-02-25 18:30:06 +08:00
|
|
|
(Left.Tok.isLiteral() ||
|
|
|
|
(!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
|
2015-03-02 02:55:26 +08:00
|
|
|
(Style.PointerAlignment != FormatStyle::PAS_Left ||
|
|
|
|
Line.IsMultiVariableDeclStmt)));
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
|
|
|
|
(!Left.is(TT_PointerOrReference) ||
|
2015-03-02 02:55:26 +08:00
|
|
|
(Style.PointerAlignment != FormatStyle::PAS_Right &&
|
|
|
|
!Line.IsMultiVariableDeclStmt)))
|
2013-05-08 22:58:20 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_PointerOrReference))
|
2015-08-24 22:28:08 +08:00
|
|
|
return Right.Tok.isLiteral() ||
|
|
|
|
Right.isOneOf(TT_BlockComment, Keywords.kw_final,
|
|
|
|
Keywords.kw_override) ||
|
2015-06-12 17:59:16 +08:00
|
|
|
(Right.is(tok::l_brace) && Right.BlockKind == BK_Block) ||
|
2015-04-23 18:23:53 +08:00
|
|
|
(!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
|
|
|
|
tok::l_paren) &&
|
2015-03-02 02:55:26 +08:00
|
|
|
(Style.PointerAlignment != FormatStyle::PAS_Right &&
|
|
|
|
!Line.IsMultiVariableDeclStmt) &&
|
|
|
|
Left.Previous &&
|
2013-05-29 22:47:47 +08:00
|
|
|
!Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Right.is(tok::star) && Left.is(tok::l_paren))
|
|
|
|
return false;
|
2013-02-10 10:08:05 +08:00
|
|
|
if (Left.is(tok::l_square))
|
2014-11-25 18:05:17 +08:00
|
|
|
return (Left.is(TT_ArrayInitializerLSquare) &&
|
2014-08-26 19:41:14 +08:00
|
|
|
Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
(Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets &&
|
|
|
|
Right.isNot(tok::r_square));
|
2013-02-10 10:08:05 +08:00
|
|
|
if (Right.is(tok::r_square))
|
2014-08-26 19:41:14 +08:00
|
|
|
return Right.MatchingParen &&
|
|
|
|
((Style.SpacesInContainerLiterals &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
|
2014-08-26 19:41:14 +08:00
|
|
|
(Style.SpacesInSquareBrackets &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
|
|
|
|
if (Right.is(tok::l_square) &&
|
|
|
|
!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
|
|
|
|
!Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2014-08-26 17:37:52 +08:00
|
|
|
if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
|
|
|
|
return !Left.Children.empty(); // No spaces in "{}".
|
|
|
|
if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
|
|
|
|
(Right.is(tok::r_brace) && Right.MatchingParen &&
|
|
|
|
Right.MatchingParen->BlockKind != BK_Block))
|
|
|
|
return !Style.Cpp11BracedListStyle;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_BlockComment))
|
2014-05-06 22:41:29 +08:00
|
|
|
return !Left.TokenText.endswith("=*/");
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Right.is(tok::l_paren)) {
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen))
|
2013-07-18 04:25:02 +08:00
|
|
|
return true;
|
2014-11-27 22:55:17 +08:00
|
|
|
return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
|
2013-12-10 18:18:34 +08:00
|
|
|
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
|
2015-08-14 20:44:06 +08:00
|
|
|
(Left.isOneOf(tok::kw_if, tok::pp_elif, tok::kw_for, tok::kw_while,
|
2015-10-07 23:09:08 +08:00
|
|
|
tok::kw_switch, tok::kw_case, TT_ForEachMacro,
|
|
|
|
TT_ObjCForIn) ||
|
2015-02-04 23:26:27 +08:00
|
|
|
(Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
|
|
|
|
tok::kw_new, tok::kw_delete) &&
|
2015-05-04 17:22:29 +08:00
|
|
|
(!Left.Previous || Left.Previous->isNot(tok::period))))) ||
|
2013-12-10 18:18:34 +08:00
|
|
|
(Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
|
2015-06-17 21:08:06 +08:00
|
|
|
(Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
|
|
|
|
Left.is(tok::r_paren)) &&
|
2013-12-10 18:18:34 +08:00
|
|
|
Line.Type != LT_PreprocessorDirective);
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
2013-05-29 22:47:47 +08:00
|
|
|
if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_UnaryOperator))
|
2013-05-24 02:05:18 +08:00
|
|
|
return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
|
2014-06-02 18:57:55 +08:00
|
|
|
if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
|
|
|
|
tok::r_paren) ||
|
2014-05-07 15:59:03 +08:00
|
|
|
Left.isSimpleTypeSpecifier()) &&
|
2013-09-04 16:20:47 +08:00
|
|
|
Right.is(tok::l_brace) && Right.getNextNonComment() &&
|
|
|
|
Right.BlockKind != BK_Block)
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2013-05-28 19:30:49 +08:00
|
|
|
if (Left.is(tok::period) || Right.is(tok::period))
|
|
|
|
return false;
|
2013-09-10 21:41:43 +08:00
|
|
|
if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
|
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
|
2014-10-21 19:13:31 +08:00
|
|
|
Left.MatchingParen->Previous &&
|
|
|
|
Left.MatchingParen->Previous->is(tok::period))
|
|
|
|
// A.<B>DoSomething();
|
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
|
2014-11-19 07:48:01 +08:00
|
|
|
return false;
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
|
2014-10-21 15:51:54 +08:00
|
|
|
const FormatToken &Right) {
|
|
|
|
const FormatToken &Left = *Right.Previous;
|
2015-06-25 16:38:46 +08:00
|
|
|
if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
|
|
|
|
return true; // Never ever merge two identifiers.
|
|
|
|
if (Style.Language == FormatStyle::LK_Cpp) {
|
|
|
|
if (Left.is(tok::kw_operator))
|
|
|
|
return Right.is(tok::coloncolon);
|
|
|
|
} else if (Style.Language == FormatStyle::LK_Proto) {
|
2014-10-21 15:51:54 +08:00
|
|
|
if (Right.is(tok::period) &&
|
2014-11-04 20:41:02 +08:00
|
|
|
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
|
2015-11-20 22:32:54 +08:00
|
|
|
Keywords.kw_repeated, Keywords.kw_extend))
|
2014-10-21 15:51:54 +08:00
|
|
|
return true;
|
|
|
|
if (Right.is(tok::l_paren) &&
|
2014-11-04 20:41:02 +08:00
|
|
|
Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
|
2014-10-21 15:51:54 +08:00
|
|
|
return true;
|
|
|
|
} else if (Style.Language == FormatStyle::LK_JavaScript) {
|
2016-03-17 20:17:59 +08:00
|
|
|
if (Left.is(TT_JsFatArrow))
|
|
|
|
return true;
|
|
|
|
if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
|
|
|
|
Keywords.kw_of) &&
|
|
|
|
(!Left.Previous || !Left.Previous->is(tok::period)))
|
2014-10-21 15:51:54 +08:00
|
|
|
return true;
|
2016-01-05 00:10:36 +08:00
|
|
|
if (Left.is(tok::kw_default) && Left.Previous &&
|
|
|
|
Left.Previous->is(tok::kw_export))
|
|
|
|
return true;
|
2015-12-30 16:00:58 +08:00
|
|
|
if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
|
|
|
|
return true;
|
2015-04-13 23:01:40 +08:00
|
|
|
if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
|
2015-02-19 01:09:53 +08:00
|
|
|
return false;
|
2016-03-22 01:57:31 +08:00
|
|
|
if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
|
|
|
|
return false;
|
2015-02-20 00:07:32 +08:00
|
|
|
if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
|
2015-02-20 00:14:18 +08:00
|
|
|
Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
|
2015-02-20 00:07:32 +08:00
|
|
|
return false;
|
2015-05-26 15:18:56 +08:00
|
|
|
if (Left.is(tok::ellipsis))
|
|
|
|
return false;
|
2015-03-15 21:59:51 +08:00
|
|
|
if (Left.is(TT_TemplateCloser) &&
|
2015-05-02 15:54:58 +08:00
|
|
|
!Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
|
2015-03-15 21:59:51 +08:00
|
|
|
Keywords.kw_implements, Keywords.kw_extends))
|
|
|
|
// Type assertions ('<type>expr') are not followed by whitespace. Other
|
|
|
|
// locations that should have whitespace following are identified by the
|
|
|
|
// above set of follower tokens.
|
|
|
|
return false;
|
2014-11-03 06:00:57 +08:00
|
|
|
} else if (Style.Language == FormatStyle::LK_Java) {
|
2014-11-24 04:54:37 +08:00
|
|
|
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
|
|
|
|
return true;
|
2014-11-04 20:41:02 +08:00
|
|
|
if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
|
2014-11-03 06:00:57 +08:00
|
|
|
return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
|
2014-11-21 20:19:07 +08:00
|
|
|
if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
|
|
|
|
tok::kw_protected) ||
|
2015-01-14 06:32:50 +08:00
|
|
|
Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
|
|
|
|
Keywords.kw_native)) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Right.is(TT_TemplateOpener))
|
2014-11-03 10:35:14 +08:00
|
|
|
return true;
|
2014-10-21 15:51:54 +08:00
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_ImplicitStringLiteral))
|
2014-10-21 15:51:54 +08:00
|
|
|
return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
|
2013-01-30 05:01:14 +08:00
|
|
|
if (Line.Type == LT_ObjCMethodDecl) {
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_ObjCMethodSpecifier))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2014-10-21 15:51:54 +08:00
|
|
|
if (Left.is(tok::r_paren) && Right.is(tok::identifier))
|
2013-01-30 05:01:14 +08:00
|
|
|
// Don't space between ')' and <id>
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Line.Type == LT_ObjCProperty &&
|
2014-10-21 15:51:54 +08:00
|
|
|
(Right.is(tok::equal) || Left.is(tok::equal)))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
|
|
|
|
2015-06-05 21:18:09 +08:00
|
|
|
if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
|
|
|
|
Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
|
2013-07-09 22:36:48 +08:00
|
|
|
return true;
|
2016-01-09 23:56:40 +08:00
|
|
|
if (Right.is(TT_OverloadedOperatorLParen))
|
|
|
|
return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
|
2014-10-21 15:51:54 +08:00
|
|
|
if (Left.is(tok::comma))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2014-10-21 15:51:54 +08:00
|
|
|
if (Right.is(tok::comma))
|
2013-02-28 21:40:17 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2015-05-05 16:40:32 +08:00
|
|
|
if (Right.is(tok::colon)) {
|
|
|
|
if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
|
2015-05-06 23:19:47 +08:00
|
|
|
!Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
|
2015-05-05 16:40:32 +08:00
|
|
|
return false;
|
|
|
|
if (Right.is(TT_ObjCMethodExpr))
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::question))
|
|
|
|
return false;
|
|
|
|
if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
|
|
|
|
return false;
|
|
|
|
if (Right.is(TT_DictLiteral))
|
|
|
|
return Style.SpacesInContainerLiterals;
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_UnaryOperator))
|
|
|
|
return Right.is(TT_BinaryOperator);
|
2015-05-15 17:05:31 +08:00
|
|
|
|
|
|
|
// If the next token is a binary operator or a selector name, we have
|
|
|
|
// incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_CastRParen))
|
2015-05-15 17:05:31 +08:00
|
|
|
return Style.SpaceAfterCStyleCast ||
|
|
|
|
Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
|
|
|
|
|
2015-06-03 06:06:07 +08:00
|
|
|
if (Left.is(tok::greater) && Right.is(tok::greater))
|
2014-11-25 18:05:17 +08:00
|
|
|
return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
|
2013-10-29 22:52:02 +08:00
|
|
|
(Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
|
2014-10-21 15:51:54 +08:00
|
|
|
if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
|
|
|
|
Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
|
2013-02-28 21:40:17 +08:00
|
|
|
return false;
|
2013-09-25 23:15:02 +08:00
|
|
|
if (!Style.SpaceBeforeAssignmentOperators &&
|
2014-10-21 15:51:54 +08:00
|
|
|
Right.getPrecedence() == prec::Assignment)
|
2013-09-25 23:15:02 +08:00
|
|
|
return false;
|
2014-10-21 17:57:09 +08:00
|
|
|
if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
|
2014-11-25 18:05:17 +08:00
|
|
|
return (Left.is(TT_TemplateOpener) &&
|
2014-10-24 04:22:22 +08:00
|
|
|
Style.Standard == FormatStyle::LS_Cpp03) ||
|
2016-01-19 22:05:32 +08:00
|
|
|
!(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren,
|
|
|
|
tok::l_square) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener));
|
|
|
|
if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
|
2014-10-21 17:57:09 +08:00
|
|
|
return Style.SpacesInAngles;
|
2014-11-25 18:05:17 +08:00
|
|
|
if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
|
2015-11-20 23:26:50 +08:00
|
|
|
(Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
|
|
|
|
!Right.is(tok::r_paren)))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2015-02-26 19:30:50 +08:00
|
|
|
if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
|
|
|
|
Right.isNot(TT_FunctionTypeLParen))
|
2014-08-06 01:58:54 +08:00
|
|
|
return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
|
|
|
|
Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
|
2014-10-20 21:56:30 +08:00
|
|
|
return false;
|
2014-10-21 15:51:54 +08:00
|
|
|
if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
|
2015-06-17 17:43:56 +08:00
|
|
|
Line.startsWith(tok::hash))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_TrailingUnaryOperator))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_RegexLiteral))
|
2014-05-08 15:01:45 +08:00
|
|
|
return false;
|
2014-10-21 15:51:54 +08:00
|
|
|
return spaceRequiredBetween(Line, Left, Right);
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:17:32 +08:00
|
|
|
// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
|
|
|
|
static bool isAllmanBrace(const FormatToken &Tok) {
|
|
|
|
return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
|
2014-11-25 18:05:17 +08:00
|
|
|
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
|
2014-06-02 18:17:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-17 17:52:48 +08:00
|
|
|
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
|
|
|
|
const FormatToken &Right) {
|
2014-03-21 20:38:57 +08:00
|
|
|
const FormatToken &Left = *Right.Previous;
|
2015-07-28 23:50:24 +08:00
|
|
|
if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
|
2014-06-04 20:40:57 +08:00
|
|
|
return true;
|
2014-11-12 03:34:57 +08:00
|
|
|
|
2015-06-17 17:44:07 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
// FIXME: This might apply to other languages and token kinds.
|
2016-03-03 06:44:03 +08:00
|
|
|
if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
|
|
|
|
Left.Previous->is(tok::string_literal))
|
2015-06-17 17:44:07 +08:00
|
|
|
return true;
|
2015-06-17 21:08:06 +08:00
|
|
|
if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
|
|
|
|
Left.Previous && Left.Previous->is(tok::equal) &&
|
|
|
|
Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
|
|
|
|
tok::kw_const) &&
|
2015-09-28 22:28:08 +08:00
|
|
|
// kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
|
|
|
|
// above.
|
|
|
|
!Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
|
2015-06-17 17:44:07 +08:00
|
|
|
// Object literals on the top level of a file are treated as "enum-style".
|
|
|
|
// Each key/value pair is put on a separate line, instead of bin-packing.
|
|
|
|
return true;
|
|
|
|
if (Left.is(tok::l_brace) && Line.Level == 0 &&
|
|
|
|
(Line.startsWith(tok::kw_enum) ||
|
|
|
|
Line.startsWith(tok::kw_export, tok::kw_enum)))
|
|
|
|
// JavaScript top-level enum key/value pairs are put on separate lines
|
|
|
|
// instead of bin-packing.
|
|
|
|
return true;
|
|
|
|
if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
|
|
|
|
!Left.Children.empty())
|
|
|
|
// Support AllowShortFunctionsOnASingleLine for JavaScript.
|
|
|
|
return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
|
2015-11-21 00:44:28 +08:00
|
|
|
Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
|
2015-06-17 17:44:07 +08:00
|
|
|
(Left.NestingLevel == 0 && Line.Level == 0 &&
|
|
|
|
Style.AllowShortFunctionsOnASingleLine ==
|
|
|
|
FormatStyle::SFS_Inline);
|
|
|
|
} else if (Style.Language == FormatStyle::LK_Java) {
|
|
|
|
if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
|
|
|
|
Right.Next->is(tok::string_literal))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:34:57 +08:00
|
|
|
// If the last token before a '}' is a comma or a trailing comment, the
|
|
|
|
// intention is to insert a line break after it in order to make shuffling
|
|
|
|
// around entries easier.
|
|
|
|
const FormatToken *BeforeClosingBrace = nullptr;
|
2015-02-27 16:41:05 +08:00
|
|
|
if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
|
|
|
|
Left.BlockKind != BK_Block && Left.MatchingParen)
|
2014-11-12 03:34:57 +08:00
|
|
|
BeforeClosingBrace = Left.MatchingParen->Previous;
|
2015-02-27 16:41:05 +08:00
|
|
|
else if (Right.MatchingParen &&
|
|
|
|
Right.MatchingParen->isOneOf(tok::l_brace,
|
|
|
|
TT_ArrayInitializerLSquare))
|
2014-11-12 03:34:57 +08:00
|
|
|
BeforeClosingBrace = &Left;
|
|
|
|
if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
|
|
|
|
BeforeClosingBrace->isTrailingComment()))
|
|
|
|
return true;
|
|
|
|
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Right.is(tok::comment))
|
2014-11-12 03:34:57 +08:00
|
|
|
return Left.BlockKind != BK_BracedInit &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Left.isNot(TT_CtorInitializerColon) &&
|
2014-04-11 20:27:47 +08:00
|
|
|
(Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
|
2015-05-11 23:15:48 +08:00
|
|
|
if (Left.isTrailingComment())
|
2015-06-17 21:08:06 +08:00
|
|
|
return true;
|
2015-05-11 23:15:48 +08:00
|
|
|
if (Left.isStringLiteral() &&
|
|
|
|
(Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral)))
|
2013-09-17 17:52:48 +08:00
|
|
|
return true;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Right.Previous->IsUnterminatedLiteral)
|
2013-09-17 17:52:48 +08:00
|
|
|
return true;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Right.is(tok::lessless) && Right.Next &&
|
|
|
|
Right.Previous->is(tok::string_literal) &&
|
|
|
|
Right.Next->is(tok::string_literal))
|
2013-09-17 17:52:48 +08:00
|
|
|
return true;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Right.Previous->ClosesTemplateDeclaration &&
|
|
|
|
Right.Previous->MatchingParen &&
|
|
|
|
Right.Previous->MatchingParen->NestingLevel == 0 &&
|
|
|
|
Style.AlwaysBreakTemplateDeclarations)
|
2013-09-17 17:52:48 +08:00
|
|
|
return true;
|
2015-01-08 21:56:57 +08:00
|
|
|
if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) &&
|
|
|
|
Style.BreakConstructorInitializersBeforeComma &&
|
|
|
|
!Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
2013-09-17 17:52:48 +08:00
|
|
|
return true;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
|
2013-12-16 15:23:08 +08:00
|
|
|
// Raw string literals are special wrt. line breaks. The author has made a
|
|
|
|
// deliberate choice and might have aligned the contents of the string
|
|
|
|
// literal accordingly. Thus, we try keep existing line breaks.
|
|
|
|
return Right.NewlinesBefore > 0;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
|
|
|
|
Style.Language == FormatStyle::LK_Proto)
|
2014-10-27 21:25:59 +08:00
|
|
|
// Don't put enums onto single lines in protocol buffers.
|
2014-01-19 17:04:08 +08:00
|
|
|
return true;
|
2015-05-10 16:42:04 +08:00
|
|
|
if (Right.is(TT_InlineASMBrace))
|
|
|
|
return Right.HasUnescapedNewline;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (isAllmanBrace(Left) || isAllmanBrace(Right))
|
2015-09-29 22:57:55 +08:00
|
|
|
return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
|
|
|
|
(Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
|
|
|
|
(Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Proto && Left.isNot(tok::l_brace) &&
|
|
|
|
Right.is(TT_SelectorName))
|
2014-07-28 22:08:09 +08:00
|
|
|
return true;
|
2015-01-08 21:56:57 +08:00
|
|
|
if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
|
|
|
|
return true;
|
2014-04-09 17:53:23 +08:00
|
|
|
|
2015-02-20 00:07:32 +08:00
|
|
|
if ((Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript) &&
|
|
|
|
Left.is(TT_LeadingJavaAnnotation) &&
|
|
|
|
Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
|
2015-10-16 00:03:01 +08:00
|
|
|
(Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
|
2015-02-20 00:07:32 +08:00
|
|
|
return true;
|
|
|
|
|
2013-09-17 17:52:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Right) {
|
|
|
|
const FormatToken &Left = *Right.Previous;
|
2014-10-17 21:36:14 +08:00
|
|
|
|
2015-06-09 19:39:22 +08:00
|
|
|
// Language-specific stuff.
|
2014-10-17 21:36:14 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java) {
|
2014-11-04 20:41:02 +08:00
|
|
|
if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
|
|
|
|
Keywords.kw_implements))
|
2014-10-17 21:36:14 +08:00
|
|
|
return false;
|
2014-11-04 20:41:02 +08:00
|
|
|
if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
|
|
|
|
Keywords.kw_implements))
|
2014-11-04 18:53:14 +08:00
|
|
|
return true;
|
2015-06-09 19:39:22 +08:00
|
|
|
} else if (Style.Language == FormatStyle::LK_JavaScript) {
|
2016-01-14 13:37:52 +08:00
|
|
|
if (Left.is(tok::kw_return))
|
|
|
|
return false; // Otherwise a semicolon is inserted.
|
2015-06-09 19:39:22 +08:00
|
|
|
if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
|
|
|
|
return false;
|
2015-07-03 18:37:23 +08:00
|
|
|
if (Left.is(TT_JsTypeColon))
|
|
|
|
return true;
|
2015-12-30 16:00:58 +08:00
|
|
|
if (Right.NestingLevel == 0 && Right.is(Keywords.kw_is))
|
|
|
|
return false;
|
2016-02-01 19:20:47 +08:00
|
|
|
if (Left.is(Keywords.kw_in))
|
|
|
|
return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
|
|
|
|
if (Right.is(Keywords.kw_in))
|
|
|
|
return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
|
2014-10-17 21:36:14 +08:00
|
|
|
}
|
|
|
|
|
2013-11-23 22:27:27 +08:00
|
|
|
if (Left.is(tok::at))
|
|
|
|
return false;
|
2014-04-28 15:34:48 +08:00
|
|
|
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
|
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
|
2015-01-14 17:51:32 +08:00
|
|
|
return !Right.is(tok::l_paren);
|
2015-03-12 23:04:53 +08:00
|
|
|
if (Right.is(TT_PointerOrReference))
|
|
|
|
return Line.IsMultiVariableDeclStmt ||
|
|
|
|
(Style.PointerAlignment == FormatStyle::PAS_Right &&
|
|
|
|
(!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
|
|
|
|
Right.is(tok::kw_operator))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2015-03-19 17:40:16 +08:00
|
|
|
if (Left.is(TT_PointerOrReference))
|
|
|
|
return false;
|
2013-11-08 08:57:11 +08:00
|
|
|
if (Right.isTrailingComment())
|
|
|
|
// We rely on MustBreakBefore being set correctly here as we should not
|
|
|
|
// change the "binding" behavior of a comment.
|
2013-12-20 05:41:37 +08:00
|
|
|
// 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
|
|
|
|
// list.
|
|
|
|
return Left.BlockKind == BK_BracedInit;
|
2013-11-08 08:57:11 +08:00
|
|
|
if (Left.is(tok::question) && Right.is(tok::colon))
|
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
|
2013-11-08 08:57:11 +08:00
|
|
|
return Style.BreakBeforeTernaryOperators;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
|
2013-11-08 08:57:11 +08:00
|
|
|
return !Style.BreakBeforeTernaryOperators;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_InheritanceColon))
|
2014-02-15 02:22:40 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(tok::colon) &&
|
|
|
|
!Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
|
2014-05-28 18:09:11 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2015-05-13 18:23:03 +08:00
|
|
|
if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
|
|
|
|
Right.Next->is(TT_ObjCMethodExpr)))
|
2015-05-19 19:06:33 +08:00
|
|
|
return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
|
2013-08-01 21:46:58 +08:00
|
|
|
if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
|
|
|
|
return true;
|
2015-08-24 23:10:01 +08:00
|
|
|
if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
|
|
|
|
TT_OverloadedOperator))
|
2013-02-14 16:42:54 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_RangeBasedForLoopColon))
|
2013-01-30 05:01:14 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_RangeBasedForLoopColon))
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
return false;
|
2015-03-12 23:04:53 +08:00
|
|
|
if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Left.is(tok::kw_operator))
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2015-04-23 20:59:09 +08:00
|
|
|
if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
|
2015-08-21 19:44:57 +08:00
|
|
|
Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
|
2013-01-30 05:01:14 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
|
2014-01-29 04:13:43 +08:00
|
|
|
return false;
|
|
|
|
if (Left.is(tok::l_paren) && Left.Previous &&
|
2015-02-26 17:49:08 +08:00
|
|
|
(Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
|
2014-01-29 04:13:43 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_ImplicitStringLiteral))
|
2013-10-30 21:54:53 +08:00
|
|
|
return false;
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
|
2013-09-05 17:29:45 +08:00
|
|
|
return false;
|
2015-07-07 21:50:50 +08:00
|
|
|
if (Right.is(tok::r_square) && Right.MatchingParen &&
|
|
|
|
Right.MatchingParen->is(TT_LambdaLSquare))
|
|
|
|
return false;
|
2013-09-05 17:29:45 +08:00
|
|
|
|
2013-05-22 16:28:26 +08:00
|
|
|
// We only break before r_brace if there was a corresponding break before
|
|
|
|
// the l_brace, which is tracked by BreakBeforeClosingBrace.
|
2013-09-05 17:29:45 +08:00
|
|
|
if (Right.is(tok::r_brace))
|
|
|
|
return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
|
2013-05-22 16:28:26 +08:00
|
|
|
|
2014-04-09 18:29:11 +08:00
|
|
|
// Allow breaking after a trailing annotation, e.g. after a method
|
|
|
|
// declaration.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_TrailingAnnotation))
|
2014-04-09 18:29:11 +08:00
|
|
|
return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
|
|
|
|
tok::less, tok::coloncolon);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-03-14 17:50:46 +08:00
|
|
|
if (Right.is(tok::kw___attribute))
|
|
|
|
return true;
|
|
|
|
|
2013-02-23 15:46:38 +08:00
|
|
|
if (Left.is(tok::identifier) && Right.is(tok::string_literal))
|
|
|
|
return true;
|
2013-07-27 00:56:36 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
|
2014-04-15 17:54:30 +08:00
|
|
|
return true;
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_CtorInitializerComma) &&
|
2013-07-27 00:56:36 +08:00
|
|
|
Style.BreakConstructorInitializersBeforeComma)
|
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_CtorInitializerComma) &&
|
2013-10-08 13:11:18 +08:00
|
|
|
Style.BreakConstructorInitializersBeforeComma)
|
|
|
|
return true;
|
2015-02-19 07:48:37 +08:00
|
|
|
if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
|
|
|
|
(Left.is(tok::less) && Right.is(tok::less)))
|
2013-09-17 16:15:46 +08:00
|
|
|
return false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Right.is(TT_BinaryOperator) &&
|
2014-09-15 19:11:00 +08:00
|
|
|
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
|
|
|
|
(Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
|
|
|
|
Right.getPrecedence() != prec::Assignment))
|
2014-05-13 16:01:47 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Left.is(TT_ArrayInitializerLSquare))
|
2013-10-22 23:30:28 +08:00
|
|
|
return true;
|
2014-08-12 21:22:26 +08:00
|
|
|
if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
|
|
|
|
return true;
|
2015-07-03 18:12:53 +08:00
|
|
|
if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
|
|
|
|
!Left.isOneOf(tok::arrowstar, tok::lessless) &&
|
2014-09-15 19:11:00 +08:00
|
|
|
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
|
|
|
|
(Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
|
|
|
|
Left.getPrecedence() == prec::Assignment))
|
|
|
|
return true;
|
|
|
|
return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
|
2013-04-06 01:22:09 +08:00
|
|
|
tok::kw_class, tok::kw_struct) ||
|
2015-03-27 02:46:28 +08:00
|
|
|
Right.isMemberAccess() ||
|
|
|
|
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
|
|
|
|
tok::colon, tok::l_square, tok::at) ||
|
2013-07-05 15:58:34 +08:00
|
|
|
(Left.is(tok::r_paren) &&
|
2014-01-29 04:13:43 +08:00
|
|
|
Right.isOneOf(tok::identifier, tok::kw_const)) ||
|
2013-10-22 23:30:28 +08:00
|
|
|
(Left.is(tok::l_paren) && !Right.is(tok::r_paren));
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2013-04-09 04:33:42 +08:00
|
|
|
void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
|
|
|
|
llvm::errs() << "AnnotatedTokens:\n";
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *Tok = Line.First;
|
2013-04-09 04:33:42 +08:00
|
|
|
while (Tok) {
|
2013-05-29 22:47:47 +08:00
|
|
|
llvm::errs() << " M=" << Tok->MustBreakBefore
|
2015-07-14 00:19:34 +08:00
|
|
|
<< " C=" << Tok->CanBreakBefore
|
|
|
|
<< " T=" << getTokenTypeName(Tok->Type)
|
2013-07-10 22:02:49 +08:00
|
|
|
<< " S=" << Tok->SpacesRequiredBefore
|
2014-06-10 22:44:02 +08:00
|
|
|
<< " B=" << Tok->BlockParameterCount
|
2013-07-10 22:02:49 +08:00
|
|
|
<< " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
|
2013-10-12 05:25:45 +08:00
|
|
|
<< " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
|
|
|
|
<< " FakeLParens=";
|
2013-04-09 04:33:42 +08:00
|
|
|
for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
|
|
|
|
llvm::errs() << Tok->FakeLParens[i] << "/";
|
|
|
|
llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
|
2014-05-09 21:11:16 +08:00
|
|
|
if (!Tok->Next)
|
2013-10-12 05:25:45 +08:00
|
|
|
assert(Tok == Line.Last);
|
2013-05-29 22:47:47 +08:00
|
|
|
Tok = Tok->Next;
|
2013-04-09 04:33:42 +08:00
|
|
|
}
|
|
|
|
llvm::errs() << "----\n";
|
|
|
|
}
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|