blob: dfe1d1ddebddb535897dc2ed9ba0c3446f38c21c [file] [log] [blame]
Daniel Jasper7a6d09b2013-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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
17#define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000018
19#include "UnwrappedLineParser.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000020#include "clang/Format/Format.h"
21#include <string>
22
23namespace clang {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000024class SourceManager;
25
26namespace format {
27
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000028enum LineType {
29 LT_Invalid,
Daniel Jasper616de8642014-11-23 16:46:28 +000030 LT_ImportStatement,
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000031 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
32 LT_ObjCMethodDecl,
Daniel Jasper616de8642014-11-23 16:46:28 +000033 LT_ObjCProperty, // An @property line.
34 LT_Other,
35 LT_PreprocessorDirective,
36 LT_VirtualFunctionDecl
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000037};
38
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000039class AnnotatedLine {
40public:
41 AnnotatedLine(const UnwrappedLine &Line)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000042 : First(Line.Tokens.front().Tok), Level(Line.Level),
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000043 InPPDirective(Line.InPPDirective),
Daniel Jasper8e357692013-05-06 08:27:33 +000044 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
Daniel Jasperbea1ab42015-03-01 18:55:26 +000045 IsMultiVariableDeclStmt(false), Affected(false),
46 LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000047 assert(!Line.Tokens.empty());
Manuel Klimek88033d72013-10-21 08:11:15 +000048
49 // Calculate Next and Previous for all tokens. Note that we must overwrite
50 // Next and Previous for every token, as previous formatting runs might have
51 // left them in a different state.
Craig Topper2145bc02014-05-09 08:15:10 +000052 First->Previous = nullptr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000053 FormatToken *Current = First;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000054 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
55 E = Line.Tokens.end();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000056 I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000057 const UnwrappedLineNode &Node = *I;
58 Current->Next = I->Tok;
59 I->Tok->Previous = Current;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000060 Current = Current->Next;
Daniel Jasper472da862013-10-24 15:23:11 +000061 Current->Children.clear();
Daniel Jaspere285b8d2015-06-17 09:43:56 +000062 for (const auto &Child : Node.Children) {
Daniel Jasperfd725c02015-01-21 17:35:29 +000063 Children.push_back(new AnnotatedLine(Child));
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000064 Current->Children.push_back(Children.back());
65 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000066 }
67 Last = Current;
Craig Topper2145bc02014-05-09 08:15:10 +000068 Last->Next = nullptr;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000069 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000070
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000071 ~AnnotatedLine() {
72 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
73 delete Children[i];
74 }
Daniel Jasperfd725c02015-01-21 17:35:29 +000075 FormatToken *Current = First;
76 while (Current) {
77 Current->Children.clear();
Daniel Jasper04b979d2015-01-21 18:35:47 +000078 Current->Role.reset();
Daniel Jasperfd725c02015-01-21 17:35:29 +000079 Current = Current->Next;
80 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000081 }
82
Daniel Jaspere285b8d2015-06-17 09:43:56 +000083 /// \c true if this line starts with the given tokens in order, ignoring
84 /// comments.
85 template <typename... Ts> bool startsWith(Ts... Tokens) const {
Eric Liu89be0472016-04-19 19:25:33 +000086 return startsWithInternal(First, Tokens...);
Daniel Jaspere285b8d2015-06-17 09:43:56 +000087 }
88
Eric Liu4cfb88a2016-04-25 15:09:22 +000089 /// \c true if this line ends with the given tokens in reversed order,
90 /// ignoring comments.
91 /// For example, given tokens [T1, T2, T3, ...], the function returns true if
92 /// this line is like "... T3 T2 T1".
93 template <typename... Ts> bool endsWith(Ts... Tokens) const {
94 return endsWithInternal(Last, Tokens...);
95 }
96
Zachary Turner448592e2015-12-18 22:20:15 +000097 /// \c true if this line looks like a function definition instead of a
98 /// function declaration. Asserts MightBeFunctionDecl.
99 bool mightBeFunctionDefinition() const {
100 assert(MightBeFunctionDecl);
101 // FIXME: Line.Last points to other characters than tok::semi
102 // and tok::lbrace.
103 return !Last->isOneOf(tok::semi, tok::comment);
104 }
105
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000106 FormatToken *First;
107 FormatToken *Last;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000108
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000109 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000110
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000111 LineType Type;
112 unsigned Level;
113 bool InPPDirective;
114 bool MustBeDeclaration;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000115 bool MightBeFunctionDecl;
Daniel Jasperbea1ab42015-03-01 18:55:26 +0000116 bool IsMultiVariableDeclStmt;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000117
Daniel Jasper5500f612013-11-25 11:08:59 +0000118 /// \c True if this line should be formatted, i.e. intersects directly or
119 /// indirectly with one of the input ranges.
120 bool Affected;
121
122 /// \c True if the leading empty lines of this line intersect with one of the
123 /// input ranges.
124 bool LeadingEmptyLinesAffected;
125
Daniel Jasper9c199562013-11-28 15:58:55 +0000126 /// \c True if a one of this line's children intersects with an input range.
127 bool ChildrenAffected;
128
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000129private:
130 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000131 AnnotatedLine(const AnnotatedLine &) = delete;
132 void operator=(const AnnotatedLine &) = delete;
Daniel Jaspere285b8d2015-06-17 09:43:56 +0000133
134 template <typename A, typename... Ts>
Eric Liu89be0472016-04-19 19:25:33 +0000135 bool startsWithInternal(const FormatToken *Tok, A K1) const {
136 // Even though we skip comments in the outer `startWithInternal` function,
137 // this loop is still necessary if it is invoked by the public interface
138 // `startsWith`.
Daniel Jaspere285b8d2015-06-17 09:43:56 +0000139 while (Tok && Tok->is(tok::comment))
140 Tok = Tok->Next;
141 return Tok && Tok->is(K1);
142 }
143
144 template <typename A, typename... Ts>
Eric Liu89be0472016-04-19 19:25:33 +0000145 bool startsWithInternal(const FormatToken *Tok, A K1, Ts... Tokens) const {
146 // Skip comments before calling `startsWithInternal(Tok, K1)` so that the
147 // second call to `startsWithInternal` takes the correct `Tok->Next`, which
148 // should be the next token of the token checked in the first call.
149 while (Tok && Tok->is(tok::comment))
150 Tok = Tok->Next;
151 return Tok && startsWithInternal(Tok, K1) &&
152 startsWithInternal(Tok->Next, Tokens...);
Daniel Jaspere285b8d2015-06-17 09:43:56 +0000153 }
Eric Liu4cfb88a2016-04-25 15:09:22 +0000154
155 template <typename A, typename... Ts>
156 bool endsWithInternal(const FormatToken *Tok, A K1) const {
157 // See the comments above in `startsWithInternal(Tok, K1)`.
158 while (Tok && Tok->is(tok::comment))
159 Tok = Tok->Previous;
160 return Tok && Tok->is(K1);
161 }
162
163 template <typename A, typename... Ts>
164 bool endsWithInternal(const FormatToken *Tok, A K1, Ts... Tokens) const {
165 // See the comments above in `startsWithInternal(Tok, K1, Tokens)`.
166 while (Tok && Tok->is(tok::comment))
167 Tok = Tok->Previous;
168 return Tok && endsWithInternal(Tok, K1) &&
169 endsWithInternal(Tok->Previous, Tokens...);
170 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000171};
172
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000173/// \brief Determines extra information about the tokens comprising an
174/// \c UnwrappedLine.
175class TokenAnnotator {
176public:
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000177 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
178 : Style(Style), Keywords(Keywords) {}
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000179
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000180 /// \brief Adapts the indent levels of comment lines to the indent of the
181 /// subsequent line.
182 // FIXME: Can/should this be done in the UnwrappedLineParser?
183 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
184
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000185 void annotate(AnnotatedLine &Line);
186 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000187
188private:
189 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +0000190 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
191 bool InFunctionDecl);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000192
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000193 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
194 const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000195
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000196 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000197
Daniel Jasperfb81b092013-09-17 09:52:48 +0000198 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
199
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000200 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000201
Zachary Turner448592e2015-12-18 22:20:15 +0000202 bool mustBreakForReturnType(const AnnotatedLine &Line) const;
203
Daniel Jasper6bee6822013-04-08 20:33:42 +0000204 void printDebugInfo(const AnnotatedLine &Line);
205
Manuel Klimek4fe43002013-05-22 12:51:29 +0000206 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
207
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000208 const FormatStyle &Style;
Nico Weber29f9dea2013-02-11 15:32:15 +0000209
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000210 const AdditionalKeywords &Keywords;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000211};
212
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000213} // end namespace format
214} // end namespace clang
215
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000216#endif