blob: cf2ef718e934da60bdf9ede95039fe4515044b9c [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
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000263// Here we blacklist certain tokens that are not usually the first token in an
264// unwrapped line. This is used in attempt to distinguish macro calls without
265// trailing semicolons from other constructs split to several lines.
266bool tokenCanStartNewLine(clang::Token Tok) {
267 // Semicolon can be a null-statement, l_square can be a start of a macro or
268 // a C++11 attribute, but this doesn't seem to be common.
269 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
270 Tok.isNot(tok::l_square) &&
271 // Tokens that can only be used as binary operators and a part of
272 // overloaded operator names.
273 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
274 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
275 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
276 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
277 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
278 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
279 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
280 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
281 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
282 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
283 Tok.isNot(tok::lesslessequal) &&
284 // Colon is used in labels, base class lists, initializer lists,
285 // range-based for loops, ternary operator, but should never be the
286 // first token in an unwrapped line.
287 Tok.isNot(tok::colon);
288}
289
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000290void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000291 assert(!FormatTok.Tok.is(tok::l_brace));
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000292 switch (FormatTok.Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000293 case tok::at:
294 nextToken();
Nico Weberd74fcdb2013-02-10 20:35:35 +0000295 if (FormatTok.Tok.is(tok::l_brace)) {
296 parseBracedList();
297 break;
298 }
Nico Weber6092d4e2013-01-07 19:05:19 +0000299 switch (FormatTok.Tok.getObjCKeywordID()) {
300 case tok::objc_public:
301 case tok::objc_protected:
302 case tok::objc_package:
303 case tok::objc_private:
304 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000305 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000306 case tok::objc_implementation:
307 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000308 case tok::objc_protocol:
309 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000310 case tok::objc_end:
311 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000312 case tok::objc_optional:
313 case tok::objc_required:
314 nextToken();
315 addUnwrappedLine();
316 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000317 default:
318 break;
319 }
320 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000321 case tok::kw_namespace:
322 parseNamespace();
323 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000324 case tok::kw_inline:
325 nextToken();
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000326 if (FormatTok.Tok.is(tok::kw_namespace)) {
327 parseNamespace();
328 return;
329 }
330 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000331 case tok::kw_public:
332 case tok::kw_protected:
333 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000334 parseAccessSpecifier();
335 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000336 case tok::kw_if:
337 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000338 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000339 case tok::kw_for:
340 case tok::kw_while:
341 parseForOrWhileLoop();
342 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000343 case tok::kw_do:
344 parseDoWhile();
345 return;
346 case tok::kw_switch:
347 parseSwitch();
348 return;
349 case tok::kw_default:
350 nextToken();
351 parseLabel();
352 return;
353 case tok::kw_case:
354 parseCaseLabel();
355 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000356 case tok::kw_return:
357 parseReturn();
358 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000359 case tok::kw_extern:
360 nextToken();
361 if (FormatTok.Tok.is(tok::string_literal)) {
362 nextToken();
363 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000364 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000365 addUnwrappedLine();
366 return;
367 }
368 }
369 // In all other cases, parse the declaration.
370 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000371 default:
372 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000373 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000374 do {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000375 switch (FormatTok.Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000376 case tok::at:
377 nextToken();
378 if (FormatTok.Tok.is(tok::l_brace))
379 parseBracedList();
380 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000381 case tok::kw_enum:
382 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000383 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000384 case tok::kw_struct:
385 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000386 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000387 parseRecord();
388 // A record declaration or definition is always the start of a structural
389 // element.
390 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000391 case tok::semi:
392 nextToken();
393 addUnwrappedLine();
394 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000395 case tok::r_brace:
396 addUnwrappedLine();
397 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000398 case tok::l_paren:
399 parseParens();
400 break;
401 case tok::l_brace:
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000402 // A block outside of parentheses must be the last part of a
403 // structural element.
404 // FIXME: Figure out cases where this is not true, and add projections for
405 // them (the one we know is missing are lambdas).
Manuel Klimek70b03f42013-01-23 09:32:48 +0000406 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000407 addUnwrappedLine();
408 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000409 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000410 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000411 if (Line->Tokens.size() == 1) {
412 if (FormatTok.Tok.is(tok::colon)) {
413 parseLabel();
414 return;
415 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000416 // Recognize function-like macro usages without trailing semicolon.
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000417 if (FormatTok.Tok.is(tok::l_paren)) {
418 parseParens();
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000419 if (FormatTok.HasUnescapedNewline &&
420 tokenCanStartNewLine(FormatTok.Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000421 addUnwrappedLine();
422 return;
423 }
424 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000425 }
426 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000427 case tok::equal:
428 nextToken();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000429 if (FormatTok.Tok.is(tok::l_brace)) {
430 parseBracedList();
431 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000432 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000433 default:
434 nextToken();
435 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000436 }
437 } while (!eof());
438}
439
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000440void UnwrappedLineParser::parseBracedList() {
441 nextToken();
442
Manuel Klimek423dd932013-04-10 09:52:05 +0000443 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
444 // replace this by using parseAssigmentExpression() inside.
445 bool StartOfExpression = true;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000446 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000447 // FIXME: When we start to support lambdas, we'll want to parse them away
448 // here, otherwise our bail-out scenarios below break. The better solution
449 // might be to just implement a more or less complete expression parser.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000450 switch (FormatTok.Tok.getKind()) {
451 case tok::l_brace:
Manuel Klimek423dd932013-04-10 09:52:05 +0000452 if (!StartOfExpression) {
453 // Probably a missing closing brace. Bail out.
454 addUnwrappedLine();
455 return;
456 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000457 parseBracedList();
Manuel Klimek423dd932013-04-10 09:52:05 +0000458 StartOfExpression = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000459 break;
460 case tok::r_brace:
461 nextToken();
462 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000463 case tok::semi:
464 // Probably a missing closing brace. Bail out.
465 return;
466 case tok::comma:
467 nextToken();
468 StartOfExpression = true;
469 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000470 default:
471 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000472 StartOfExpression = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000473 break;
474 }
475 } while (!eof());
476}
477
Manuel Klimekc44ee892013-01-21 10:07:49 +0000478void UnwrappedLineParser::parseReturn() {
479 nextToken();
480
481 do {
482 switch (FormatTok.Tok.getKind()) {
483 case tok::l_brace:
484 parseBracedList();
Manuel Klimek423dd932013-04-10 09:52:05 +0000485 if (FormatTok.Tok.isNot(tok::semi)) {
486 // Assume missing ';'.
487 addUnwrappedLine();
488 return;
489 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000490 break;
491 case tok::l_paren:
492 parseParens();
493 break;
494 case tok::r_brace:
495 // Assume missing ';'.
496 addUnwrappedLine();
497 return;
498 case tok::semi:
499 nextToken();
500 addUnwrappedLine();
501 return;
502 default:
503 nextToken();
504 break;
505 }
506 } while (!eof());
507}
508
Daniel Jasperbac016b2012-12-03 18:12:45 +0000509void UnwrappedLineParser::parseParens() {
510 assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
511 nextToken();
512 do {
513 switch (FormatTok.Tok.getKind()) {
514 case tok::l_paren:
515 parseParens();
516 break;
517 case tok::r_paren:
518 nextToken();
519 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000520 case tok::l_brace: {
521 nextToken();
522 ScopedLineState LineState(*this);
523 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
524 /*MustBeDeclaration=*/ false);
525 Line->Level += 1;
526 parseLevel(/*HasOpeningBrace=*/ true);
527 Line->Level -= 1;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000528 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000529 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000530 case tok::at:
531 nextToken();
532 if (FormatTok.Tok.is(tok::l_brace))
533 parseBracedList();
534 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000535 default:
536 nextToken();
537 break;
538 }
539 } while (!eof());
540}
541
542void UnwrappedLineParser::parseIfThenElse() {
543 assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
544 nextToken();
Manuel Klimekd4658432013-01-11 18:28:36 +0000545 if (FormatTok.Tok.is(tok::l_paren))
546 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000547 bool NeedsUnwrappedLine = false;
548 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000549 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000550 NeedsUnwrappedLine = true;
551 } else {
552 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000553 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000554 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000555 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000556 }
557 if (FormatTok.Tok.is(tok::kw_else)) {
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 addUnwrappedLine();
562 } else if (FormatTok.Tok.is(tok::kw_if)) {
563 parseIfThenElse();
564 } else {
565 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000566 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000567 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000568 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000569 }
570 } else if (NeedsUnwrappedLine) {
571 addUnwrappedLine();
572 }
573}
574
Alexander Kornienko15757312012-12-06 18:03:27 +0000575void UnwrappedLineParser::parseNamespace() {
576 assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
577 nextToken();
578 if (FormatTok.Tok.is(tok::identifier))
579 nextToken();
580 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000581 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000582 // Munch the semicolon after a namespace. This is more common than one would
583 // think. Puttin the semicolon into its own line is very ugly.
584 if (FormatTok.Tok.is(tok::semi))
585 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000586 addUnwrappedLine();
587 }
588 // FIXME: Add error handling.
589}
590
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000591void UnwrappedLineParser::parseForOrWhileLoop() {
592 assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
593 "'for' or 'while' expected");
594 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000595 if (FormatTok.Tok.is(tok::l_paren))
596 parseParens();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000597 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000598 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000599 addUnwrappedLine();
600 } else {
601 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000602 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000603 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000604 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000605 }
606}
607
Daniel Jasperbac016b2012-12-03 18:12:45 +0000608void UnwrappedLineParser::parseDoWhile() {
609 assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
610 nextToken();
611 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000612 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000613 } else {
614 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000615 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000616 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000617 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000618 }
619
Alexander Kornienko393b0082012-12-04 15:40:36 +0000620 // FIXME: Add error handling.
621 if (!FormatTok.Tok.is(tok::kw_while)) {
622 addUnwrappedLine();
623 return;
624 }
625
Daniel Jasperbac016b2012-12-03 18:12:45 +0000626 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000627 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000628}
629
630void UnwrappedLineParser::parseLabel() {
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000631 if (FormatTok.Tok.isNot(tok::colon))
632 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000633 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000634 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000635 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000636 --Line->Level;
Daniel Jasperc30eb512013-03-19 18:33:58 +0000637 if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000638 parseBlock(/*MustBeDeclaration=*/ false);
Nico Weber94fb7292013-01-18 05:50:57 +0000639 if (FormatTok.Tok.is(tok::kw_break))
640 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000641 }
642 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000643 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000644}
645
646void UnwrappedLineParser::parseCaseLabel() {
647 assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
648 // FIXME: fix handling of complex expressions here.
649 do {
650 nextToken();
651 } while (!eof() && !FormatTok.Tok.is(tok::colon));
652 parseLabel();
653}
654
655void UnwrappedLineParser::parseSwitch() {
656 assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
657 nextToken();
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000658 if (FormatTok.Tok.is(tok::l_paren))
659 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000660 if (FormatTok.Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000661 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000662 addUnwrappedLine();
663 } else {
664 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000665 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000666 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000667 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000668 }
669}
670
671void UnwrappedLineParser::parseAccessSpecifier() {
672 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000673 // Otherwise, we don't know what it is, and we'd better keep the next token.
674 if (FormatTok.Tok.is(tok::colon))
675 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000676 addUnwrappedLine();
677}
678
679void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000680 nextToken();
681 if (FormatTok.Tok.is(tok::identifier) ||
682 FormatTok.Tok.is(tok::kw___attribute) ||
683 FormatTok.Tok.is(tok::kw___declspec)) {
684 nextToken();
685 // We can have macros or attributes in between 'enum' and the enum name.
686 if (FormatTok.Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000687 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000688 }
Manuel Klimek308232c2013-01-21 19:17:52 +0000689 if (FormatTok.Tok.is(tok::identifier))
690 nextToken();
691 }
692 if (FormatTok.Tok.is(tok::l_brace)) {
693 nextToken();
694 addUnwrappedLine();
695 ++Line->Level;
696 do {
697 switch (FormatTok.Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000698 case tok::l_paren:
699 parseParens();
700 break;
701 case tok::r_brace:
702 addUnwrappedLine();
703 nextToken();
704 --Line->Level;
705 return;
706 case tok::comma:
707 nextToken();
708 addUnwrappedLine();
709 break;
710 default:
711 nextToken();
712 break;
713 }
714 } while (!eof());
715 }
716 // We fall through to parsing a structural element afterwards, so that in
717 // enum A {} n, m;
718 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000719}
720
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000721void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000722 nextToken();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000723 if (FormatTok.Tok.is(tok::identifier) ||
724 FormatTok.Tok.is(tok::kw___attribute) ||
725 FormatTok.Tok.is(tok::kw___declspec)) {
726 nextToken();
727 // We can have macros or attributes in between 'class' and the class name.
728 if (FormatTok.Tok.is(tok::l_paren)) {
729 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000730 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000731 // The actual identifier can be a nested name specifier, and in macros
732 // it is often token-pasted.
Manuel Klimek7f5b0252013-01-21 10:17:14 +0000733 while (FormatTok.Tok.is(tok::identifier) ||
Daniel Jasperf9955d32013-03-20 12:37:50 +0000734 FormatTok.Tok.is(tok::coloncolon) || FormatTok.Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000735 nextToken();
736
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000737 // Note that parsing away template declarations here leads to incorrectly
738 // accepting function declarations as record declarations.
739 // In general, we cannot solve this problem. Consider:
740 // class A<int> B() {}
741 // which can be a function definition or a class definition when B() is a
742 // macro. If we find enough real-world cases where this is a problem, we
743 // can parse for the 'template' keyword in the beginning of the statement,
744 // and thus rule out the record production in case there is no template
745 // (this would still leave us with an ambiguity between template function
746 // and class declarations).
747 if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) {
Daniel Jasper6fe554e2013-03-20 15:12:38 +0000748 while (!eof() && FormatTok.Tok.isNot(tok::l_brace)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000749 if (FormatTok.Tok.is(tok::semi))
750 return;
751 nextToken();
752 }
753 }
754 }
755 if (FormatTok.Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000756 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000757 // We fall through to parsing a structural element afterwards, so
758 // class A {} n, m;
759 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000760}
761
Nico Weber1abe6ea2013-01-09 21:15:03 +0000762void UnwrappedLineParser::parseObjCProtocolList() {
763 assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
764 do
765 nextToken();
766 while (!eof() && FormatTok.Tok.isNot(tok::greater));
767 nextToken(); // Skip '>'.
768}
769
770void UnwrappedLineParser::parseObjCUntilAtEnd() {
771 do {
772 if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
773 nextToken();
774 addUnwrappedLine();
775 break;
776 }
777 parseStructuralElement();
778 } while (!eof());
779}
780
Nico Weber50767d82013-01-09 23:25:37 +0000781void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000782 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000783 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000784
785 // @interface can be followed by either a base class, or a category.
786 if (FormatTok.Tok.is(tok::colon)) {
787 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000788 nextToken(); // base class name
Nico Weber27d13672013-01-09 20:25:35 +0000789 } else if (FormatTok.Tok.is(tok::l_paren))
790 // Skip category, if present.
791 parseParens();
792
Nico Weber1abe6ea2013-01-09 21:15:03 +0000793 if (FormatTok.Tok.is(tok::less))
794 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000795
796 // If instance variables are present, keep the '{' on the first line too.
797 if (FormatTok.Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000798 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000799
800 // With instance variables, this puts '}' on its own line. Without instance
801 // variables, this ends the @interface line.
802 addUnwrappedLine();
803
Nico Weber1abe6ea2013-01-09 21:15:03 +0000804 parseObjCUntilAtEnd();
805}
Nico Weber27d13672013-01-09 20:25:35 +0000806
Nico Weber1abe6ea2013-01-09 21:15:03 +0000807void UnwrappedLineParser::parseObjCProtocol() {
808 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000809 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +0000810
811 if (FormatTok.Tok.is(tok::less))
812 parseObjCProtocolList();
813
814 // Check for protocol declaration.
815 if (FormatTok.Tok.is(tok::semi)) {
816 nextToken();
817 return addUnwrappedLine();
818 }
819
820 addUnwrappedLine();
821 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +0000822}
823
Daniel Jasperbac016b2012-12-03 18:12:45 +0000824void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000825 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +0000826 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +0000827 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +0000828 llvm::dbgs() << "Line(" << Line->Level << ")"
829 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimek8fa37992013-01-16 12:31:12 +0000830 for (std::list<FormatToken>::iterator I = Line->Tokens.begin(),
831 E = Line->Tokens.end();
832 I != E; ++I) {
833 llvm::dbgs() << I->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000834
Manuel Klimek8fa37992013-01-16 12:31:12 +0000835 }
836 llvm::dbgs() << "\n";
837 });
Manuel Klimek525fe162013-01-18 14:04:34 +0000838 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000839 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +0000840 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +0000841 for (std::vector<UnwrappedLine>::iterator
842 I = PreprocessorDirectives.begin(),
843 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000844 I != E; ++I) {
845 CurrentLines->push_back(*I);
846 }
847 PreprocessorDirectives.clear();
848 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000849}
850
Daniel Jasperf9955d32013-03-20 12:37:50 +0000851bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000852
Manuel Klimek86721d22013-01-22 16:31:55 +0000853void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
854 bool JustComments = Line->Tokens.empty();
855 for (SmallVectorImpl<FormatToken>::const_iterator
856 I = CommentsBeforeNextToken.begin(),
857 E = CommentsBeforeNextToken.end();
858 I != E; ++I) {
Manuel Klimekb3507cd2013-02-06 16:40:56 +0000859 if (I->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000860 addUnwrappedLine();
861 }
862 pushToken(*I);
863 }
864 if (NewlineBeforeNext && JustComments) {
865 addUnwrappedLine();
866 }
867 CommentsBeforeNextToken.clear();
868}
869
Daniel Jasperbac016b2012-12-03 18:12:45 +0000870void UnwrappedLineParser::nextToken() {
871 if (eof())
872 return;
Manuel Klimekb3507cd2013-02-06 16:40:56 +0000873 flushComments(FormatTok.NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +0000874 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000875 readToken();
876}
877
878void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +0000879 bool CommentsInCurrentLine = true;
880 do {
881 FormatTok = Tokens->getNextToken();
882 while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000883 (FormatTok.HasUnescapedNewline || FormatTok.IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000884 // If there is an unfinished unwrapped line, we flush the preprocessor
885 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +0000886 bool SwitchToPreprocessorLines =
887 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +0000888 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +0000889 // Comments stored before the preprocessor directive need to be output
890 // before the preprocessor directive, at the same level as the
891 // preprocessor directive, as we consider them to apply to the directive.
892 flushComments(FormatTok.NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +0000893 parsePPDirective();
894 }
895 if (!FormatTok.Tok.is(tok::comment))
896 return;
Manuel Klimekb3507cd2013-02-06 16:40:56 +0000897 if (FormatTok.NewlinesBefore > 0 || FormatTok.IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000898 CommentsInCurrentLine = false;
899 }
900 if (CommentsInCurrentLine) {
901 pushToken(FormatTok);
902 } else {
903 CommentsBeforeNextToken.push_back(FormatTok);
904 }
905 } while (!eof());
906}
907
908void UnwrappedLineParser::pushToken(const FormatToken &Tok) {
909 Line->Tokens.push_back(Tok);
910 if (MustBreakBeforeNextToken) {
911 Line->Tokens.back().MustBreakBefore = true;
912 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000913 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000914}
915
Daniel Jaspercd162382013-01-07 13:26:07 +0000916} // end namespace format
917} // end namespace clang