FormatToken.h revision 3b71155dc93fb3556dfe8b758ae6e448b6fbb9c8
13b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko//===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===// 23b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko// 33b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko// The LLVM Compiler Infrastructure 43b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko// 53b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko// This file is distributed under the University of Illinois Open Source 63b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko// License. See LICENSE.TXT for details. 73b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko// 83b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko//===----------------------------------------------------------------------===// 93b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// 103b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// \file 113b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// \brief This file contains the declaration of the FormatToken, a wrapper 123b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// around Token with additional information related to formatting. 133b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// 143b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko//===----------------------------------------------------------------------===// 153b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 163b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko#ifndef LLVM_CLANG_FORMAT_FORMAT_TOKEN_H 173b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko#define LLVM_CLANG_FORMAT_FORMAT_TOKEN_H 183b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 193b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko#include "clang/Basic/OperatorPrecedence.h" 203b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko#include "clang/Lex/Lexer.h" 213b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 223b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienkonamespace clang { 233b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienkonamespace format { 243b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 253b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienkoenum TokenType { 263b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_BinaryOperator, 273b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_BlockComment, 283b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_CastRParen, 293b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ConditionalExpr, 303b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_CtorInitializerColon, 313b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_DesignatedInitializerPeriod, 323b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ImplicitStringLiteral, 333b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_InlineASMColon, 343b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_InheritanceColon, 353b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_FunctionTypeLParen, 363b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_LineComment, 373b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCArrayLiteral, 383b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCBlockLParen, 393b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCDecl, 403b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCDictLiteral, 413b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCForIn, 423b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCMethodExpr, 433b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCMethodSpecifier, 443b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCProperty, 453b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_ObjCSelectorName, 463b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_OverloadedOperator, 473b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_OverloadedOperatorLParen, 483b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_PointerOrReference, 493b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_PureVirtualSpecifier, 503b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_RangeBasedForLoopColon, 513b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_StartOfName, 523b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_TemplateCloser, 533b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_TemplateOpener, 543b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_TrailingUnaryOperator, 553b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_UnaryOperator, 563b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TT_Unknown 573b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko}; 583b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 593b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// \brief A wrapper around a \c Token storing information about the 603b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko/// whitespace characters preceeding it. 613b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienkostruct FormatToken { 623b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken() 633b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0), 643b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TokenLength(0), IsFirst(false), MustBreakBefore(false), 653b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Type(TT_Unknown), SpacesRequiredBefore(0), CanBreakBefore(false), 663b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko ClosesTemplateDeclaration(false), ParameterCount(0), TotalLength(0), 673b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0), 683b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko LongestObjCSelectorName(0), FakeRParens(0), LastInChainOfCalls(false), 693b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko PartOfMultiVariableDeclStmt(false), MatchingParen(NULL), Previous(NULL), 703b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Next(NULL) {} 713b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 723b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The \c Token. 733b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Token Tok; 743b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 753b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The number of newlines immediately before the \c Token. 763b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 773b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// This can be used to determine what the user wrote in the original code 783b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// and thereby e.g. leave an empty line between two function definitions. 793b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned NewlinesBefore; 803b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 813b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Whether there is at least one unescaped newline before the \c 823b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// Token. 833b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool HasUnescapedNewline; 843b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 853b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The range of the whitespace immediately preceeding the \c Token. 863b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko SourceRange WhitespaceRange; 873b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 883b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The offset just past the last '\n' in this token's leading 893b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'. 903b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned LastNewlineOffset; 913b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 923b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The length of the non-whitespace parts of the token. This is 933b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// necessary because we need to handle escaped newlines that are stored 943b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// with the token. 953b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned TokenLength; 963b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 973b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Indicates that this is the first token. 983b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool IsFirst; 993b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1003b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Whether there must be a line break before this token. 1013b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 1023b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// This happens for example when a preprocessor directive ended directly 1033b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// before the token. 1043b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool MustBreakBefore; 1053b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1063b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Returns actual token start location without leading escaped 1073b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// newlines and whitespace. 1083b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 1093b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// This can be different to Tok.getLocation(), which includes leading escaped 1103b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// newlines. 1113b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko SourceLocation getStartOfNonWhitespace() const { 1123b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return WhitespaceRange.getEnd(); 1133b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 1143b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1153b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The raw text of the token. 1163b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 1173b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// Contains the raw token text without leading whitespace and without leading 1183b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// escaped newlines. 1193b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko StringRef TokenText; 1203b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1213b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko TokenType Type; 1223b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1233b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned SpacesRequiredBefore; 1243b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool CanBreakBefore; 1253b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1263b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool ClosesTemplateDeclaration; 1273b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1283b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Number of parameters, if this is "(", "[" or "<". 1293b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 1303b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// This is initialized to 1 as we don't need to distinguish functions with 1313b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 0 parameters from functions with 1 parameter. Thus, we can simply count 1323b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// the number of commas. 1333b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned ParameterCount; 1343b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1353b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The total length of the line up to and including this token. 1363b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned TotalLength; 1373b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1383b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The length of following tokens until the next natural split point, 1393b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// or the next token that can be broken. 1403b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned UnbreakableTailLength; 1413b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1423b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko // FIXME: Come up with a 'cleaner' concept. 1433b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief The binding strength of a token. This is a combined value of 1443b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// operator precedence, parenthesis nesting, etc. 1453b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned BindingStrength; 1463b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1473b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Penalty for inserting a line break before this token. 1483b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned SplitPenalty; 1493b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1503b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief If this is the first ObjC selector name in an ObjC method 1513b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// definition or call, this contains the length of the longest name. 1523b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned LongestObjCSelectorName; 1533b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1543b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Stores the number of required fake parentheses and the 1553b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// corresponding operator precedence. 1563b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 1573b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// If multiple fake parentheses start at a token, this vector stores them in 1583b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// reverse order, i.e. inner fake parenthesis first. 1593b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko SmallVector<prec::Level, 4> FakeLParens; 1603b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Insert this many fake ) after this token for correct indentation. 1613b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko unsigned FakeRParens; 1623b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1633b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Is this the last "." or "->" in a builder-type call? 1643b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool LastInChainOfCalls; 1653b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1663b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Is this token part of a \c DeclStmt defining multiple variables? 1673b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// 1683b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// Only set if \c Type == \c TT_StartOfName. 1693b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool PartOfMultiVariableDeclStmt; 1703b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1713b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool is(tok::TokenKind Kind) const { return Tok.is(Kind); } 1723b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1733b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { 1743b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return is(K1) || is(K2); 1753b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 1763b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1773b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const { 1783b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return is(K1) || is(K2) || is(K3); 1793b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 1803b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1813b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3, 1823b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS, 1833b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K6 = tok::NUM_TOKENS, 1843b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K7 = tok::NUM_TOKENS, 1853b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K8 = tok::NUM_TOKENS, 1863b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K9 = tok::NUM_TOKENS, 1873b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K10 = tok::NUM_TOKENS, 1883b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K11 = tok::NUM_TOKENS, 1893b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko tok::TokenKind K12 = tok::NUM_TOKENS) const { 1903b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) || 1913b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko is(K8) || is(K9) || is(K10) || is(K11) || is(K12); 1923b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 1933b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1943b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); } 1953b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 1963b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { 1973b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return Tok.isObjCAtKeyword(Kind); 1983b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 1993b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2003b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isAccessSpecifier(bool ColonRequired = true) const { 2013b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) && 2023b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko (!ColonRequired || (Next && Next->is(tok::colon))); 2033b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2043b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2053b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isObjCAccessSpecifier() const { 2063b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) || 2073b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Next->isObjCAtKeyword(tok::objc_protected) || 2083b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Next->isObjCAtKeyword(tok::objc_package) || 2093b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Next->isObjCAtKeyword(tok::objc_private)); 2103b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2113b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2123b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Returns whether \p Tok is ([{ or a template opening <. 2133b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool opensScope() const { 2143b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) || 2153b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Type == TT_TemplateOpener; 2163b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2173b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2183b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Returns whether \p Tok is )]} or a template opening >. 2193b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool closesScope() const { 2203b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) || 2213b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Type == TT_TemplateCloser; 2223b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2233b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2243b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isUnaryOperator() const { 2253b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko switch (Tok.getKind()) { 2263b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::plus: 2273b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::plusplus: 2283b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::minus: 2293b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::minusminus: 2303b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::exclaim: 2313b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::tilde: 2323b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::kw_sizeof: 2333b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko case tok::kw_alignof: 2343b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return true; 2353b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko default: 2363b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return false; 2373b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2383b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2393b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isBinaryOperator() const { 2403b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko // Comma is a binary operator, but does not behave as such wrt. formatting. 2413b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return getPrecedence() > prec::Comma; 2423b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2433b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko bool isTrailingComment() const { 2443b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return is(tok::comment) && (!Next || Next->NewlinesBefore > 0); 2453b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2463b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2473b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko prec::Level getPrecedence() const { 2483b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return getBinOpPrecedence(Tok.getKind(), true, true); 2493b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2503b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2513b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Returns the previous token ignoring comments. 2523b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken *getPreviousNoneComment() const { 2533b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken *Tok = Previous; 2543b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko while (Tok != NULL && Tok->is(tok::comment)) 2553b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Tok = Tok->Previous; 2563b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return Tok; 2573b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2583b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2593b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko /// \brief Returns the next token ignoring comments. 2603b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko const FormatToken *getNextNoneComment() const { 2613b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko const FormatToken *Tok = Next; 2623b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko while (Tok != NULL && Tok->is(tok::comment)) 2633b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko Tok = Tok->Next; 2643b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko return Tok; 2653b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko } 2663b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2673b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken *MatchingParen; 2683b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2693b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken *Previous; 2703b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken *Next; 2713b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2723b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienkoprivate: 2733b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko // Disallow copying. 2743b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko FormatToken(const FormatToken &); 2753b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko void operator=(const FormatToken &); 2763b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko}; 2773b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2783b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko} // namespace format 2793b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko} // namespace clang 2803b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko 2813b71155dc93fb3556dfe8b758ae6e448b6fbb9c8Alexander Kornienko#endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H 282