2013-01-30 05:01:14 +08:00
|
|
|
//===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
|
|
|
|
#define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
|
|
|
|
|
|
|
|
#include "UnwrappedLineParser.h"
|
|
|
|
#include "clang/Basic/OperatorPrecedence.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class Lexer;
|
|
|
|
class SourceManager;
|
|
|
|
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
enum TokenType {
|
|
|
|
TT_BinaryOperator,
|
|
|
|
TT_BlockComment,
|
|
|
|
TT_CastRParen,
|
|
|
|
TT_ConditionalExpr,
|
|
|
|
TT_CtorInitializerColon,
|
|
|
|
TT_ImplicitStringLiteral,
|
2013-03-14 21:45:21 +08:00
|
|
|
TT_InlineASMColon,
|
2013-02-14 16:42:54 +08:00
|
|
|
TT_InheritanceColon,
|
2013-01-30 05:01:14 +08:00
|
|
|
TT_LineComment,
|
2013-02-10 10:08:05 +08:00
|
|
|
TT_ObjCArrayLiteral,
|
2013-01-30 05:01:14 +08:00
|
|
|
TT_ObjCBlockLParen,
|
|
|
|
TT_ObjCDecl,
|
2013-02-11 23:32:15 +08:00
|
|
|
TT_ObjCForIn,
|
2013-01-30 05:01:14 +08:00
|
|
|
TT_ObjCMethodExpr,
|
2013-02-10 10:08:05 +08:00
|
|
|
TT_ObjCMethodSpecifier,
|
2013-01-30 05:01:14 +08:00
|
|
|
TT_ObjCProperty,
|
2013-02-05 18:07:47 +08:00
|
|
|
TT_ObjCSelectorName,
|
2013-05-10 15:59:58 +08:00
|
|
|
TT_OverloadedOperator,
|
2013-02-11 16:01:18 +08:00
|
|
|
TT_OverloadedOperatorLParen,
|
2013-01-30 05:01:14 +08:00
|
|
|
TT_PointerOrReference,
|
|
|
|
TT_PureVirtualSpecifier,
|
|
|
|
TT_RangeBasedForLoopColon,
|
|
|
|
TT_StartOfName,
|
|
|
|
TT_TemplateCloser,
|
|
|
|
TT_TemplateOpener,
|
|
|
|
TT_TrailingUnaryOperator,
|
|
|
|
TT_UnaryOperator,
|
|
|
|
TT_Unknown
|
|
|
|
};
|
|
|
|
|
|
|
|
enum LineType {
|
|
|
|
LT_Invalid,
|
|
|
|
LT_Other,
|
|
|
|
LT_BuilderTypeCall,
|
|
|
|
LT_PreprocessorDirective,
|
|
|
|
LT_VirtualFunctionDecl,
|
|
|
|
LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
|
|
|
|
LT_ObjCMethodDecl,
|
|
|
|
LT_ObjCProperty // An @property line.
|
|
|
|
};
|
|
|
|
|
|
|
|
class AnnotatedToken {
|
|
|
|
public:
|
|
|
|
explicit AnnotatedToken(const FormatToken &FormatTok)
|
2013-02-11 20:36:37 +08:00
|
|
|
: FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0),
|
2013-01-30 05:01:14 +08:00
|
|
|
CanBreakBefore(false), MustBreakBefore(false),
|
|
|
|
ClosesTemplateDeclaration(false), MatchingParen(NULL),
|
2013-05-14 18:44:17 +08:00
|
|
|
ParameterCount(0), TotalLength(FormatTok.TokenLength),
|
|
|
|
BindingStrength(0), SplitPenalty(0), LongestObjCSelectorName(0),
|
|
|
|
DefinesFunctionType(false), Parent(NULL), FakeRParens(0),
|
2013-05-22 13:27:42 +08:00
|
|
|
LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false) {}
|
2013-01-30 05:01:14 +08:00
|
|
|
|
|
|
|
bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
|
2013-03-13 22:41:29 +08:00
|
|
|
|
|
|
|
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
|
|
|
|
return is(K1) || is(K2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
|
|
|
|
return is(K1) || is(K2) || is(K3);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOneOf(
|
|
|
|
tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
|
|
|
|
tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
|
|
|
|
tok::TokenKind K6 = tok::NUM_TOKENS, tok::TokenKind K7 = tok::NUM_TOKENS,
|
|
|
|
tok::TokenKind K8 = tok::NUM_TOKENS, tok::TokenKind K9 = tok::NUM_TOKENS,
|
|
|
|
tok::TokenKind K10 = tok::NUM_TOKENS,
|
|
|
|
tok::TokenKind K11 = tok::NUM_TOKENS,
|
|
|
|
tok::TokenKind K12 = tok::NUM_TOKENS) const {
|
|
|
|
return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
|
|
|
|
is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
|
|
|
|
}
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
|
|
|
|
|
|
|
|
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
|
|
|
|
return FormatTok.Tok.isObjCAtKeyword(Kind);
|
|
|
|
}
|
|
|
|
|
2013-03-28 01:08:02 +08:00
|
|
|
bool isAccessSpecifier(bool ColonRequired = true) const {
|
|
|
|
return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
|
|
|
|
(!ColonRequired ||
|
|
|
|
(!Children.empty() && Children[0].is(tok::colon)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isObjCAccessSpecifier() const {
|
|
|
|
return is(tok::at) && !Children.empty() &&
|
|
|
|
(Children[0].isObjCAtKeyword(tok::objc_public) ||
|
|
|
|
Children[0].isObjCAtKeyword(tok::objc_protected) ||
|
|
|
|
Children[0].isObjCAtKeyword(tok::objc_package) ||
|
|
|
|
Children[0].isObjCAtKeyword(tok::objc_private));
|
|
|
|
}
|
|
|
|
|
2013-04-10 17:49:49 +08:00
|
|
|
/// \brief Returns whether \p Tok is ([{ or a template opening <.
|
|
|
|
bool opensScope() const;
|
|
|
|
/// \brief Returns whether \p Tok is )]} or a template opening >.
|
|
|
|
bool closesScope() const;
|
|
|
|
|
|
|
|
bool isUnaryOperator() const;
|
|
|
|
bool isBinaryOperator() const;
|
|
|
|
bool isTrailingComment() const;
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
FormatToken FormatTok;
|
|
|
|
|
|
|
|
TokenType Type;
|
|
|
|
|
2013-02-11 20:36:37 +08:00
|
|
|
unsigned SpacesRequiredBefore;
|
2013-01-30 05:01:14 +08:00
|
|
|
bool CanBreakBefore;
|
|
|
|
bool MustBreakBefore;
|
|
|
|
|
|
|
|
bool ClosesTemplateDeclaration;
|
|
|
|
|
|
|
|
AnnotatedToken *MatchingParen;
|
|
|
|
|
|
|
|
/// \brief Number of parameters, if this is "(", "[" or "<".
|
|
|
|
///
|
|
|
|
/// This is initialized to 1 as we don't need to distinguish functions with
|
|
|
|
/// 0 parameters from functions with 1 parameter. Thus, we can simply count
|
|
|
|
/// the number of commas.
|
|
|
|
unsigned ParameterCount;
|
|
|
|
|
|
|
|
/// \brief The total length of the line up to and including this token.
|
|
|
|
unsigned TotalLength;
|
|
|
|
|
2013-02-04 15:21:18 +08:00
|
|
|
// FIXME: Come up with a 'cleaner' concept.
|
|
|
|
/// \brief The binding strength of a token. This is a combined value of
|
|
|
|
/// operator precedence, parenthesis nesting, etc.
|
|
|
|
unsigned BindingStrength;
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
/// \brief Penalty for inserting a line break before this token.
|
|
|
|
unsigned SplitPenalty;
|
|
|
|
|
2013-02-05 18:07:47 +08:00
|
|
|
/// \brief If this is the first ObjC selector name in an ObjC method
|
|
|
|
/// definition or call, this contains the length of the longest name.
|
|
|
|
unsigned LongestObjCSelectorName;
|
|
|
|
|
2013-05-08 22:58:20 +08:00
|
|
|
/// \brief \c true if this is a "(" that starts a function type definition.
|
|
|
|
bool DefinesFunctionType;
|
|
|
|
|
2013-01-30 05:01:14 +08:00
|
|
|
std::vector<AnnotatedToken> Children;
|
|
|
|
AnnotatedToken *Parent;
|
|
|
|
|
2013-04-09 04:33:42 +08:00
|
|
|
/// \brief Stores the number of required fake parentheses and the
|
|
|
|
/// corresponding operator precedence.
|
|
|
|
///
|
|
|
|
/// If multiple fake parentheses start at a token, this vector stores them in
|
|
|
|
/// reverse order, i.e. inner fake parenthesis first.
|
|
|
|
SmallVector<prec::Level, 4> FakeLParens;
|
2013-02-09 00:49:27 +08:00
|
|
|
/// \brief Insert this many fake ) after this token for correct indentation.
|
2013-02-08 23:28:42 +08:00
|
|
|
unsigned FakeRParens;
|
|
|
|
|
2013-03-02 00:48:32 +08:00
|
|
|
/// \brief Is this the last "." or "->" in a builder-type call?
|
|
|
|
bool LastInChainOfCalls;
|
|
|
|
|
2013-04-03 21:36:17 +08:00
|
|
|
/// \brief Is this token part of a \c DeclStmt defining multiple variables?
|
|
|
|
///
|
|
|
|
/// Only set if \c Type == \c TT_StartOfName.
|
|
|
|
bool PartOfMultiVariableDeclStmt;
|
|
|
|
|
2013-04-10 17:49:49 +08:00
|
|
|
/// \brief Returns the previous token ignoring comments.
|
|
|
|
AnnotatedToken *getPreviousNoneComment() const;
|
|
|
|
|
|
|
|
/// \brief Returns the next token ignoring comments.
|
|
|
|
const AnnotatedToken *getNextNoneComment() const;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class AnnotatedLine {
|
|
|
|
public:
|
|
|
|
AnnotatedLine(const UnwrappedLine &Line)
|
|
|
|
: First(Line.Tokens.front()), Level(Line.Level),
|
|
|
|
InPPDirective(Line.InPPDirective),
|
2013-05-06 16:27:33 +08:00
|
|
|
MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
|
|
|
|
StartsDefinition(false) {
|
2013-01-30 05:01:14 +08:00
|
|
|
assert(!Line.Tokens.empty());
|
|
|
|
AnnotatedToken *Current = &First;
|
|
|
|
for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
|
|
|
|
E = Line.Tokens.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Current->Children.push_back(AnnotatedToken(*I));
|
|
|
|
Current->Children[0].Parent = Current;
|
|
|
|
Current = &Current->Children[0];
|
|
|
|
}
|
|
|
|
Last = Current;
|
|
|
|
}
|
|
|
|
AnnotatedLine(const AnnotatedLine &Other)
|
|
|
|
: First(Other.First), Type(Other.Type), Level(Other.Level),
|
|
|
|
InPPDirective(Other.InPPDirective),
|
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
|
|
|
MustBeDeclaration(Other.MustBeDeclaration),
|
2013-05-06 16:27:33 +08:00
|
|
|
MightBeFunctionDecl(Other.MightBeFunctionDecl),
|
|
|
|
StartsDefinition(Other.StartsDefinition) {
|
2013-01-30 05:01:14 +08:00
|
|
|
Last = &First;
|
|
|
|
while (!Last->Children.empty()) {
|
|
|
|
Last->Children[0].Parent = Last;
|
|
|
|
Last = &Last->Children[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnotatedToken First;
|
|
|
|
AnnotatedToken *Last;
|
|
|
|
|
|
|
|
LineType Type;
|
|
|
|
unsigned Level;
|
|
|
|
bool InPPDirective;
|
|
|
|
bool MustBeDeclaration;
|
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
|
|
|
bool MightBeFunctionDecl;
|
2013-05-06 16:27:33 +08:00
|
|
|
bool StartsDefinition;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
|
|
|
|
return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determines extra information about the tokens comprising an
|
|
|
|
/// \c UnwrappedLine.
|
|
|
|
class TokenAnnotator {
|
|
|
|
public:
|
2013-02-11 23:32:15 +08:00
|
|
|
TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
|
|
|
|
IdentifierInfo &Ident_in)
|
|
|
|
: Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
|
2013-01-30 05:01:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
void annotate(AnnotatedLine &Line);
|
|
|
|
void calculateFormattingInformation(AnnotatedLine &Line);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief Calculate the penalty for splitting before \c Tok.
|
2013-02-06 22:22:40 +08:00
|
|
|
unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
bool spaceRequiredBetween(const AnnotatedLine &Line,
|
|
|
|
const AnnotatedToken &Left,
|
2013-01-30 05:01:14 +08:00
|
|
|
const AnnotatedToken &Right);
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
bool spaceRequiredBefore(const AnnotatedLine &Line,
|
|
|
|
const AnnotatedToken &Tok);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
|
2013-01-30 05:01:14 +08:00
|
|
|
|
2013-04-09 04:33:42 +08:00
|
|
|
void printDebugInfo(const AnnotatedLine &Line);
|
|
|
|
|
2013-02-06 22:22:40 +08:00
|
|
|
const FormatStyle &Style;
|
2013-01-30 05:01:14 +08:00
|
|
|
SourceManager &SourceMgr;
|
|
|
|
Lexer &Lex;
|
2013-02-11 23:32:15 +08:00
|
|
|
|
|
|
|
// Contextual keywords:
|
|
|
|
IdentifierInfo &Ident_in;
|
2013-01-30 05:01:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
|