TokenAnnotator.h revision bf71ba2988b34c45af968f0965e28ac952e4a15f
132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===// 232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// 332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// The LLVM Compiler Infrastructure 432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// 532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// This file is distributed under the University of Illinois Open Source 632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// License. See LICENSE.TXT for details. 732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// 832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===----------------------------------------------------------------------===// 932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// 1032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \file 1132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief This file implements a token annotator, i.e. creates 1232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \c AnnotatedTokens out of \c FormatTokens with required extra information. 1332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// 1432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===----------------------------------------------------------------------===// 1532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H 1732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H 1832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "UnwrappedLineParser.h" 2032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "clang/Basic/OperatorPrecedence.h" 2132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "clang/Format/Format.h" 2232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include <string> 2332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jaspernamespace clang { 2532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass Lexer; 2632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass SourceManager; 2732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jaspernamespace format { 2932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 3032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperenum TokenType { 3132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_BinaryOperator, 3232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_BlockComment, 3332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_CastRParen, 3432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_ConditionalExpr, 3532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_CtorInitializerColon, 3632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_ImplicitStringLiteral, 37923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper TT_InlineASMColon, 386cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper TT_InheritanceColon, 3932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_LineComment, 40051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber TT_ObjCArrayLiteral, 4132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_ObjCBlockLParen, 4232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_ObjCDecl, 43c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber TT_ObjCForIn, 4432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_ObjCMethodExpr, 45051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber TT_ObjCMethodSpecifier, 4632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_ObjCProperty, 4763d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper TT_ObjCSelectorName, 482b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper TT_OverloadedOperatorLParen, 4932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_PointerOrReference, 5032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_PureVirtualSpecifier, 5132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_RangeBasedForLoopColon, 5232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_StartOfName, 5332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_TemplateCloser, 5432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_TemplateOpener, 5532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_TrailingUnaryOperator, 5632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_UnaryOperator, 5732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TT_Unknown 5832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 5932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 6032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperenum LineType { 6132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_Invalid, 6232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_Other, 6332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_BuilderTypeCall, 6432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_PreprocessorDirective, 6532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_VirtualFunctionDecl, 6632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_ObjCDecl, // An @interface, @implementation, or @protocol line. 6732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_ObjCMethodDecl, 6832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LT_ObjCProperty // An @property line. 6932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 7032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 7132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass AnnotatedToken { 7232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic: 7332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper explicit AnnotatedToken(const FormatToken &FormatTok) 74729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0), 7532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CanBreakBefore(false), MustBreakBefore(false), 7632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper ClosesTemplateDeclaration(false), MatchingParen(NULL), 779fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper ParameterCount(0), BindingStrength(0), SplitPenalty(0), 78bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper LongestObjCSelectorName(0), Parent(NULL), 798ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper FakeRParens(0), LastInChainOfCalls(false), 808ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper PartOfMultiVariableDeclStmt(false) {} 8132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 8232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); } 83e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko 84e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { 85e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return is(K1) || is(K2); 86e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } 87e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko 88e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const { 89e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return is(K1) || is(K2) || is(K3); 90e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } 91e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko 92e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko bool isOneOf( 93e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3, 94e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS, 95e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K6 = tok::NUM_TOKENS, tok::TokenKind K7 = tok::NUM_TOKENS, 96e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K8 = tok::NUM_TOKENS, tok::TokenKind K9 = tok::NUM_TOKENS, 97e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K10 = tok::NUM_TOKENS, 98e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K11 = tok::NUM_TOKENS, 99e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::TokenKind K12 = tok::NUM_TOKENS) const { 100e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) || 101e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko is(K8) || is(K9) || is(K10) || is(K11) || is(K12); 102e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } 103e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko 10432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); } 10532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 10632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { 10732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return FormatTok.Tok.isObjCAtKeyword(Kind); 10832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 10932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11094b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko bool isAccessSpecifier(bool ColonRequired = true) const { 11194b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) && 11294b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko (!ColonRequired || 11394b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko (!Children.empty() && Children[0].is(tok::colon))); 11494b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko } 11594b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko 11694b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko bool isObjCAccessSpecifier() const { 11794b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko return is(tok::at) && !Children.empty() && 11894b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko (Children[0].isObjCAtKeyword(tok::objc_public) || 11994b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko Children[0].isObjCAtKeyword(tok::objc_protected) || 12094b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko Children[0].isObjCAtKeyword(tok::objc_package) || 12194b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko Children[0].isObjCAtKeyword(tok::objc_private)); 12294b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko } 12394b748ff16172f0da2b842fe0d5bae438f07034fAlexander Kornienko 12432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper FormatToken FormatTok; 12532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 12632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper TokenType Type; 12732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 128729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper unsigned SpacesRequiredBefore; 12932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool CanBreakBefore; 13032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool MustBreakBefore; 13132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 13232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool ClosesTemplateDeclaration; 13332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 13432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedToken *MatchingParen; 13532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 13632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// \brief Number of parameters, if this is "(", "[" or "<". 13732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// 13832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// This is initialized to 1 as we don't need to distinguish functions with 13932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// 0 parameters from functions with 1 parameter. Thus, we can simply count 14032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// the number of commas. 14132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper unsigned ParameterCount; 14232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 14332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// \brief The total length of the line up to and including this token. 14432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper unsigned TotalLength; 14532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1460178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // FIXME: Come up with a 'cleaner' concept. 1470178673f541685cf5067814dfeee2644078e39a9Daniel Jasper /// \brief The binding strength of a token. This is a combined value of 1480178673f541685cf5067814dfeee2644078e39a9Daniel Jasper /// operator precedence, parenthesis nesting, etc. 1490178673f541685cf5067814dfeee2644078e39a9Daniel Jasper unsigned BindingStrength; 1500178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 15132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// \brief Penalty for inserting a line break before this token. 15232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper unsigned SplitPenalty; 15332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 15463d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper /// \brief If this is the first ObjC selector name in an ObjC method 15563d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper /// definition or call, this contains the length of the longest name. 15663d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper unsigned LongestObjCSelectorName; 15763d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper 15832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper std::vector<AnnotatedToken> Children; 15932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedToken *Parent; 16032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 161bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper /// \brief Stores the number of required fake parentheses and the 162bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper /// corresponding operator precedence. 163bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper /// 164bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper /// If multiple fake parentheses start at a token, this vector stores them in 165bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper /// reverse order, i.e. inner fake parenthesis first. 166bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper SmallVector<prec::Level, 4> FakeLParens; 167087387a1e9ce5abeb4f348e14f64e5c2273eaedbDaniel Jasper /// \brief Insert this many fake ) after this token for correct indentation. 16829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper unsigned FakeRParens; 16929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 170248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper /// \brief Is this the last "." or "->" in a builder-type call? 171248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper bool LastInChainOfCalls; 172248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper 1738ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper /// \brief Is this token part of a \c DeclStmt defining multiple variables? 1748ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper /// 1758ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper /// Only set if \c Type == \c TT_StartOfName. 1768ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper bool PartOfMultiVariableDeclStmt; 1778ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper 17832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper const AnnotatedToken *getPreviousNoneComment() const { 17932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedToken *Tok = Parent; 18032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (Tok != NULL && Tok->is(tok::comment)) 18132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok = Tok->Parent; 18232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Tok; 18332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 18432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 18532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 18632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass AnnotatedLine { 18732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic: 18832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedLine(const UnwrappedLine &Line) 18932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper : First(Line.Tokens.front()), Level(Line.Level), 19032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper InPPDirective(Line.InPPDirective), 1913c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper MustBeDeclaration(Line.MustBeDeclaration), 1923c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper MightBeFunctionDecl(false) { 19332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper assert(!Line.Tokens.empty()); 19432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedToken *Current = &First; 19532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(), 19632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper E = Line.Tokens.end(); 19732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper I != E; ++I) { 19832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Current->Children.push_back(AnnotatedToken(*I)); 19932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Current->Children[0].Parent = Current; 20032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Current = &Current->Children[0]; 20132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 20232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Last = Current; 20332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 20432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedLine(const AnnotatedLine &Other) 20532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper : First(Other.First), Type(Other.Type), Level(Other.Level), 20632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper InPPDirective(Other.InPPDirective), 2073c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper MustBeDeclaration(Other.MustBeDeclaration), 2083c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper MightBeFunctionDecl(Other.MightBeFunctionDecl) { 20932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Last = &First; 21032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (!Last->Children.empty()) { 21132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Last->Children[0].Parent = Last; 21232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Last = &Last->Children[0]; 21332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 21432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 21532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 21632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedToken First; 21732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper AnnotatedToken *Last; 21832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 21932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LineType Type; 22032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper unsigned Level; 22132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool InPPDirective; 22232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool MustBeDeclaration; 2233c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper bool MightBeFunctionDecl; 22432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 22532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 22632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperinline prec::Level getPrecedence(const AnnotatedToken &Tok) { 22732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true); 22832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 22932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 230bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper/// \brief Returns whether \p Tok is ([{ or a template opening <. 231bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasperbool opensScope(const AnnotatedToken &Tok); 232bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper/// \brief Returns whether \p Tok is )]} or a template opening >. 233bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasperbool closesScope(const AnnotatedToken &Tok); 234bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper 23532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief Determines extra information about the tokens comprising an 23632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \c UnwrappedLine. 23732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass TokenAnnotator { 23832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic: 239c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex, 240c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber IdentifierInfo &Ident_in) 241c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) { 24232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 24332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2448ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper void annotate(AnnotatedLine &Line); 2458ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper void calculateFormattingInformation(AnnotatedLine &Line); 24632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 24732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperprivate: 24832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper /// \brief Calculate the penalty for splitting before \c Tok. 2498ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok); 25032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2518ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper bool spaceRequiredBetween(const AnnotatedLine &Line, 2528ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper const AnnotatedToken &Left, 25332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper const AnnotatedToken &Right); 25432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2558ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper bool spaceRequiredBefore(const AnnotatedLine &Line, 2568ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper const AnnotatedToken &Tok); 25732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2588ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right); 25932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 260bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper void printDebugInfo(const AnnotatedLine &Line); 261bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper 2628ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper const FormatStyle &Style; 26332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper SourceManager &SourceMgr; 26432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Lexer &Lex; 265c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber 266c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber // Contextual keywords: 267c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber IdentifierInfo &Ident_in; 26832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 26932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 27032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // end namespace format 27132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // end namespace clang 27232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 27332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H 274