blob: 12b7f7866fb334cfffc279aef34a6518f85d4468 [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
Daniel Jasperbac016b2012-12-03 18:12:45 +000022namespace clang {
23namespace format {
24
Manuel Klimek70b03f42013-01-23 09:32:48 +000025class ScopedDeclarationState {
26public:
27 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
28 bool MustBeDeclaration)
29 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000030 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000031 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000032 }
33 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000034 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000035 if (!Stack.empty())
36 Line.MustBeDeclaration = Stack.back();
37 else
38 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000039 }
40private:
41 UnwrappedLine &Line;
42 std::vector<bool> &Stack;
43};
44
Manuel Klimekd4397b92013-01-04 23:34:14 +000045class ScopedMacroState : public FormatTokenSource {
46public:
47 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
48 FormatToken &ResetToken)
49 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimekc37b4d62013-01-05 22:14:16 +000050 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000051 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000052 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000053 Line.InPPDirective = true;
54 }
55
56 ~ScopedMacroState() {
57 TokenSource = PreviousTokenSource;
58 ResetToken = Token;
59 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000060 Line.Level = PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000061 }
62
63 virtual FormatToken getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000064 // The \c UnwrappedLineParser guards against this by never calling
65 // \c getNextToken() after it has encountered the first eof token.
66 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000067 Token = PreviousTokenSource->getNextToken();
68 if (eof())
69 return createEOF();
70 return Token;
71 }
72
73private:
Alexander Kornienko3d713a72013-04-08 22:16:06 +000074 bool eof() { return Token.HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000075
76 FormatToken createEOF() {
77 FormatToken FormatTok;
78 FormatTok.Tok.startToken();
79 FormatTok.Tok.setKind(tok::eof);
80 return FormatTok;
81 }
82
83 UnwrappedLine &Line;
84 FormatTokenSource *&TokenSource;
85 FormatToken &ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000086 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +000087 FormatTokenSource *PreviousTokenSource;
88
89 FormatToken Token;
90};
91
Manuel Klimekbb42bf12013-01-10 11:52:21 +000092class ScopedLineState {
93public:
Manuel Klimek525fe162013-01-18 14:04:34 +000094 ScopedLineState(UnwrappedLineParser &Parser,
95 bool SwitchToPreprocessorLines = false)
96 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
97 if (SwitchToPreprocessorLines)
98 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +000099 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000100 Parser.Line.reset(new UnwrappedLine());
101 Parser.Line->Level = PreBlockLine->Level;
102 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000103 }
104
105 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000106 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000107 Parser.addUnwrappedLine();
108 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000109 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000110 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000111 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000112 if (SwitchToPreprocessorLines)
113 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000114 }
115
116private:
117 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000118 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000119
120 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000121};
122
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000123UnwrappedLineParser::UnwrappedLineParser(
124 clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
125 FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000126 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
127 CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens),
128 Callback(Callback) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000129
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000130bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000131 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimekd4397b92013-01-04 23:34:14 +0000132 readToken();
Manuel Klimek525fe162013-01-18 14:04:34 +0000133 bool Error = parseFile();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000134 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000135 I != E; ++I) {
136 Callback.consumeUnwrappedLine(*I);
137 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000138
139 // Create line with eof token.
140 pushToken(FormatTok);
141 Callback.consumeUnwrappedLine(*Line);
142
Manuel Klimek525fe162013-01-18 14:04:34 +0000143 return Error;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000144}
145
146bool UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000147 ScopedDeclarationState DeclarationState(
148 *Line, DeclarationScopeStack,
149 /*MustBeDeclaration=*/ !Line->InPPDirective);
Daniel Jasperf9955d32013-03-20 12:37:50 +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
Nico Weberd74fcdb2013-02-10 20:35:35 +0000191bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
192 unsigned AddLevels) {
Alexander Kornienkoa3a2b3a2012-12-06 17:49:17 +0000193 assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000194 nextToken();
195
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000196 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000197
Manuel Klimek70b03f42013-01-23 09:32:48 +0000198 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
199 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000200 Line->Level += AddLevels;
Daniel Jasperf9955d32013-03-20 12:37:50 +0000201 parseLevel(/*HasOpeningBrace=*/ true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000202
Manuel Klimek86721d22013-01-22 16:31:55 +0000203 if (!FormatTok.Tok.is(tok::r_brace)) {
204 Line->Level -= AddLevels;
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000205 return true;
Manuel Klimek86721d22013-01-22 16:31:55 +0000206 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000207
Daniel Jasperf9955d32013-03-20 12:37:50 +0000208 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000209 Line->Level -= AddLevels;
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000210 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000211}
212
213void UnwrappedLineParser::parsePPDirective() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000214 assert(FormatTok.Tok.is(tok::hash) && "'#' expected");
Manuel Klimek526ed112013-01-09 15:25:02 +0000215 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka080a182013-01-02 16:30:12 +0000216 nextToken();
217
Manuel Klimeka080a182013-01-02 16:30:12 +0000218 if (FormatTok.Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000219 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000220 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000222
Manuel Klimekd4397b92013-01-04 23:34:14 +0000223 switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
224 case tok::pp_define:
225 parsePPDefine();
226 break;
227 default:
228 parsePPUnknown();
229 break;
230 }
231}
232
233void UnwrappedLineParser::parsePPDefine() {
234 nextToken();
235
236 if (FormatTok.Tok.getKind() != tok::identifier) {
237 parsePPUnknown();
238 return;
239 }
240 nextToken();
Manuel Klimek7ccbc212013-01-23 14:37:36 +0000241 if (FormatTok.Tok.getKind() == tok::l_paren &&
242 FormatTok.WhiteSpaceLength == 0) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000243 parseParens();
244 }
245 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000246 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000247
248 // Errors during a preprocessor directive can only affect the layout of the
249 // preprocessor directive, and thus we ignore them. An alternative approach
250 // would be to use the same approach we use on the file level (no
251 // re-indentation if there was a structural error) within the macro
252 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000253 parseFile();
254}
255
256void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000257 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000258 nextToken();
259 } while (!eof());
260 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000261}
262
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000263void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000264 assert(!FormatTok.Tok.is(tok::l_brace));
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();
Nico Weberd74fcdb2013-02-10 20:35:35 +0000268 if (FormatTok.Tok.is(tok::l_brace)) {
269 parseBracedList();
270 break;
271 }
Nico Weber6092d4e2013-01-07 19:05:19 +0000272 switch (FormatTok.Tok.getObjCKeywordID()) {
273 case tok::objc_public:
274 case tok::objc_protected:
275 case tok::objc_package:
276 case tok::objc_private:
277 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000278 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000279 case tok::objc_implementation:
280 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000281 case tok::objc_protocol:
282 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000283 case tok::objc_end:
284 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000285 case tok::objc_optional:
286 case tok::objc_required:
287 nextToken();
288 addUnwrappedLine();
289 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000290 default:
291 break;
292 }
293 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000294 case tok::kw_namespace:
295 parseNamespace();
296 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000297 case tok::kw_inline:
298 nextToken();
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000299 if (FormatTok.Tok.is(tok::kw_namespace)) {
300 parseNamespace();
301 return;
302 }
303 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000304 case tok::kw_public:
305 case tok::kw_protected:
306 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000307 parseAccessSpecifier();
308 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000309 case tok::kw_if:
310 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000311 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000312 case tok::kw_for:
313 case tok::kw_while:
314 parseForOrWhileLoop();
315 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000316 case tok::kw_do:
317 parseDoWhile();
318 return;
319 case tok::kw_switch:
320 parseSwitch();
321 return;
322 case tok::kw_default:
323 nextToken();
324 parseLabel();
325 return;
326 case tok::kw_case:
327 parseCaseLabel();
328 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000329 case tok::kw_return:
330 parseReturn();
331 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000332 case tok::kw_extern:
333 nextToken();
334 if (FormatTok.Tok.is(tok::string_literal)) {
335 nextToken();
336 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000337 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000338 addUnwrappedLine();
339 return;
340 }
341 }
342 // In all other cases, parse the declaration.
343 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000344 default:
345 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000346 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000347 do {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000348 switch (FormatTok.Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000349 case tok::at:
350 nextToken();
351 if (FormatTok.Tok.is(tok::l_brace))
352 parseBracedList();
353 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000354 case tok::kw_enum:
355 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000356 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000357 case tok::kw_struct:
358 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000359 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000360 parseRecord();
361 // A record declaration or definition is always the start of a structural
362 // element.
363 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000364 case tok::semi:
365 nextToken();
366 addUnwrappedLine();
367 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000368 case tok::r_brace:
369 addUnwrappedLine();
370 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000371 case tok::l_paren:
372 parseParens();
373 break;
374 case tok::l_brace:
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000375 // A block outside of parentheses must be the last part of a
376 // structural element.
377 // FIXME: Figure out cases where this is not true, and add projections for
378 // them (the one we know is missing are lambdas).
Manuel Klimek70b03f42013-01-23 09:32:48 +0000379 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000380 addUnwrappedLine();
381 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000382 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000383 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000384 if (Line->Tokens.size() == 1) {
385 if (FormatTok.Tok.is(tok::colon)) {
386 parseLabel();
387 return;
388 }
389 // Recognize function-like macro usages without trailing semicolon in
390 // declaration context.
391 if (FormatTok.Tok.is(tok::l_paren)) {
392 parseParens();
393 if (Line->MustBeDeclaration && FormatTok.HasUnescapedNewline) {
394 addUnwrappedLine();
395 return;
396 }
397 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000398 }
399 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000400 case tok::equal:
401 nextToken();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000402 if (FormatTok.Tok.is(tok::l_brace)) {
403 parseBracedList();
404 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000405 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000406 default:
407 nextToken();
408 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000409 }
410 } while (!eof());
411}
412
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000413void UnwrappedLineParser::parseBracedList() {
414 nextToken();
415
416 do {
417 switch (FormatTok.Tok.getKind()) {
418 case tok::l_brace:
419 parseBracedList();
420 break;
421 case tok::r_brace:
422 nextToken();
423 return;
424 default:
425 nextToken();
426 break;
427 }
428 } while (!eof());
429}
430
Manuel Klimekc44ee892013-01-21 10:07:49 +0000431void UnwrappedLineParser::parseReturn() {
432 nextToken();
433
434 do {
435 switch (FormatTok.Tok.getKind()) {
436 case tok::l_brace:
437 parseBracedList();
438 break;
439 case tok::l_paren:
440 parseParens();
441 break;
442 case tok::r_brace:
443 // Assume missing ';'.
444 addUnwrappedLine();
445 return;
446 case tok::semi:
447 nextToken();
448 addUnwrappedLine();
449 return;
450 default:
451 nextToken();
452 break;
453 }
454 } while (!eof());
455}
456
Daniel Jasperbac016b2012-12-03 18:12:45 +0000457void UnwrappedLineParser::parseParens() {
458 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
459 nextToken();
460 do {
461 switch (FormatTok.Tok.getKind()) {
462 case tok::l_paren:
463 parseParens();
464 break;
465 case tok::r_paren:
466 nextToken();
467 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000468 case tok::l_brace: {
469 nextToken();
470 ScopedLineState LineState(*this);
471 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
472 /*MustBeDeclaration=*/ false);
473 Line->Level += 1;
474 parseLevel(/*HasOpeningBrace=*/ true);
475 Line->Level -= 1;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000476 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000477 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000478 case tok::at:
479 nextToken();
480 if (FormatTok.Tok.is(tok::l_brace))
481 parseBracedList();
482 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000483 default:
484 nextToken();
485 break;
486 }
487 } while (!eof());
488}
489
490void UnwrappedLineParser::parseIfThenElse() {
491 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
492 nextToken();
Manuel Klimekd4658432013-01-11 18:28:36 +0000493 if (FormatTok.Tok.is(tok::l_paren))
494 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000495 bool NeedsUnwrappedLine = false;
496 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000497 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000498 NeedsUnwrappedLine = true;
499 } else {
500 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000501 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000502 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000503 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000504 }
505 if (FormatTok.Tok.is(tok::kw_else)) {
506 nextToken();
507 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000508 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000509 addUnwrappedLine();
510 } else if (FormatTok.Tok.is(tok::kw_if)) {
511 parseIfThenElse();
512 } else {
513 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000514 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000515 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000516 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000517 }
518 } else if (NeedsUnwrappedLine) {
519 addUnwrappedLine();
520 }
521}
522
Alexander Kornienko15757312012-12-06 18:03:27 +0000523void UnwrappedLineParser::parseNamespace() {
524 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
525 nextToken();
526 if (FormatTok.Tok.is(tok::identifier))
527 nextToken();
528 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000529 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000530 // Munch the semicolon after a namespace. This is more common than one would
531 // think. Puttin the semicolon into its own line is very ugly.
532 if (FormatTok.Tok.is(tok::semi))
533 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000534 addUnwrappedLine();
535 }
536 // FIXME: Add error handling.
537}
538
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000539void UnwrappedLineParser::parseForOrWhileLoop() {
540 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
541 "'for' or 'while' expected");
542 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000543 if (FormatTok.Tok.is(tok::l_paren))
544 parseParens();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000545 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000546 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000547 addUnwrappedLine();
548 } else {
549 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000550 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000551 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000552 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000553 }
554}
555
Daniel Jasperbac016b2012-12-03 18:12:45 +0000556void UnwrappedLineParser::parseDoWhile() {
557 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
558 nextToken();
559 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000560 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000561 } else {
562 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000563 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000564 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000565 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000566 }
567
Alexander Kornienko393b0082012-12-04 15:40:36 +0000568 // FIXME: Add error handling.
569 if (!FormatTok.Tok.is(tok::kw_while)) {
570 addUnwrappedLine();
571 return;
572 }
573
Daniel Jasperbac016b2012-12-03 18:12:45 +0000574 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000575 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000576}
577
578void UnwrappedLineParser::parseLabel() {
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000579 if (FormatTok.Tok.isNot(tok::colon))
580 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000581 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000582 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000583 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000584 --Line->Level;
Daniel Jasperc30eb512013-03-19 18:33:58 +0000585 if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000586 parseBlock(/*MustBeDeclaration=*/ false);
Nico Weber94fb7292013-01-18 05:50:57 +0000587 if (FormatTok.Tok.is(tok::kw_break))
588 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000589 }
590 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000591 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000592}
593
594void UnwrappedLineParser::parseCaseLabel() {
595 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
596 // FIXME: fix handling of complex expressions here.
597 do {
598 nextToken();
599 } while (!eof() && !FormatTok.Tok.is(tok::colon));
600 parseLabel();
601}
602
603void UnwrappedLineParser::parseSwitch() {
604 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
605 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000606 if (FormatTok.Tok.is(tok::l_paren))
607 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000608 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000609 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000610 addUnwrappedLine();
611 } else {
612 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000613 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000614 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000615 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000616 }
617}
618
619void UnwrappedLineParser::parseAccessSpecifier() {
620 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000621 // Otherwise, we don't know what it is, and we'd better keep the next token.
622 if (FormatTok.Tok.is(tok::colon))
623 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000624 addUnwrappedLine();
625}
626
627void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000628 nextToken();
629 if (FormatTok.Tok.is(tok::identifier) ||
630 FormatTok.Tok.is(tok::kw___attribute) ||
631 FormatTok.Tok.is(tok::kw___declspec)) {
632 nextToken();
633 // We can have macros or attributes in between 'enum' and the enum name.
634 if (FormatTok.Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000635 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000636 }
Manuel Klimek308232c2013-01-21 19:17:52 +0000637 if (FormatTok.Tok.is(tok::identifier))
638 nextToken();
639 }
640 if (FormatTok.Tok.is(tok::l_brace)) {
641 nextToken();
642 addUnwrappedLine();
643 ++Line->Level;
644 do {
645 switch (FormatTok.Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000646 case tok::l_paren:
647 parseParens();
648 break;
649 case tok::r_brace:
650 addUnwrappedLine();
651 nextToken();
652 --Line->Level;
653 return;
654 case tok::comma:
655 nextToken();
656 addUnwrappedLine();
657 break;
658 default:
659 nextToken();
660 break;
661 }
662 } while (!eof());
663 }
664 // We fall through to parsing a structural element afterwards, so that in
665 // enum A {} n, m;
666 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000667}
668
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000669void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000670 nextToken();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000671 if (FormatTok.Tok.is(tok::identifier) ||
672 FormatTok.Tok.is(tok::kw___attribute) ||
673 FormatTok.Tok.is(tok::kw___declspec)) {
674 nextToken();
675 // We can have macros or attributes in between 'class' and the class name.
676 if (FormatTok.Tok.is(tok::l_paren)) {
677 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000678 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000679 // The actual identifier can be a nested name specifier, and in macros
680 // it is often token-pasted.
Manuel Klimek7f5b0252013-01-21 10:17:14 +0000681 while (FormatTok.Tok.is(tok::identifier) ||
Daniel Jasperf9955d32013-03-20 12:37:50 +0000682 FormatTok.Tok.is(tok::coloncolon) || FormatTok.Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000683 nextToken();
684
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000685 // Note that parsing away template declarations here leads to incorrectly
686 // accepting function declarations as record declarations.
687 // In general, we cannot solve this problem. Consider:
688 // class A<int> B() {}
689 // which can be a function definition or a class definition when B() is a
690 // macro. If we find enough real-world cases where this is a problem, we
691 // can parse for the 'template' keyword in the beginning of the statement,
692 // and thus rule out the record production in case there is no template
693 // (this would still leave us with an ambiguity between template function
694 // and class declarations).
695 if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) {
Daniel Jasper6fe554e2013-03-20 15:12:38 +0000696 while (!eof() && FormatTok.Tok.isNot(tok::l_brace)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000697 if (FormatTok.Tok.is(tok::semi))
698 return;
699 nextToken();
700 }
701 }
702 }
703 if (FormatTok.Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000704 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000705 // We fall through to parsing a structural element afterwards, so
706 // class A {} n, m;
707 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000708}
709
Nico Weber1abe6ea2013-01-09 21:15:03 +0000710void UnwrappedLineParser::parseObjCProtocolList() {
711 assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
712 do
713 nextToken();
714 while (!eof() && FormatTok.Tok.isNot(tok::greater));
715 nextToken(); // Skip '>'.
716}
717
718void UnwrappedLineParser::parseObjCUntilAtEnd() {
719 do {
720 if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
721 nextToken();
722 addUnwrappedLine();
723 break;
724 }
725 parseStructuralElement();
726 } while (!eof());
727}
728
Nico Weber50767d82013-01-09 23:25:37 +0000729void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000730 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000731 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000732
733 // @interface can be followed by either a base class, or a category.
734 if (FormatTok.Tok.is(tok::colon)) {
735 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000736 nextToken(); // base class name
Nico Weber27d13672013-01-09 20:25:35 +0000737 } else if (FormatTok.Tok.is(tok::l_paren))
738 // Skip category, if present.
739 parseParens();
740
Nico Weber1abe6ea2013-01-09 21:15:03 +0000741 if (FormatTok.Tok.is(tok::less))
742 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000743
744 // If instance variables are present, keep the '{' on the first line too.
745 if (FormatTok.Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000746 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000747
748 // With instance variables, this puts '}' on its own line. Without instance
749 // variables, this ends the @interface line.
750 addUnwrappedLine();
751
Nico Weber1abe6ea2013-01-09 21:15:03 +0000752 parseObjCUntilAtEnd();
753}
Nico Weber27d13672013-01-09 20:25:35 +0000754
Nico Weber1abe6ea2013-01-09 21:15:03 +0000755void UnwrappedLineParser::parseObjCProtocol() {
756 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000757 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +0000758
759 if (FormatTok.Tok.is(tok::less))
760 parseObjCProtocolList();
761
762 // Check for protocol declaration.
763 if (FormatTok.Tok.is(tok::semi)) {
764 nextToken();
765 return addUnwrappedLine();
766 }
767
768 addUnwrappedLine();
769 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +0000770}
771
Daniel Jasperbac016b2012-12-03 18:12:45 +0000772void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000773 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +0000774 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +0000775 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +0000776 llvm::dbgs() << "Line(" << Line->Level << ")"
777 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimek8fa37992013-01-16 12:31:12 +0000778 for (std::list<FormatToken>::iterator I = Line->Tokens.begin(),
779 E = Line->Tokens.end();
780 I != E; ++I) {
781 llvm::dbgs() << I->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000782
Manuel Klimek8fa37992013-01-16 12:31:12 +0000783 }
784 llvm::dbgs() << "\n";
785 });
Manuel Klimek525fe162013-01-18 14:04:34 +0000786 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000787 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +0000788 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +0000789 for (std::vector<UnwrappedLine>::iterator
790 I = PreprocessorDirectives.begin(),
791 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000792 I != E; ++I) {
793 CurrentLines->push_back(*I);
794 }
795 PreprocessorDirectives.clear();
796 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000797}
798
Daniel Jasperf9955d32013-03-20 12:37:50 +0000799bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000800
Manuel Klimek86721d22013-01-22 16:31:55 +0000801void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
802 bool JustComments = Line->Tokens.empty();
803 for (SmallVectorImpl<FormatToken>::const_iterator
804 I = CommentsBeforeNextToken.begin(),
805 E = CommentsBeforeNextToken.end();
806 I != E; ++I) {
Manuel Klimekb3507cd2013-02-06 16:40:56 +0000807 if (I->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000808 addUnwrappedLine();
809 }
810 pushToken(*I);
811 }
812 if (NewlineBeforeNext && JustComments) {
813 addUnwrappedLine();
814 }
815 CommentsBeforeNextToken.clear();
816}
817
Daniel Jasperbac016b2012-12-03 18:12:45 +0000818void UnwrappedLineParser::nextToken() {
819 if (eof())
820 return;
Manuel Klimekb3507cd2013-02-06 16:40:56 +0000821 flushComments(FormatTok.NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +0000822 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000823 readToken();
824}
825
826void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +0000827 bool CommentsInCurrentLine = true;
828 do {
829 FormatTok = Tokens->getNextToken();
830 while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000831 (FormatTok.HasUnescapedNewline || FormatTok.IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000832 // If there is an unfinished unwrapped line, we flush the preprocessor
833 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +0000834 bool SwitchToPreprocessorLines =
835 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +0000836 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +0000837 // Comments stored before the preprocessor directive need to be output
838 // before the preprocessor directive, at the same level as the
839 // preprocessor directive, as we consider them to apply to the directive.
840 flushComments(FormatTok.NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +0000841 parsePPDirective();
842 }
843 if (!FormatTok.Tok.is(tok::comment))
844 return;
Manuel Klimekb3507cd2013-02-06 16:40:56 +0000845 if (FormatTok.NewlinesBefore > 0 || FormatTok.IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000846 CommentsInCurrentLine = false;
847 }
848 if (CommentsInCurrentLine) {
849 pushToken(FormatTok);
850 } else {
851 CommentsBeforeNextToken.push_back(FormatTok);
852 }
853 } while (!eof());
854}
855
856void UnwrappedLineParser::pushToken(const FormatToken &Tok) {
857 Line->Tokens.push_back(Tok);
858 if (MustBreakBeforeNextToken) {
859 Line->Tokens.back().MustBreakBefore = true;
860 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000861 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000862}
863
Daniel Jaspercd162382013-01-07 13:26:07 +0000864} // end namespace format
865} // end namespace clang