2013-06-04 00:45:03 +08:00
|
|
|
//===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-06-04 00:45:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file contains the declaration of the FormatToken, a wrapper
|
2013-06-04 00:45:03 +08:00
|
|
|
/// around Token with additional information related to formatting.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
|
|
|
|
#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2014-11-04 20:41:02 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2013-06-04 00:45:03 +08:00
|
|
|
#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>
|
2017-07-04 23:30:21 +08:00
|
|
|
#include <unordered_set>
|
2013-06-04 00:45:03 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2017-09-20 17:51:03 +08:00
|
|
|
#define LIST_TOKEN_TYPES \
|
|
|
|
TYPE(ArrayInitializerLSquare) \
|
|
|
|
TYPE(ArraySubscriptLSquare) \
|
2018-03-12 23:42:38 +08:00
|
|
|
TYPE(AttributeColon) \
|
2017-09-20 17:51:03 +08:00
|
|
|
TYPE(AttributeParen) \
|
2018-03-12 23:42:38 +08:00
|
|
|
TYPE(AttributeSquare) \
|
2017-09-20 17:51:03 +08:00
|
|
|
TYPE(BinaryOperator) \
|
|
|
|
TYPE(BitFieldColon) \
|
|
|
|
TYPE(BlockComment) \
|
|
|
|
TYPE(CastRParen) \
|
|
|
|
TYPE(ConditionalExpr) \
|
|
|
|
TYPE(ConflictAlternative) \
|
|
|
|
TYPE(ConflictEnd) \
|
|
|
|
TYPE(ConflictStart) \
|
|
|
|
TYPE(CtorInitializerColon) \
|
|
|
|
TYPE(CtorInitializerComma) \
|
|
|
|
TYPE(DesignatedInitializerLSquare) \
|
|
|
|
TYPE(DesignatedInitializerPeriod) \
|
|
|
|
TYPE(DictLiteral) \
|
|
|
|
TYPE(ForEachMacro) \
|
|
|
|
TYPE(FunctionAnnotationRParen) \
|
|
|
|
TYPE(FunctionDeclarationName) \
|
|
|
|
TYPE(FunctionLBrace) \
|
|
|
|
TYPE(FunctionTypeLParen) \
|
|
|
|
TYPE(ImplicitStringLiteral) \
|
|
|
|
TYPE(InheritanceColon) \
|
|
|
|
TYPE(InheritanceComma) \
|
|
|
|
TYPE(InlineASMBrace) \
|
|
|
|
TYPE(InlineASMColon) \
|
|
|
|
TYPE(JavaAnnotation) \
|
|
|
|
TYPE(JsComputedPropertyName) \
|
|
|
|
TYPE(JsExponentiation) \
|
|
|
|
TYPE(JsExponentiationEqual) \
|
|
|
|
TYPE(JsFatArrow) \
|
|
|
|
TYPE(JsNonNullAssertion) \
|
|
|
|
TYPE(JsTypeColon) \
|
|
|
|
TYPE(JsTypeOperator) \
|
|
|
|
TYPE(JsTypeOptionalQuestion) \
|
|
|
|
TYPE(LambdaArrow) \
|
|
|
|
TYPE(LambdaLSquare) \
|
|
|
|
TYPE(LeadingJavaAnnotation) \
|
|
|
|
TYPE(LineComment) \
|
|
|
|
TYPE(MacroBlockBegin) \
|
|
|
|
TYPE(MacroBlockEnd) \
|
|
|
|
TYPE(ObjCBlockLBrace) \
|
|
|
|
TYPE(ObjCBlockLParen) \
|
|
|
|
TYPE(ObjCDecl) \
|
|
|
|
TYPE(ObjCForIn) \
|
|
|
|
TYPE(ObjCMethodExpr) \
|
|
|
|
TYPE(ObjCMethodSpecifier) \
|
|
|
|
TYPE(ObjCProperty) \
|
|
|
|
TYPE(ObjCStringLiteral) \
|
|
|
|
TYPE(OverloadedOperator) \
|
|
|
|
TYPE(OverloadedOperatorLParen) \
|
|
|
|
TYPE(PointerOrReference) \
|
|
|
|
TYPE(PureVirtualSpecifier) \
|
|
|
|
TYPE(RangeBasedForLoopColon) \
|
|
|
|
TYPE(RegexLiteral) \
|
|
|
|
TYPE(SelectorName) \
|
|
|
|
TYPE(StartOfName) \
|
clang-format: better handle statement macros
Summary:
Some macros are used in the body of function, and actually contain the trailing semicolon: they should thus be automatically followed by a new line, and not get merged with the next line. This is for example the case with Qt's Q_UNUSED macro:
void foo(int a, int b) {
Q_UNUSED(a)
return b;
}
This patch deals with these cases by introducing a new option to specify list of statement macros. This re-uses the system already in place for foreach macros, to ensure there is no impact on performance.
Reviewers: krasimir, djasper, klimek
Reviewed By: krasimir
Subscribers: acoomans, mgrang, alexfh, klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D33440
llvm-svn: 343602
2018-10-03 00:37:51 +08:00
|
|
|
TYPE(StatementMacro) \
|
2017-09-20 17:51:03 +08:00
|
|
|
TYPE(StructuredBindingLSquare) \
|
|
|
|
TYPE(TemplateCloser) \
|
|
|
|
TYPE(TemplateOpener) \
|
|
|
|
TYPE(TemplateString) \
|
2018-02-13 18:20:39 +08:00
|
|
|
TYPE(ProtoExtensionLSquare) \
|
2017-09-20 17:51:03 +08:00
|
|
|
TYPE(TrailingAnnotation) \
|
|
|
|
TYPE(TrailingReturnArrow) \
|
|
|
|
TYPE(TrailingUnaryOperator) \
|
|
|
|
TYPE(UnaryOperator) \
|
2015-07-14 00:19:34 +08:00
|
|
|
TYPE(Unknown)
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
enum TokenType {
|
2015-07-14 00:19:34 +08:00
|
|
|
#define TYPE(X) TT_##X,
|
2017-09-20 17:51:03 +08:00
|
|
|
LIST_TOKEN_TYPES
|
2015-07-14 00:19:34 +08:00
|
|
|
#undef TYPE
|
2017-09-20 17:51:03 +08:00
|
|
|
NUM_TOKEN_TYPES
|
2013-06-04 00:45:03 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determines the name of a token type.
|
2015-07-14 00:19:34 +08:00
|
|
|
const char *getTokenTypeName(TokenType Type);
|
|
|
|
|
2013-07-09 17:06:29 +08:00
|
|
|
// Represents what type of block a set of braces open.
|
2015-06-17 21:08:06 +08:00
|
|
|
enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
|
2013-07-09 17:06:29 +08:00
|
|
|
|
2013-07-10 22:02:49 +08:00
|
|
|
// The packing kind of a function's parameters.
|
2015-06-17 21:08:06 +08:00
|
|
|
enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
|
2013-07-10 22:02:49 +08:00
|
|
|
|
2015-06-17 21:08:06 +08:00
|
|
|
enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
|
2013-10-12 05:25:45 +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 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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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 {
|
2015-05-04 16:51:40 +08:00
|
|
|
FormatToken() {}
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The \c Token.
|
2013-06-04 00:45:03 +08:00
|
|
|
Token Tok;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The number of newlines immediately before the \c Token.
|
2013-06-04 00:45:03 +08:00
|
|
|
///
|
|
|
|
/// 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.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned NewlinesBefore = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether there is at least one unescaped newline before the \c
|
2013-06-04 00:45:03 +08:00
|
|
|
/// Token.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool HasUnescapedNewline = false;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The range of the whitespace immediately preceding the \c Token.
|
2013-06-04 00:45:03 +08:00
|
|
|
SourceRange WhitespaceRange;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The offset just past the last '\n' in this token's leading
|
2013-06-04 00:45:03 +08:00
|
|
|
/// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned LastNewlineOffset = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The width of the non-whitespace parts of the token (or its first
|
2013-09-10 17:38:25 +08:00
|
|
|
/// 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.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned ColumnWidth = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Contains the width in columns of the last line of a multi-line
|
2013-09-02 21:58:14 +08:00
|
|
|
/// token.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned LastLineColumnWidth = 0;
|
2013-09-02 21:58:14 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether the token text contains newlines (escaped or not).
|
2015-05-04 16:51:40 +08:00
|
|
|
bool IsMultiline = false;
|
2013-09-02 21:58:14 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Indicates that this is the first token of the file.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool IsFirst = false;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether there must be a line break before this token.
|
2013-06-04 00:45:03 +08:00
|
|
|
///
|
|
|
|
/// This happens for example when a preprocessor directive ended directly
|
|
|
|
/// before the token.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool MustBreakBefore = false;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The raw text of the token.
|
2013-06-04 00:45:03 +08:00
|
|
|
///
|
|
|
|
/// Contains the raw token text without leading whitespace and without leading
|
|
|
|
/// escaped newlines.
|
|
|
|
StringRef TokenText;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set to \c true if this token is an unterminated literal.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool IsUnterminatedLiteral = 0;
|
2013-07-17 04:28:33 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Contains the kind of block if this token is a brace.
|
2015-05-04 16:51:40 +08:00
|
|
|
BraceBlockKind BlockKind = BK_Unknown;
|
2013-07-09 17:06:29 +08:00
|
|
|
|
2015-05-04 16:51:40 +08:00
|
|
|
TokenType Type = TT_Unknown;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The number of spaces that should be inserted before this token.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned SpacesRequiredBefore = 0;
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if it is allowed to break before this token.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool CanBreakBefore = false;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if this is the ">" of "template<..>".
|
2015-05-04 16:51:40 +08:00
|
|
|
bool ClosesTemplateDeclaration = false;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Number of parameters, if this is "(", "[" or "<".
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned ParameterCount = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Number of parameters that are nested blocks,
|
2014-06-03 20:02:45 +08:00
|
|
|
/// if this is "(", "[" or "<".
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned BlockParameterCount = 0;
|
2014-06-03 20:02:45 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
|
2015-05-04 15:39:00 +08:00
|
|
|
/// the surrounding bracket.
|
2015-05-04 16:51:40 +08:00
|
|
|
tok::TokenKind ParentBracket = tok::unknown;
|
2015-05-04 15:39:00 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A token can have a special role that can carry extra information
|
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
|
|
|
/// 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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If this is an opening parenthesis, how are the parameters packed?
|
2015-05-04 16:51:40 +08:00
|
|
|
ParameterPackingKind PackingKind = PPK_Inconclusive;
|
2013-07-10 22:02:49 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The total length of the unwrapped line up to and including this
|
2013-08-29 23:21:40 +08:00
|
|
|
/// token.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned TotalLength = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The original 0-based column of this token, including expanded tabs.
|
2013-09-10 17:38:25 +08:00
|
|
|
/// The configured TabWidth is used as tab width.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned OriginalColumn = 0;
|
2013-08-29 23:21:40 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The length of following tokens until the next natural split point,
|
2013-06-04 00:45:03 +08:00
|
|
|
/// or the next token that can be broken.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned UnbreakableTailLength = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
|
|
|
// FIXME: Come up with a 'cleaner' concept.
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The binding strength of a token. This is a combined value of
|
2013-06-04 00:45:03 +08:00
|
|
|
/// operator precedence, parenthesis nesting, etc.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned BindingStrength = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The nesting level of this token, i.e. the number of surrounding (),
|
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
|
|
|
/// [], {} or <>.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned NestingLevel = 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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The indent level of this token. Copied from the surrounding line.
|
2017-01-31 19:25:01 +08:00
|
|
|
unsigned IndentLevel = 0;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Penalty for inserting a line break before this token.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned SplitPenalty = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If this is the first ObjC selector name in an ObjC method
|
2013-06-04 00:45:03 +08:00
|
|
|
/// 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.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned LongestObjCSelectorName = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-07-09 13:58:51 +08:00
|
|
|
/// If this is the first ObjC selector name in an ObjC method
|
|
|
|
/// definition or call, this contains the number of parts that the whole
|
|
|
|
/// selector consist of.
|
2018-02-07 18:35:08 +08:00
|
|
|
unsigned ObjCSelectorNameParts = 0;
|
|
|
|
|
2018-07-09 14:54:52 +08:00
|
|
|
/// The 0-based index of the parameter/argument. For ObjC it is set
|
|
|
|
/// for the selector name token.
|
|
|
|
/// For now calculated only for ObjC.
|
|
|
|
unsigned ParameterIndex = 0;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stores the number of required fake parentheses and the
|
2013-06-04 00:45:03 +08:00
|
|
|
/// 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;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Insert this many fake ) after this token for correct indentation.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned FakeRParens = 0;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if this token starts a binary expression, i.e. has at least
|
2013-09-06 16:08:14 +08:00
|
|
|
/// one fake l_paren with a precedence greater than prec::Unknown.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool StartsBinaryExpression = false;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if this token ends a binary expression.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool EndsBinaryExpression = false;
|
2013-09-06 16:08:14 +08:00
|
|
|
|
2018-10-02 02:18:00 +08:00
|
|
|
/// If this is an operator (or "."/"->") in a sequence of operators
|
2014-04-16 20:26:54 +08:00
|
|
|
/// with the same precedence, contains the 0-based operator index.
|
2015-05-04 16:51:40 +08:00
|
|
|
unsigned OperatorIndex = 0;
|
2014-04-16 20:26:54 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If this is an operator (or "."/"->") in a sequence of operators
|
2016-01-05 21:03:50 +08:00
|
|
|
/// with the same precedence, points to the next operator.
|
|
|
|
FormatToken *NextOperator = nullptr;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Is this token part of a \c DeclStmt defining multiple variables?
|
2013-06-04 00:45:03 +08:00
|
|
|
///
|
|
|
|
/// Only set if \c Type == \c TT_StartOfName.
|
2015-05-04 16:51:40 +08:00
|
|
|
bool PartOfMultiVariableDeclStmt = false;
|
2013-06-04 00:45:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Does this line comment continue a line comment section?
|
2017-02-02 22:36:50 +08:00
|
|
|
///
|
|
|
|
/// Only set to true if \c Type == \c TT_LineComment.
|
|
|
|
bool ContinuesLineCommentSection = false;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If this is a bracket, this points to the matching one.
|
2015-05-04 16:51:40 +08:00
|
|
|
FormatToken *MatchingParen = nullptr;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The previous token in the unwrapped line.
|
2015-05-04 16:51:40 +08:00
|
|
|
FormatToken *Previous = nullptr;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The next token in the unwrapped line.
|
2015-05-04 16:51:40 +08:00
|
|
|
FormatToken *Next = nullptr;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If this token starts a block, this contains all the unwrapped lines
|
2015-05-04 16:51:40 +08:00
|
|
|
/// in it.
|
|
|
|
SmallVector<AnnotatedLine *, 1> Children;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stores the formatting decision for the token once it was made.
|
2015-05-04 16:51:40 +08:00
|
|
|
FormatDecision Decision = FD_Unformatted;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If \c true, this token has been fully formatted (indented and
|
2015-05-04 16:51:40 +08:00
|
|
|
/// potentially re-formatted inside), and we do not allow further formatting
|
|
|
|
/// changes.
|
|
|
|
bool Finalized = false;
|
2014-04-01 20:55:11 +08:00
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
|
2014-11-21 20:14:12 +08:00
|
|
|
bool is(TokenType TT) const { return Type == TT; }
|
2014-11-04 20:41:02 +08:00
|
|
|
bool is(const IdentifierInfo *II) const {
|
|
|
|
return II && II == Tok.getIdentifierInfo();
|
|
|
|
}
|
2015-08-14 20:44:06 +08:00
|
|
|
bool is(tok::PPKeywordKind Kind) const {
|
|
|
|
return Tok.getIdentifierInfo() &&
|
|
|
|
Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
template <typename A, typename B> bool isOneOf(A K1, B K2) const {
|
2014-11-24 03:03:25 +08:00
|
|
|
return is(K1) || is(K2);
|
2013-06-04 00:45:03 +08:00
|
|
|
}
|
2015-02-16 04:11:14 +08:00
|
|
|
template <typename A, typename B, typename... Ts>
|
|
|
|
bool isOneOf(A K1, B K2, Ts... Ks) const {
|
|
|
|
return is(K1) || isOneOf(K2, Ks...);
|
2014-11-24 23:42:34 +08:00
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
template <typename T> bool isNot(T Kind) const { return !is(Kind); }
|
2014-11-04 20:41:02 +08:00
|
|
|
|
2018-09-17 15:46:20 +08:00
|
|
|
bool closesScopeAfterBlock() const {
|
|
|
|
if (BlockKind == BK_Block)
|
|
|
|
return true;
|
|
|
|
if (closesScope())
|
|
|
|
return Previous->closesScopeAfterBlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-29 22:41:02 +08:00
|
|
|
/// \c true if this token starts a sequence with the given tokens in order,
|
|
|
|
/// following the ``Next`` pointers, ignoring comments.
|
|
|
|
template <typename A, typename... Ts>
|
|
|
|
bool startsSequence(A K1, Ts... Tokens) const {
|
|
|
|
return startsSequenceInternal(K1, Tokens...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \c true if this token ends a sequence with the given tokens in order,
|
|
|
|
/// following the ``Previous`` pointers, ignoring comments.
|
|
|
|
template <typename A, typename... Ts>
|
|
|
|
bool endsSequence(A K1, Ts... Tokens) const {
|
|
|
|
return endsSequenceInternal(K1, Tokens...);
|
|
|
|
}
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether the token is a simple-type-specifier.
|
2014-01-16 17:11:55 +08:00
|
|
|
bool isSimpleTypeSpecifier() const;
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
bool isObjCAccessSpecifier() const {
|
2017-09-20 17:51:03 +08:00
|
|
|
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));
|
2013-06-04 00:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns whether \p Tok is ([{ or an opening < of a template or in
|
2018-02-06 19:34:34 +08:00
|
|
|
/// protos.
|
2013-06-04 00:45:03 +08:00
|
|
|
bool opensScope() const {
|
2017-01-31 21:03:07 +08:00
|
|
|
if (is(TT_TemplateString) && TokenText.endswith("${"))
|
|
|
|
return true;
|
2018-02-06 19:34:34 +08:00
|
|
|
if (is(TT_DictLiteral) && is(tok::less))
|
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
|
|
|
|
TT_TemplateOpener);
|
2013-06-04 00:45:03 +08:00
|
|
|
}
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns whether \p Tok is )]} or a closing > of a template or in
|
2018-02-06 19:34:34 +08:00
|
|
|
/// protos.
|
2013-06-04 00:45:03 +08:00
|
|
|
bool closesScope() const {
|
2017-01-31 21:03:07 +08:00
|
|
|
if (is(TT_TemplateString) && TokenText.startswith("}"))
|
|
|
|
return true;
|
2018-02-06 19:34:34 +08:00
|
|
|
if (is(TT_DictLiteral) && is(tok::greater))
|
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
|
|
|
|
TT_TemplateCloser);
|
2013-06-04 00:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true if this is a "." or "->" accessing a member.
|
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
|
|
|
bool isMemberAccess() const {
|
2014-07-09 21:07:57 +08:00
|
|
|
return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
|
2015-03-27 02:46:28 +08:00
|
|
|
!isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
|
|
|
|
TT_LambdaArrow);
|
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 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 {
|
2014-08-26 17:37:52 +08:00
|
|
|
return is(tok::comment) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
|
2013-06-04 00:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true if this is a keyword that can be used
|
2014-08-06 22:15:41 +08:00
|
|
|
/// like a function call (e.g. sizeof, typeid, ...).
|
|
|
|
bool isFunctionLikeKeyword() const {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::kw_throw:
|
|
|
|
case tok::kw_typeid:
|
|
|
|
case tok::kw_return:
|
|
|
|
case tok::kw_sizeof:
|
|
|
|
case tok::kw_alignof:
|
|
|
|
case tok::kw_alignas:
|
|
|
|
case tok::kw_decltype:
|
|
|
|
case tok::kw_noexcept:
|
|
|
|
case tok::kw_static_assert:
|
|
|
|
case tok::kw___attribute:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true if this is a string literal that's like a label,
|
clang-format: Keep string-literal-label + value pairs on a line.
We have previously done that for <<-operators. This patch also adds
this logic for "," and "+".
Before:
string v = "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaa: " +
aaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa;
string v = StrCat("aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa, "aaaaaaaaaaaaaaaa: ",
aaaaaaaaaaaaaaaa, "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa);
After:
string v = "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa +
"aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa +
"aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa;
string v = StrCat("aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa,
"aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa,
"aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa);
llvm-svn: 289531
2016-12-13 19:16:42 +08:00
|
|
|
/// e.g. ends with "=" or ":".
|
|
|
|
bool isLabelString() const {
|
|
|
|
if (!is(tok::string_literal))
|
|
|
|
return false;
|
|
|
|
StringRef Content = TokenText;
|
|
|
|
if (Content.startswith("\"") || Content.startswith("'"))
|
|
|
|
Content = Content.drop_front(1);
|
|
|
|
if (Content.endswith("\"") || Content.endswith("'"))
|
|
|
|
Content = Content.drop_back(1);
|
|
|
|
Content = Content.trim();
|
|
|
|
return Content.size() > 1 &&
|
|
|
|
(Content.back() == ':' || Content.back() == '=');
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns actual token start location without leading escaped
|
2015-05-04 16:51:40 +08:00
|
|
|
/// newlines and whitespace.
|
|
|
|
///
|
|
|
|
/// This can be different to Tok.getLocation(), which includes leading escaped
|
|
|
|
/// newlines.
|
|
|
|
SourceLocation getStartOfNonWhitespace() const {
|
|
|
|
return WhitespaceRange.getEnd();
|
|
|
|
}
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
prec::Level getPrecedence() const {
|
2018-01-24 01:29:41 +08:00
|
|
|
return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
|
|
|
|
/*CPlusPlus11=*/true);
|
2013-06-04 00:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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;
|
2014-05-09 16:15:10 +08:00
|
|
|
while (Tok && Tok->is(tok::comment))
|
2013-06-04 00:45:03 +08:00
|
|
|
Tok = Tok->Previous;
|
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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;
|
2014-05-09 16:15:10 +08:00
|
|
|
while (Tok && Tok->is(tok::comment))
|
2013-06-04 00:45:03 +08:00
|
|
|
Tok = Tok->Next;
|
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true if this tokens starts a block-type list, i.e. a
|
2013-10-22 23:45:58 +08:00
|
|
|
/// list that should be indented with a block indent.
|
2015-10-27 21:42:08 +08:00
|
|
|
bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
|
2017-02-20 22:51:16 +08:00
|
|
|
if (is(TT_TemplateString) && opensScope())
|
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
return is(TT_ArrayInitializerLSquare) ||
|
2018-02-20 00:00:21 +08:00
|
|
|
is(TT_ProtoExtensionLSquare) ||
|
2014-12-12 17:40:58 +08:00
|
|
|
(is(tok::l_brace) &&
|
|
|
|
(BlockKind == BK_Block || is(TT_DictLiteral) ||
|
2017-06-27 21:43:07 +08:00
|
|
|
(!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
|
2017-07-03 23:05:14 +08:00
|
|
|
(is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
|
|
|
|
Style.Language == FormatStyle::LK_TextProto));
|
2013-10-22 23:45:58 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns whether the token is the left square bracket of a C++
|
2017-09-20 17:29:37 +08:00
|
|
|
/// structured binding declaration.
|
|
|
|
bool isCppStructuredBinding(const FormatStyle &Style) const {
|
|
|
|
if (!Style.isCpp() || isNot(tok::l_square))
|
|
|
|
return false;
|
2017-09-20 17:51:03 +08:00
|
|
|
const FormatToken *T = this;
|
2017-09-20 17:29:37 +08:00
|
|
|
do {
|
|
|
|
T = T->getPreviousNonComment();
|
|
|
|
} while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
|
|
|
|
tok::ampamp));
|
|
|
|
return T && T->is(tok::kw_auto);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Same as opensBlockOrBlockTypeList, but for the closing token.
|
2015-10-27 21:42:08 +08:00
|
|
|
bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
|
2017-02-20 22:51:16 +08:00
|
|
|
if (is(TT_TemplateString) && closesScope())
|
|
|
|
return true;
|
2015-10-27 21:42:08 +08:00
|
|
|
return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
|
2013-10-22 23:30:28 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the actual namespace token, if this token starts a namespace
|
clang-format: add options to merge empty record body
Summary:
This patch introduces a few extra BraceWrapping options, similar to
`SplitEmptyFunction`, to allow merging empty 'record' bodies (e.g.
class, struct, union and namespace):
* SplitEmptyClass
* SplitEmptyStruct
* SplitEmptyUnion
* SplitEmptyNamespace
The `SplitEmptyFunction` option name has also been simplified/
shortened (from `SplitEmptyFunctionBody`).
These options are helpful when the correspond AfterXXX option is
enabled, to allow merging the empty record:
class Foo
{};
In addition, this fixes an unexpected merging of short records, when
the AfterXXXX options are used, which caused to be formatted like
this:
class Foo
{ void Foo(); };
This is now properly formatted as:
class Foo
{
void Foo();
};
Reviewers: djasper, krasimir
Reviewed By: djasper
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D34395
llvm-svn: 306874
2017-07-01 04:25:55 +08:00
|
|
|
/// block.
|
|
|
|
const FormatToken *getNamespaceToken() const {
|
|
|
|
const FormatToken *NamespaceTok = this;
|
|
|
|
if (is(tok::comment))
|
|
|
|
NamespaceTok = NamespaceTok->getNextNonComment();
|
2018-09-05 15:44:02 +08:00
|
|
|
// Detect "(inline|export)? namespace" in the beginning of a line.
|
|
|
|
if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
|
clang-format: add options to merge empty record body
Summary:
This patch introduces a few extra BraceWrapping options, similar to
`SplitEmptyFunction`, to allow merging empty 'record' bodies (e.g.
class, struct, union and namespace):
* SplitEmptyClass
* SplitEmptyStruct
* SplitEmptyUnion
* SplitEmptyNamespace
The `SplitEmptyFunction` option name has also been simplified/
shortened (from `SplitEmptyFunctionBody`).
These options are helpful when the correspond AfterXXX option is
enabled, to allow merging the empty record:
class Foo
{};
In addition, this fixes an unexpected merging of short records, when
the AfterXXXX options are used, which caused to be formatted like
this:
class Foo
{ void Foo(); };
This is now properly formatted as:
class Foo
{
void Foo();
};
Reviewers: djasper, krasimir
Reviewed By: djasper
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D34395
llvm-svn: 306874
2017-07-01 04:25:55 +08:00
|
|
|
NamespaceTok = NamespaceTok->getNextNonComment();
|
|
|
|
return NamespaceTok && NamespaceTok->is(tok::kw_namespace) ? NamespaceTok
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
private:
|
|
|
|
// Disallow copying.
|
2015-02-16 06:54:08 +08:00
|
|
|
FormatToken(const FormatToken &) = delete;
|
|
|
|
void operator=(const FormatToken &) = delete;
|
2016-05-29 22:41:02 +08:00
|
|
|
|
|
|
|
template <typename A, typename... Ts>
|
|
|
|
bool startsSequenceInternal(A K1, Ts... Tokens) const {
|
|
|
|
if (is(tok::comment) && Next)
|
|
|
|
return Next->startsSequenceInternal(K1, Tokens...);
|
|
|
|
return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:51:03 +08:00
|
|
|
template <typename A> bool startsSequenceInternal(A K1) const {
|
2016-05-29 22:41:02 +08:00
|
|
|
if (is(tok::comment) && Next)
|
|
|
|
return Next->startsSequenceInternal(K1);
|
|
|
|
return is(K1);
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:51:03 +08:00
|
|
|
template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
|
2016-05-29 22:41:02 +08:00
|
|
|
if (is(tok::comment) && Previous)
|
|
|
|
return Previous->endsSequenceInternal(K1);
|
|
|
|
return is(K1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename A, typename... Ts>
|
|
|
|
bool endsSequenceInternal(A K1, Ts... Tokens) const {
|
|
|
|
if (is(tok::comment) && Previous)
|
|
|
|
return Previous->endsSequenceInternal(K1, Tokens...);
|
|
|
|
return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
|
|
|
|
}
|
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();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// After the \c TokenAnnotator has finished annotating all the tokens,
|
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
|
|
|
/// this function precomputes required information for formatting.
|
|
|
|
virtual void precomputeFormattingInfos(const FormatToken *Token);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Apply the special formatting that the given role demands.
|
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
|
|
|
///
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Same as \c formatFromToken, but assumes that the first token has
|
2014-01-09 21:42:56 +08:00
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Notifies the \c Role that a comma was found.
|
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 CommaFound(const FormatToken *Token) {}
|
|
|
|
|
[clang-format] tweaked another case of lambda formatting
Summary:
This is done in order to improve cases where the lambda's body is moved too far to the right. Consider the following snippet with column limit set to 79:
```
void f() {
leader::MakeThisCallHere(&leader_service_,
cq_.get(),
[this, liveness](const leader::ReadRecordReq& req,
std::function<void()> done) {
logger_->HandleReadRecord(
req, resp, std::move(done));
});
leader::MakeAnother(&leader_service_,
cq_.get(),
[this, liveness](const leader::ReadRecordReq& req,
std::function<void()> done) {
logger_->HandleReadRecord(
req, resp, std::move(done), a);
});
}
```
The tool favors extra indentation for the lambda body and so the code incurs extra wrapping and adjacent calls are indented to a different level. I find this behavior annoying and I'd like the tool to favor new lines and, thus, use the extra width.
The fix, reduced, brings the following formatting.
Before:
function(1,
[] {
DoStuff();
//
},
1);
After:
function(
1,
[] {
DoStuff();
//
},
1);
Refer to the new tests in FormatTest.cpp
Contributed by oleg.smolsky!
Reviewers: djasper, klimek, krasimir
Subscribers: cfe-commits, owenpan
Tags: #clang
Differential Revision: https://reviews.llvm.org/D52676
llvm-svn: 345753
2018-11-01 01:56:57 +08:00
|
|
|
virtual const FormatToken *lastComma() { return nullptr; }
|
|
|
|
|
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
|
|
|
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
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
void precomputeFormattingInfos(const FormatToken *Token) override;
|
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
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun) override;
|
2014-01-09 21:42:56 +08:00
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun) override;
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Adds \p Token as the next comma to the \c CommaSeparated list.
|
2014-05-09 21:11:16 +08:00
|
|
|
void CommaFound(const FormatToken *Token) override {
|
|
|
|
Commas.push_back(Token);
|
|
|
|
}
|
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
|
|
|
|
[clang-format] tweaked another case of lambda formatting
Summary:
This is done in order to improve cases where the lambda's body is moved too far to the right. Consider the following snippet with column limit set to 79:
```
void f() {
leader::MakeThisCallHere(&leader_service_,
cq_.get(),
[this, liveness](const leader::ReadRecordReq& req,
std::function<void()> done) {
logger_->HandleReadRecord(
req, resp, std::move(done));
});
leader::MakeAnother(&leader_service_,
cq_.get(),
[this, liveness](const leader::ReadRecordReq& req,
std::function<void()> done) {
logger_->HandleReadRecord(
req, resp, std::move(done), a);
});
}
```
The tool favors extra indentation for the lambda body and so the code incurs extra wrapping and adjacent calls are indented to a different level. I find this behavior annoying and I'd like the tool to favor new lines and, thus, use the extra width.
The fix, reduced, brings the following formatting.
Before:
function(1,
[] {
DoStuff();
//
},
1);
After:
function(
1,
[] {
DoStuff();
//
},
1);
Refer to the new tests in FormatTest.cpp
Contributed by oleg.smolsky!
Reviewers: djasper, klimek, krasimir
Subscribers: cfe-commits, owenpan
Tags: #clang
Differential Revision: https://reviews.llvm.org/D52676
llvm-svn: 345753
2018-11-01 01:56:57 +08:00
|
|
|
const FormatToken *lastComma() override {
|
|
|
|
if (Commas.empty())
|
|
|
|
return nullptr;
|
|
|
|
return Commas.back();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A struct that holds information on how to format a given list with
|
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
|
|
|
/// a specific number of columns.
|
|
|
|
struct ColumnFormat {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The number of columns to use.
|
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
|
|
|
unsigned Columns;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The total width in characters.
|
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
|
|
|
unsigned TotalWidth;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The number of lines required for this format.
|
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
|
|
|
unsigned LineCount;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The size of each column in characters.
|
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
|
|
|
SmallVector<unsigned, 8> ColumnSizes;
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Calculate which \c ColumnFormat fits best into
|
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
|
|
|
/// \p RemainingCharacters.
|
|
|
|
const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The ordered \c FormatTokens making up the commas of this list.
|
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
|
|
|
SmallVector<const FormatToken *, 8> Commas;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The length of each of the list's items in characters including the
|
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
|
|
|
/// trailing comma.
|
|
|
|
SmallVector<unsigned, 8> ItemLengths;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Precomputed formats that can be used for this list.
|
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
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Encapsulates keywords that are context sensitive or for languages not
|
2014-11-04 20:41:02 +08:00
|
|
|
/// properly supported by Clang's lexer.
|
|
|
|
struct AdditionalKeywords {
|
|
|
|
AdditionalKeywords(IdentifierTable &IdentTable) {
|
2015-08-24 22:28:08 +08:00
|
|
|
kw_final = &IdentTable.get("final");
|
|
|
|
kw_override = &IdentTable.get("override");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_in = &IdentTable.get("in");
|
2016-02-11 21:24:15 +08:00
|
|
|
kw_of = &IdentTable.get("of");
|
2014-12-05 18:42:21 +08:00
|
|
|
kw_CF_ENUM = &IdentTable.get("CF_ENUM");
|
|
|
|
kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_NS_ENUM = &IdentTable.get("NS_ENUM");
|
2014-12-05 18:42:21 +08:00
|
|
|
kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
|
2014-11-04 20:41:02 +08:00
|
|
|
|
2016-05-20 19:24:24 +08:00
|
|
|
kw_as = &IdentTable.get("as");
|
2016-04-25 06:05:09 +08:00
|
|
|
kw_async = &IdentTable.get("async");
|
|
|
|
kw_await = &IdentTable.get("await");
|
2016-11-11 00:20:58 +08:00
|
|
|
kw_declare = &IdentTable.get("declare");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_finally = &IdentTable.get("finally");
|
2016-03-22 22:32:20 +08:00
|
|
|
kw_from = &IdentTable.get("from");
|
2016-04-25 06:05:09 +08:00
|
|
|
kw_function = &IdentTable.get("function");
|
2017-04-26 20:34:15 +08:00
|
|
|
kw_get = &IdentTable.get("get");
|
2015-02-20 00:07:32 +08:00
|
|
|
kw_import = &IdentTable.get("import");
|
2018-09-27 14:48:13 +08:00
|
|
|
kw_infer = &IdentTable.get("infer");
|
2015-12-30 16:00:58 +08:00
|
|
|
kw_is = &IdentTable.get("is");
|
2015-09-28 22:28:08 +08:00
|
|
|
kw_let = &IdentTable.get("let");
|
2016-11-11 00:20:58 +08:00
|
|
|
kw_module = &IdentTable.get("module");
|
2017-07-07 21:17:10 +08:00
|
|
|
kw_readonly = &IdentTable.get("readonly");
|
2017-04-26 20:34:15 +08:00
|
|
|
kw_set = &IdentTable.get("set");
|
2016-06-24 03:52:32 +08:00
|
|
|
kw_type = &IdentTable.get("type");
|
2017-08-02 01:42:16 +08:00
|
|
|
kw_typeof = &IdentTable.get("typeof");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_var = &IdentTable.get("var");
|
2016-04-25 06:05:09 +08:00
|
|
|
kw_yield = &IdentTable.get("yield");
|
2014-11-04 20:41:02 +08:00
|
|
|
|
2014-11-21 20:19:07 +08:00
|
|
|
kw_abstract = &IdentTable.get("abstract");
|
2015-09-16 07:48:17 +08:00
|
|
|
kw_assert = &IdentTable.get("assert");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_extends = &IdentTable.get("extends");
|
|
|
|
kw_implements = &IdentTable.get("implements");
|
2014-11-25 18:05:17 +08:00
|
|
|
kw_instanceof = &IdentTable.get("instanceof");
|
2014-11-11 00:30:02 +08:00
|
|
|
kw_interface = &IdentTable.get("interface");
|
2015-01-14 06:32:50 +08:00
|
|
|
kw_native = &IdentTable.get("native");
|
2014-11-27 02:03:42 +08:00
|
|
|
kw_package = &IdentTable.get("package");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_synchronized = &IdentTable.get("synchronized");
|
|
|
|
kw_throws = &IdentTable.get("throws");
|
2015-02-04 23:26:27 +08:00
|
|
|
kw___except = &IdentTable.get("__except");
|
2017-02-11 03:36:52 +08:00
|
|
|
kw___has_include = &IdentTable.get("__has_include");
|
|
|
|
kw___has_include_next = &IdentTable.get("__has_include_next");
|
2014-11-04 20:41:02 +08:00
|
|
|
|
2015-04-22 17:45:42 +08:00
|
|
|
kw_mark = &IdentTable.get("mark");
|
|
|
|
|
2015-11-20 22:32:54 +08:00
|
|
|
kw_extend = &IdentTable.get("extend");
|
2014-11-04 20:41:02 +08:00
|
|
|
kw_option = &IdentTable.get("option");
|
|
|
|
kw_optional = &IdentTable.get("optional");
|
|
|
|
kw_repeated = &IdentTable.get("repeated");
|
|
|
|
kw_required = &IdentTable.get("required");
|
|
|
|
kw_returns = &IdentTable.get("returns");
|
2015-04-07 23:04:40 +08:00
|
|
|
|
|
|
|
kw_signals = &IdentTable.get("signals");
|
2015-12-01 20:05:04 +08:00
|
|
|
kw_qsignals = &IdentTable.get("Q_SIGNALS");
|
2015-04-07 23:04:40 +08:00
|
|
|
kw_slots = &IdentTable.get("slots");
|
|
|
|
kw_qslots = &IdentTable.get("Q_SLOTS");
|
2017-07-05 20:24:01 +08:00
|
|
|
|
|
|
|
// Keep this at the end of the constructor to make sure everything here is
|
|
|
|
// already initialized.
|
|
|
|
JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
|
|
|
|
{kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
|
2017-07-07 21:17:10 +08:00
|
|
|
kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
|
2017-08-02 01:42:16 +08:00
|
|
|
kw_set, kw_type, kw_typeof, kw_var, kw_yield,
|
2017-07-05 20:24:01 +08:00
|
|
|
// Keywords from the Java section.
|
|
|
|
kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
|
2014-11-04 20:41:02 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 23:26:27 +08:00
|
|
|
// Context sensitive keywords.
|
2015-08-24 22:28:08 +08:00
|
|
|
IdentifierInfo *kw_final;
|
|
|
|
IdentifierInfo *kw_override;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_in;
|
2016-02-11 21:24:15 +08:00
|
|
|
IdentifierInfo *kw_of;
|
2014-12-05 18:42:21 +08:00
|
|
|
IdentifierInfo *kw_CF_ENUM;
|
|
|
|
IdentifierInfo *kw_CF_OPTIONS;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_NS_ENUM;
|
2014-12-05 18:42:21 +08:00
|
|
|
IdentifierInfo *kw_NS_OPTIONS;
|
2015-02-04 23:26:27 +08:00
|
|
|
IdentifierInfo *kw___except;
|
2017-02-11 03:36:52 +08:00
|
|
|
IdentifierInfo *kw___has_include;
|
|
|
|
IdentifierInfo *kw___has_include_next;
|
2014-11-04 20:41:02 +08:00
|
|
|
|
|
|
|
// JavaScript keywords.
|
2016-05-20 19:24:24 +08:00
|
|
|
IdentifierInfo *kw_as;
|
2016-04-25 06:05:09 +08:00
|
|
|
IdentifierInfo *kw_async;
|
|
|
|
IdentifierInfo *kw_await;
|
2016-11-11 00:20:58 +08:00
|
|
|
IdentifierInfo *kw_declare;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_finally;
|
2016-03-22 22:32:20 +08:00
|
|
|
IdentifierInfo *kw_from;
|
2016-04-25 06:05:09 +08:00
|
|
|
IdentifierInfo *kw_function;
|
2017-04-26 20:34:15 +08:00
|
|
|
IdentifierInfo *kw_get;
|
2015-02-20 00:07:32 +08:00
|
|
|
IdentifierInfo *kw_import;
|
2018-09-27 14:48:13 +08:00
|
|
|
IdentifierInfo *kw_infer;
|
2015-12-30 16:00:58 +08:00
|
|
|
IdentifierInfo *kw_is;
|
2015-09-28 22:28:08 +08:00
|
|
|
IdentifierInfo *kw_let;
|
2016-11-11 00:20:58 +08:00
|
|
|
IdentifierInfo *kw_module;
|
2017-07-07 21:17:10 +08:00
|
|
|
IdentifierInfo *kw_readonly;
|
2017-04-26 20:34:15 +08:00
|
|
|
IdentifierInfo *kw_set;
|
2016-06-24 03:52:32 +08:00
|
|
|
IdentifierInfo *kw_type;
|
2017-08-02 01:42:16 +08:00
|
|
|
IdentifierInfo *kw_typeof;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_var;
|
2016-04-25 06:05:09 +08:00
|
|
|
IdentifierInfo *kw_yield;
|
2014-11-04 20:41:02 +08:00
|
|
|
|
|
|
|
// Java keywords.
|
2014-11-21 20:19:07 +08:00
|
|
|
IdentifierInfo *kw_abstract;
|
2015-09-16 07:48:17 +08:00
|
|
|
IdentifierInfo *kw_assert;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_extends;
|
|
|
|
IdentifierInfo *kw_implements;
|
2014-11-25 18:05:17 +08:00
|
|
|
IdentifierInfo *kw_instanceof;
|
2014-11-11 00:30:02 +08:00
|
|
|
IdentifierInfo *kw_interface;
|
2015-01-14 06:32:50 +08:00
|
|
|
IdentifierInfo *kw_native;
|
2014-11-27 02:03:42 +08:00
|
|
|
IdentifierInfo *kw_package;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_synchronized;
|
|
|
|
IdentifierInfo *kw_throws;
|
|
|
|
|
2015-04-22 17:45:42 +08:00
|
|
|
// Pragma keywords.
|
|
|
|
IdentifierInfo *kw_mark;
|
|
|
|
|
2014-11-04 20:41:02 +08:00
|
|
|
// Proto keywords.
|
2015-11-20 22:32:54 +08:00
|
|
|
IdentifierInfo *kw_extend;
|
2014-11-04 20:41:02 +08:00
|
|
|
IdentifierInfo *kw_option;
|
|
|
|
IdentifierInfo *kw_optional;
|
|
|
|
IdentifierInfo *kw_repeated;
|
|
|
|
IdentifierInfo *kw_required;
|
|
|
|
IdentifierInfo *kw_returns;
|
2015-04-07 23:04:40 +08:00
|
|
|
|
|
|
|
// QT keywords.
|
|
|
|
IdentifierInfo *kw_signals;
|
2015-12-01 20:05:04 +08:00
|
|
|
IdentifierInfo *kw_qsignals;
|
2015-04-07 23:04:40 +08:00
|
|
|
IdentifierInfo *kw_slots;
|
|
|
|
IdentifierInfo *kw_qslots;
|
2017-07-04 23:30:21 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true if \p Tok is a true JavaScript identifier, returns
|
2017-07-04 23:30:21 +08:00
|
|
|
/// \c false if it is a keyword or a pseudo keyword.
|
|
|
|
bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
|
|
|
|
return Tok.is(tok::identifier) &&
|
|
|
|
JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
|
|
|
|
JsExtraKeywords.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The JavaScript keywords beyond the C++ keyword set.
|
2017-07-04 23:30:21 +08:00
|
|
|
std::unordered_set<IdentifierInfo *> JsExtraKeywords;
|
2014-11-04 20:41:02 +08:00
|
|
|
};
|
|
|
|
|
2013-06-04 00:45:03 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|