2013-06-04 00:45:03 +08:00
|
|
|
//===--- FormatToken.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 contains the declaration of the FormatToken, a wrapper
|
|
|
|
/// around Token with additional information related to formatting.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
|
|
|
|
#define LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
|
|
|
|
|
|
|
|
#include "clang/Basic/OperatorPrecedence.h"
|
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
|
|
|
#include "clang/Format/Format.h"
|
2013-06-04 00:45:03 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2013-06-04 00:45:03 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
enum TokenType {
|
2013-10-22 23:30:28 +08:00
|
|
|
TT_ArrayInitializerLSquare,
|
|
|
|
TT_ArraySubscriptLSquare,
|
2014-01-29 04:13:43 +08:00
|
|
|
TT_AttributeParen,
|
2013-06-04 00:45:03 +08:00
|
|
|
TT_BinaryOperator,
|
2013-10-10 21:36:20 +08:00
|
|
|
TT_BitFieldColon,
|
2013-06-04 00:45:03 +08:00
|
|
|
TT_BlockComment,
|
|
|
|
TT_CastRParen,
|
|
|
|
TT_ConditionalExpr,
|
|
|
|
TT_CtorInitializerColon,
|
2013-07-27 00:56:36 +08:00
|
|
|
TT_CtorInitializerComma,
|
2013-06-04 00:45:03 +08:00
|
|
|
TT_DesignatedInitializerPeriod,
|
2013-10-24 18:31:50 +08:00
|
|
|
TT_DictLiteral,
|
2013-06-04 00:45:03 +08:00
|
|
|
TT_ImplicitStringLiteral,
|
|
|
|
TT_InlineASMColon,
|
|
|
|
TT_InheritanceColon,
|
2013-11-21 00:33:05 +08:00
|
|
|
TT_FunctionLBrace,
|
2013-06-04 00:45:03 +08:00
|
|
|
TT_FunctionTypeLParen,
|
2013-09-05 17:29:45 +08:00
|
|
|
TT_LambdaLSquare,
|
2013-06-04 00:45:03 +08:00
|
|
|
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,
|
2013-12-16 23:01:54 +08:00
|
|
|
TT_TrailingAnnotation,
|
2013-07-09 22:36:48 +08:00
|
|
|
TT_TrailingReturnArrow,
|
2013-06-04 00:45:03 +08:00
|
|
|
TT_TrailingUnaryOperator,
|
|
|
|
TT_UnaryOperator,
|
|
|
|
TT_Unknown
|
|
|
|
};
|
|
|
|
|
2013-07-09 17:06:29 +08:00
|
|
|
// Represents what type of block a set of braces open.
|
|
|
|
enum BraceBlockKind {
|
|
|
|
BK_Unknown,
|
|
|
|
BK_Block,
|
|
|
|
BK_BracedInit
|
|
|
|
};
|
|
|
|
|
2013-07-10 22:02:49 +08:00
|
|
|
// The packing kind of a function's parameters.
|
|
|
|
enum ParameterPackingKind {
|
|
|
|
PPK_BinPacked,
|
|
|
|
PPK_OnePerLine,
|
|
|
|
PPK_Inconclusive
|
|
|
|
};
|
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
enum FormatDecision {
|
|
|
|
FD_Unformatted,
|
|
|
|
FD_Continue,
|
|
|
|
FD_Break
|
|
|
|
};
|
|
|
|
|
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
|
|
|
class TokenRole;
|
2013-09-05 17:29:45 +08:00
|
|
|
class AnnotatedLine;
|
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
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
/// \brief A wrapper around a \c Token storing information about the
|
2013-12-06 00:25:25 +08:00
|
|
|
/// whitespace characters preceding it.
|
2013-06-04 00:45:03 +08:00
|
|
|
struct FormatToken {
|
|
|
|
FormatToken()
|
2013-09-02 21:58:14 +08:00
|
|
|
: NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
|
2013-09-10 17:38:25 +08:00
|
|
|
ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
|
2013-09-02 21:58:14 +08:00
|
|
|
IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
|
2013-08-30 01:32:57 +08:00
|
|
|
BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
|
|
|
|
CanBreakBefore(false), ClosesTemplateDeclaration(false),
|
|
|
|
ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0),
|
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
|
|
|
UnbreakableTailLength(0), BindingStrength(0), NestingLevel(0),
|
|
|
|
SplitPenalty(0), LongestObjCSelectorName(0), FakeRParens(0),
|
2013-09-06 16:08:14 +08:00
|
|
|
StartsBinaryExpression(false), EndsBinaryExpression(false),
|
|
|
|
LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false),
|
2013-10-12 05:25:45 +08:00
|
|
|
MatchingParen(NULL), Previous(NULL), Next(NULL),
|
|
|
|
Decision(FD_Unformatted), Finalized(false) {}
|
2013-06-04 00:45:03 +08:00
|
|
|
|
|
|
|
/// \brief The \c Token.
|
|
|
|
Token Tok;
|
|
|
|
|
|
|
|
/// \brief The number of newlines immediately before the \c Token.
|
|
|
|
///
|
|
|
|
/// This can be used to determine what the user wrote in the original code
|
|
|
|
/// and thereby e.g. leave an empty line between two function definitions.
|
|
|
|
unsigned NewlinesBefore;
|
|
|
|
|
|
|
|
/// \brief Whether there is at least one unescaped newline before the \c
|
|
|
|
/// Token.
|
|
|
|
bool HasUnescapedNewline;
|
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
/// \brief The range of the whitespace immediately preceding the \c Token.
|
2013-06-04 00:45:03 +08:00
|
|
|
SourceRange WhitespaceRange;
|
|
|
|
|
|
|
|
/// \brief The offset just past the last '\n' in this token's leading
|
|
|
|
/// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
|
|
|
|
unsigned LastNewlineOffset;
|
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
/// \brief The width of the non-whitespace parts of the token (or its first
|
|
|
|
/// line for multi-line tokens) in columns.
|
2013-06-05 22:09:10 +08:00
|
|
|
/// We need this to correctly measure number of columns a token spans.
|
2013-09-10 17:38:25 +08:00
|
|
|
unsigned ColumnWidth;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
/// \brief Contains the width in columns of the last line of a multi-line
|
2013-09-02 21:58:14 +08:00
|
|
|
/// token.
|
2013-09-05 22:08:34 +08:00
|
|
|
unsigned LastLineColumnWidth;
|
2013-09-02 21:58:14 +08:00
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
/// \brief Whether the token text contains newlines (escaped or not).
|
|
|
|
bool IsMultiline;
|
2013-09-02 21:58:14 +08:00
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
/// \brief Indicates that this is the first token.
|
|
|
|
bool IsFirst;
|
|
|
|
|
|
|
|
/// \brief Whether there must be a line break before this token.
|
|
|
|
///
|
|
|
|
/// This happens for example when a preprocessor directive ended directly
|
|
|
|
/// before the token.
|
|
|
|
bool MustBreakBefore;
|
|
|
|
|
|
|
|
/// \brief Returns actual token start location without leading escaped
|
|
|
|
/// newlines and whitespace.
|
|
|
|
///
|
|
|
|
/// This can be different to Tok.getLocation(), which includes leading escaped
|
|
|
|
/// newlines.
|
|
|
|
SourceLocation getStartOfNonWhitespace() const {
|
|
|
|
return WhitespaceRange.getEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief The raw text of the token.
|
|
|
|
///
|
|
|
|
/// Contains the raw token text without leading whitespace and without leading
|
|
|
|
/// escaped newlines.
|
|
|
|
StringRef TokenText;
|
|
|
|
|
2013-07-17 04:28:33 +08:00
|
|
|
/// \brief Set to \c true if this token is an unterminated literal.
|
|
|
|
bool IsUnterminatedLiteral;
|
|
|
|
|
2013-07-09 17:06:29 +08:00
|
|
|
/// \brief Contains the kind of block if this token is a brace.
|
|
|
|
BraceBlockKind BlockKind;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
TokenType Type;
|
|
|
|
|
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
|
|
|
/// \brief The number of spaces that should be inserted before this token.
|
2013-06-04 00:45:03 +08:00
|
|
|
unsigned SpacesRequiredBefore;
|
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
|
|
|
|
|
|
|
/// \brief \c true if it is allowed to break before this token.
|
2013-06-04 00:45:03 +08:00
|
|
|
bool CanBreakBefore;
|
|
|
|
|
|
|
|
bool ClosesTemplateDeclaration;
|
|
|
|
|
|
|
|
/// \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;
|
|
|
|
|
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
|
|
|
/// \brief A token can have a special role that can carry extra information
|
|
|
|
/// about the token's formatting.
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<TokenRole> Role;
|
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
|
|
|
|
2013-07-10 22:02:49 +08:00
|
|
|
/// \brief If this is an opening parenthesis, how are the parameters packed?
|
|
|
|
ParameterPackingKind PackingKind;
|
|
|
|
|
2013-08-29 23:21:40 +08:00
|
|
|
/// \brief The total length of the unwrapped line up to and including this
|
|
|
|
/// token.
|
2013-06-04 00:45:03 +08:00
|
|
|
unsigned TotalLength;
|
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
/// \brief The original 0-based column of this token, including expanded tabs.
|
|
|
|
/// The configured TabWidth is used as tab width.
|
2013-08-29 23:21:40 +08:00
|
|
|
unsigned OriginalColumn;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
/// \brief The length of following tokens until the next natural split point,
|
|
|
|
/// or the next token that can be broken.
|
|
|
|
unsigned UnbreakableTailLength;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
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
|
|
|
/// \brief The nesting level of this token, i.e. the number of surrounding (),
|
|
|
|
/// [], {} or <>.
|
|
|
|
unsigned NestingLevel;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
/// \brief Penalty for inserting a line break before this token.
|
|
|
|
unsigned SplitPenalty;
|
|
|
|
|
|
|
|
/// \brief If this is the first ObjC selector name in an ObjC method
|
|
|
|
/// definition or call, this contains the length of the longest name.
|
2013-12-23 15:29:06 +08:00
|
|
|
///
|
|
|
|
/// This being set to 0 means that the selectors should not be colon-aligned,
|
|
|
|
/// e.g. because several of them are block-type.
|
2013-06-04 00:45:03 +08:00
|
|
|
unsigned LongestObjCSelectorName;
|
|
|
|
|
|
|
|
/// \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;
|
|
|
|
/// \brief Insert this many fake ) after this token for correct indentation.
|
|
|
|
unsigned FakeRParens;
|
|
|
|
|
2013-09-06 16:08:14 +08:00
|
|
|
/// \brief \c true if this token starts a binary expression, i.e. has at least
|
|
|
|
/// one fake l_paren with a precedence greater than prec::Unknown.
|
|
|
|
bool StartsBinaryExpression;
|
|
|
|
/// \brief \c true if this token ends a binary expression.
|
|
|
|
bool EndsBinaryExpression;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
/// \brief Is this the last "." or "->" in a builder-type call?
|
|
|
|
bool LastInChainOfCalls;
|
|
|
|
|
|
|
|
/// \brief Is this token part of a \c DeclStmt defining multiple variables?
|
|
|
|
///
|
|
|
|
/// Only set if \c Type == \c TT_StartOfName.
|
|
|
|
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); }
|
2013-12-20 14:22:01 +08:00
|
|
|
bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
|
2013-06-04 00:45:03 +08:00
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2014-01-16 17:11:55 +08:00
|
|
|
/// \brief Determine whether the token is a simple-type-specifier.
|
|
|
|
bool isSimpleTypeSpecifier() const;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns whether \p Tok is ([{ or a template opening <.
|
|
|
|
bool opensScope() const {
|
|
|
|
return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
|
|
|
|
Type == TT_TemplateOpener;
|
|
|
|
}
|
2013-06-26 03:25:12 +08:00
|
|
|
/// \brief Returns whether \p Tok is )]} or a template closing >.
|
2013-06-04 00:45:03 +08:00
|
|
|
bool closesScope() const {
|
|
|
|
return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
|
|
|
|
Type == TT_TemplateCloser;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/// \brief Returns \c true if this is a "." or "->" accessing a member.
|
|
|
|
bool isMemberAccess() const {
|
|
|
|
return isOneOf(tok::arrow, tok::period) &&
|
|
|
|
Type != TT_DesignatedInitializerPeriod;
|
|
|
|
}
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
bool isBinaryOperator() const {
|
|
|
|
// Comma is a binary operator, but does not behave as such wrt. formatting.
|
|
|
|
return getPrecedence() > prec::Comma;
|
|
|
|
}
|
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
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
bool isTrailingComment() const {
|
|
|
|
return is(tok::comment) && (!Next || Next->NewlinesBefore > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
prec::Level getPrecedence() const {
|
|
|
|
return getBinOpPrecedence(Tok.getKind(), true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns the previous token ignoring comments.
|
2013-07-04 22:47:51 +08:00
|
|
|
FormatToken *getPreviousNonComment() const {
|
2013-06-04 00:45:03 +08:00
|
|
|
FormatToken *Tok = Previous;
|
|
|
|
while (Tok != NULL && Tok->is(tok::comment))
|
|
|
|
Tok = Tok->Previous;
|
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns the next token ignoring comments.
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *getNextNonComment() const {
|
2013-06-04 00:45:03 +08:00
|
|
|
const FormatToken *Tok = Next;
|
|
|
|
while (Tok != NULL && Tok->is(tok::comment))
|
|
|
|
Tok = Tok->Next;
|
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
2013-10-22 23:45:58 +08:00
|
|
|
/// \brief Returns \c true if this tokens starts a block-type list, i.e. a
|
|
|
|
/// list that should be indented with a block indent.
|
|
|
|
bool opensBlockTypeList(const FormatStyle &Style) const {
|
|
|
|
return Type == TT_ArrayInitializerLSquare ||
|
|
|
|
(is(tok::l_brace) &&
|
2013-10-24 18:31:50 +08:00
|
|
|
(BlockKind == BK_Block || Type == TT_DictLiteral ||
|
2013-10-22 23:45:58 +08:00
|
|
|
!Style.Cpp11BracedListStyle));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Same as opensBlockTypeList, but for the closing token.
|
2013-10-22 23:30:28 +08:00
|
|
|
bool closesBlockTypeList(const FormatStyle &Style) const {
|
2013-10-22 23:45:58 +08:00
|
|
|
return MatchingParen && MatchingParen->opensBlockTypeList(Style);
|
2013-10-22 23:30:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
FormatToken *MatchingParen;
|
|
|
|
|
|
|
|
FormatToken *Previous;
|
|
|
|
FormatToken *Next;
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
SmallVector<AnnotatedLine *, 1> Children;
|
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
/// \brief Stores the formatting decision for the token once it was made.
|
|
|
|
FormatDecision Decision;
|
|
|
|
|
|
|
|
/// \brief If \c true, this token has been fully formatted (indented and
|
|
|
|
/// potentially re-formatted inside), and we do not allow further formatting
|
|
|
|
/// changes.
|
|
|
|
bool Finalized;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
private:
|
|
|
|
// Disallow copying.
|
2013-07-01 12:07:34 +08:00
|
|
|
FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
|
2013-06-04 00:45:03 +08:00
|
|
|
};
|
|
|
|
|
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
|
|
|
class ContinuationIndenter;
|
|
|
|
struct LineState;
|
|
|
|
|
|
|
|
class TokenRole {
|
|
|
|
public:
|
|
|
|
TokenRole(const FormatStyle &Style) : Style(Style) {}
|
|
|
|
virtual ~TokenRole();
|
|
|
|
|
|
|
|
/// \brief After the \c TokenAnnotator has finished annotating all the tokens,
|
|
|
|
/// this function precomputes required information for formatting.
|
|
|
|
virtual void precomputeFormattingInfos(const FormatToken *Token);
|
|
|
|
|
|
|
|
/// \brief Apply the special formatting that the given role demands.
|
|
|
|
///
|
2014-01-09 21:42:56 +08:00
|
|
|
/// Assumes that the token having this role is already formatted.
|
|
|
|
///
|
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
|
|
|
/// Continues formatting from \p State leaving indentation to \p Indenter and
|
|
|
|
/// returns the total penalty that this formatting incurs.
|
2014-01-09 21:42:56 +08:00
|
|
|
virtual unsigned formatFromToken(LineState &State,
|
|
|
|
ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Same as \c formatFromToken, but assumes that the first token has
|
|
|
|
/// already been set thereby deciding on the first line break.
|
|
|
|
virtual unsigned formatAfterToken(LineState &State,
|
|
|
|
ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun) {
|
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
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Notifies the \c Role that a comma was found.
|
|
|
|
virtual void CommaFound(const FormatToken *Token) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const FormatStyle &Style;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CommaSeparatedList : public TokenRole {
|
|
|
|
public:
|
2014-01-09 21:42:56 +08:00
|
|
|
CommaSeparatedList(const FormatStyle &Style)
|
|
|
|
: TokenRole(Style), HasNestedBracedList(false) {}
|
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
|
|
|
|
|
|
|
virtual void precomputeFormattingInfos(const FormatToken *Token);
|
|
|
|
|
2014-01-09 21:42:56 +08:00
|
|
|
virtual unsigned formatAfterToken(LineState &State,
|
|
|
|
ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun);
|
|
|
|
|
|
|
|
virtual unsigned formatFromToken(LineState &State,
|
|
|
|
ContinuationIndenter *Indenter, bool DryRun);
|
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
|
|
|
|
|
|
|
/// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
|
|
|
|
virtual void CommaFound(const FormatToken *Token) { Commas.push_back(Token); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief A struct that holds information on how to format a given list with
|
|
|
|
/// a specific number of columns.
|
|
|
|
struct ColumnFormat {
|
|
|
|
/// \brief The number of columns to use.
|
|
|
|
unsigned Columns;
|
|
|
|
|
|
|
|
/// \brief The total width in characters.
|
|
|
|
unsigned TotalWidth;
|
|
|
|
|
|
|
|
/// \brief The number of lines required for this format.
|
|
|
|
unsigned LineCount;
|
|
|
|
|
|
|
|
/// \brief The size of each column in characters.
|
|
|
|
SmallVector<unsigned, 8> ColumnSizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Calculate which \c ColumnFormat fits best into
|
|
|
|
/// \p RemainingCharacters.
|
|
|
|
const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
|
|
|
|
|
|
|
|
/// \brief The ordered \c FormatTokens making up the commas of this list.
|
|
|
|
SmallVector<const FormatToken *, 8> Commas;
|
|
|
|
|
|
|
|
/// \brief The length of each of the list's items in characters including the
|
|
|
|
/// trailing comma.
|
|
|
|
SmallVector<unsigned, 8> ItemLengths;
|
|
|
|
|
|
|
|
/// \brief Precomputed formats that can be used for this list.
|
|
|
|
SmallVector<ColumnFormat, 4> Formats;
|
2014-01-09 21:42:56 +08:00
|
|
|
|
|
|
|
bool HasNestedBracedList;
|
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
|
|
|
};
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
|