blob: d0750af1b0fa4e11590d5bc86c8e10a24300ed6f [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
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000018
Stephen Hines6bcf27b2014-05-29 04:14:42 -070019#define DEBUG_TYPE "format-parser"
20
Daniel Jasperbac016b2012-12-03 18:12:45 +000021namespace clang {
22namespace format {
23
Manuel Klimek96e888b2013-05-28 11:55:06 +000024class FormatTokenSource {
25public:
26 virtual ~FormatTokenSource() {}
27 virtual FormatToken *getNextToken() = 0;
28
29 virtual unsigned getPosition() = 0;
30 virtual FormatToken *setPosition(unsigned Position) = 0;
31};
32
Craig Toppere50947f2013-07-01 04:21:54 +000033namespace {
34
Manuel Klimek70b03f42013-01-23 09:32:48 +000035class ScopedDeclarationState {
36public:
37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000040 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000041 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000042 }
43 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000044 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000045 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000049 }
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000050
Manuel Klimek70b03f42013-01-23 09:32:48 +000051private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54};
55
Manuel Klimekd4397b92013-01-04 23:34:14 +000056class ScopedMacroState : public FormatTokenSource {
57public:
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek96e888b2013-05-28 11:55:06 +000059 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimekd4397b92013-01-04 23:34:14 +000060 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek67d080d2013-04-12 14:13:36 +000061 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070063 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000065 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000066 Line.InPPDirective = true;
67 }
68
69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000073 Line.Level = PreviousLineLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +000074 StructuralError = PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +000075 }
76
Stephen Hines651f13c2014-04-23 16:59:28 -070077 FormatToken *getNextToken() override {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek96e888b2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimekd4397b92013-01-04 23:34:14 +000084 return Token;
85 }
86
Stephen Hines651f13c2014-04-23 16:59:28 -070087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimek80829bd2013-05-23 09:41:43 +000088
Stephen Hines651f13c2014-04-23 16:59:28 -070089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimekd4397b92013-01-04 23:34:14 +000094private:
Manuel Klimek96e888b2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000096
Manuel Klimek96e888b2013-05-28 11:55:06 +000097 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000113 bool &StructuralError;
114 bool PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000115
Manuel Klimek96e888b2013-05-28 11:55:06 +0000116 FormatToken *Token;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000117};
118
Craig Toppere50947f2013-07-01 04:21:54 +0000119} // end anonymous namespace
120
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000121class ScopedLineState {
122public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
Daniel Jasper567dcf92013-09-05 09:29:45 +0000125 : Parser(Parser) {
126 OriginalLines = Parser.CurrentLines;
Manuel Klimek525fe162013-01-18 14:04:34 +0000127 if (SwitchToPreprocessorLines)
128 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000129 else if (!Parser.Line->Tokens.empty())
130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Stephen Hines651f13c2014-04-23 16:59:28 -0700131 PreBlockLine = Parser.Line.release();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000132 Parser.Line.reset(new UnwrappedLine());
133 Parser.Line->Level = PreBlockLine->Level;
134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000135 }
136
137 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000138 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000139 Parser.addUnwrappedLine();
140 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000141 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000142 Parser.Line.reset(PreBlockLine);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144 Parser.MustBreakBeforeNextToken = true;
145 Parser.CurrentLines = OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000146 }
147
148private:
149 UnwrappedLineParser &Parser;
150
151 UnwrappedLine *PreBlockLine;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000152 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000153};
154
Stephen Hines651f13c2014-04-23 16:59:28 -0700155class CompoundStatementIndenter {
156public:
157 CompoundStatementIndenter(UnwrappedLineParser *Parser,
158 const FormatStyle &Style, unsigned &LineLevel)
159 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
160 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
161 Parser->addUnwrappedLine();
162 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
163 Parser->addUnwrappedLine();
164 ++LineLevel;
165 }
166 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700167 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Stephen Hines651f13c2014-04-23 16:59:28 -0700168
169private:
170 unsigned &LineLevel;
171 unsigned OldLineLevel;
172};
173
Craig Toppere50947f2013-07-01 04:21:54 +0000174namespace {
175
Manuel Klimek80829bd2013-05-23 09:41:43 +0000176class IndexedTokenSource : public FormatTokenSource {
177public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000178 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000179 : Tokens(Tokens), Position(-1) {}
180
Stephen Hines651f13c2014-04-23 16:59:28 -0700181 FormatToken *getNextToken() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000182 ++Position;
183 return Tokens[Position];
184 }
185
Stephen Hines651f13c2014-04-23 16:59:28 -0700186 unsigned getPosition() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000187 assert(Position >= 0);
188 return Position;
189 }
190
Stephen Hines651f13c2014-04-23 16:59:28 -0700191 FormatToken *setPosition(unsigned P) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000192 Position = P;
193 return Tokens[Position];
194 }
195
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000196 void reset() { Position = -1; }
197
Manuel Klimek80829bd2013-05-23 09:41:43 +0000198private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000199 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000200 int Position;
201};
202
Craig Toppere50947f2013-07-01 04:21:54 +0000203} // end anonymous namespace
204
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000205UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000206 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000207 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700209 CurrentLines(&Lines), StructuralError(false), Style(Style),
210 Tokens(nullptr), Callback(Callback), AllTokens(Tokens),
211 PPBranchLevel(-1) {}
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000212
213void UnwrappedLineParser::reset() {
214 PPBranchLevel = -1;
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700217 FormatTok = nullptr;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
223 PPStack.clear();
224}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000225
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000226bool UnwrappedLineParser::parse() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000227 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000228 do {
229 DEBUG(llvm::dbgs() << "----\n");
230 reset();
231 Tokens = &TokenSource;
232 TokenSource.reset();
Daniel Jasper516fb312013-03-01 18:11:39 +0000233
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000234 readToken();
235 parseFile();
236 // Create line with eof token.
237 pushToken(FormatTok);
238 addUnwrappedLine();
239
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
241 E = Lines.end();
242 I != E; ++I) {
243 Callback.consumeUnwrappedLine(*I);
244 }
245 Callback.finishRun();
246 Lines.clear();
247 while (!PPLevelBranchIndex.empty() &&
Daniel Jasperac4d0182013-10-12 13:32:56 +0000248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251 }
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256 }
257 } while (!PPLevelBranchIndex.empty());
258
Manuel Klimek67d080d2013-04-12 14:13:36 +0000259 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000260}
261
Manuel Klimek67d080d2013-04-12 14:13:36 +0000262void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000266 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000267 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000268 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000269 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000270}
271
Manuel Klimek67d080d2013-04-12 14:13:36 +0000272void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000273 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000274 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000275 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000276 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000277 nextToken();
278 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000279 break;
280 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000283 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000284 addUnwrappedLine();
285 break;
286 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000287 if (HasOpeningBrace)
288 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000289 StructuralError = true;
290 nextToken();
291 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000292 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000293 case tok::kw_default:
294 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
297 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000298 SwitchLabelEncountered = true;
299 parseStructuralElement();
300 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000301 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000302 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000303 break;
304 }
305 } while (!eof());
306}
307
Manuel Klimek80829bd2013-05-23 09:41:43 +0000308void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
312 // definitions, too.
313 unsigned StoredPosition = Tokens->getPosition();
314 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000315 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000316 // Keep a stack of positions of lbrace tokens. We will
317 // update information about whether an lbrace starts a
318 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000319 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000320 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000321 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000322 // Get next none-comment token.
323 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000324 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000325 do {
326 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000327 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000328 } while (NextTok->is(tok::comment));
329
Manuel Klimek96e888b2013-05-28 11:55:06 +0000330 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000331 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000332 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000333 break;
334 case tok::r_brace:
335 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000336 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700337 bool ProbablyBracedList = false;
338 if (Style.Language == FormatStyle::LK_Proto) {
339 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
340 } else {
341 // Using OriginalColumn to distinguish between ObjC methods and
342 // binary operators is a bit hacky.
343 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
344 NextTok->OriginalColumn == 0;
345
346 // If there is a comma, semicolon or right paren after the closing
347 // brace, we assume this is a braced initializer list. Note that
348 // regardless how we mark inner braces here, we will overwrite the
349 // BlockKind later if we parse a braced list (where all blocks
350 // inside are by default braced lists), or when we explicitly detect
351 // blocks (for example while parsing lambdas).
352 //
353 // We exclude + and - as they can be ObjC visibility modifiers.
354 ProbablyBracedList =
355 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
356 tok::r_paren, tok::r_square, tok::l_brace,
357 tok::l_paren) ||
358 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
359 }
360 if (ProbablyBracedList) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000361 Tok->BlockKind = BK_BracedInit;
362 LBraceStack.back()->BlockKind = BK_BracedInit;
363 } else {
364 Tok->BlockKind = BK_Block;
365 LBraceStack.back()->BlockKind = BK_Block;
366 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000367 }
368 LBraceStack.pop_back();
369 }
370 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700371 case tok::at:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000372 case tok::semi:
373 case tok::kw_if:
374 case tok::kw_while:
375 case tok::kw_for:
376 case tok::kw_switch:
377 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000378 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000379 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000380 break;
381 default:
382 break;
383 }
384 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000385 Position += ReadTokens;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000386 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000387 // Assume other blocks for all unclosed opening braces.
388 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000389 if (LBraceStack[i]->BlockKind == BK_Unknown)
390 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000391 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000392
Manuel Klimek80829bd2013-05-23 09:41:43 +0000393 FormatTok = Tokens->setPosition(StoredPosition);
394}
395
Manuel Klimekaabfb272013-10-12 22:46:56 +0000396void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
397 bool MunchSemi) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000398 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000399 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000400 nextToken();
401
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000402 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000403
Manuel Klimek70b03f42013-01-23 09:32:48 +0000404 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
405 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000406 if (AddLevel)
407 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000408 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000409
Manuel Klimek96e888b2013-05-28 11:55:06 +0000410 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000411 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000412 StructuralError = true;
413 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000414 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000415
Daniel Jasperf9955d32013-03-20 12:37:50 +0000416 nextToken(); // Munch the closing brace.
Manuel Klimekaabfb272013-10-12 22:46:56 +0000417 if (MunchSemi && FormatTok->Tok.is(tok::semi))
418 nextToken();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000419 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000420}
421
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700422static bool IsGoogScope(const UnwrappedLine &Line) {
423 if (Line.Tokens.size() < 4)
424 return false;
425 auto I = Line.Tokens.begin();
426 if (I->Tok->TokenText != "goog")
427 return false;
428 ++I;
429 if (I->Tok->isNot(tok::period))
430 return false;
431 ++I;
432 if (I->Tok->TokenText != "scope")
433 return false;
434 ++I;
435 return I->Tok->is(tok::l_paren);
436}
437
Manuel Klimek753a5112013-09-04 13:25:30 +0000438void UnwrappedLineParser::parseChildBlock() {
439 FormatTok->BlockKind = BK_Block;
440 nextToken();
441 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700442 bool GoogScope =
443 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek753a5112013-09-04 13:25:30 +0000444 ScopedLineState LineState(*this);
445 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
446 /*MustBeDeclaration=*/false);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700447 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000448 parseLevel(/*HasOpeningBrace=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700449 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000450 }
451 nextToken();
452}
453
Daniel Jasperbac016b2012-12-03 18:12:45 +0000454void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000455 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000456 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000457 nextToken();
458
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700459 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000460 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000461 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000462 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000463
Manuel Klimek96e888b2013-05-28 11:55:06 +0000464 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000465 case tok::pp_define:
466 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000467 return;
468 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000469 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000470 break;
471 case tok::pp_ifdef:
472 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000473 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000474 break;
475 case tok::pp_else:
476 parsePPElse();
477 break;
478 case tok::pp_elif:
479 parsePPElIf();
480 break;
481 case tok::pp_endif:
482 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000483 break;
484 default:
485 parsePPUnknown();
486 break;
487 }
488}
489
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700490void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
491 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000492 PPStack.push_back(PP_Unreachable);
493 else
494 PPStack.push_back(PP_Conditional);
495}
496
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700497void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000498 ++PPBranchLevel;
499 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
500 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
501 PPLevelBranchIndex.push_back(0);
502 PPLevelBranchCount.push_back(0);
503 }
504 PPChainBranchIndex.push(0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700505 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
506 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000507}
508
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700509void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000510 if (!PPStack.empty())
511 PPStack.pop_back();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000512 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
513 if (!PPChainBranchIndex.empty())
514 ++PPChainBranchIndex.top();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700515 conditionalCompilationCondition(
516 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
517 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000518}
519
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700520void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000521 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
522 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
523 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000524 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
525 }
526 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700527 // Guard against #endif's without #if.
528 if (PPBranchLevel > 0)
529 --PPBranchLevel;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000530 if (!PPChainBranchIndex.empty())
531 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000532 if (!PPStack.empty())
533 PPStack.pop_back();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700534}
535
536void UnwrappedLineParser::parsePPIf(bool IfDef) {
537 nextToken();
538 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
539 StringRef(FormatTok->Tok.getLiteralData(),
540 FormatTok->Tok.getLength()) == "0") ||
541 FormatTok->Tok.is(tok::kw_false);
542 conditionalCompilationStart(!IfDef && IsLiteralFalse);
543 parsePPUnknown();
544}
545
546void UnwrappedLineParser::parsePPElse() {
547 conditionalCompilationAlternative();
548 parsePPUnknown();
549}
550
551void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
552
553void UnwrappedLineParser::parsePPEndIf() {
554 conditionalCompilationEnd();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000555 parsePPUnknown();
556}
557
Manuel Klimekd4397b92013-01-04 23:34:14 +0000558void UnwrappedLineParser::parsePPDefine() {
559 nextToken();
560
Manuel Klimek96e888b2013-05-28 11:55:06 +0000561 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000562 parsePPUnknown();
563 return;
564 }
565 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000566 if (FormatTok->Tok.getKind() == tok::l_paren &&
567 FormatTok->WhitespaceRange.getBegin() ==
568 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000569 parseParens();
570 }
571 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000572 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000573
574 // Errors during a preprocessor directive can only affect the layout of the
575 // preprocessor directive, and thus we ignore them. An alternative approach
576 // would be to use the same approach we use on the file level (no
577 // re-indentation if there was a structural error) within the macro
578 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000579 parseFile();
580}
581
582void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000583 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000584 nextToken();
585 } while (!eof());
586 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000587}
588
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000589// Here we blacklist certain tokens that are not usually the first token in an
590// unwrapped line. This is used in attempt to distinguish macro calls without
591// trailing semicolons from other constructs split to several lines.
592bool tokenCanStartNewLine(clang::Token Tok) {
593 // Semicolon can be a null-statement, l_square can be a start of a macro or
594 // a C++11 attribute, but this doesn't seem to be common.
595 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
596 Tok.isNot(tok::l_square) &&
597 // Tokens that can only be used as binary operators and a part of
598 // overloaded operator names.
599 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
600 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
601 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
602 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
603 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
604 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
605 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
606 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
607 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
608 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
609 Tok.isNot(tok::lesslessequal) &&
610 // Colon is used in labels, base class lists, initializer lists,
611 // range-based for loops, ternary operator, but should never be the
612 // first token in an unwrapped line.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700613 Tok.isNot(tok::colon) &&
614 // 'noexcept' is a trailing annotation.
615 Tok.isNot(tok::kw_noexcept);
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000616}
617
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000618void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000619 assert(!FormatTok->Tok.is(tok::l_brace));
620 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000621 case tok::at:
622 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000623 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000624 parseBracedList();
625 break;
626 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000627 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000628 case tok::objc_public:
629 case tok::objc_protected:
630 case tok::objc_package:
631 case tok::objc_private:
632 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000633 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000634 case tok::objc_implementation:
635 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000636 case tok::objc_protocol:
637 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000638 case tok::objc_end:
639 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000640 case tok::objc_optional:
641 case tok::objc_required:
642 nextToken();
643 addUnwrappedLine();
644 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000645 default:
646 break;
647 }
648 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000649 case tok::kw_namespace:
650 parseNamespace();
651 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000652 case tok::kw_inline:
653 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000654 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000655 parseNamespace();
656 return;
657 }
658 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000659 case tok::kw_public:
660 case tok::kw_protected:
661 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000662 parseAccessSpecifier();
663 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000664 case tok::kw_if:
665 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000666 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000667 case tok::kw_for:
668 case tok::kw_while:
669 parseForOrWhileLoop();
670 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000671 case tok::kw_do:
672 parseDoWhile();
673 return;
674 case tok::kw_switch:
675 parseSwitch();
676 return;
677 case tok::kw_default:
678 nextToken();
679 parseLabel();
680 return;
681 case tok::kw_case:
682 parseCaseLabel();
683 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700684 case tok::kw_try:
685 parseTryCatch();
686 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000687 case tok::kw_extern:
688 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000689 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000690 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000691 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000692 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000693 addUnwrappedLine();
694 return;
695 }
696 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700697 break;
698 case tok::identifier:
699 if (FormatTok->IsForEachMacro) {
700 parseForOrWhileLoop();
701 return;
702 }
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000703 // In all other cases, parse the declaration.
704 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000705 default:
706 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000707 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000708 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000709 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000710 case tok::at:
711 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000712 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000713 parseBracedList();
714 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000715 case tok::kw_enum:
716 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000717 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700718 case tok::kw_typedef:
719 nextToken();
720 // FIXME: Use the IdentifierTable instead.
721 if (FormatTok->TokenText == "NS_ENUM")
722 parseEnum();
723 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000724 case tok::kw_struct:
725 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000726 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000727 parseRecord();
728 // A record declaration or definition is always the start of a structural
729 // element.
730 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000731 case tok::semi:
732 nextToken();
733 addUnwrappedLine();
734 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000735 case tok::r_brace:
736 addUnwrappedLine();
737 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000738 case tok::l_paren:
739 parseParens();
740 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000741 case tok::caret:
742 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700743 if (FormatTok->Tok.isAnyIdentifier() ||
744 FormatTok->isSimpleTypeSpecifier())
745 nextToken();
746 if (FormatTok->is(tok::l_paren))
747 parseParens();
748 if (FormatTok->is(tok::l_brace))
Manuel Klimek753a5112013-09-04 13:25:30 +0000749 parseChildBlock();
Manuel Klimek753a5112013-09-04 13:25:30 +0000750 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000751 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000752 if (!tryToParseBracedList()) {
753 // A block outside of parentheses must be the last part of a
754 // structural element.
755 // FIXME: Figure out cases where this is not true, and add projections
756 // for them (the one we know is missing are lambdas).
Stephen Hines651f13c2014-04-23 16:59:28 -0700757 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000758 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700759 FormatTok->Type = TT_FunctionLBrace;
Nico Weber27268772013-06-26 00:30:14 +0000760 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000761 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000762 return;
763 }
764 // Otherwise this was a braced init list, and the structural
765 // element continues.
766 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700767 case tok::kw_try:
768 // We arrive here when parsing function-try blocks.
769 parseTryCatch();
770 return;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000771 case tok::identifier: {
772 StringRef Text = FormatTok->TokenText;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700773 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") {
774 tryToParseJSFunction();
775 break;
776 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000777 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000778 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000779 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000780 parseLabel();
781 return;
782 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000783 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000784 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000785 parseParens();
Stephen Hines651f13c2014-04-23 16:59:28 -0700786 if (FormatTok->NewlinesBefore > 0 &&
787 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000788 addUnwrappedLine();
789 return;
790 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000791 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
792 Text == Text.upper()) {
793 // Recognize free-standing macros like Q_OBJECT.
794 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000795 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000796 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000797 }
798 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000799 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000800 case tok::equal:
801 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000802 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000803 parseBracedList();
804 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000805 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000806 case tok::l_square:
Stephen Hines651f13c2014-04-23 16:59:28 -0700807 parseSquare();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000808 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000809 default:
810 nextToken();
811 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000812 }
813 } while (!eof());
814}
815
Stephen Hines651f13c2014-04-23 16:59:28 -0700816bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000817 // FIXME: This is a dirty way to access the previous token. Find a better
818 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000819 if (!Line->Tokens.empty() &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700820 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
821 Line->Tokens.back().Tok->closesScope() ||
822 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000823 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700824 return false;
Daniel Jasper2d657052013-09-05 11:49:39 +0000825 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000826 assert(FormatTok->is(tok::l_square));
827 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000828 if (!tryToParseLambdaIntroducer())
Stephen Hines651f13c2014-04-23 16:59:28 -0700829 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000830
831 while (FormatTok->isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700832 if (FormatTok->isSimpleTypeSpecifier()) {
833 nextToken();
834 continue;
835 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000836 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000837 case tok::l_brace:
838 break;
839 case tok::l_paren:
840 parseParens();
841 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700842 case tok::less:
843 case tok::greater:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000844 case tok::identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700845 case tok::coloncolon:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000846 case tok::kw_mutable:
847 nextToken();
848 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700849 case tok::arrow:
850 FormatTok->Type = TT_TrailingReturnArrow;
851 nextToken();
852 break;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000853 default:
Stephen Hines651f13c2014-04-23 16:59:28 -0700854 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000855 }
856 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000857 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000858 parseChildBlock();
Stephen Hines651f13c2014-04-23 16:59:28 -0700859 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000860}
861
862bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
863 nextToken();
864 if (FormatTok->is(tok::equal)) {
865 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000866 if (FormatTok->is(tok::r_square)) {
867 nextToken();
868 return true;
869 }
870 if (FormatTok->isNot(tok::comma))
871 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000872 nextToken();
873 } else if (FormatTok->is(tok::amp)) {
874 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000875 if (FormatTok->is(tok::r_square)) {
876 nextToken();
877 return true;
878 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000879 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
880 return false;
881 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000882 if (FormatTok->is(tok::comma))
883 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000884 } else if (FormatTok->is(tok::r_square)) {
885 nextToken();
886 return true;
887 }
888 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000889 if (FormatTok->is(tok::amp))
890 nextToken();
891 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
892 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000893 nextToken();
894 if (FormatTok->is(tok::comma)) {
895 nextToken();
896 } else if (FormatTok->is(tok::r_square)) {
897 nextToken();
898 return true;
899 } else {
900 return false;
901 }
902 } while (!eof());
903 return false;
904}
905
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700906void UnwrappedLineParser::tryToParseJSFunction() {
907 nextToken();
908 if (FormatTok->isNot(tok::l_paren))
909 return;
910 nextToken();
911 while (FormatTok->isNot(tok::l_brace)) {
912 // Err on the side of caution in order to avoid consuming the full file in
913 // case of incomplete code.
914 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
915 tok::comment))
916 return;
917 nextToken();
918 }
919 parseChildBlock();
920}
921
Manuel Klimek80829bd2013-05-23 09:41:43 +0000922bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000923 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000924 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000925 assert(FormatTok->BlockKind != BK_Unknown);
926 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000927 return false;
928 parseBracedList();
929 return true;
930}
931
Daniel Jasper57981202013-09-13 09:20:45 +0000932bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
933 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000934 nextToken();
935
Manuel Klimek423dd932013-04-10 09:52:05 +0000936 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
937 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000938 do {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700939 if (Style.Language == FormatStyle::LK_JavaScript &&
940 FormatTok->TokenText == "function") {
941 tryToParseJSFunction();
942 continue;
943 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000944 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +0000945 case tok::caret:
946 nextToken();
947 if (FormatTok->is(tok::l_brace)) {
948 parseChildBlock();
949 }
950 break;
951 case tok::l_square:
952 tryToParseLambda();
953 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000954 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +0000955 // Assume there are no blocks inside a braced init list apart
956 // from the ones we explicitly parse out (like lambdas).
957 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000958 parseBracedList();
959 break;
960 case tok::r_brace:
961 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +0000962 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +0000963 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +0000964 HasError = true;
965 if (!ContinueOnSemicolons)
966 return !HasError;
967 nextToken();
968 break;
Manuel Klimek423dd932013-04-10 09:52:05 +0000969 case tok::comma:
970 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000971 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000972 default:
973 nextToken();
974 break;
975 }
976 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +0000977 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000978}
979
Daniel Jasperbac016b2012-12-03 18:12:45 +0000980void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000981 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000982 nextToken();
983 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000984 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000985 case tok::l_paren:
986 parseParens();
987 break;
988 case tok::r_paren:
989 nextToken();
990 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000991 case tok::r_brace:
992 // A "}" inside parenthesis is an error if there wasn't a matching "{".
993 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000994 case tok::l_square:
995 tryToParseLambda();
996 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000997 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000998 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +0000999 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +00001000 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001001 break;
Nico Weber2afbe522013-02-10 04:38:23 +00001002 }
Nico Weberd74fcdb2013-02-10 20:35:35 +00001003 case tok::at:
1004 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001005 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +00001006 parseBracedList();
1007 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001008 default:
1009 nextToken();
1010 break;
1011 }
1012 } while (!eof());
1013}
1014
Stephen Hines651f13c2014-04-23 16:59:28 -07001015void UnwrappedLineParser::parseSquare() {
1016 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1017 if (tryToParseLambda())
1018 return;
1019 do {
1020 switch (FormatTok->Tok.getKind()) {
1021 case tok::l_paren:
1022 parseParens();
1023 break;
1024 case tok::r_square:
1025 nextToken();
1026 return;
1027 case tok::r_brace:
1028 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1029 return;
1030 case tok::l_square:
1031 parseSquare();
1032 break;
1033 case tok::l_brace: {
1034 if (!tryToParseBracedList()) {
1035 parseChildBlock();
1036 }
1037 break;
1038 }
1039 case tok::at:
1040 nextToken();
1041 if (FormatTok->Tok.is(tok::l_brace))
1042 parseBracedList();
1043 break;
1044 default:
1045 nextToken();
1046 break;
1047 }
1048 } while (!eof());
1049}
1050
Daniel Jasperbac016b2012-12-03 18:12:45 +00001051void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001052 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001053 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001054 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +00001055 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001056 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001057 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001058 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001059 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001060 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1061 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001062 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001063 } else {
Manuel Klimeke4907052013-08-02 21:31:59 +00001064 NeedsUnwrappedLine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001065 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001066 } else {
1067 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001068 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001069 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001070 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001071 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001072 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001073 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001074 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001075 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001076 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001077 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001078 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001079 parseIfThenElse();
1080 } else {
1081 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001082 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001083 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001084 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001085 }
1086 } else if (NeedsUnwrappedLine) {
1087 addUnwrappedLine();
1088 }
1089}
1090
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001091void UnwrappedLineParser::parseTryCatch() {
1092 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1093 nextToken();
1094 bool NeedsUnwrappedLine = false;
1095 if (FormatTok->is(tok::colon)) {
1096 // We are in a function try block, what comes is an initializer list.
1097 nextToken();
1098 while (FormatTok->is(tok::identifier)) {
1099 nextToken();
1100 if (FormatTok->is(tok::l_paren))
1101 parseParens();
1102 else
1103 StructuralError = true;
1104 if (FormatTok->is(tok::comma))
1105 nextToken();
1106 }
1107 }
1108 if (FormatTok->is(tok::l_brace)) {
1109 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1110 parseBlock(/*MustBeDeclaration=*/false);
1111 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1112 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1113 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1114 addUnwrappedLine();
1115 } else {
1116 NeedsUnwrappedLine = true;
1117 }
1118 } else if (!FormatTok->is(tok::kw_catch)) {
1119 // The C++ standard requires a compound-statement after a try.
1120 // If there's none, we try to assume there's a structuralElement
1121 // and try to continue.
1122 StructuralError = true;
1123 addUnwrappedLine();
1124 ++Line->Level;
1125 parseStructuralElement();
1126 --Line->Level;
1127 }
1128 while (FormatTok->is(tok::kw_catch) ||
1129 (Style.Language == FormatStyle::LK_JavaScript &&
1130 FormatTok->TokenText == "finally")) {
1131 nextToken();
1132 while (FormatTok->isNot(tok::l_brace)) {
1133 if (FormatTok->is(tok::l_paren)) {
1134 parseParens();
1135 continue;
1136 }
1137 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1138 return;
1139 nextToken();
1140 }
1141 NeedsUnwrappedLine = false;
1142 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1143 parseBlock(/*MustBeDeclaration=*/false);
1144 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1145 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1146 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1147 addUnwrappedLine();
1148 } else {
1149 NeedsUnwrappedLine = true;
1150 }
1151 }
1152 if (NeedsUnwrappedLine) {
1153 addUnwrappedLine();
1154 }
1155}
1156
Alexander Kornienko15757312012-12-06 18:03:27 +00001157void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001158 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +00001159 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001160 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +00001161 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001162 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001163 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001164 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1165 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimek44135b82013-05-13 12:51:40 +00001166 addUnwrappedLine();
1167
Daniel Jaspereff18b92013-07-31 23:16:02 +00001168 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1169 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1170 DeclarationScopeStack.size() > 1);
1171 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001172 // Munch the semicolon after a namespace. This is more common than one would
1173 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001174 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001175 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +00001176 addUnwrappedLine();
1177 }
1178 // FIXME: Add error handling.
1179}
1180
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001181void UnwrappedLineParser::parseForOrWhileLoop() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001182 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1183 FormatTok->IsForEachMacro) &&
1184 "'for', 'while' or foreach macro expected");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001185 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001186 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001187 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001188 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001189 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001190 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001191 addUnwrappedLine();
1192 } else {
1193 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001194 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001195 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001196 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001197 }
1198}
1199
Daniel Jasperbac016b2012-12-03 18:12:45 +00001200void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001201 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001202 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001203 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001204 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001205 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001206 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1207 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001208 } else {
1209 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001210 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001211 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001212 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001213 }
1214
Alexander Kornienko393b0082012-12-04 15:40:36 +00001215 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001216 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001217 addUnwrappedLine();
1218 return;
1219 }
1220
Daniel Jasperbac016b2012-12-03 18:12:45 +00001221 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001222 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001223}
1224
1225void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001226 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001227 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001228 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001229 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001230 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001231 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001232 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001233 if (FormatTok->Tok.is(tok::kw_break)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001234 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1235 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1236 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001237 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001238 }
Manuel Klimeke4907052013-08-02 21:31:59 +00001239 parseStructuralElement();
1240 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001241 addUnwrappedLine();
1242 } else {
1243 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001244 }
Manuel Klimek526ed112013-01-09 15:25:02 +00001245 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001246}
1247
1248void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001249 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001250 // FIXME: fix handling of complex expressions here.
1251 do {
1252 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001253 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001254 parseLabel();
1255}
1256
1257void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001258 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001259 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001260 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001261 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001262 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001263 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jaspereff18b92013-07-31 23:16:02 +00001264 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001265 addUnwrappedLine();
1266 } else {
1267 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001268 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001269 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001270 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001271 }
1272}
1273
1274void UnwrappedLineParser::parseAccessSpecifier() {
1275 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001276 // Understand Qt's slots.
1277 if (FormatTok->is(tok::identifier) &&
1278 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1279 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001280 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001281 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001282 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001283 addUnwrappedLine();
1284}
1285
1286void UnwrappedLineParser::parseEnum() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001287 if (FormatTok->Tok.is(tok::kw_enum)) {
1288 // Won't be 'enum' for NS_ENUMs.
1289 nextToken();
1290 }
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001291 // Eat up enum class ...
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001292 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1293 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001294 while (FormatTok->Tok.getIdentifierInfo() ||
1295 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001296 nextToken();
1297 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001298 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001299 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001300 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001301 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001302 nextToken();
1303 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001304 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper57981202013-09-13 09:20:45 +00001305 FormatTok->BlockKind = BK_Block;
1306 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1307 if (HasError) {
1308 if (FormatTok->is(tok::semi))
1309 nextToken();
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001310 addUnwrappedLine();
Daniel Jasper57981202013-09-13 09:20:45 +00001311 }
Manuel Klimek308232c2013-01-21 19:17:52 +00001312 }
1313 // We fall through to parsing a structural element afterwards, so that in
1314 // enum A {} n, m;
1315 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001316}
1317
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001318void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +00001319 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001320 if (FormatTok->Tok.is(tok::identifier) ||
1321 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek3d712892013-10-07 09:15:41 +00001322 FormatTok->Tok.is(tok::kw___declspec) ||
1323 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001324 nextToken();
1325 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001326 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001327 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001328 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001329 // The actual identifier can be a nested name specifier, and in macros
1330 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001331 while (FormatTok->Tok.is(tok::identifier) ||
1332 FormatTok->Tok.is(tok::coloncolon) ||
1333 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001334 nextToken();
1335
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001336 // Note that parsing away template declarations here leads to incorrectly
1337 // accepting function declarations as record declarations.
1338 // In general, we cannot solve this problem. Consider:
1339 // class A<int> B() {}
1340 // which can be a function definition or a class definition when B() is a
1341 // macro. If we find enough real-world cases where this is a problem, we
1342 // can parse for the 'template' keyword in the beginning of the statement,
1343 // and thus rule out the record production in case there is no template
1344 // (this would still leave us with an ambiguity between template function
1345 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001346 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1347 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1348 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001349 return;
1350 nextToken();
1351 }
1352 }
1353 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001354 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001355 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001356 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1357 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimek44135b82013-05-13 12:51:40 +00001358 addUnwrappedLine();
1359
Stephen Hines651f13c2014-04-23 16:59:28 -07001360 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekaabfb272013-10-12 22:46:56 +00001361 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001362 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001363 // We fall through to parsing a structural element afterwards, so
1364 // class A {} n, m;
1365 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001366}
1367
Nico Weber1abe6ea2013-01-09 21:15:03 +00001368void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001369 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001370 do
1371 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001372 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001373 nextToken(); // Skip '>'.
1374}
1375
1376void UnwrappedLineParser::parseObjCUntilAtEnd() {
1377 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001378 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001379 nextToken();
1380 addUnwrappedLine();
1381 break;
1382 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001383 if (FormatTok->is(tok::l_brace)) {
1384 parseBlock(/*MustBeDeclaration=*/false);
1385 // In ObjC interfaces, nothing should be following the "}".
1386 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001387 } else if (FormatTok->is(tok::r_brace)) {
1388 // Ignore stray "}". parseStructuralElement doesn't consume them.
1389 nextToken();
1390 addUnwrappedLine();
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001391 } else {
1392 parseStructuralElement();
1393 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001394 } while (!eof());
1395}
1396
Nico Weber50767d82013-01-09 23:25:37 +00001397void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001398 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001399 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001400
1401 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001402 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001403 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001404 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001405 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001406 // Skip category, if present.
1407 parseParens();
1408
Manuel Klimek96e888b2013-05-28 11:55:06 +00001409 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001410 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001411
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001412 if (FormatTok->Tok.is(tok::l_brace)) {
1413 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1414 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1415 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001416 parseBlock(/*MustBeDeclaration=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001417 }
Nico Weber27d13672013-01-09 20:25:35 +00001418
1419 // With instance variables, this puts '}' on its own line. Without instance
1420 // variables, this ends the @interface line.
1421 addUnwrappedLine();
1422
Nico Weber1abe6ea2013-01-09 21:15:03 +00001423 parseObjCUntilAtEnd();
1424}
Nico Weber27d13672013-01-09 20:25:35 +00001425
Nico Weber1abe6ea2013-01-09 21:15:03 +00001426void UnwrappedLineParser::parseObjCProtocol() {
1427 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001428 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001429
Manuel Klimek96e888b2013-05-28 11:55:06 +00001430 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001431 parseObjCProtocolList();
1432
1433 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001434 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001435 nextToken();
1436 return addUnwrappedLine();
1437 }
1438
1439 addUnwrappedLine();
1440 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001441}
1442
Daniel Jasperd98927d2013-09-05 16:05:56 +00001443LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1444 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001445 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1446 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1447 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1448 E = Line.Tokens.end();
1449 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001450 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001451 }
1452 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1453 E = Line.Tokens.end();
1454 I != E; ++I) {
1455 const UnwrappedLineNode &Node = *I;
1456 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1457 I = Node.Children.begin(),
1458 E = Node.Children.end();
1459 I != E; ++I) {
1460 printDebugInfo(*I, "\nChild: ");
1461 }
1462 }
1463 llvm::dbgs() << "\n";
1464}
1465
Daniel Jasperbac016b2012-12-03 18:12:45 +00001466void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001467 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001468 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001469 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001470 if (CurrentLines == &Lines)
1471 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001472 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001473 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001474 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001475 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001476 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001477 I = PreprocessorDirectives.begin(),
1478 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001479 I != E; ++I) {
1480 CurrentLines->push_back(*I);
1481 }
1482 PreprocessorDirectives.clear();
1483 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001484}
1485
Manuel Klimek96e888b2013-05-28 11:55:06 +00001486bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001487
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001488bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1489 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1490 FormatTok.NewlinesBefore > 0;
1491}
1492
Manuel Klimek86721d22013-01-22 16:31:55 +00001493void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1494 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001495 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001496 I = CommentsBeforeNextToken.begin(),
1497 E = CommentsBeforeNextToken.end();
1498 I != E; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001499 if (isOnNewLine(**I) && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001500 addUnwrappedLine();
1501 }
1502 pushToken(*I);
1503 }
1504 if (NewlineBeforeNext && JustComments) {
1505 addUnwrappedLine();
1506 }
1507 CommentsBeforeNextToken.clear();
1508}
1509
Daniel Jasperbac016b2012-12-03 18:12:45 +00001510void UnwrappedLineParser::nextToken() {
1511 if (eof())
1512 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001513 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001514 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001515 readToken();
1516}
1517
1518void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001519 bool CommentsInCurrentLine = true;
1520 do {
1521 FormatTok = Tokens->getNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001522 assert(FormatTok);
Manuel Klimek96e888b2013-05-28 11:55:06 +00001523 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1524 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001525 // If there is an unfinished unwrapped line, we flush the preprocessor
1526 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001527 bool SwitchToPreprocessorLines =
1528 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001529 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001530 // Comments stored before the preprocessor directive need to be output
1531 // before the preprocessor directive, at the same level as the
1532 // preprocessor directive, as we consider them to apply to the directive.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001533 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001534 parsePPDirective();
1535 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001536 while (FormatTok->Type == TT_ConflictStart ||
1537 FormatTok->Type == TT_ConflictEnd ||
1538 FormatTok->Type == TT_ConflictAlternative) {
1539 if (FormatTok->Type == TT_ConflictStart) {
1540 conditionalCompilationStart(/*Unreachable=*/false);
1541 } else if (FormatTok->Type == TT_ConflictAlternative) {
1542 conditionalCompilationAlternative();
1543 } else if (FormatTok->Type == TT_ConflictEnd) {
1544 conditionalCompilationEnd();
1545 }
1546 FormatTok = Tokens->getNextToken();
1547 FormatTok->MustBreakBefore = true;
1548 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001549
1550 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1551 !Line->InPPDirective) {
1552 continue;
1553 }
1554
Manuel Klimek96e888b2013-05-28 11:55:06 +00001555 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001556 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001557 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001558 CommentsInCurrentLine = false;
1559 }
1560 if (CommentsInCurrentLine) {
1561 pushToken(FormatTok);
1562 } else {
1563 CommentsBeforeNextToken.push_back(FormatTok);
1564 }
1565 } while (!eof());
1566}
1567
Manuel Klimek96e888b2013-05-28 11:55:06 +00001568void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001569 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001570 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001571 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001572 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001573 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001574}
1575
Daniel Jaspercd162382013-01-07 13:26:07 +00001576} // end namespace format
1577} // end namespace clang