blob: f79cc712f43279e197196b7cf997de8ce84ee64c [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///
Daniel Jasperbac016b2012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimek8fa37992013-01-16 12:31:12 +000016#define DEBUG_TYPE "format-parser"
Daniel Jasperbac016b2012-12-03 18:12:45 +000017
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000018#include "UnwrappedLineParser.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000019#include "clang/Basic/Diagnostic.h"
20#include "llvm/Support/Debug.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000021
22// Uncomment to get debug output from tests:
23// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
Manuel Klimek4c60fc62013-01-10 10:05:08 +000024
Daniel Jasperbac016b2012-12-03 18:12:45 +000025namespace clang {
26namespace format {
27
Manuel Klimek70b03f42013-01-23 09:32:48 +000028class ScopedDeclarationState {
29public:
30 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
31 bool MustBeDeclaration)
32 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000033 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000034 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000035 }
36 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000037 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000038 if (!Stack.empty())
39 Line.MustBeDeclaration = Stack.back();
40 else
41 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000042 }
43private:
44 UnwrappedLine &Line;
45 std::vector<bool> &Stack;
46};
47
Manuel Klimekd4397b92013-01-04 23:34:14 +000048class ScopedMacroState : public FormatTokenSource {
49public:
50 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
51 FormatToken &ResetToken)
52 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimekc37b4d62013-01-05 22:14:16 +000053 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000054 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000055 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000056 Line.InPPDirective = true;
57 }
58
59 ~ScopedMacroState() {
60 TokenSource = PreviousTokenSource;
61 ResetToken = Token;
62 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000063 Line.Level = PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 }
65
66 virtual FormatToken getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000067 // The \c UnwrappedLineParser guards against this by never calling
68 // \c getNextToken() after it has encountered the first eof token.
69 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000070 Token = PreviousTokenSource->getNextToken();
71 if (eof())
72 return createEOF();
73 return Token;
74 }
75
76private:
77 bool eof() {
78 return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
79 }
80
81 FormatToken createEOF() {
82 FormatToken FormatTok;
83 FormatTok.Tok.startToken();
84 FormatTok.Tok.setKind(tok::eof);
85 return FormatTok;
86 }
87
88 UnwrappedLine &Line;
89 FormatTokenSource *&TokenSource;
90 FormatToken &ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000091 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000092 FormatTokenSource *PreviousTokenSource;
93
94 FormatToken Token;
95};
96
Manuel Klimekbb42bf12013-01-10 11:52:21 +000097class ScopedLineState {
98public:
Manuel Klimek525fe162013-01-18 14:04:34 +000099 ScopedLineState(UnwrappedLineParser &Parser,
100 bool SwitchToPreprocessorLines = false)
101 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
102 if (SwitchToPreprocessorLines)
103 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000104 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000105 Parser.Line.reset(new UnwrappedLine());
106 Parser.Line->Level = PreBlockLine->Level;
107 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000108 }
109
110 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000111 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000112 Parser.addUnwrappedLine();
113 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000114 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000115 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000116 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000117 if (SwitchToPreprocessorLines)
118 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000119 }
120
121private:
122 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000123 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000124
125 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000126};
127
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000128UnwrappedLineParser::UnwrappedLineParser(
129 clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
130 FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000131 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
132 CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens),
133 Callback(Callback) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000134
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000135bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000136 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimekd4397b92013-01-04 23:34:14 +0000137 readToken();
Manuel Klimek525fe162013-01-18 14:04:34 +0000138 bool Error = parseFile();
139 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(),
140 E = Lines.end();
141 I != E; ++I) {
142 Callback.consumeUnwrappedLine(*I);
143 }
144 return Error;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000145}
146
147bool UnwrappedLineParser::parseFile() {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000148 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
149 /*MustBeDeclaration=*/ true);
Manuel Klimeka5342db2013-01-06 20:07:31 +0000150 bool Error = parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000151 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000152 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000153 addUnwrappedLine();
154 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000155}
156
Manuel Klimeka5342db2013-01-06 20:07:31 +0000157bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000158 bool Error = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000159 do {
160 switch (FormatTok.Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000161 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000162 nextToken();
163 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000164 break;
165 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000166 // FIXME: Add parameter whether this can happen - if this happens, we must
167 // be in a non-declaration context.
168 Error |= parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000169 addUnwrappedLine();
170 break;
171 case tok::r_brace:
Manuel Klimeka5342db2013-01-06 20:07:31 +0000172 if (HasOpeningBrace) {
173 return false;
174 } else {
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000175 Diag.Report(FormatTok.Tok.getLocation(),
176 Diag.getCustomDiagID(clang::DiagnosticsEngine::Error,
Alexander Kornienko276a2092013-01-11 16:03:45 +0000177 "unexpected '}'"));
Manuel Klimeka5342db2013-01-06 20:07:31 +0000178 Error = true;
179 nextToken();
180 addUnwrappedLine();
181 }
182 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000183 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000184 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000185 break;
186 }
187 } while (!eof());
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000188 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000189}
190
Manuel Klimek70b03f42013-01-23 09:32:48 +0000191bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels) {
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +0000192 assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000193 nextToken();
194
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000195 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000196
Manuel Klimek70b03f42013-01-23 09:32:48 +0000197 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
198 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000199 Line->Level += AddLevels;
200 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000201
Manuel Klimek86721d22013-01-22 16:31:55 +0000202 if (!FormatTok.Tok.is(tok::r_brace)) {
203 Line->Level -= AddLevels;
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000204 return true;
Manuel Klimek86721d22013-01-22 16:31:55 +0000205 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000206
Manuel Klimekde768542013-01-07 18:10:23 +0000207 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000208 Line->Level -= AddLevels;
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000209 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000210}
211
212void UnwrappedLineParser::parsePPDirective() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000213 assert(FormatTok.Tok.is(tok::hash) && "'#' expected");
Manuel Klimek526ed112013-01-09 15:25:02 +0000214 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka080a182013-01-02 16:30:12 +0000215 nextToken();
216
Manuel Klimeka080a182013-01-02 16:30:12 +0000217 if (FormatTok.Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000218 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000219 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000220 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000221
Manuel Klimekd4397b92013-01-04 23:34:14 +0000222 switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
223 case tok::pp_define:
224 parsePPDefine();
225 break;
226 default:
227 parsePPUnknown();
228 break;
229 }
230}
231
232void UnwrappedLineParser::parsePPDefine() {
233 nextToken();
234
235 if (FormatTok.Tok.getKind() != tok::identifier) {
236 parsePPUnknown();
237 return;
238 }
239 nextToken();
Manuel Klimek7ccbc212013-01-23 14:37:36 +0000240 if (FormatTok.Tok.getKind() == tok::l_paren &&
241 FormatTok.WhiteSpaceLength == 0) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000242 parseParens();
243 }
244 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000245 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000246
247 // Errors during a preprocessor directive can only affect the layout of the
248 // preprocessor directive, and thus we ignore them. An alternative approach
249 // would be to use the same approach we use on the file level (no
250 // re-indentation if there was a structural error) within the macro
251 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000252 parseFile();
253}
254
255void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000256 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000257 nextToken();
258 } while (!eof());
259 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000260}
261
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000262void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000263 assert(!FormatTok.Tok.is(tok::l_brace));
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000264 int TokenNumber = 0;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000265 switch (FormatTok.Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000266 case tok::at:
267 nextToken();
268 switch (FormatTok.Tok.getObjCKeywordID()) {
269 case tok::objc_public:
270 case tok::objc_protected:
271 case tok::objc_package:
272 case tok::objc_private:
273 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000274 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000275 case tok::objc_implementation:
276 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000277 case tok::objc_protocol:
278 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000279 case tok::objc_end:
280 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000281 case tok::objc_optional:
282 case tok::objc_required:
283 nextToken();
284 addUnwrappedLine();
285 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000286 default:
287 break;
288 }
289 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000290 case tok::kw_namespace:
291 parseNamespace();
292 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000293 case tok::kw_inline:
294 nextToken();
295 TokenNumber++;
296 if (FormatTok.Tok.is(tok::kw_namespace)) {
297 parseNamespace();
298 return;
299 }
300 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000301 case tok::kw_public:
302 case tok::kw_protected:
303 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000304 parseAccessSpecifier();
305 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000306 case tok::kw_if:
307 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000308 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000309 case tok::kw_for:
310 case tok::kw_while:
311 parseForOrWhileLoop();
312 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000313 case tok::kw_do:
314 parseDoWhile();
315 return;
316 case tok::kw_switch:
317 parseSwitch();
318 return;
319 case tok::kw_default:
320 nextToken();
321 parseLabel();
322 return;
323 case tok::kw_case:
324 parseCaseLabel();
325 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000326 case tok::kw_return:
327 parseReturn();
328 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000329 case tok::kw_extern:
330 nextToken();
331 if (FormatTok.Tok.is(tok::string_literal)) {
332 nextToken();
333 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000334 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000335 addUnwrappedLine();
336 return;
337 }
338 }
339 // In all other cases, parse the declaration.
340 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000341 default:
342 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000343 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000344 do {
345 ++TokenNumber;
346 switch (FormatTok.Tok.getKind()) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000347 case tok::kw_enum:
348 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000349 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000350 case tok::kw_struct:
351 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000352 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000353 parseRecord();
354 // A record declaration or definition is always the start of a structural
355 // element.
356 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000357 case tok::semi:
358 nextToken();
359 addUnwrappedLine();
360 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000361 case tok::r_brace:
362 addUnwrappedLine();
363 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000364 case tok::l_paren:
365 parseParens();
366 break;
367 case tok::l_brace:
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000368 // A block outside of parentheses must be the last part of a
369 // structural element.
370 // FIXME: Figure out cases where this is not true, and add projections for
371 // them (the one we know is missing are lambdas).
Manuel Klimek70b03f42013-01-23 09:32:48 +0000372 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000373 addUnwrappedLine();
374 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000375 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000376 nextToken();
377 if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
378 parseLabel();
379 return;
380 }
381 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000382 case tok::equal:
383 nextToken();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000384 if (FormatTok.Tok.is(tok::l_brace)) {
385 parseBracedList();
386 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000387 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000388 default:
389 nextToken();
390 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000391 }
392 } while (!eof());
393}
394
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000395void UnwrappedLineParser::parseBracedList() {
396 nextToken();
397
398 do {
399 switch (FormatTok.Tok.getKind()) {
400 case tok::l_brace:
401 parseBracedList();
402 break;
403 case tok::r_brace:
404 nextToken();
405 return;
406 default:
407 nextToken();
408 break;
409 }
410 } while (!eof());
411}
412
Manuel Klimekc44ee892013-01-21 10:07:49 +0000413void UnwrappedLineParser::parseReturn() {
414 nextToken();
415
416 do {
417 switch (FormatTok.Tok.getKind()) {
418 case tok::l_brace:
419 parseBracedList();
420 break;
421 case tok::l_paren:
422 parseParens();
423 break;
424 case tok::r_brace:
425 // Assume missing ';'.
426 addUnwrappedLine();
427 return;
428 case tok::semi:
429 nextToken();
430 addUnwrappedLine();
431 return;
432 default:
433 nextToken();
434 break;
435 }
436 } while (!eof());
437}
438
Daniel Jasperbac016b2012-12-03 18:12:45 +0000439void UnwrappedLineParser::parseParens() {
440 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
441 nextToken();
442 do {
443 switch (FormatTok.Tok.getKind()) {
444 case tok::l_paren:
445 parseParens();
446 break;
447 case tok::r_paren:
448 nextToken();
449 return;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000450 case tok::l_brace:
451 {
452 nextToken();
453 ScopedLineState LineState(*this);
Manuel Klimek70b03f42013-01-23 09:32:48 +0000454 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
455 /*MustBeDeclaration=*/ false);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000456 Line->Level += 1;
Manuel Klimek70b03f42013-01-23 09:32:48 +0000457 parseLevel(/*HasOpeningBrace=*/ true);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000458 Line->Level -= 1;
459 }
460 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000461 default:
462 nextToken();
463 break;
464 }
465 } while (!eof());
466}
467
468void UnwrappedLineParser::parseIfThenElse() {
469 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
470 nextToken();
Manuel Klimekd4658432013-01-11 18:28:36 +0000471 if (FormatTok.Tok.is(tok::l_paren))
472 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000473 bool NeedsUnwrappedLine = false;
474 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000475 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000476 NeedsUnwrappedLine = true;
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 if (FormatTok.Tok.is(tok::kw_else)) {
484 nextToken();
485 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000486 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000487 addUnwrappedLine();
488 } else if (FormatTok.Tok.is(tok::kw_if)) {
489 parseIfThenElse();
490 } else {
491 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000492 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000493 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000494 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000495 }
496 } else if (NeedsUnwrappedLine) {
497 addUnwrappedLine();
498 }
499}
500
Alexander Kornienko15757312012-12-06 18:03:27 +0000501void UnwrappedLineParser::parseNamespace() {
502 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
503 nextToken();
504 if (FormatTok.Tok.is(tok::identifier))
505 nextToken();
506 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000507 parseBlock(/*MustBeDeclaration=*/ true, 0);
Alexander Kornienko15757312012-12-06 18:03:27 +0000508 addUnwrappedLine();
509 }
510 // FIXME: Add error handling.
511}
512
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000513void UnwrappedLineParser::parseForOrWhileLoop() {
514 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
515 "'for' or 'while' expected");
516 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000517 if (FormatTok.Tok.is(tok::l_paren))
518 parseParens();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000519 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000520 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000521 addUnwrappedLine();
522 } else {
523 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000524 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000525 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000526 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000527 }
528}
529
Daniel Jasperbac016b2012-12-03 18:12:45 +0000530void UnwrappedLineParser::parseDoWhile() {
531 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
532 nextToken();
533 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000534 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000535 } else {
536 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000537 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000538 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000539 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000540 }
541
Alexander Kornienko393b0082012-12-04 15:40:36 +0000542 // FIXME: Add error handling.
543 if (!FormatTok.Tok.is(tok::kw_while)) {
544 addUnwrappedLine();
545 return;
546 }
547
Daniel Jasperbac016b2012-12-03 18:12:45 +0000548 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000549 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000550}
551
552void UnwrappedLineParser::parseLabel() {
553 // FIXME: remove all asserts.
554 assert(FormatTok.Tok.is(tok::colon) && "':' expected");
555 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000556 unsigned OldLineLevel = Line->Level;
557 if (Line->Level > 0)
558 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000559 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000560 parseBlock(/*MustBeDeclaration=*/ false);
Nico Weber94fb7292013-01-18 05:50:57 +0000561 if (FormatTok.Tok.is(tok::kw_break))
562 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000563 }
564 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000565 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000566}
567
568void UnwrappedLineParser::parseCaseLabel() {
569 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
570 // FIXME: fix handling of complex expressions here.
571 do {
572 nextToken();
573 } while (!eof() && !FormatTok.Tok.is(tok::colon));
574 parseLabel();
575}
576
577void UnwrappedLineParser::parseSwitch() {
578 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
579 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000580 if (FormatTok.Tok.is(tok::l_paren))
581 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000582 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000583 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000584 addUnwrappedLine();
585 } else {
586 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000587 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000588 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000589 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000590 }
591}
592
593void UnwrappedLineParser::parseAccessSpecifier() {
594 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000595 // Otherwise, we don't know what it is, and we'd better keep the next token.
596 if (FormatTok.Tok.is(tok::colon))
597 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000598 addUnwrappedLine();
599}
600
601void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000602 nextToken();
603 if (FormatTok.Tok.is(tok::identifier) ||
604 FormatTok.Tok.is(tok::kw___attribute) ||
605 FormatTok.Tok.is(tok::kw___declspec)) {
606 nextToken();
607 // We can have macros or attributes in between 'enum' and the enum name.
608 if (FormatTok.Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000609 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000610 }
Manuel Klimek308232c2013-01-21 19:17:52 +0000611 if (FormatTok.Tok.is(tok::identifier))
612 nextToken();
613 }
614 if (FormatTok.Tok.is(tok::l_brace)) {
615 nextToken();
616 addUnwrappedLine();
617 ++Line->Level;
618 do {
619 switch (FormatTok.Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000620 case tok::l_paren:
621 parseParens();
622 break;
623 case tok::r_brace:
624 addUnwrappedLine();
625 nextToken();
626 --Line->Level;
627 return;
628 case tok::comma:
629 nextToken();
630 addUnwrappedLine();
631 break;
632 default:
633 nextToken();
634 break;
635 }
636 } while (!eof());
637 }
638 // We fall through to parsing a structural element afterwards, so that in
639 // enum A {} n, m;
640 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000641}
642
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000643void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000644 nextToken();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000645 if (FormatTok.Tok.is(tok::identifier) ||
646 FormatTok.Tok.is(tok::kw___attribute) ||
647 FormatTok.Tok.is(tok::kw___declspec)) {
648 nextToken();
649 // We can have macros or attributes in between 'class' and the class name.
650 if (FormatTok.Tok.is(tok::l_paren)) {
651 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000652 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000653 // The actual identifier can be a nested name specifier, and in macros
654 // it is often token-pasted.
Manuel Klimek7f5b0252013-01-21 10:17:14 +0000655 while (FormatTok.Tok.is(tok::identifier) ||
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000656 FormatTok.Tok.is(tok::coloncolon) ||
657 FormatTok.Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000658 nextToken();
659
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000660 // Note that parsing away template declarations here leads to incorrectly
661 // accepting function declarations as record declarations.
662 // In general, we cannot solve this problem. Consider:
663 // class A<int> B() {}
664 // which can be a function definition or a class definition when B() is a
665 // macro. If we find enough real-world cases where this is a problem, we
666 // can parse for the 'template' keyword in the beginning of the statement,
667 // and thus rule out the record production in case there is no template
668 // (this would still leave us with an ambiguity between template function
669 // and class declarations).
670 if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000671 while (FormatTok.Tok.isNot(tok::l_brace)) {
672 if (FormatTok.Tok.is(tok::semi))
673 return;
674 nextToken();
675 }
676 }
677 }
678 if (FormatTok.Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000679 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000680 // We fall through to parsing a structural element afterwards, so
681 // class A {} n, m;
682 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000683}
684
Nico Weber1abe6ea2013-01-09 21:15:03 +0000685void UnwrappedLineParser::parseObjCProtocolList() {
686 assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
687 do
688 nextToken();
689 while (!eof() && FormatTok.Tok.isNot(tok::greater));
690 nextToken(); // Skip '>'.
691}
692
693void UnwrappedLineParser::parseObjCUntilAtEnd() {
694 do {
695 if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
696 nextToken();
697 addUnwrappedLine();
698 break;
699 }
700 parseStructuralElement();
701 } while (!eof());
702}
703
Nico Weber50767d82013-01-09 23:25:37 +0000704void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000705 nextToken();
706 nextToken(); // interface name
707
708 // @interface can be followed by either a base class, or a category.
709 if (FormatTok.Tok.is(tok::colon)) {
710 nextToken();
711 nextToken(); // base class name
712 } else if (FormatTok.Tok.is(tok::l_paren))
713 // Skip category, if present.
714 parseParens();
715
Nico Weber1abe6ea2013-01-09 21:15:03 +0000716 if (FormatTok.Tok.is(tok::less))
717 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000718
719 // If instance variables are present, keep the '{' on the first line too.
720 if (FormatTok.Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000721 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000722
723 // With instance variables, this puts '}' on its own line. Without instance
724 // variables, this ends the @interface line.
725 addUnwrappedLine();
726
Nico Weber1abe6ea2013-01-09 21:15:03 +0000727 parseObjCUntilAtEnd();
728}
Nico Weber27d13672013-01-09 20:25:35 +0000729
Nico Weber1abe6ea2013-01-09 21:15:03 +0000730void UnwrappedLineParser::parseObjCProtocol() {
731 nextToken();
732 nextToken(); // protocol name
733
734 if (FormatTok.Tok.is(tok::less))
735 parseObjCProtocolList();
736
737 // Check for protocol declaration.
738 if (FormatTok.Tok.is(tok::semi)) {
739 nextToken();
740 return addUnwrappedLine();
741 }
742
743 addUnwrappedLine();
744 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +0000745}
746
Daniel Jasperbac016b2012-12-03 18:12:45 +0000747void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000748 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +0000749 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +0000750 DEBUG({
Manuel Klimek86721d22013-01-22 16:31:55 +0000751 llvm::dbgs() << "Line(" << Line->Level << "): ";
Manuel Klimek8fa37992013-01-16 12:31:12 +0000752 for (std::list<FormatToken>::iterator I = Line->Tokens.begin(),
753 E = Line->Tokens.end();
754 I != E; ++I) {
755 llvm::dbgs() << I->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000756
Manuel Klimek8fa37992013-01-16 12:31:12 +0000757 }
758 llvm::dbgs() << "\n";
759 });
Manuel Klimek525fe162013-01-18 14:04:34 +0000760 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000761 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +0000762 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
763 for (std::vector<UnwrappedLine>::iterator I = PreprocessorDirectives
764 .begin(), E = PreprocessorDirectives.end();
765 I != E; ++I) {
766 CurrentLines->push_back(*I);
767 }
768 PreprocessorDirectives.clear();
769 }
770
Daniel Jasperbac016b2012-12-03 18:12:45 +0000771}
772
773bool UnwrappedLineParser::eof() const {
774 return FormatTok.Tok.is(tok::eof);
775}
776
Manuel Klimek86721d22013-01-22 16:31:55 +0000777void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
778 bool JustComments = Line->Tokens.empty();
779 for (SmallVectorImpl<FormatToken>::const_iterator
780 I = CommentsBeforeNextToken.begin(),
781 E = CommentsBeforeNextToken.end();
782 I != E; ++I) {
783 if (I->HasUnescapedNewline && JustComments) {
784 addUnwrappedLine();
785 }
786 pushToken(*I);
787 }
788 if (NewlineBeforeNext && JustComments) {
789 addUnwrappedLine();
790 }
791 CommentsBeforeNextToken.clear();
792}
793
Daniel Jasperbac016b2012-12-03 18:12:45 +0000794void UnwrappedLineParser::nextToken() {
795 if (eof())
796 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000797 flushComments(FormatTok.HasUnescapedNewline);
798 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000799 readToken();
800}
801
802void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +0000803 bool CommentsInCurrentLine = true;
804 do {
805 FormatTok = Tokens->getNextToken();
806 while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
807 ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
808 FormatTok.IsFirst)) {
809 // If there is an unfinished unwrapped line, we flush the preprocessor
810 // directives only after that unwrapped line was finished later.
811 bool SwitchToPreprocessorLines = !Line->Tokens.empty() &&
812 CurrentLines == &Lines;
813 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
814 parsePPDirective();
815 }
816 if (!FormatTok.Tok.is(tok::comment))
817 return;
818 if (FormatTok.HasUnescapedNewline || FormatTok.IsFirst) {
819 CommentsInCurrentLine = false;
820 }
821 if (CommentsInCurrentLine) {
822 pushToken(FormatTok);
823 } else {
824 CommentsBeforeNextToken.push_back(FormatTok);
825 }
826 } while (!eof());
827}
828
829void UnwrappedLineParser::pushToken(const FormatToken &Tok) {
830 Line->Tokens.push_back(Tok);
831 if (MustBreakBeforeNextToken) {
832 Line->Tokens.back().MustBreakBefore = true;
833 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000834 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000835}
836
Daniel Jaspercd162382013-01-07 13:26:07 +0000837} // end namespace format
838} // end namespace clang