blob: 393416de029c389b4b0c1984bbb420d32908760b [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
Manuel Klimek8fa37992013-01-16 12:31:12 +000019#define DEBUG_TYPE "format-parser"
Daniel Jasperbac016b2012-12-03 18:12:45 +000020
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000021#include "UnwrappedLineParser.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000022#include "clang/Basic/Diagnostic.h"
23#include "llvm/Support/Debug.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000024
25// Uncomment to get debug output from tests:
26// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
Manuel Klimek4c60fc62013-01-10 10:05:08 +000027
Daniel Jasperbac016b2012-12-03 18:12:45 +000028namespace clang {
29namespace format {
30
Manuel Klimekd4397b92013-01-04 23:34:14 +000031class ScopedMacroState : public FormatTokenSource {
32public:
33 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
34 FormatToken &ResetToken)
35 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimekc37b4d62013-01-05 22:14:16 +000036 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000037 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000038 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000039 Line.InPPDirective = true;
40 }
41
42 ~ScopedMacroState() {
43 TokenSource = PreviousTokenSource;
44 ResetToken = Token;
45 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000046 Line.Level = PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000047 }
48
49 virtual FormatToken getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000050 // The \c UnwrappedLineParser guards against this by never calling
51 // \c getNextToken() after it has encountered the first eof token.
52 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000053 Token = PreviousTokenSource->getNextToken();
54 if (eof())
55 return createEOF();
56 return Token;
57 }
58
59private:
60 bool eof() {
61 return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
62 }
63
64 FormatToken createEOF() {
65 FormatToken FormatTok;
66 FormatTok.Tok.startToken();
67 FormatTok.Tok.setKind(tok::eof);
68 return FormatTok;
69 }
70
71 UnwrappedLine &Line;
72 FormatTokenSource *&TokenSource;
73 FormatToken &ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000074 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000075 FormatTokenSource *PreviousTokenSource;
76
77 FormatToken Token;
78};
79
Manuel Klimekbb42bf12013-01-10 11:52:21 +000080class ScopedLineState {
81public:
Manuel Klimek525fe162013-01-18 14:04:34 +000082 ScopedLineState(UnwrappedLineParser &Parser,
83 bool SwitchToPreprocessorLines = false)
84 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
85 if (SwitchToPreprocessorLines)
86 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +000087 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +000088 Parser.Line.reset(new UnwrappedLine());
89 Parser.Line->Level = PreBlockLine->Level;
90 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +000091 }
92
93 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +000094 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +000095 Parser.addUnwrappedLine();
96 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +000097 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +000098 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +000099 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000100 if (SwitchToPreprocessorLines)
101 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000102 }
103
104private:
105 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000106 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000107
108 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000109};
110
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000111UnwrappedLineParser::UnwrappedLineParser(
112 clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
113 FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000114 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
115 CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens),
116 Callback(Callback) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000117
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000118bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000119 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimekd4397b92013-01-04 23:34:14 +0000120 readToken();
Manuel Klimek525fe162013-01-18 14:04:34 +0000121 bool Error = parseFile();
122 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(),
123 E = Lines.end();
124 I != E; ++I) {
125 Callback.consumeUnwrappedLine(*I);
126 }
127 return Error;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000128}
129
130bool UnwrappedLineParser::parseFile() {
Manuel Klimeka5342db2013-01-06 20:07:31 +0000131 bool Error = parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000132 // Make sure to format the remaining tokens.
133 addUnwrappedLine();
134 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000135}
136
Manuel Klimeka5342db2013-01-06 20:07:31 +0000137bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000138 bool Error = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000139 do {
140 switch (FormatTok.Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000141 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000142 nextToken();
143 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000144 break;
145 case tok::l_brace:
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000146 Error |= parseBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000147 addUnwrappedLine();
148 break;
149 case tok::r_brace:
Manuel Klimeka5342db2013-01-06 20:07:31 +0000150 if (HasOpeningBrace) {
151 return false;
152 } else {
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000153 Diag.Report(FormatTok.Tok.getLocation(),
154 Diag.getCustomDiagID(clang::DiagnosticsEngine::Error,
Alexander Kornienko276a2092013-01-11 16:03:45 +0000155 "unexpected '}'"));
Manuel Klimeka5342db2013-01-06 20:07:31 +0000156 Error = true;
157 nextToken();
158 addUnwrappedLine();
159 }
160 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000161 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000162 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000163 break;
164 }
165 } while (!eof());
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000166 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000167}
168
Alexander Kornienko15757312012-12-06 18:03:27 +0000169bool UnwrappedLineParser::parseBlock(unsigned AddLevels) {
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +0000170 assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000171 nextToken();
172
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000173 if (!FormatTok.Tok.is(tok::r_brace)) {
174 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000175
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000176 Line->Level += AddLevels;
177 parseLevel(/*HasOpeningBrace=*/true);
178 Line->Level -= AddLevels;
Alexander Kornienko15757312012-12-06 18:03:27 +0000179
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000180 if (!FormatTok.Tok.is(tok::r_brace))
181 return true;
Alexander Kornienko393b0082012-12-04 15:40:36 +0000182
Manuel Klimek36fab8d2013-01-10 13:24:24 +0000183 }
Manuel Klimekde768542013-01-07 18:10:23 +0000184 nextToken(); // Munch the closing brace.
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000185 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000186}
187
188void UnwrappedLineParser::parsePPDirective() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000189 assert(FormatTok.Tok.is(tok::hash) && "'#' expected");
Manuel Klimek526ed112013-01-09 15:25:02 +0000190 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka080a182013-01-02 16:30:12 +0000191 nextToken();
192
Manuel Klimeka080a182013-01-02 16:30:12 +0000193 if (FormatTok.Tok.getIdentifierInfo() == NULL) {
194 addUnwrappedLine();
Manuel Klimeka080a182013-01-02 16:30:12 +0000195 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000196 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000197
Manuel Klimekd4397b92013-01-04 23:34:14 +0000198 switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
199 case tok::pp_define:
200 parsePPDefine();
201 break;
202 default:
203 parsePPUnknown();
204 break;
205 }
206}
207
208void UnwrappedLineParser::parsePPDefine() {
209 nextToken();
210
211 if (FormatTok.Tok.getKind() != tok::identifier) {
212 parsePPUnknown();
213 return;
214 }
215 nextToken();
216 if (FormatTok.Tok.getKind() == tok::l_paren) {
217 parseParens();
218 }
219 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000220 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000221
222 // Errors during a preprocessor directive can only affect the layout of the
223 // preprocessor directive, and thus we ignore them. An alternative approach
224 // would be to use the same approach we use on the file level (no
225 // re-indentation if there was a structural error) within the macro
226 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000227 parseFile();
228}
229
230void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000231 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000232 nextToken();
233 } while (!eof());
234 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000235}
236
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000237void UnwrappedLineParser::parseComments() {
Daniel Jasper33182dd2012-12-05 14:57:28 +0000238 // Consume leading line comments, e.g. for branches without compounds.
239 while (FormatTok.Tok.is(tok::comment)) {
240 nextToken();
241 addUnwrappedLine();
242 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000243}
244
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000245void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000246 assert(!FormatTok.Tok.is(tok::l_brace));
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000247 parseComments();
Daniel Jasper33182dd2012-12-05 14:57:28 +0000248
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000249 int TokenNumber = 0;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000250 switch (FormatTok.Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000251 case tok::at:
252 nextToken();
253 switch (FormatTok.Tok.getObjCKeywordID()) {
254 case tok::objc_public:
255 case tok::objc_protected:
256 case tok::objc_package:
257 case tok::objc_private:
258 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000259 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000260 case tok::objc_implementation:
261 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000262 case tok::objc_protocol:
263 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000264 case tok::objc_end:
265 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000266 case tok::objc_optional:
267 case tok::objc_required:
268 nextToken();
269 addUnwrappedLine();
270 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000271 default:
272 break;
273 }
274 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000275 case tok::kw_namespace:
276 parseNamespace();
277 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000278 case tok::kw_inline:
279 nextToken();
280 TokenNumber++;
281 if (FormatTok.Tok.is(tok::kw_namespace)) {
282 parseNamespace();
283 return;
284 }
285 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000286 case tok::kw_public:
287 case tok::kw_protected:
288 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000289 parseAccessSpecifier();
290 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000291 case tok::kw_if:
292 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000293 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000294 case tok::kw_for:
295 case tok::kw_while:
296 parseForOrWhileLoop();
297 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000298 case tok::kw_do:
299 parseDoWhile();
300 return;
301 case tok::kw_switch:
302 parseSwitch();
303 return;
304 case tok::kw_default:
305 nextToken();
306 parseLabel();
307 return;
308 case tok::kw_case:
309 parseCaseLabel();
310 return;
311 default:
312 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000313 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000314 do {
315 ++TokenNumber;
316 switch (FormatTok.Tok.getKind()) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000317 case tok::kw_enum:
318 parseEnum();
319 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000320 case tok::kw_struct:
321 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000322 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000323 parseRecord();
324 // A record declaration or definition is always the start of a structural
325 // element.
326 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000327 case tok::semi:
328 nextToken();
329 addUnwrappedLine();
330 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000331 case tok::r_brace:
332 addUnwrappedLine();
333 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000334 case tok::l_paren:
335 parseParens();
336 break;
337 case tok::l_brace:
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000338 // A block outside of parentheses must be the last part of a
339 // structural element.
340 // FIXME: Figure out cases where this is not true, and add projections for
341 // them (the one we know is missing are lambdas).
Daniel Jasperbac016b2012-12-03 18:12:45 +0000342 parseBlock();
343 addUnwrappedLine();
344 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000345 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000346 nextToken();
347 if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
348 parseLabel();
349 return;
350 }
351 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000352 case tok::equal:
353 nextToken();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000354 if (FormatTok.Tok.is(tok::l_brace)) {
355 parseBracedList();
356 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000357 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000358 default:
359 nextToken();
360 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000361 }
362 } while (!eof());
363}
364
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000365void UnwrappedLineParser::parseBracedList() {
366 nextToken();
367
368 do {
369 switch (FormatTok.Tok.getKind()) {
370 case tok::l_brace:
371 parseBracedList();
372 break;
373 case tok::r_brace:
374 nextToken();
375 return;
376 default:
377 nextToken();
378 break;
379 }
380 } while (!eof());
381}
382
Daniel Jasperbac016b2012-12-03 18:12:45 +0000383void UnwrappedLineParser::parseParens() {
384 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
385 nextToken();
386 do {
387 switch (FormatTok.Tok.getKind()) {
388 case tok::l_paren:
389 parseParens();
390 break;
391 case tok::r_paren:
392 nextToken();
393 return;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000394 case tok::l_brace:
395 {
396 nextToken();
397 ScopedLineState LineState(*this);
398 Line->Level += 1;
399 parseLevel(/*HasOpeningBrace=*/true);
400 Line->Level -= 1;
401 }
402 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000403 default:
404 nextToken();
405 break;
406 }
407 } while (!eof());
408}
409
410void UnwrappedLineParser::parseIfThenElse() {
411 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
412 nextToken();
Manuel Klimekd4658432013-01-11 18:28:36 +0000413 if (FormatTok.Tok.is(tok::l_paren))
414 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000415 bool NeedsUnwrappedLine = false;
416 if (FormatTok.Tok.is(tok::l_brace)) {
417 parseBlock();
418 NeedsUnwrappedLine = true;
419 } else {
420 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000421 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000422 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000423 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000424 }
425 if (FormatTok.Tok.is(tok::kw_else)) {
426 nextToken();
427 if (FormatTok.Tok.is(tok::l_brace)) {
428 parseBlock();
429 addUnwrappedLine();
430 } else if (FormatTok.Tok.is(tok::kw_if)) {
431 parseIfThenElse();
432 } else {
433 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000434 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000435 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000436 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000437 }
438 } else if (NeedsUnwrappedLine) {
439 addUnwrappedLine();
440 }
441}
442
Alexander Kornienko15757312012-12-06 18:03:27 +0000443void UnwrappedLineParser::parseNamespace() {
444 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
445 nextToken();
446 if (FormatTok.Tok.is(tok::identifier))
447 nextToken();
448 if (FormatTok.Tok.is(tok::l_brace)) {
449 parseBlock(0);
450 addUnwrappedLine();
451 }
452 // FIXME: Add error handling.
453}
454
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000455void UnwrappedLineParser::parseForOrWhileLoop() {
456 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
457 "'for' or 'while' expected");
458 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000459 if (FormatTok.Tok.is(tok::l_paren))
460 parseParens();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000461 if (FormatTok.Tok.is(tok::l_brace)) {
462 parseBlock();
463 addUnwrappedLine();
464 } else {
465 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000466 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000467 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000468 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000469 }
470}
471
Daniel Jasperbac016b2012-12-03 18:12:45 +0000472void UnwrappedLineParser::parseDoWhile() {
473 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
474 nextToken();
475 if (FormatTok.Tok.is(tok::l_brace)) {
476 parseBlock();
477 } else {
478 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000479 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000480 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000481 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000482 }
483
Alexander Kornienko393b0082012-12-04 15:40:36 +0000484 // FIXME: Add error handling.
485 if (!FormatTok.Tok.is(tok::kw_while)) {
486 addUnwrappedLine();
487 return;
488 }
489
Daniel Jasperbac016b2012-12-03 18:12:45 +0000490 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000491 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000492}
493
494void UnwrappedLineParser::parseLabel() {
495 // FIXME: remove all asserts.
496 assert(FormatTok.Tok.is(tok::colon) && "':' expected");
497 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000498 unsigned OldLineLevel = Line->Level;
499 if (Line->Level > 0)
500 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000501 if (FormatTok.Tok.is(tok::l_brace)) {
502 parseBlock();
Nico Weber94fb7292013-01-18 05:50:57 +0000503 if (FormatTok.Tok.is(tok::kw_break))
504 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000505 }
506 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000507 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000508}
509
510void UnwrappedLineParser::parseCaseLabel() {
511 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
512 // FIXME: fix handling of complex expressions here.
513 do {
514 nextToken();
515 } while (!eof() && !FormatTok.Tok.is(tok::colon));
516 parseLabel();
517}
518
519void UnwrappedLineParser::parseSwitch() {
520 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
521 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000522 if (FormatTok.Tok.is(tok::l_paren))
523 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000524 if (FormatTok.Tok.is(tok::l_brace)) {
Alexander Kornienko15757312012-12-06 18:03:27 +0000525 parseBlock(Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000526 addUnwrappedLine();
527 } else {
528 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000529 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000530 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000531 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000532 }
533}
534
535void UnwrappedLineParser::parseAccessSpecifier() {
536 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000537 // Otherwise, we don't know what it is, and we'd better keep the next token.
538 if (FormatTok.Tok.is(tok::colon))
539 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000540 addUnwrappedLine();
541}
542
543void UnwrappedLineParser::parseEnum() {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000544 bool HasContents = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000545 do {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000546 switch (FormatTok.Tok.getKind()) {
547 case tok::l_brace:
548 nextToken();
549 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000550 ++Line->Level;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000551 parseComments();
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000552 break;
553 case tok::l_paren:
554 parseParens();
555 break;
556 case tok::comma:
557 nextToken();
558 addUnwrappedLine();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000559 parseComments();
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000560 break;
561 case tok::r_brace:
562 if (HasContents)
563 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000564 --Line->Level;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000565 nextToken();
566 break;
567 case tok::semi:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000568 nextToken();
569 addUnwrappedLine();
570 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000571 default:
572 HasContents = true;
573 nextToken();
574 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000575 }
576 } while (!eof());
577}
578
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000579void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000580 nextToken();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000581 if (FormatTok.Tok.is(tok::identifier) ||
582 FormatTok.Tok.is(tok::kw___attribute) ||
583 FormatTok.Tok.is(tok::kw___declspec)) {
584 nextToken();
585 // We can have macros or attributes in between 'class' and the class name.
586 if (FormatTok.Tok.is(tok::l_paren)) {
587 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000588 }
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000589 if (FormatTok.Tok.is(tok::identifier))
590 nextToken();
591
592 if (FormatTok.Tok.is(tok::colon)) {
593 while (FormatTok.Tok.isNot(tok::l_brace)) {
594 if (FormatTok.Tok.is(tok::semi))
595 return;
596 nextToken();
597 }
598 }
599 }
600 if (FormatTok.Tok.is(tok::l_brace))
601 parseBlock();
Manuel Klimekde768542013-01-07 18:10:23 +0000602}
603
Nico Weber1abe6ea2013-01-09 21:15:03 +0000604void UnwrappedLineParser::parseObjCProtocolList() {
605 assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
606 do
607 nextToken();
608 while (!eof() && FormatTok.Tok.isNot(tok::greater));
609 nextToken(); // Skip '>'.
610}
611
612void UnwrappedLineParser::parseObjCUntilAtEnd() {
613 do {
614 if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
615 nextToken();
616 addUnwrappedLine();
617 break;
618 }
619 parseStructuralElement();
620 } while (!eof());
621}
622
Nico Weber50767d82013-01-09 23:25:37 +0000623void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000624 nextToken();
625 nextToken(); // interface name
626
627 // @interface can be followed by either a base class, or a category.
628 if (FormatTok.Tok.is(tok::colon)) {
629 nextToken();
630 nextToken(); // base class name
631 } else if (FormatTok.Tok.is(tok::l_paren))
632 // Skip category, if present.
633 parseParens();
634
Nico Weber1abe6ea2013-01-09 21:15:03 +0000635 if (FormatTok.Tok.is(tok::less))
636 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000637
638 // If instance variables are present, keep the '{' on the first line too.
639 if (FormatTok.Tok.is(tok::l_brace))
640 parseBlock();
641
642 // With instance variables, this puts '}' on its own line. Without instance
643 // variables, this ends the @interface line.
644 addUnwrappedLine();
645
Nico Weber1abe6ea2013-01-09 21:15:03 +0000646 parseObjCUntilAtEnd();
647}
Nico Weber27d13672013-01-09 20:25:35 +0000648
Nico Weber1abe6ea2013-01-09 21:15:03 +0000649void UnwrappedLineParser::parseObjCProtocol() {
650 nextToken();
651 nextToken(); // protocol name
652
653 if (FormatTok.Tok.is(tok::less))
654 parseObjCProtocolList();
655
656 // Check for protocol declaration.
657 if (FormatTok.Tok.is(tok::semi)) {
658 nextToken();
659 return addUnwrappedLine();
660 }
661
662 addUnwrappedLine();
663 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +0000664}
665
Daniel Jasperbac016b2012-12-03 18:12:45 +0000666void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000667 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +0000668 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000669 // Consume trailing comments.
670 while (!eof() && FormatTok.NewlinesBefore == 0 &&
671 FormatTok.Tok.is(tok::comment)) {
672 nextToken();
673 }
Manuel Klimek8fa37992013-01-16 12:31:12 +0000674 DEBUG({
675 llvm::dbgs() << "Line: ";
676 for (std::list<FormatToken>::iterator I = Line->Tokens.begin(),
677 E = Line->Tokens.end();
678 I != E; ++I) {
679 llvm::dbgs() << I->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000680
Manuel Klimek8fa37992013-01-16 12:31:12 +0000681 }
682 llvm::dbgs() << "\n";
683 });
Manuel Klimek525fe162013-01-18 14:04:34 +0000684 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000685 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +0000686 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
687 for (std::vector<UnwrappedLine>::iterator I = PreprocessorDirectives
688 .begin(), E = PreprocessorDirectives.end();
689 I != E; ++I) {
690 CurrentLines->push_back(*I);
691 }
692 PreprocessorDirectives.clear();
693 }
694
Daniel Jasperbac016b2012-12-03 18:12:45 +0000695}
696
697bool UnwrappedLineParser::eof() const {
698 return FormatTok.Tok.is(tok::eof);
699}
700
701void UnwrappedLineParser::nextToken() {
702 if (eof())
703 return;
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000704 Line->Tokens.push_back(FormatTok);
Manuel Klimek526ed112013-01-09 15:25:02 +0000705 if (MustBreakBeforeNextToken) {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000706 Line->Tokens.back().MustBreakBefore = true;
Manuel Klimek526ed112013-01-09 15:25:02 +0000707 MustBreakBeforeNextToken = false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000708 }
Manuel Klimekd4397b92013-01-04 23:34:14 +0000709 readToken();
710}
711
712void UnwrappedLineParser::readToken() {
713 FormatTok = Tokens->getNextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000714 while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000715 ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
716 FormatTok.IsFirst)) {
Manuel Klimek525fe162013-01-18 14:04:34 +0000717 // If there is an unfinished unwrapped line, we flush the preprocessor
718 // directives only after that unwrapped line was finished later.
719 bool SwitchToPreprocessorLines = !Line->Tokens.empty() &&
720 CurrentLines == &Lines;
721 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000722 parsePPDirective();
723 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000724}
725
Daniel Jaspercd162382013-01-07 13:26:07 +0000726} // end namespace format
727} // end namespace clang