blob: 929684400bc9e33be2a2951fb75d5f5ae29d7936 [file] [log] [blame]
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001//===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements a token annotator, i.e. creates
12/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
17#define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
18
19#include "UnwrappedLineParser.h"
20#include "clang/Basic/OperatorPrecedence.h"
21#include "clang/Format/Format.h"
22#include <string>
23
24namespace clang {
25class Lexer;
26class SourceManager;
27
28namespace format {
29
30enum TokenType {
31 TT_BinaryOperator,
32 TT_BlockComment,
33 TT_CastRParen,
34 TT_ConditionalExpr,
35 TT_CtorInitializerColon,
36 TT_ImplicitStringLiteral,
Daniel Jasper923ebef2013-03-14 13:45:21 +000037 TT_InlineASMColon,
Daniel Jasper6cabab42013-02-14 08:42:54 +000038 TT_InheritanceColon,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000039 TT_LineComment,
Nico Weber051860e2013-02-10 02:08:05 +000040 TT_ObjCArrayLiteral,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000041 TT_ObjCBlockLParen,
42 TT_ObjCDecl,
Nico Weberc2e6d2a2013-02-11 15:32:15 +000043 TT_ObjCForIn,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000044 TT_ObjCMethodExpr,
Nico Weber051860e2013-02-10 02:08:05 +000045 TT_ObjCMethodSpecifier,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000046 TT_ObjCProperty,
Daniel Jasper63d7ced2013-02-05 10:07:47 +000047 TT_ObjCSelectorName,
Daniel Jasper2b4c9242013-02-11 08:01:18 +000048 TT_OverloadedOperatorLParen,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000049 TT_PointerOrReference,
50 TT_PureVirtualSpecifier,
51 TT_RangeBasedForLoopColon,
52 TT_StartOfName,
53 TT_TemplateCloser,
54 TT_TemplateOpener,
55 TT_TrailingUnaryOperator,
56 TT_UnaryOperator,
57 TT_Unknown
58};
59
60enum LineType {
61 LT_Invalid,
62 LT_Other,
63 LT_BuilderTypeCall,
64 LT_PreprocessorDirective,
65 LT_VirtualFunctionDecl,
66 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
67 LT_ObjCMethodDecl,
68 LT_ObjCProperty // An @property line.
69};
70
71class AnnotatedToken {
72public:
73 explicit AnnotatedToken(const FormatToken &FormatTok)
Daniel Jasper729a7432013-02-11 12:36:37 +000074 : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0),
Daniel Jasper32d28ee2013-01-29 21:01:14 +000075 CanBreakBefore(false), MustBreakBefore(false),
76 ClosesTemplateDeclaration(false), MatchingParen(NULL),
Daniel Jasper9fc56f22013-02-14 15:01:34 +000077 ParameterCount(0), BindingStrength(0), SplitPenalty(0),
Daniel Jasper29f123b2013-02-08 15:28:42 +000078 LongestObjCSelectorName(0), Parent(NULL), FakeLParens(0),
Daniel Jasper24849712013-03-01 16:48:32 +000079 FakeRParens(0), LastInChainOfCalls(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000080 }
81
82 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
Alexander Kornienkoe74de282013-03-13 14:41:29 +000083
84 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
85 return is(K1) || is(K2);
86 }
87
88 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
89 return is(K1) || is(K2) || is(K3);
90 }
91
92 bool isOneOf(
93 tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
94 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
95 tok::TokenKind K6 = tok::NUM_TOKENS, tok::TokenKind K7 = tok::NUM_TOKENS,
96 tok::TokenKind K8 = tok::NUM_TOKENS, tok::TokenKind K9 = tok::NUM_TOKENS,
97 tok::TokenKind K10 = tok::NUM_TOKENS,
98 tok::TokenKind K11 = tok::NUM_TOKENS,
99 tok::TokenKind K12 = tok::NUM_TOKENS) const {
100 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
101 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
102 }
103
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000104 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
105
106 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
107 return FormatTok.Tok.isObjCAtKeyword(Kind);
108 }
109
110 FormatToken FormatTok;
111
112 TokenType Type;
113
Daniel Jasper729a7432013-02-11 12:36:37 +0000114 unsigned SpacesRequiredBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000115 bool CanBreakBefore;
116 bool MustBreakBefore;
117
118 bool ClosesTemplateDeclaration;
119
120 AnnotatedToken *MatchingParen;
121
122 /// \brief Number of parameters, if this is "(", "[" or "<".
123 ///
124 /// This is initialized to 1 as we don't need to distinguish functions with
125 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
126 /// the number of commas.
127 unsigned ParameterCount;
128
129 /// \brief The total length of the line up to and including this token.
130 unsigned TotalLength;
131
Daniel Jasper01786732013-02-04 07:21:18 +0000132 // FIXME: Come up with a 'cleaner' concept.
133 /// \brief The binding strength of a token. This is a combined value of
134 /// operator precedence, parenthesis nesting, etc.
135 unsigned BindingStrength;
136
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000137 /// \brief Penalty for inserting a line break before this token.
138 unsigned SplitPenalty;
139
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000140 /// \brief If this is the first ObjC selector name in an ObjC method
141 /// definition or call, this contains the length of the longest name.
142 unsigned LongestObjCSelectorName;
143
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000144 std::vector<AnnotatedToken> Children;
145 AnnotatedToken *Parent;
146
Daniel Jasper29f123b2013-02-08 15:28:42 +0000147 /// \brief Insert this many fake ( before this token for correct indentation.
148 unsigned FakeLParens;
Daniel Jasper087387a2013-02-08 16:49:27 +0000149 /// \brief Insert this many fake ) after this token for correct indentation.
Daniel Jasper29f123b2013-02-08 15:28:42 +0000150 unsigned FakeRParens;
151
Daniel Jasper24849712013-03-01 16:48:32 +0000152 /// \brief Is this the last "." or "->" in a builder-type call?
153 bool LastInChainOfCalls;
154
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000155 const AnnotatedToken *getPreviousNoneComment() const {
156 AnnotatedToken *Tok = Parent;
157 while (Tok != NULL && Tok->is(tok::comment))
158 Tok = Tok->Parent;
159 return Tok;
160 }
161};
162
163class AnnotatedLine {
164public:
165 AnnotatedLine(const UnwrappedLine &Line)
166 : First(Line.Tokens.front()), Level(Line.Level),
167 InPPDirective(Line.InPPDirective),
Daniel Jasper3c08a812013-02-24 18:54:32 +0000168 MustBeDeclaration(Line.MustBeDeclaration),
169 MightBeFunctionDecl(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000170 assert(!Line.Tokens.empty());
171 AnnotatedToken *Current = &First;
172 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
173 E = Line.Tokens.end();
174 I != E; ++I) {
175 Current->Children.push_back(AnnotatedToken(*I));
176 Current->Children[0].Parent = Current;
177 Current = &Current->Children[0];
178 }
179 Last = Current;
180 }
181 AnnotatedLine(const AnnotatedLine &Other)
182 : First(Other.First), Type(Other.Type), Level(Other.Level),
183 InPPDirective(Other.InPPDirective),
Daniel Jasper3c08a812013-02-24 18:54:32 +0000184 MustBeDeclaration(Other.MustBeDeclaration),
185 MightBeFunctionDecl(Other.MightBeFunctionDecl) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000186 Last = &First;
187 while (!Last->Children.empty()) {
188 Last->Children[0].Parent = Last;
189 Last = &Last->Children[0];
190 }
191 }
192
193 AnnotatedToken First;
194 AnnotatedToken *Last;
195
196 LineType Type;
197 unsigned Level;
198 bool InPPDirective;
199 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000200 bool MightBeFunctionDecl;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000201};
202
203inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
204 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
205}
206
207/// \brief Determines extra information about the tokens comprising an
208/// \c UnwrappedLine.
209class TokenAnnotator {
210public:
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000211 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
212 IdentifierInfo &Ident_in)
213 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000214 }
215
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000216 void annotate(AnnotatedLine &Line);
217 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000218
219private:
220 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000221 unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000222
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000223 bool spaceRequiredBetween(const AnnotatedLine &Line,
224 const AnnotatedToken &Left,
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000225 const AnnotatedToken &Right);
226
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000227 bool spaceRequiredBefore(const AnnotatedLine &Line,
228 const AnnotatedToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000229
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000230 bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000231
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000232 const FormatStyle &Style;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000233 SourceManager &SourceMgr;
234 Lexer &Lex;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000235
236 // Contextual keywords:
237 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000238};
239
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000240} // end namespace format
241} // end namespace clang
242
243#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H