blob: c049ac607d3f0ac101b210b9f7bb52662394ca0c [file] [log] [blame]
Daniel Jasperbac016b2012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
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 contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
14/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
15/// where it can be used to format real code.
16///
17//===----------------------------------------------------------------------===//
18
19#include "UnwrappedLineParser.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000020#include "llvm/Support/raw_ostream.h"
21
22namespace clang {
23namespace format {
24
Manuel Klimekd4397b92013-01-04 23:34:14 +000025class ScopedMacroState : public FormatTokenSource {
26public:
27 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
28 FormatToken &ResetToken)
29 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimekc37b4d62013-01-05 22:14:16 +000030 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000031 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000032 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000033 Line.InPPDirective = true;
34 }
35
36 ~ScopedMacroState() {
37 TokenSource = PreviousTokenSource;
38 ResetToken = Token;
39 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000040 Line.Level = PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000041 }
42
43 virtual FormatToken getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000044 // The \c UnwrappedLineParser guards against this by never calling
45 // \c getNextToken() after it has encountered the first eof token.
46 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000047 Token = PreviousTokenSource->getNextToken();
48 if (eof())
49 return createEOF();
50 return Token;
51 }
52
53private:
54 bool eof() {
55 return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
56 }
57
58 FormatToken createEOF() {
59 FormatToken FormatTok;
60 FormatTok.Tok.startToken();
61 FormatTok.Tok.setKind(tok::eof);
62 return FormatTok;
63 }
64
65 UnwrappedLine &Line;
66 FormatTokenSource *&TokenSource;
67 FormatToken &ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000068 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000069 FormatTokenSource *PreviousTokenSource;
70
71 FormatToken Token;
72};
73
Alexander Kornienko469a21b2012-12-07 16:15:44 +000074UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
75 FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +000076 UnwrappedLineConsumer &Callback)
Manuel Klimek526ed112013-01-09 15:25:02 +000077 : Line(new UnwrappedLine), RootTokenInitialized(false),
78 LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Style(Style),
79 Tokens(&Tokens), Callback(Callback) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000080}
81
Alexander Kornienkocff563c2012-12-04 17:27:50 +000082bool UnwrappedLineParser::parse() {
Manuel Klimekd4397b92013-01-04 23:34:14 +000083 readToken();
84 return parseFile();
85}
86
87bool UnwrappedLineParser::parseFile() {
Manuel Klimeka5342db2013-01-06 20:07:31 +000088 bool Error = parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +000089 // Make sure to format the remaining tokens.
90 addUnwrappedLine();
91 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +000092}
93
Manuel Klimeka5342db2013-01-06 20:07:31 +000094bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Alexander Kornienkocff563c2012-12-04 17:27:50 +000095 bool Error = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +000096 do {
97 switch (FormatTok.Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +000099 nextToken();
100 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000101 break;
102 case tok::l_brace:
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000103 Error |= parseBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000104 addUnwrappedLine();
105 break;
106 case tok::r_brace:
Manuel Klimeka5342db2013-01-06 20:07:31 +0000107 if (HasOpeningBrace) {
108 return false;
109 } else {
110 // Stray '}' is an error.
111 Error = true;
112 nextToken();
113 addUnwrappedLine();
114 }
115 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000116 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000117 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000118 break;
119 }
120 } while (!eof());
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000121 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000122}
123
Alexander Kornienko15757312012-12-06 18:03:27 +0000124bool UnwrappedLineParser::parseBlock(unsigned AddLevels) {
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +0000125 assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000126 nextToken();
127
Daniel Jasperbac016b2012-12-03 18:12:45 +0000128 addUnwrappedLine();
129
Manuel Klimek526ed112013-01-09 15:25:02 +0000130 Line->Level += AddLevels;
Manuel Klimeka5342db2013-01-06 20:07:31 +0000131 parseLevel(/*HasOpeningBrace=*/true);
Manuel Klimek526ed112013-01-09 15:25:02 +0000132 Line->Level -= AddLevels;
Alexander Kornienko15757312012-12-06 18:03:27 +0000133
Alexander Kornienko393b0082012-12-04 15:40:36 +0000134 if (!FormatTok.Tok.is(tok::r_brace))
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000135 return true;
Alexander Kornienko393b0082012-12-04 15:40:36 +0000136
Manuel Klimekde768542013-01-07 18:10:23 +0000137 nextToken(); // Munch the closing brace.
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000138 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000139}
140
141void UnwrappedLineParser::parsePPDirective() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000142 assert(FormatTok.Tok.is(tok::hash) && "'#' expected");
Manuel Klimek526ed112013-01-09 15:25:02 +0000143 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka080a182013-01-02 16:30:12 +0000144 nextToken();
145
Manuel Klimeka080a182013-01-02 16:30:12 +0000146 if (FormatTok.Tok.getIdentifierInfo() == NULL) {
147 addUnwrappedLine();
Manuel Klimeka080a182013-01-02 16:30:12 +0000148 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000149 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000150
Manuel Klimekd4397b92013-01-04 23:34:14 +0000151 switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
152 case tok::pp_define:
153 parsePPDefine();
154 break;
155 default:
156 parsePPUnknown();
157 break;
158 }
159}
160
161void UnwrappedLineParser::parsePPDefine() {
162 nextToken();
163
164 if (FormatTok.Tok.getKind() != tok::identifier) {
165 parsePPUnknown();
166 return;
167 }
168 nextToken();
169 if (FormatTok.Tok.getKind() == tok::l_paren) {
170 parseParens();
171 }
172 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000173 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000174
175 // Errors during a preprocessor directive can only affect the layout of the
176 // preprocessor directive, and thus we ignore them. An alternative approach
177 // would be to use the same approach we use on the file level (no
178 // re-indentation if there was a structural error) within the macro
179 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000180 parseFile();
181}
182
183void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000184 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000185 nextToken();
186 } while (!eof());
187 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000188}
189
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000190void UnwrappedLineParser::parseComments() {
Daniel Jasper33182dd2012-12-05 14:57:28 +0000191 // Consume leading line comments, e.g. for branches without compounds.
192 while (FormatTok.Tok.is(tok::comment)) {
193 nextToken();
194 addUnwrappedLine();
195 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000196}
197
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000198void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000199 parseComments();
Daniel Jasper33182dd2012-12-05 14:57:28 +0000200
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000201 int TokenNumber = 0;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000202 switch (FormatTok.Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000203 case tok::at:
204 nextToken();
205 switch (FormatTok.Tok.getObjCKeywordID()) {
206 case tok::objc_public:
207 case tok::objc_protected:
208 case tok::objc_package:
209 case tok::objc_private:
210 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000211 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000212 case tok::objc_implementation:
213 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000214 case tok::objc_protocol:
215 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000216 case tok::objc_end:
217 return; // Handled by the caller.
Nico Weber6092d4e2013-01-07 19:05:19 +0000218 default:
219 break;
220 }
221 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000222 case tok::kw_namespace:
223 parseNamespace();
224 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000225 case tok::kw_inline:
226 nextToken();
227 TokenNumber++;
228 if (FormatTok.Tok.is(tok::kw_namespace)) {
229 parseNamespace();
230 return;
231 }
232 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000233 case tok::kw_public:
234 case tok::kw_protected:
235 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000236 parseAccessSpecifier();
237 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000238 case tok::kw_if:
239 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000240 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000241 case tok::kw_for:
242 case tok::kw_while:
243 parseForOrWhileLoop();
244 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000245 case tok::kw_do:
246 parseDoWhile();
247 return;
248 case tok::kw_switch:
249 parseSwitch();
250 return;
251 case tok::kw_default:
252 nextToken();
253 parseLabel();
254 return;
255 case tok::kw_case:
256 parseCaseLabel();
257 return;
258 default:
259 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000260 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000261 do {
262 ++TokenNumber;
263 switch (FormatTok.Tok.getKind()) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000264 case tok::kw_enum:
265 parseEnum();
266 return;
Manuel Klimekde768542013-01-07 18:10:23 +0000267 case tok::kw_struct: // fallthrough
268 case tok::kw_class:
269 parseStructOrClass();
270 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000271 case tok::semi:
272 nextToken();
273 addUnwrappedLine();
274 return;
275 case tok::l_paren:
276 parseParens();
277 break;
278 case tok::l_brace:
279 parseBlock();
280 addUnwrappedLine();
281 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000282 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000283 nextToken();
284 if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
285 parseLabel();
286 return;
287 }
288 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000289 case tok::equal:
290 nextToken();
291 // Skip initializers as they will be formatted by a later step.
292 if (FormatTok.Tok.is(tok::l_brace))
293 nextToken();
294 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000295 default:
296 nextToken();
297 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000298 }
299 } while (!eof());
300}
301
302void UnwrappedLineParser::parseParens() {
303 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
304 nextToken();
305 do {
306 switch (FormatTok.Tok.getKind()) {
307 case tok::l_paren:
308 parseParens();
309 break;
310 case tok::r_paren:
311 nextToken();
312 return;
313 default:
314 nextToken();
315 break;
316 }
317 } while (!eof());
318}
319
320void UnwrappedLineParser::parseIfThenElse() {
321 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
322 nextToken();
323 parseParens();
324 bool NeedsUnwrappedLine = false;
325 if (FormatTok.Tok.is(tok::l_brace)) {
326 parseBlock();
327 NeedsUnwrappedLine = true;
328 } else {
329 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000330 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000331 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000332 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000333 }
334 if (FormatTok.Tok.is(tok::kw_else)) {
335 nextToken();
336 if (FormatTok.Tok.is(tok::l_brace)) {
337 parseBlock();
338 addUnwrappedLine();
339 } else if (FormatTok.Tok.is(tok::kw_if)) {
340 parseIfThenElse();
341 } else {
342 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000343 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000344 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000345 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000346 }
347 } else if (NeedsUnwrappedLine) {
348 addUnwrappedLine();
349 }
350}
351
Alexander Kornienko15757312012-12-06 18:03:27 +0000352void UnwrappedLineParser::parseNamespace() {
353 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
354 nextToken();
355 if (FormatTok.Tok.is(tok::identifier))
356 nextToken();
357 if (FormatTok.Tok.is(tok::l_brace)) {
358 parseBlock(0);
359 addUnwrappedLine();
360 }
361 // FIXME: Add error handling.
362}
363
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000364void UnwrappedLineParser::parseForOrWhileLoop() {
365 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
366 "'for' or 'while' expected");
367 nextToken();
368 parseParens();
369 if (FormatTok.Tok.is(tok::l_brace)) {
370 parseBlock();
371 addUnwrappedLine();
372 } else {
373 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000374 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000375 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000376 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000377 }
378}
379
Daniel Jasperbac016b2012-12-03 18:12:45 +0000380void UnwrappedLineParser::parseDoWhile() {
381 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
382 nextToken();
383 if (FormatTok.Tok.is(tok::l_brace)) {
384 parseBlock();
385 } else {
386 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000387 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000388 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000389 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000390 }
391
Alexander Kornienko393b0082012-12-04 15:40:36 +0000392 // FIXME: Add error handling.
393 if (!FormatTok.Tok.is(tok::kw_while)) {
394 addUnwrappedLine();
395 return;
396 }
397
Daniel Jasperbac016b2012-12-03 18:12:45 +0000398 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000399 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000400}
401
402void UnwrappedLineParser::parseLabel() {
403 // FIXME: remove all asserts.
404 assert(FormatTok.Tok.is(tok::colon) && "':' expected");
405 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000406 unsigned OldLineLevel = Line->Level;
407 if (Line->Level > 0)
408 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000409 if (FormatTok.Tok.is(tok::l_brace)) {
410 parseBlock();
411 }
412 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000413 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000414}
415
416void UnwrappedLineParser::parseCaseLabel() {
417 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
418 // FIXME: fix handling of complex expressions here.
419 do {
420 nextToken();
421 } while (!eof() && !FormatTok.Tok.is(tok::colon));
422 parseLabel();
423}
424
425void UnwrappedLineParser::parseSwitch() {
426 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
427 nextToken();
428 parseParens();
429 if (FormatTok.Tok.is(tok::l_brace)) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000430 parseBlock(Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000431 addUnwrappedLine();
432 } else {
433 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000434 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000435 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000436 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000437 }
438}
439
440void UnwrappedLineParser::parseAccessSpecifier() {
441 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000442 // Otherwise, we don't know what it is, and we'd better keep the next token.
443 if (FormatTok.Tok.is(tok::colon))
444 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000445 addUnwrappedLine();
446}
447
448void UnwrappedLineParser::parseEnum() {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000449 bool HasContents = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000450 do {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000451 switch (FormatTok.Tok.getKind()) {
452 case tok::l_brace:
453 nextToken();
454 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000455 ++Line->Level;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000456 parseComments();
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000457 break;
458 case tok::l_paren:
459 parseParens();
460 break;
461 case tok::comma:
462 nextToken();
463 addUnwrappedLine();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000464 parseComments();
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000465 break;
466 case tok::r_brace:
467 if (HasContents)
468 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000469 --Line->Level;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000470 nextToken();
471 break;
472 case tok::semi:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000473 nextToken();
474 addUnwrappedLine();
475 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000476 default:
477 HasContents = true;
478 nextToken();
479 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000480 }
481 } while (!eof());
482}
483
Manuel Klimekde768542013-01-07 18:10:23 +0000484void UnwrappedLineParser::parseStructOrClass() {
485 nextToken();
486 do {
487 switch (FormatTok.Tok.getKind()) {
488 case tok::l_brace:
489 // FIXME: Think about how to resolve the error handling here.
490 parseBlock();
491 parseStructuralElement();
492 return;
493 case tok::semi:
494 nextToken();
495 addUnwrappedLine();
496 return;
497 default:
498 nextToken();
499 break;
500 }
501 } while (!eof());
502}
503
Nico Weber1abe6ea2013-01-09 21:15:03 +0000504void UnwrappedLineParser::parseObjCProtocolList() {
505 assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
506 do
507 nextToken();
508 while (!eof() && FormatTok.Tok.isNot(tok::greater));
509 nextToken(); // Skip '>'.
510}
511
512void UnwrappedLineParser::parseObjCUntilAtEnd() {
513 do {
514 if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
515 nextToken();
516 addUnwrappedLine();
517 break;
518 }
519 parseStructuralElement();
520 } while (!eof());
521}
522
Nico Weber50767d82013-01-09 23:25:37 +0000523void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000524 nextToken();
525 nextToken(); // interface name
526
527 // @interface can be followed by either a base class, or a category.
528 if (FormatTok.Tok.is(tok::colon)) {
529 nextToken();
530 nextToken(); // base class name
531 } else if (FormatTok.Tok.is(tok::l_paren))
532 // Skip category, if present.
533 parseParens();
534
Nico Weber1abe6ea2013-01-09 21:15:03 +0000535 if (FormatTok.Tok.is(tok::less))
536 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000537
538 // If instance variables are present, keep the '{' on the first line too.
539 if (FormatTok.Tok.is(tok::l_brace))
540 parseBlock();
541
542 // With instance variables, this puts '}' on its own line. Without instance
543 // variables, this ends the @interface line.
544 addUnwrappedLine();
545
Nico Weber1abe6ea2013-01-09 21:15:03 +0000546 parseObjCUntilAtEnd();
547}
Nico Weber27d13672013-01-09 20:25:35 +0000548
Nico Weber1abe6ea2013-01-09 21:15:03 +0000549void UnwrappedLineParser::parseObjCProtocol() {
550 nextToken();
551 nextToken(); // protocol name
552
553 if (FormatTok.Tok.is(tok::less))
554 parseObjCProtocolList();
555
556 // Check for protocol declaration.
557 if (FormatTok.Tok.is(tok::semi)) {
558 nextToken();
559 return addUnwrappedLine();
560 }
561
562 addUnwrappedLine();
563 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +0000564}
565
Daniel Jasperbac016b2012-12-03 18:12:45 +0000566void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000567 if (!RootTokenInitialized)
568 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000569 // Consume trailing comments.
570 while (!eof() && FormatTok.NewlinesBefore == 0 &&
571 FormatTok.Tok.is(tok::comment)) {
572 nextToken();
573 }
Manuel Klimek526ed112013-01-09 15:25:02 +0000574 Callback.consumeUnwrappedLine(*Line);
Daniel Jasper26f7e782013-01-08 14:56:18 +0000575 RootTokenInitialized = false;
Manuel Klimek526ed112013-01-09 15:25:02 +0000576 LastInCurrentLine = NULL;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000577}
578
579bool UnwrappedLineParser::eof() const {
580 return FormatTok.Tok.is(tok::eof);
581}
582
583void UnwrappedLineParser::nextToken() {
584 if (eof())
585 return;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000586 if (RootTokenInitialized) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000587 assert(LastInCurrentLine->Children.empty());
Daniel Jasper26f7e782013-01-08 14:56:18 +0000588 LastInCurrentLine->Children.push_back(FormatTok);
589 LastInCurrentLine = &LastInCurrentLine->Children.back();
590 } else {
Manuel Klimek526ed112013-01-09 15:25:02 +0000591 Line->RootToken = FormatTok;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000592 RootTokenInitialized = true;
Manuel Klimek526ed112013-01-09 15:25:02 +0000593 LastInCurrentLine = &Line->RootToken;
594 }
595 if (MustBreakBeforeNextToken) {
596 LastInCurrentLine->MustBreakBefore = true;
597 MustBreakBeforeNextToken = false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000598 }
Manuel Klimekd4397b92013-01-04 23:34:14 +0000599 readToken();
600}
601
602void UnwrappedLineParser::readToken() {
603 FormatTok = Tokens->getNextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000604 while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000605 ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
606 FormatTok.IsFirst)) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000607 UnwrappedLine* StoredLine = Line.take();
608 Line.reset(new UnwrappedLine(*StoredLine));
609 assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());
610 FormatToken *StoredLastInCurrentLine = LastInCurrentLine;
611 bool PreviousInitialized = RootTokenInitialized;
612 RootTokenInitialized = false;
613 LastInCurrentLine = NULL;
614
Manuel Klimekd4397b92013-01-04 23:34:14 +0000615 parsePPDirective();
Manuel Klimek526ed112013-01-09 15:25:02 +0000616
617 assert(!RootTokenInitialized);
618 Line.reset(StoredLine);
619 RootTokenInitialized = PreviousInitialized;
620 LastInCurrentLine = StoredLastInCurrentLine;
621 assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());
622 MustBreakBeforeNextToken = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000623 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000624}
625
Daniel Jaspercd162382013-01-07 13:26:07 +0000626} // end namespace format
627} // end namespace clang