blob: 688937c1e6aa5af2f691c0fab159f32cccf66b0a [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:
212 return parseObjCInterface();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000213 case tok::objc_protocol:
214 return parseObjCProtocol();
Nico Weber6092d4e2013-01-07 19:05:19 +0000215 default:
216 break;
217 }
218 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000219 case tok::kw_namespace:
220 parseNamespace();
221 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000222 case tok::kw_inline:
223 nextToken();
224 TokenNumber++;
225 if (FormatTok.Tok.is(tok::kw_namespace)) {
226 parseNamespace();
227 return;
228 }
229 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000230 case tok::kw_public:
231 case tok::kw_protected:
232 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000233 parseAccessSpecifier();
234 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000235 case tok::kw_if:
236 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000237 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000238 case tok::kw_for:
239 case tok::kw_while:
240 parseForOrWhileLoop();
241 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000242 case tok::kw_do:
243 parseDoWhile();
244 return;
245 case tok::kw_switch:
246 parseSwitch();
247 return;
248 case tok::kw_default:
249 nextToken();
250 parseLabel();
251 return;
252 case tok::kw_case:
253 parseCaseLabel();
254 return;
255 default:
256 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000257 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000258 do {
259 ++TokenNumber;
260 switch (FormatTok.Tok.getKind()) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000261 case tok::kw_enum:
262 parseEnum();
263 return;
Manuel Klimekde768542013-01-07 18:10:23 +0000264 case tok::kw_struct: // fallthrough
265 case tok::kw_class:
266 parseStructOrClass();
267 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000268 case tok::semi:
269 nextToken();
270 addUnwrappedLine();
271 return;
272 case tok::l_paren:
273 parseParens();
274 break;
275 case tok::l_brace:
276 parseBlock();
277 addUnwrappedLine();
278 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000279 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000280 nextToken();
281 if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
282 parseLabel();
283 return;
284 }
285 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000286 case tok::equal:
287 nextToken();
288 // Skip initializers as they will be formatted by a later step.
289 if (FormatTok.Tok.is(tok::l_brace))
290 nextToken();
291 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000292 default:
293 nextToken();
294 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000295 }
296 } while (!eof());
297}
298
299void UnwrappedLineParser::parseParens() {
300 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
301 nextToken();
302 do {
303 switch (FormatTok.Tok.getKind()) {
304 case tok::l_paren:
305 parseParens();
306 break;
307 case tok::r_paren:
308 nextToken();
309 return;
310 default:
311 nextToken();
312 break;
313 }
314 } while (!eof());
315}
316
317void UnwrappedLineParser::parseIfThenElse() {
318 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
319 nextToken();
320 parseParens();
321 bool NeedsUnwrappedLine = false;
322 if (FormatTok.Tok.is(tok::l_brace)) {
323 parseBlock();
324 NeedsUnwrappedLine = true;
325 } else {
326 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000327 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000328 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000329 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000330 }
331 if (FormatTok.Tok.is(tok::kw_else)) {
332 nextToken();
333 if (FormatTok.Tok.is(tok::l_brace)) {
334 parseBlock();
335 addUnwrappedLine();
336 } else if (FormatTok.Tok.is(tok::kw_if)) {
337 parseIfThenElse();
338 } else {
339 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000340 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000341 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000342 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000343 }
344 } else if (NeedsUnwrappedLine) {
345 addUnwrappedLine();
346 }
347}
348
Alexander Kornienko15757312012-12-06 18:03:27 +0000349void UnwrappedLineParser::parseNamespace() {
350 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
351 nextToken();
352 if (FormatTok.Tok.is(tok::identifier))
353 nextToken();
354 if (FormatTok.Tok.is(tok::l_brace)) {
355 parseBlock(0);
356 addUnwrappedLine();
357 }
358 // FIXME: Add error handling.
359}
360
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000361void UnwrappedLineParser::parseForOrWhileLoop() {
362 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
363 "'for' or 'while' expected");
364 nextToken();
365 parseParens();
366 if (FormatTok.Tok.is(tok::l_brace)) {
367 parseBlock();
368 addUnwrappedLine();
369 } else {
370 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000371 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000372 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000373 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000374 }
375}
376
Daniel Jasperbac016b2012-12-03 18:12:45 +0000377void UnwrappedLineParser::parseDoWhile() {
378 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
379 nextToken();
380 if (FormatTok.Tok.is(tok::l_brace)) {
381 parseBlock();
382 } else {
383 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000384 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000385 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000386 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000387 }
388
Alexander Kornienko393b0082012-12-04 15:40:36 +0000389 // FIXME: Add error handling.
390 if (!FormatTok.Tok.is(tok::kw_while)) {
391 addUnwrappedLine();
392 return;
393 }
394
Daniel Jasperbac016b2012-12-03 18:12:45 +0000395 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000396 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000397}
398
399void UnwrappedLineParser::parseLabel() {
400 // FIXME: remove all asserts.
401 assert(FormatTok.Tok.is(tok::colon) && "':' expected");
402 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000403 unsigned OldLineLevel = Line->Level;
404 if (Line->Level > 0)
405 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000406 if (FormatTok.Tok.is(tok::l_brace)) {
407 parseBlock();
408 }
409 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000410 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000411}
412
413void UnwrappedLineParser::parseCaseLabel() {
414 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
415 // FIXME: fix handling of complex expressions here.
416 do {
417 nextToken();
418 } while (!eof() && !FormatTok.Tok.is(tok::colon));
419 parseLabel();
420}
421
422void UnwrappedLineParser::parseSwitch() {
423 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
424 nextToken();
425 parseParens();
426 if (FormatTok.Tok.is(tok::l_brace)) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000427 parseBlock(Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000428 addUnwrappedLine();
429 } else {
430 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000431 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000432 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000433 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000434 }
435}
436
437void UnwrappedLineParser::parseAccessSpecifier() {
438 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000439 // Otherwise, we don't know what it is, and we'd better keep the next token.
440 if (FormatTok.Tok.is(tok::colon))
441 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000442 addUnwrappedLine();
443}
444
445void UnwrappedLineParser::parseEnum() {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000446 bool HasContents = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000447 do {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000448 switch (FormatTok.Tok.getKind()) {
449 case tok::l_brace:
450 nextToken();
451 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000452 ++Line->Level;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000453 parseComments();
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000454 break;
455 case tok::l_paren:
456 parseParens();
457 break;
458 case tok::comma:
459 nextToken();
460 addUnwrappedLine();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000461 parseComments();
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000462 break;
463 case tok::r_brace:
464 if (HasContents)
465 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000466 --Line->Level;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000467 nextToken();
468 break;
469 case tok::semi:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000470 nextToken();
471 addUnwrappedLine();
472 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000473 default:
474 HasContents = true;
475 nextToken();
476 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000477 }
478 } while (!eof());
479}
480
Manuel Klimekde768542013-01-07 18:10:23 +0000481void UnwrappedLineParser::parseStructOrClass() {
482 nextToken();
483 do {
484 switch (FormatTok.Tok.getKind()) {
485 case tok::l_brace:
486 // FIXME: Think about how to resolve the error handling here.
487 parseBlock();
488 parseStructuralElement();
489 return;
490 case tok::semi:
491 nextToken();
492 addUnwrappedLine();
493 return;
494 default:
495 nextToken();
496 break;
497 }
498 } while (!eof());
499}
500
Nico Weber1abe6ea2013-01-09 21:15:03 +0000501void UnwrappedLineParser::parseObjCProtocolList() {
502 assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
503 do
504 nextToken();
505 while (!eof() && FormatTok.Tok.isNot(tok::greater));
506 nextToken(); // Skip '>'.
507}
508
509void UnwrappedLineParser::parseObjCUntilAtEnd() {
510 do {
511 if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
512 nextToken();
513 addUnwrappedLine();
514 break;
515 }
516 parseStructuralElement();
517 } while (!eof());
518}
519
Nico Weber27d13672013-01-09 20:25:35 +0000520void UnwrappedLineParser::parseObjCInterface() {
521 nextToken();
522 nextToken(); // interface name
523
524 // @interface can be followed by either a base class, or a category.
525 if (FormatTok.Tok.is(tok::colon)) {
526 nextToken();
527 nextToken(); // base class name
528 } else if (FormatTok.Tok.is(tok::l_paren))
529 // Skip category, if present.
530 parseParens();
531
Nico Weber1abe6ea2013-01-09 21:15:03 +0000532 if (FormatTok.Tok.is(tok::less))
533 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000534
535 // If instance variables are present, keep the '{' on the first line too.
536 if (FormatTok.Tok.is(tok::l_brace))
537 parseBlock();
538
539 // With instance variables, this puts '}' on its own line. Without instance
540 // variables, this ends the @interface line.
541 addUnwrappedLine();
542
Nico Weber1abe6ea2013-01-09 21:15:03 +0000543 parseObjCUntilAtEnd();
544}
Nico Weber27d13672013-01-09 20:25:35 +0000545
Nico Weber1abe6ea2013-01-09 21:15:03 +0000546void UnwrappedLineParser::parseObjCProtocol() {
547 nextToken();
548 nextToken(); // protocol name
549
550 if (FormatTok.Tok.is(tok::less))
551 parseObjCProtocolList();
552
553 // Check for protocol declaration.
554 if (FormatTok.Tok.is(tok::semi)) {
555 nextToken();
556 return addUnwrappedLine();
557 }
558
559 addUnwrappedLine();
560 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +0000561}
562
Daniel Jasperbac016b2012-12-03 18:12:45 +0000563void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000564 if (!RootTokenInitialized)
565 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000566 // Consume trailing comments.
567 while (!eof() && FormatTok.NewlinesBefore == 0 &&
568 FormatTok.Tok.is(tok::comment)) {
569 nextToken();
570 }
Manuel Klimek526ed112013-01-09 15:25:02 +0000571 Callback.consumeUnwrappedLine(*Line);
Daniel Jasper26f7e782013-01-08 14:56:18 +0000572 RootTokenInitialized = false;
Manuel Klimek526ed112013-01-09 15:25:02 +0000573 LastInCurrentLine = NULL;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000574}
575
576bool UnwrappedLineParser::eof() const {
577 return FormatTok.Tok.is(tok::eof);
578}
579
580void UnwrappedLineParser::nextToken() {
581 if (eof())
582 return;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000583 if (RootTokenInitialized) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000584 assert(LastInCurrentLine->Children.empty());
Daniel Jasper26f7e782013-01-08 14:56:18 +0000585 LastInCurrentLine->Children.push_back(FormatTok);
586 LastInCurrentLine = &LastInCurrentLine->Children.back();
587 } else {
Manuel Klimek526ed112013-01-09 15:25:02 +0000588 Line->RootToken = FormatTok;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000589 RootTokenInitialized = true;
Manuel Klimek526ed112013-01-09 15:25:02 +0000590 LastInCurrentLine = &Line->RootToken;
591 }
592 if (MustBreakBeforeNextToken) {
593 LastInCurrentLine->MustBreakBefore = true;
594 MustBreakBeforeNextToken = false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000595 }
Manuel Klimekd4397b92013-01-04 23:34:14 +0000596 readToken();
597}
598
599void UnwrappedLineParser::readToken() {
600 FormatTok = Tokens->getNextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000601 while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000602 ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
603 FormatTok.IsFirst)) {
Manuel Klimek526ed112013-01-09 15:25:02 +0000604 UnwrappedLine* StoredLine = Line.take();
605 Line.reset(new UnwrappedLine(*StoredLine));
606 assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());
607 FormatToken *StoredLastInCurrentLine = LastInCurrentLine;
608 bool PreviousInitialized = RootTokenInitialized;
609 RootTokenInitialized = false;
610 LastInCurrentLine = NULL;
611
Manuel Klimekd4397b92013-01-04 23:34:14 +0000612 parsePPDirective();
Manuel Klimek526ed112013-01-09 15:25:02 +0000613
614 assert(!RootTokenInitialized);
615 Line.reset(StoredLine);
616 RootTokenInitialized = PreviousInitialized;
617 LastInCurrentLine = StoredLastInCurrentLine;
618 assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());
619 MustBreakBeforeNextToken = true;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000620 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000621}
622
Daniel Jaspercd162382013-01-07 13:26:07 +0000623} // end namespace format
624} // end namespace clang