aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/FormatToken.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Format/FormatToken.h')
-rw-r--r--lib/Format/FormatToken.h99
1 files changed, 76 insertions, 23 deletions
diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h
index 2145ee28dcc0..c376c5009559 100644
--- a/lib/Format/FormatToken.h
+++ b/lib/Format/FormatToken.h
@@ -19,7 +19,7 @@
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
-#include "llvm/ADT/OwningPtr.h"
+#include <memory>
namespace clang {
namespace format {
@@ -27,36 +27,45 @@ namespace format {
enum TokenType {
TT_ArrayInitializerLSquare,
TT_ArraySubscriptLSquare,
+ TT_AttributeParen,
TT_BinaryOperator,
TT_BitFieldColon,
TT_BlockComment,
TT_CastRParen,
TT_ConditionalExpr,
+ TT_ConflictAlternative,
+ TT_ConflictEnd,
+ TT_ConflictStart,
TT_CtorInitializerColon,
TT_CtorInitializerComma,
TT_DesignatedInitializerPeriod,
TT_DictLiteral,
+ TT_FunctionDeclarationName,
+ TT_FunctionLBrace,
+ TT_FunctionTypeLParen,
TT_ImplicitStringLiteral,
- TT_InlineASMColon,
TT_InheritanceColon,
- TT_FunctionTypeLParen,
+ TT_InlineASMColon,
TT_LambdaLSquare,
TT_LineComment,
+ TT_ObjCBlockLBrace,
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_RegexLiteral,
+ TT_SelectorName,
TT_StartOfName,
TT_TemplateCloser,
TT_TemplateOpener,
+ TT_TrailingAnnotation,
TT_TrailingReturnArrow,
TT_TrailingUnaryOperator,
TT_UnaryOperator,
@@ -87,7 +96,7 @@ class TokenRole;
class AnnotatedLine;
/// \brief A wrapper around a \c Token storing information about the
-/// whitespace characters preceeding it.
+/// whitespace characters preceding it.
struct FormatToken {
FormatToken()
: NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
@@ -95,12 +104,14 @@ struct FormatToken {
IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
CanBreakBefore(false), ClosesTemplateDeclaration(false),
- ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0),
- UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0),
+ ParameterCount(0), BlockParameterCount(0),
+ PackingKind(PPK_Inconclusive), TotalLength(0), UnbreakableTailLength(0),
+ BindingStrength(0), NestingLevel(0), SplitPenalty(0),
LongestObjCSelectorName(0), FakeRParens(0),
StartsBinaryExpression(false), EndsBinaryExpression(false),
- LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false),
- MatchingParen(NULL), Previous(NULL), Next(NULL),
+ OperatorIndex(0), LastOperator(false),
+ PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
+ MatchingParen(nullptr), Previous(nullptr), Next(nullptr),
Decision(FD_Unformatted), Finalized(false) {}
/// \brief The \c Token.
@@ -116,7 +127,7 @@ struct FormatToken {
/// Token.
bool HasUnescapedNewline;
- /// \brief The range of the whitespace immediately preceeding the \c Token.
+ /// \brief The range of the whitespace immediately preceding the \c Token.
SourceRange WhitespaceRange;
/// \brief The offset just past the last '\n' in this token's leading
@@ -182,9 +193,13 @@ struct FormatToken {
/// the number of commas.
unsigned ParameterCount;
+ /// \brief Number of parameters that are nested blocks,
+ /// if this is "(", "[" or "<".
+ unsigned BlockParameterCount;
+
/// \brief A token can have a special role that can carry extra information
/// about the token's formatting.
- llvm::OwningPtr<TokenRole> Role;
+ std::unique_ptr<TokenRole> Role;
/// \brief If this is an opening parenthesis, how are the parameters packed?
ParameterPackingKind PackingKind;
@@ -206,11 +221,18 @@ struct FormatToken {
/// operator precedence, parenthesis nesting, etc.
unsigned BindingStrength;
+ /// \brief The nesting level of this token, i.e. the number of surrounding (),
+ /// [], {} or <>.
+ unsigned NestingLevel;
+
/// \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.
+ ///
+ /// This being set to 0 means that the selectors should not be colon-aligned,
+ /// e.g. because several of them are block-type.
unsigned LongestObjCSelectorName;
/// \brief Stores the number of required fake parentheses and the
@@ -228,14 +250,22 @@ struct FormatToken {
/// \brief \c true if this token ends a binary expression.
bool EndsBinaryExpression;
- /// \brief Is this the last "." or "->" in a builder-type call?
- bool LastInChainOfCalls;
+ /// \brief Is this is an operator (or "."/"->") in a sequence of operators
+ /// with the same precedence, contains the 0-based operator index.
+ unsigned OperatorIndex;
+
+ /// \brief Is this the last operator (or "."/"->") in a sequence of operators
+ /// with the same precedence?
+ bool LastOperator;
/// \brief Is this token part of a \c DeclStmt defining multiple variables?
///
/// Only set if \c Type == \c TT_StartOfName.
bool PartOfMultiVariableDeclStmt;
+ /// \brief Is this a foreach macro?
+ bool IsForEachMacro;
+
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
@@ -260,6 +290,7 @@ struct FormatToken {
}
bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
+ bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
return Tok.isObjCAtKeyword(Kind);
@@ -270,6 +301,9 @@ struct FormatToken {
(!ColonRequired || (Next && Next->is(tok::colon)));
}
+ /// \brief Determine whether the token is a simple-type-specifier.
+ bool isSimpleTypeSpecifier() const;
+
bool isObjCAccessSpecifier() const {
return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
Next->isObjCAtKeyword(tok::objc_protected) ||
@@ -290,7 +324,7 @@ struct FormatToken {
/// \brief Returns \c true if this is a "." or "->" accessing a member.
bool isMemberAccess() const {
- return isOneOf(tok::arrow, tok::period) &&
+ return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Type != TT_DesignatedInitializerPeriod;
}
@@ -326,7 +360,7 @@ struct FormatToken {
/// \brief Returns the previous token ignoring comments.
FormatToken *getPreviousNonComment() const {
FormatToken *Tok = Previous;
- while (Tok != NULL && Tok->is(tok::comment))
+ while (Tok && Tok->is(tok::comment))
Tok = Tok->Previous;
return Tok;
}
@@ -334,7 +368,7 @@ struct FormatToken {
/// \brief Returns the next token ignoring comments.
const FormatToken *getNextNonComment() const {
const FormatToken *Tok = Next;
- while (Tok != NULL && Tok->is(tok::comment))
+ while (Tok && Tok->is(tok::comment))
Tok = Tok->Next;
return Tok;
}
@@ -388,10 +422,21 @@ public:
/// \brief Apply the special formatting that the given role demands.
///
+ /// Assumes that the token having this role is already formatted.
+ ///
/// Continues formatting from \p State leaving indentation to \p Indenter and
/// returns the total penalty that this formatting incurs.
- virtual unsigned format(LineState &State, ContinuationIndenter *Indenter,
- bool DryRun) {
+ 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) {
return 0;
}
@@ -404,15 +449,21 @@ protected:
class CommaSeparatedList : public TokenRole {
public:
- CommaSeparatedList(const FormatStyle &Style) : TokenRole(Style) {}
+ CommaSeparatedList(const FormatStyle &Style)
+ : TokenRole(Style), HasNestedBracedList(false) {}
- virtual void precomputeFormattingInfos(const FormatToken *Token);
+ void precomputeFormattingInfos(const FormatToken *Token) override;
+
+ unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
+ bool DryRun) override;
- virtual unsigned format(LineState &State, ContinuationIndenter *Indenter,
- bool DryRun);
+ unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
+ bool DryRun) override;
/// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
- virtual void CommaFound(const FormatToken *Token) { Commas.push_back(Token); }
+ void CommaFound(const FormatToken *Token) override {
+ Commas.push_back(Token);
+ }
private:
/// \brief A struct that holds information on how to format a given list with
@@ -444,6 +495,8 @@ private:
/// \brief Precomputed formats that can be used for this list.
SmallVector<ColumnFormat, 4> Formats;
+
+ bool HasNestedBracedList;
};
} // namespace format