blob: 1b39442610f4030df5ebb801b11671159f021104 [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 Klimek70b03f42013-01-23 09:32:48 +000031class ScopedDeclarationState {
32public:
33 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
34 bool MustBeDeclaration)
35 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000036 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000037 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000038 }
39 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000040 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000041 if (!Stack.empty())
42 Line.MustBeDeclaration = Stack.back();
43 else
44 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000045 }
46private:
47 UnwrappedLine &Line;
48 std::vector<bool> &Stack;
49};
50
Manuel Klimekd4397b92013-01-04 23:34:14 +000051class ScopedMacroState : public FormatTokenSource {
52public:
53 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
54 FormatToken &ResetToken)
55 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimekc37b4d62013-01-05 22:14:16 +000056 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000057 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000058 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000059 Line.InPPDirective = true;
60 }
61
62 ~ScopedMacroState() {
63 TokenSource = PreviousTokenSource;
64 ResetToken = Token;
65 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000066 Line.Level = PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000067 }
68
69 virtual FormatToken getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000070 // The \c UnwrappedLineParser guards against this by never calling
71 // \c getNextToken() after it has encountered the first eof token.
72 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000073 Token = PreviousTokenSource->getNextToken();
74 if (eof())
75 return createEOF();
76 return Token;
77 }
78
79private:
80 bool eof() {
81 return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
82 }
83
84 FormatToken createEOF() {
85 FormatToken FormatTok;
86 FormatTok.Tok.startToken();
87 FormatTok.Tok.setKind(tok::eof);
88 return FormatTok;
89 }
90
91 UnwrappedLine &Line;
92 FormatTokenSource *&TokenSource;
93 FormatToken &ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000094 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000095 FormatTokenSource *PreviousTokenSource;
96
97 FormatToken Token;
98};
99
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000100class ScopedLineState {
101public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000102 ScopedLineState(UnwrappedLineParser &Parser,
103 bool SwitchToPreprocessorLines = false)
104 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
105 if (SwitchToPreprocessorLines)
106 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000107 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000108 Parser.Line.reset(new UnwrappedLine());
109 Parser.Line->Level = PreBlockLine->Level;
110 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000111 }
112
113 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000114 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000115 Parser.addUnwrappedLine();
116 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000117 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000118 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000119 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000120 if (SwitchToPreprocessorLines)
121 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000122 }
123
124private:
125 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000126 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000127
128 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000129};
130
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000131UnwrappedLineParser::UnwrappedLineParser(
132 clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
133 FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000134 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
135 CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens),
136 Callback(Callback) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000137
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000138bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000139 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimekd4397b92013-01-04 23:34:14 +0000140 readToken();
Manuel Klimek525fe162013-01-18 14:04:34 +0000141 bool Error = parseFile();
142 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(),
143 E = Lines.end();
144 I != E; ++I) {
145 Callback.consumeUnwrappedLine(*I);
146 }
147 return Error;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000148}
149
150bool UnwrappedLineParser::parseFile() {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000151 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
152 /*MustBeDeclaration=*/ true);
Manuel Klimeka5342db2013-01-06 20:07:31 +0000153 bool Error = parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000154 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000155 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000156 addUnwrappedLine();
157 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000158}
159
Manuel Klimeka5342db2013-01-06 20:07:31 +0000160bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000161 bool Error = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000162 do {
163 switch (FormatTok.Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000164 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000165 nextToken();
166 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000167 break;
168 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000169 // FIXME: Add parameter whether this can happen - if this happens, we must
170 // be in a non-declaration context.
171 Error |= parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000172 addUnwrappedLine();
173 break;
174 case tok::r_brace:
Manuel Klimeka5342db2013-01-06 20:07:31 +0000175 if (HasOpeningBrace) {
176 return false;
177 } else {
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000178 Diag.Report(FormatTok.Tok.getLocation(),
179 Diag.getCustomDiagID(clang::DiagnosticsEngine::Error,
Alexander Kornienko276a2092013-01-11 16:03:45 +0000180 "unexpected '}'"));
Manuel Klimeka5342db2013-01-06 20:07:31 +0000181 Error = true;
182 nextToken();
183 addUnwrappedLine();
184 }
185 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000186 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000187 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000188 break;
189 }
190 } while (!eof());
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000191 return Error;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000192}
193
Manuel Klimek70b03f42013-01-23 09:32:48 +0000194bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels) {
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +0000195 assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000196 nextToken();
197
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000198 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000199
Manuel Klimek70b03f42013-01-23 09:32:48 +0000200 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
201 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000202 Line->Level += AddLevels;
203 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000204
Manuel Klimek86721d22013-01-22 16:31:55 +0000205 if (!FormatTok.Tok.is(tok::r_brace)) {
206 Line->Level -= AddLevels;
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000207 return true;
Manuel Klimek86721d22013-01-22 16:31:55 +0000208 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000209
Manuel Klimekde768542013-01-07 18:10:23 +0000210 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000211 Line->Level -= AddLevels;
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000212 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000213}
214
215void UnwrappedLineParser::parsePPDirective() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000216 assert(FormatTok.Tok.is(tok::hash) && "'#' expected");
Manuel Klimek526ed112013-01-09 15:25:02 +0000217 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka080a182013-01-02 16:30:12 +0000218 nextToken();
219
Manuel Klimeka080a182013-01-02 16:30:12 +0000220 if (FormatTok.Tok.getIdentifierInfo() == NULL) {
221 addUnwrappedLine();
Manuel Klimeka080a182013-01-02 16:30:12 +0000222 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000223 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000224
Manuel Klimekd4397b92013-01-04 23:34:14 +0000225 switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
226 case tok::pp_define:
227 parsePPDefine();
228 break;
229 default:
230 parsePPUnknown();
231 break;
232 }
233}
234
235void UnwrappedLineParser::parsePPDefine() {
236 nextToken();
237
238 if (FormatTok.Tok.getKind() != tok::identifier) {
239 parsePPUnknown();
240 return;
241 }
242 nextToken();
243 if (FormatTok.Tok.getKind() == tok::l_paren) {
244 parseParens();
245 }
246 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000247 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000248
249 // Errors during a preprocessor directive can only affect the layout of the
250 // preprocessor directive, and thus we ignore them. An alternative approach
251 // would be to use the same approach we use on the file level (no
252 // re-indentation if there was a structural error) within the macro
253 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000254 parseFile();
255}
256
257void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000258 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000259 nextToken();
260 } while (!eof());
261 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000262}
263
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000264void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000265 assert(!FormatTok.Tok.is(tok::l_brace));
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000266 int TokenNumber = 0;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000267 switch (FormatTok.Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000268 case tok::at:
269 nextToken();
270 switch (FormatTok.Tok.getObjCKeywordID()) {
271 case tok::objc_public:
272 case tok::objc_protected:
273 case tok::objc_package:
274 case tok::objc_private:
275 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000276 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000277 case tok::objc_implementation:
278 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000279 case tok::objc_protocol:
280 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000281 case tok::objc_end:
282 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000283 case tok::objc_optional:
284 case tok::objc_required:
285 nextToken();
286 addUnwrappedLine();
287 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000288 default:
289 break;
290 }
291 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000292 case tok::kw_namespace:
293 parseNamespace();
294 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000295 case tok::kw_inline:
296 nextToken();
297 TokenNumber++;
298 if (FormatTok.Tok.is(tok::kw_namespace)) {
299 parseNamespace();
300 return;
301 }
302 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000303 case tok::kw_public:
304 case tok::kw_protected:
305 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000306 parseAccessSpecifier();
307 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000308 case tok::kw_if:
309 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000310 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000311 case tok::kw_for:
312 case tok::kw_while:
313 parseForOrWhileLoop();
314 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000315 case tok::kw_do:
316 parseDoWhile();
317 return;
318 case tok::kw_switch:
319 parseSwitch();
320 return;
321 case tok::kw_default:
322 nextToken();
323 parseLabel();
324 return;
325 case tok::kw_case:
326 parseCaseLabel();
327 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000328 case tok::kw_return:
329 parseReturn();
330 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000331 case tok::kw_extern:
332 nextToken();
333 if (FormatTok.Tok.is(tok::string_literal)) {
334 nextToken();
335 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000336 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000337 addUnwrappedLine();
338 return;
339 }
340 }
341 // In all other cases, parse the declaration.
342 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000343 default:
344 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000345 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000346 do {
347 ++TokenNumber;
348 switch (FormatTok.Tok.getKind()) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000349 case tok::kw_enum:
350 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000351 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000352 case tok::kw_struct:
353 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000354 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000355 parseRecord();
356 // A record declaration or definition is always the start of a structural
357 // element.
358 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000359 case tok::semi:
360 nextToken();
361 addUnwrappedLine();
362 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000363 case tok::r_brace:
364 addUnwrappedLine();
365 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000366 case tok::l_paren:
367 parseParens();
368 break;
369 case tok::l_brace:
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000370 // A block outside of parentheses must be the last part of a
371 // structural element.
372 // FIXME: Figure out cases where this is not true, and add projections for
373 // them (the one we know is missing are lambdas).
Manuel Klimek70b03f42013-01-23 09:32:48 +0000374 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000375 addUnwrappedLine();
376 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000377 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000378 nextToken();
379 if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
380 parseLabel();
381 return;
382 }
383 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000384 case tok::equal:
385 nextToken();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000386 if (FormatTok.Tok.is(tok::l_brace)) {
387 parseBracedList();
388 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000389 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000390 default:
391 nextToken();
392 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000393 }
394 } while (!eof());
395}
396
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000397void UnwrappedLineParser::parseBracedList() {
398 nextToken();
399
400 do {
401 switch (FormatTok.Tok.getKind()) {
402 case tok::l_brace:
403 parseBracedList();
404 break;
405 case tok::r_brace:
406 nextToken();
407 return;
408 default:
409 nextToken();
410 break;
411 }
412 } while (!eof());
413}
414
Manuel Klimekc44ee892013-01-21 10:07:49 +0000415void UnwrappedLineParser::parseReturn() {
416 nextToken();
417
418 do {
419 switch (FormatTok.Tok.getKind()) {
420 case tok::l_brace:
421 parseBracedList();
422 break;
423 case tok::l_paren:
424 parseParens();
425 break;
426 case tok::r_brace:
427 // Assume missing ';'.
428 addUnwrappedLine();
429 return;
430 case tok::semi:
431 nextToken();
432 addUnwrappedLine();
433 return;
434 default:
435 nextToken();
436 break;
437 }
438 } while (!eof());
439}
440
Daniel Jasperbac016b2012-12-03 18:12:45 +0000441void UnwrappedLineParser::parseParens() {
442 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
443 nextToken();
444 do {
445 switch (FormatTok.Tok.getKind()) {
446 case tok::l_paren:
447 parseParens();
448 break;
449 case tok::r_paren:
450 nextToken();
451 return;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000452 case tok::l_brace:
453 {
454 nextToken();
455 ScopedLineState LineState(*this);
Manuel Klimek70b03f42013-01-23 09:32:48 +0000456 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
457 /*MustBeDeclaration=*/ false);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000458 Line->Level += 1;
Manuel Klimek70b03f42013-01-23 09:32:48 +0000459 parseLevel(/*HasOpeningBrace=*/ true);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000460 Line->Level -= 1;
461 }
462 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000463 default:
464 nextToken();
465 break;
466 }
467 } while (!eof());
468}
469
470void UnwrappedLineParser::parseIfThenElse() {
471 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
472 nextToken();
Manuel Klimekd4658432013-01-11 18:28:36 +0000473 if (FormatTok.Tok.is(tok::l_paren))
474 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000475 bool NeedsUnwrappedLine = false;
476 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000477 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000478 NeedsUnwrappedLine = true;
479 } else {
480 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000481 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000482 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000483 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000484 }
485 if (FormatTok.Tok.is(tok::kw_else)) {
486 nextToken();
487 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000488 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000489 addUnwrappedLine();
490 } else if (FormatTok.Tok.is(tok::kw_if)) {
491 parseIfThenElse();
492 } else {
493 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000494 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000495 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000496 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000497 }
498 } else if (NeedsUnwrappedLine) {
499 addUnwrappedLine();
500 }
501}
502
Alexander Kornienko15757312012-12-06 18:03:27 +0000503void UnwrappedLineParser::parseNamespace() {
504 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
505 nextToken();
506 if (FormatTok.Tok.is(tok::identifier))
507 nextToken();
508 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000509 parseBlock(/*MustBeDeclaration=*/ true, 0);
Alexander Kornienko15757312012-12-06 18:03:27 +0000510 addUnwrappedLine();
511 }
512 // FIXME: Add error handling.
513}
514
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000515void UnwrappedLineParser::parseForOrWhileLoop() {
516 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
517 "'for' or 'while' expected");
518 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000519 if (FormatTok.Tok.is(tok::l_paren))
520 parseParens();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000521 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000522 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000523 addUnwrappedLine();
524 } else {
525 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000526 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000527 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000528 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000529 }
530}
531
Daniel Jasperbac016b2012-12-03 18:12:45 +0000532void UnwrappedLineParser::parseDoWhile() {
533 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
534 nextToken();
535 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000536 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000537 } else {
538 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000539 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000540 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000541 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000542 }
543
Alexander Kornienko393b0082012-12-04 15:40:36 +0000544 // FIXME: Add error handling.
545 if (!FormatTok.Tok.is(tok::kw_while)) {
546 addUnwrappedLine();
547 return;
548 }
549
Daniel Jasperbac016b2012-12-03 18:12:45 +0000550 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000551 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000552}
553
554void UnwrappedLineParser::parseLabel() {
555 // FIXME: remove all asserts.
556 assert(FormatTok.Tok.is(tok::colon) && "':' expected");
557 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000558 unsigned OldLineLevel = Line->Level;
559 if (Line->Level > 0)
560 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000561 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000562 parseBlock(/*MustBeDeclaration=*/ false);
Nico Weber94fb7292013-01-18 05:50:57 +0000563 if (FormatTok.Tok.is(tok::kw_break))
564 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000565 }
566 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000567 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000568}
569
570void UnwrappedLineParser::parseCaseLabel() {
571 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
572 // FIXME: fix handling of complex expressions here.
573 do {
574 nextToken();
575 } while (!eof() && !FormatTok.Tok.is(tok::colon));
576 parseLabel();
577}
578
579void UnwrappedLineParser::parseSwitch() {
580 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
581 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000582 if (FormatTok.Tok.is(tok::l_paren))
583 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000584 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000585 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000586 addUnwrappedLine();
587 } else {
588 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000589 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000590 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000591 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000592 }
593}
594
595void UnwrappedLineParser::parseAccessSpecifier() {
596 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000597 // Otherwise, we don't know what it is, and we'd better keep the next token.
598 if (FormatTok.Tok.is(tok::colon))
599 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000600 addUnwrappedLine();
601}
602
603void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000604 nextToken();
605 if (FormatTok.Tok.is(tok::identifier) ||
606 FormatTok.Tok.is(tok::kw___attribute) ||
607 FormatTok.Tok.is(tok::kw___declspec)) {
608 nextToken();
609 // We can have macros or attributes in between 'enum' and the enum name.
610 if (FormatTok.Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000611 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000612 }
Manuel Klimek308232c2013-01-21 19:17:52 +0000613 if (FormatTok.Tok.is(tok::identifier))
614 nextToken();
615 }
616 if (FormatTok.Tok.is(tok::l_brace)) {
617 nextToken();
618 addUnwrappedLine();
619 ++Line->Level;
620 do {
621 switch (FormatTok.Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000622 case tok::l_paren:
623 parseParens();
624 break;
625 case tok::r_brace:
626 addUnwrappedLine();
627 nextToken();
628 --Line->Level;
629 return;
630 case tok::comma:
631 nextToken();
632 addUnwrappedLine();
633 break;
634 default:
635 nextToken();
636 break;
637 }
638 } while (!eof());
639 }
640 // We fall through to parsing a structural element afterwards, so that in
641 // enum A {} n, m;
642 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000643}
644
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000645void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000646 nextToken();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000647 if (FormatTok.Tok.is(tok::identifier) ||
648 FormatTok.Tok.is(tok::kw___attribute) ||
649 FormatTok.Tok.is(tok::kw___declspec)) {
650 nextToken();
651 // We can have macros or attributes in between 'class' and the class name.
652 if (FormatTok.Tok.is(tok::l_paren)) {
653 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000654 }
Manuel Klimek7f5b0252013-01-21 10:17:14 +0000655 // The actual identifier can be a nested name specifier.
656 while (FormatTok.Tok.is(tok::identifier) ||
657 FormatTok.Tok.is(tok::coloncolon))
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