#ifndef LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
#define LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/OwningPtr.h"
namespace clang {
namespace format {
enum TokenType {
TT_ArrayInitializerLSquare,
TT_ArraySubscriptLSquare,
TT_BinaryOperator,
TT_BitFieldColon,
TT_BlockComment,
TT_CastRParen,
TT_ConditionalExpr,
TT_CtorInitializerColon,
TT_CtorInitializerComma,
TT_DesignatedInitializerPeriod,
TT_DictLiteral,
TT_ImplicitStringLiteral,
TT_InlineASMColon,
TT_InheritanceColon,
TT_FunctionTypeLParen,
TT_LambdaLSquare,
TT_LineComment,
TT_ObjCBlockLParen,
TT_ObjCDecl,
TT_ObjCForIn,
TT_ObjCMethodExpr,
TT_ObjCMethodSpecifier,
TT_ObjCProperty,
TT_ObjCSelectorName,
TT_OverloadedOperator,
TT_OverloadedOperatorLParen,
TT_PointerOrReference,
TT_PureVirtualSpecifier,
TT_RangeBasedForLoopColon,
TT_StartOfName,
TT_TemplateCloser,
TT_TemplateOpener,
TT_TrailingReturnArrow,
TT_TrailingUnaryOperator,
TT_UnaryOperator,
TT_Unknown
};
enum BraceBlockKind {
BK_Unknown,
BK_Block,
BK_BracedInit
};
enum ParameterPackingKind {
PPK_BinPacked,
PPK_OnePerLine,
PPK_Inconclusive
};
enum FormatDecision {
FD_Unformatted,
FD_Continue,
FD_Break
};
class TokenRole;
class AnnotatedLine;
struct FormatToken {
FormatToken()
: NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
CanBreakBefore(false), ClosesTemplateDeclaration(false),
ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0),
UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0),
LongestObjCSelectorName(0), FakeRParens(0),
StartsBinaryExpression(false), EndsBinaryExpression(false),
LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false),
MatchingParen(NULL), Previous(NULL), Next(NULL),
Decision(FD_Unformatted), Finalized(false) {}
Token Tok;
unsigned NewlinesBefore;
bool HasUnescapedNewline;
SourceRange WhitespaceRange;
unsigned LastNewlineOffset;
unsigned ColumnWidth;
unsigned LastLineColumnWidth;
bool IsMultiline;
bool IsFirst;
bool MustBreakBefore;
SourceLocation getStartOfNonWhitespace() const {
return WhitespaceRange.getEnd();
}
StringRef TokenText;
bool IsUnterminatedLiteral;
BraceBlockKind BlockKind;
TokenType Type;
unsigned SpacesRequiredBefore;
bool CanBreakBefore;
bool ClosesTemplateDeclaration;
unsigned ParameterCount;
llvm::OwningPtr<TokenRole> Role;
ParameterPackingKind PackingKind;
unsigned TotalLength;
unsigned OriginalColumn;
unsigned UnbreakableTailLength;
unsigned BindingStrength;
unsigned SplitPenalty;
unsigned LongestObjCSelectorName;
SmallVector<prec::Level, 4> FakeLParens;
unsigned FakeRParens;
bool StartsBinaryExpression;
bool EndsBinaryExpression;
bool LastInChainOfCalls;
bool PartOfMultiVariableDeclStmt;
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
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);
}
bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
return Tok.isObjCAtKeyword(Kind);
}
bool isAccessSpecifier(bool ColonRequired = true) const {
return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
(!ColonRequired || (Next && Next->is(tok::colon)));
}
bool isObjCAccessSpecifier() const {
return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
Next->isObjCAtKeyword(tok::objc_protected) ||
Next->isObjCAtKeyword(tok::objc_package) ||
Next->isObjCAtKeyword(tok::objc_private));
}
bool opensScope() const {
return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
Type == TT_TemplateOpener;
}
bool closesScope() const {
return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
Type == TT_TemplateCloser;
}
bool isMemberAccess() const {
return isOneOf(tok::arrow, tok::period) &&
Type != TT_DesignatedInitializerPeriod;
}
bool isUnaryOperator() const {
switch (Tok.getKind()) {
case tok::plus:
case tok::plusplus:
case tok::minus:
case tok::minusminus:
case tok::exclaim:
case tok::tilde:
case tok::kw_sizeof:
case tok::kw_alignof:
return true;
default:
return false;
}
}
bool isBinaryOperator() const {
return getPrecedence() > prec::Comma;
}
bool isTrailingComment() const {
return is(tok::comment) && (!Next || Next->NewlinesBefore > 0);
}
prec::Level getPrecedence() const {
return getBinOpPrecedence(Tok.getKind(), true, true);
}
FormatToken *getPreviousNonComment() const {
FormatToken *Tok = Previous;
while (Tok != NULL && Tok->is(tok::comment))
Tok = Tok->Previous;
return Tok;
}
const FormatToken *getNextNonComment() const {
const FormatToken *Tok = Next;
while (Tok != NULL && Tok->is(tok::comment))
Tok = Tok->Next;
return Tok;
}
bool opensBlockTypeList(const FormatStyle &Style) const {
return Type == TT_ArrayInitializerLSquare ||
(is(tok::l_brace) &&
(BlockKind == BK_Block || Type == TT_DictLiteral ||
!Style.Cpp11BracedListStyle));
}
bool closesBlockTypeList(const FormatStyle &Style) const {
return MatchingParen && MatchingParen->opensBlockTypeList(Style);
}
FormatToken *MatchingParen;
FormatToken *Previous;
FormatToken *Next;
SmallVector<AnnotatedLine *, 1> Children;
FormatDecision Decision;
bool Finalized;
private:
FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
};
class ContinuationIndenter;
struct LineState;
class TokenRole {
public:
TokenRole(const FormatStyle &Style) : Style(Style) {}
virtual ~TokenRole();
virtual void precomputeFormattingInfos(const FormatToken *Token);
virtual unsigned format(LineState &State, ContinuationIndenter *Indenter,
bool DryRun) {
return 0;
}
virtual void CommaFound(const FormatToken *Token) {}
protected:
const FormatStyle &Style;
};
class CommaSeparatedList : public TokenRole {
public:
CommaSeparatedList(const FormatStyle &Style) : TokenRole(Style) {}
virtual void precomputeFormattingInfos(const FormatToken *Token);
virtual unsigned format(LineState &State, ContinuationIndenter *Indenter,
bool DryRun);
virtual void CommaFound(const FormatToken *Token) { Commas.push_back(Token); }
private:
struct ColumnFormat {
unsigned Columns;
unsigned TotalWidth;
unsigned LineCount;
SmallVector<unsigned, 8> ColumnSizes;
};
const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
SmallVector<const FormatToken *, 8> Commas;
SmallVector<unsigned, 8> ItemLengths;
SmallVector<ColumnFormat, 4> Formats;
};
} }
#endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H