blob: c80c297de22fa044db601705ee59d2741a229ae1 [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 "llvm/Support/Debug.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000020
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),
Manuel Klimek96e888b2013-05-28 11:55:06 +000063 PreviousStructuralError(StructuralError), Token(NULL) {
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
Manuel Klimek96e888b2013-05-28 11:55:06 +000077 virtual FormatToken *getNextToken() {
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
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000087 virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); }
Manuel Klimek80829bd2013-05-23 09:41:43 +000088
Manuel Klimek96e888b2013-05-28 11:55:06 +000089 virtual FormatToken *setPosition(unsigned Position) {
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)
125 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000128 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000129 Parser.Line.reset(new UnwrappedLine());
130 Parser.Line->Level = PreBlockLine->Level;
131 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000132 }
133
134 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000135 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000136 Parser.addUnwrappedLine();
137 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000138 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000139 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000140 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000141 if (SwitchToPreprocessorLines)
142 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000143 }
144
145private:
146 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000147 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000148
149 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000150};
151
Craig Toppere50947f2013-07-01 04:21:54 +0000152namespace {
153
Manuel Klimek80829bd2013-05-23 09:41:43 +0000154class IndexedTokenSource : public FormatTokenSource {
155public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000156 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000157 : Tokens(Tokens), Position(-1) {}
158
Manuel Klimek96e888b2013-05-28 11:55:06 +0000159 virtual FormatToken *getNextToken() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000160 ++Position;
161 return Tokens[Position];
162 }
163
164 virtual unsigned getPosition() {
165 assert(Position >= 0);
166 return Position;
167 }
168
Manuel Klimek96e888b2013-05-28 11:55:06 +0000169 virtual FormatToken *setPosition(unsigned P) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000170 Position = P;
171 return Tokens[Position];
172 }
173
174private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000175 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000176 int Position;
177};
178
Craig Toppere50947f2013-07-01 04:21:54 +0000179} // end anonymous namespace
180
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000181UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000182 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000183 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000184 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000185 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000186 Callback(Callback), AllTokens(Tokens) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000187
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000188bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000189 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek80829bd2013-05-23 09:41:43 +0000190 IndexedTokenSource TokenSource(AllTokens);
191 Tokens = &TokenSource;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000192 readToken();
Manuel Klimek67d080d2013-04-12 14:13:36 +0000193 parseFile();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000194 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000195 I != E; ++I) {
196 Callback.consumeUnwrappedLine(*I);
197 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000198
199 // Create line with eof token.
200 pushToken(FormatTok);
201 Callback.consumeUnwrappedLine(*Line);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000202 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000203}
204
Manuel Klimek67d080d2013-04-12 14:13:36 +0000205void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000206 ScopedDeclarationState DeclarationState(
207 *Line, DeclarationScopeStack,
208 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000209 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000210 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000211 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000212 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000213}
214
Manuel Klimek67d080d2013-04-12 14:13:36 +0000215void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000216 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000218 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000219 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000220 nextToken();
221 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000222 break;
223 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000224 // FIXME: Add parameter whether this can happen - if this happens, we must
225 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000226 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000227 addUnwrappedLine();
228 break;
229 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000230 if (HasOpeningBrace)
231 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000232 StructuralError = true;
233 nextToken();
234 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000235 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000236 case tok::kw_default:
237 case tok::kw_case:
238 if (!SwitchLabelEncountered)
239 Line->Level += Style.IndentCaseLabels;
240 SwitchLabelEncountered = true;
241 parseStructuralElement();
242 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000243 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000244 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000245 break;
246 }
247 } while (!eof());
248}
249
Manuel Klimek80829bd2013-05-23 09:41:43 +0000250void UnwrappedLineParser::calculateBraceTypes() {
251 // We'll parse forward through the tokens until we hit
252 // a closing brace or eof - note that getNextToken() will
253 // parse macros, so this will magically work inside macro
254 // definitions, too.
255 unsigned StoredPosition = Tokens->getPosition();
256 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000257 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000258 // Keep a stack of positions of lbrace tokens. We will
259 // update information about whether an lbrace starts a
260 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000261 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000262 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000263 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000264 // Get next none-comment token.
265 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000266 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000267 do {
268 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000269 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000270 } while (NextTok->is(tok::comment));
271
Manuel Klimek96e888b2013-05-28 11:55:06 +0000272 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000273 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000274 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000275 break;
276 case tok::r_brace:
277 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000278 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000279 // If there is a comma, semicolon or right paren after the closing
280 // brace, we assume this is a braced initializer list.
281
282 // FIXME: Note that this currently works only because we do not
283 // use the brace information while inside a braced init list.
284 // Thus, if the parent is a braced init list, we consider all
285 // brace blocks inside it braced init list. That works good enough
286 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jaspereb483662013-05-31 10:09:55 +0000287 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000288 tok::l_brace, tok::colon)) {
289 Tok->BlockKind = BK_BracedInit;
290 LBraceStack.back()->BlockKind = BK_BracedInit;
291 } else {
292 Tok->BlockKind = BK_Block;
293 LBraceStack.back()->BlockKind = BK_Block;
294 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000295 }
296 LBraceStack.pop_back();
297 }
298 break;
299 case tok::semi:
300 case tok::kw_if:
301 case tok::kw_while:
302 case tok::kw_for:
303 case tok::kw_switch:
304 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000305 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000306 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000307 break;
308 default:
309 break;
310 }
311 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000312 Position += ReadTokens;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000313 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000314 // Assume other blocks for all unclosed opening braces.
315 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000316 if (LBraceStack[i]->BlockKind == BK_Unknown)
317 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000318 }
319 FormatTok = Tokens->setPosition(StoredPosition);
320}
321
Daniel Jaspereff18b92013-07-31 23:16:02 +0000322void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000323 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000324 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000325 nextToken();
326
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000327 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000328
Manuel Klimek70b03f42013-01-23 09:32:48 +0000329 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
330 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000331 if (AddLevel)
332 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000333 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000334
Manuel Klimek96e888b2013-05-28 11:55:06 +0000335 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000336 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000337 StructuralError = true;
338 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000339 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000340
Daniel Jasperf9955d32013-03-20 12:37:50 +0000341 nextToken(); // Munch the closing brace.
Daniel Jaspere865cc52013-07-25 11:31:57 +0000342 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000343}
344
345void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000346 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000347 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000348 nextToken();
349
Manuel Klimek96e888b2013-05-28 11:55:06 +0000350 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000351 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000352 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000353 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000354
Manuel Klimek96e888b2013-05-28 11:55:06 +0000355 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000356 case tok::pp_define:
357 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000358 return;
359 case tok::pp_if:
360 parsePPIf();
361 break;
362 case tok::pp_ifdef:
363 case tok::pp_ifndef:
364 parsePPIfdef();
365 break;
366 case tok::pp_else:
367 parsePPElse();
368 break;
369 case tok::pp_elif:
370 parsePPElIf();
371 break;
372 case tok::pp_endif:
373 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000374 break;
375 default:
376 parsePPUnknown();
377 break;
378 }
379}
380
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000381void UnwrappedLineParser::pushPPConditional() {
382 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
383 PPStack.push_back(PP_Unreachable);
384 else
385 PPStack.push_back(PP_Conditional);
386}
387
388void UnwrappedLineParser::parsePPIf() {
389 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000390 if ((FormatTok->Tok.isLiteral() &&
391 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000392 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000393 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000394 PPStack.push_back(PP_Unreachable);
395 } else {
396 pushPPConditional();
397 }
398 parsePPUnknown();
399}
400
401void UnwrappedLineParser::parsePPIfdef() {
402 pushPPConditional();
403 parsePPUnknown();
404}
405
406void UnwrappedLineParser::parsePPElse() {
407 if (!PPStack.empty())
408 PPStack.pop_back();
409 pushPPConditional();
410 parsePPUnknown();
411}
412
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000413void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000414
415void UnwrappedLineParser::parsePPEndIf() {
416 if (!PPStack.empty())
417 PPStack.pop_back();
418 parsePPUnknown();
419}
420
Manuel Klimekd4397b92013-01-04 23:34:14 +0000421void UnwrappedLineParser::parsePPDefine() {
422 nextToken();
423
Manuel Klimek96e888b2013-05-28 11:55:06 +0000424 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000425 parsePPUnknown();
426 return;
427 }
428 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000429 if (FormatTok->Tok.getKind() == tok::l_paren &&
430 FormatTok->WhitespaceRange.getBegin() ==
431 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000432 parseParens();
433 }
434 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000435 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000436
437 // Errors during a preprocessor directive can only affect the layout of the
438 // preprocessor directive, and thus we ignore them. An alternative approach
439 // would be to use the same approach we use on the file level (no
440 // re-indentation if there was a structural error) within the macro
441 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000442 parseFile();
443}
444
445void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000446 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000447 nextToken();
448 } while (!eof());
449 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000450}
451
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000452// Here we blacklist certain tokens that are not usually the first token in an
453// unwrapped line. This is used in attempt to distinguish macro calls without
454// trailing semicolons from other constructs split to several lines.
455bool tokenCanStartNewLine(clang::Token Tok) {
456 // Semicolon can be a null-statement, l_square can be a start of a macro or
457 // a C++11 attribute, but this doesn't seem to be common.
458 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
459 Tok.isNot(tok::l_square) &&
460 // Tokens that can only be used as binary operators and a part of
461 // overloaded operator names.
462 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
463 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
464 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
465 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
466 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
467 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
468 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
469 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
470 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
471 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
472 Tok.isNot(tok::lesslessequal) &&
473 // Colon is used in labels, base class lists, initializer lists,
474 // range-based for loops, ternary operator, but should never be the
475 // first token in an unwrapped line.
476 Tok.isNot(tok::colon);
477}
478
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000479void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000480 assert(!FormatTok->Tok.is(tok::l_brace));
481 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000482 case tok::at:
483 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000484 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000485 parseBracedList();
486 break;
487 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000488 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000489 case tok::objc_public:
490 case tok::objc_protected:
491 case tok::objc_package:
492 case tok::objc_private:
493 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000494 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000495 case tok::objc_implementation:
496 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000497 case tok::objc_protocol:
498 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000499 case tok::objc_end:
500 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000501 case tok::objc_optional:
502 case tok::objc_required:
503 nextToken();
504 addUnwrappedLine();
505 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000506 default:
507 break;
508 }
509 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000510 case tok::kw_namespace:
511 parseNamespace();
512 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000513 case tok::kw_inline:
514 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000515 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000516 parseNamespace();
517 return;
518 }
519 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000520 case tok::kw_public:
521 case tok::kw_protected:
522 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000523 parseAccessSpecifier();
524 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000525 case tok::kw_if:
526 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000527 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000528 case tok::kw_for:
529 case tok::kw_while:
530 parseForOrWhileLoop();
531 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000532 case tok::kw_do:
533 parseDoWhile();
534 return;
535 case tok::kw_switch:
536 parseSwitch();
537 return;
538 case tok::kw_default:
539 nextToken();
540 parseLabel();
541 return;
542 case tok::kw_case:
543 parseCaseLabel();
544 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000545 case tok::kw_return:
546 parseReturn();
547 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000548 case tok::kw_extern:
549 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000550 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000551 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000552 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000553 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000554 addUnwrappedLine();
555 return;
556 }
557 }
558 // In all other cases, parse the declaration.
559 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000560 default:
561 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000562 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000563 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000564 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000565 case tok::at:
566 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000567 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000568 parseBracedList();
569 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000570 case tok::kw_enum:
571 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000572 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000573 case tok::kw_struct:
574 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000575 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000576 parseRecord();
577 // A record declaration or definition is always the start of a structural
578 // element.
579 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000580 case tok::semi:
581 nextToken();
582 addUnwrappedLine();
583 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000584 case tok::r_brace:
585 addUnwrappedLine();
586 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000587 case tok::l_paren:
588 parseParens();
589 break;
590 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000591 if (!tryToParseBracedList()) {
592 // A block outside of parentheses must be the last part of a
593 // structural element.
594 // FIXME: Figure out cases where this is not true, and add projections
595 // for them (the one we know is missing are lambdas).
596 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Manuel Klimeke4907052013-08-02 21:31:59 +0000597 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
598 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000599 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000600 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000601 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000602 return;
603 }
604 // Otherwise this was a braced init list, and the structural
605 // element continues.
606 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000607 case tok::identifier: {
608 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000609 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000610 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000611 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000612 parseLabel();
613 return;
614 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000615 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000616 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000617 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000618 if (FormatTok->HasUnescapedNewline &&
619 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000620 addUnwrappedLine();
621 return;
622 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000623 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
624 Text == Text.upper()) {
625 // Recognize free-standing macros like Q_OBJECT.
626 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000627 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000628 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000629 }
630 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000631 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000632 case tok::equal:
633 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000634 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000635 parseBracedList();
636 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000637 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000638 default:
639 nextToken();
640 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000641 }
642 } while (!eof());
643}
644
Manuel Klimek80829bd2013-05-23 09:41:43 +0000645bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000646 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000647 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000648 assert(FormatTok->BlockKind != BK_Unknown);
649 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000650 return false;
651 parseBracedList();
652 return true;
653}
654
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000655void UnwrappedLineParser::parseBracedList() {
656 nextToken();
657
Manuel Klimek423dd932013-04-10 09:52:05 +0000658 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
659 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000660 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000661 // FIXME: When we start to support lambdas, we'll want to parse them away
662 // here, otherwise our bail-out scenarios below break. The better solution
663 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000664 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000665 case tok::l_brace:
666 parseBracedList();
667 break;
668 case tok::r_brace:
669 nextToken();
670 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000671 case tok::semi:
672 // Probably a missing closing brace. Bail out.
673 return;
674 case tok::comma:
675 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000676 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000677 default:
678 nextToken();
679 break;
680 }
681 } while (!eof());
682}
683
Manuel Klimekc44ee892013-01-21 10:07:49 +0000684void UnwrappedLineParser::parseReturn() {
685 nextToken();
686
687 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000688 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000689 case tok::l_brace:
690 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000691 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000692 // Assume missing ';'.
693 addUnwrappedLine();
694 return;
695 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000696 break;
697 case tok::l_paren:
698 parseParens();
699 break;
700 case tok::r_brace:
701 // Assume missing ';'.
702 addUnwrappedLine();
703 return;
704 case tok::semi:
705 nextToken();
706 addUnwrappedLine();
707 return;
708 default:
709 nextToken();
710 break;
711 }
712 } while (!eof());
713}
714
Daniel Jasperbac016b2012-12-03 18:12:45 +0000715void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000716 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000717 nextToken();
718 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000719 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000720 case tok::l_paren:
721 parseParens();
722 break;
723 case tok::r_paren:
724 nextToken();
725 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000726 case tok::r_brace:
727 // A "}" inside parenthesis is an error if there wasn't a matching "{".
728 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000729 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000730 if (!tryToParseBracedList()) {
731 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000732 {
733 ScopedLineState LineState(*this);
734 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000735 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000736 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000737 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000738 Line->Level -= 1;
739 }
740 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000741 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000742 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000743 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000744 case tok::at:
745 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000746 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000747 parseBracedList();
748 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000749 default:
750 nextToken();
751 break;
752 }
753 } while (!eof());
754}
755
756void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000757 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000758 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000759 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000760 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000761 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000762 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000763 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
764 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000765 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000766 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
767 addUnwrappedLine();
768 else
769 NeedsUnwrappedLine = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000770 } else {
771 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000772 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000773 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000774 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000775 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000776 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000777 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000778 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000779 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
780 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000781 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000782 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000783 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000784 parseIfThenElse();
785 } else {
786 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000787 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000788 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000789 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000790 }
791 } else if (NeedsUnwrappedLine) {
792 addUnwrappedLine();
793 }
794}
795
Alexander Kornienko15757312012-12-06 18:03:27 +0000796void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000797 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000798 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000799 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000800 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000801 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000802 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
803 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000804 addUnwrappedLine();
805
Daniel Jaspereff18b92013-07-31 23:16:02 +0000806 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
807 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
808 DeclarationScopeStack.size() > 1);
809 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000810 // Munch the semicolon after a namespace. This is more common than one would
811 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000812 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000813 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000814 addUnwrappedLine();
815 }
816 // FIXME: Add error handling.
817}
818
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000819void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000820 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000821 "'for' or 'while' expected");
822 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000823 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000824 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000825 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000826 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
827 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000828 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000829 addUnwrappedLine();
830 } else {
831 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000832 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000833 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000834 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000835 }
836}
837
Daniel Jasperbac016b2012-12-03 18:12:45 +0000838void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000839 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000840 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000841 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000842 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
843 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000844 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000845 } else {
846 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000847 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000848 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000849 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000850 }
851
Alexander Kornienko393b0082012-12-04 15:40:36 +0000852 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000853 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000854 addUnwrappedLine();
855 return;
856 }
857
Daniel Jasperbac016b2012-12-03 18:12:45 +0000858 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000859 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000860}
861
862void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000863 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000864 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000865 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000866 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000867 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000868 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000869 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000870 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
871 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000872 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000873 if (FormatTok->Tok.is(tok::kw_break)) {
874 // "break;" after "}" on its own line only for BS_Allman
875 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
876 addUnwrappedLine();
877 parseStructuralElement();
878 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000879 }
880 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000881 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000882}
883
884void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000885 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000886 // FIXME: fix handling of complex expressions here.
887 do {
888 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000889 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000890 parseLabel();
891}
892
893void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000894 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000895 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000896 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000897 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000898 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000899 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
900 addUnwrappedLine();
Daniel Jaspereff18b92013-07-31 23:16:02 +0000901 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000902 addUnwrappedLine();
903 } else {
904 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000905 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000906 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000907 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000908 }
909}
910
911void UnwrappedLineParser::parseAccessSpecifier() {
912 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000913 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000914 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000915 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000916 addUnwrappedLine();
917}
918
919void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000920 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000921 if (FormatTok->Tok.is(tok::identifier) ||
922 FormatTok->Tok.is(tok::kw___attribute) ||
923 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000924 nextToken();
925 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000926 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000927 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000928 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000929 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000930 nextToken();
931 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000932 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000933 nextToken();
934 addUnwrappedLine();
935 ++Line->Level;
936 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000937 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000938 case tok::l_paren:
939 parseParens();
940 break;
941 case tok::r_brace:
942 addUnwrappedLine();
943 nextToken();
944 --Line->Level;
945 return;
946 case tok::comma:
947 nextToken();
948 addUnwrappedLine();
949 break;
950 default:
951 nextToken();
952 break;
953 }
954 } while (!eof());
955 }
956 // We fall through to parsing a structural element afterwards, so that in
957 // enum A {} n, m;
958 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000959}
960
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000961void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000962 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000963 if (FormatTok->Tok.is(tok::identifier) ||
964 FormatTok->Tok.is(tok::kw___attribute) ||
965 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000966 nextToken();
967 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000968 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000969 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000970 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000971 // The actual identifier can be a nested name specifier, and in macros
972 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000973 while (FormatTok->Tok.is(tok::identifier) ||
974 FormatTok->Tok.is(tok::coloncolon) ||
975 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000976 nextToken();
977
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000978 // Note that parsing away template declarations here leads to incorrectly
979 // accepting function declarations as record declarations.
980 // In general, we cannot solve this problem. Consider:
981 // class A<int> B() {}
982 // which can be a function definition or a class definition when B() is a
983 // macro. If we find enough real-world cases where this is a problem, we
984 // can parse for the 'template' keyword in the beginning of the statement,
985 // and thus rule out the record production in case there is no template
986 // (this would still leave us with an ambiguity between template function
987 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000988 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
989 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
990 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000991 return;
992 nextToken();
993 }
994 }
995 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000996 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000997 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
998 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000999 addUnwrappedLine();
1000
Nico Weber27268772013-06-26 00:30:14 +00001001 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +00001002 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001003 // We fall through to parsing a structural element afterwards, so
1004 // class A {} n, m;
1005 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001006}
1007
Nico Weber1abe6ea2013-01-09 21:15:03 +00001008void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001009 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001010 do
1011 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001012 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001013 nextToken(); // Skip '>'.
1014}
1015
1016void UnwrappedLineParser::parseObjCUntilAtEnd() {
1017 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001018 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001019 nextToken();
1020 addUnwrappedLine();
1021 break;
1022 }
1023 parseStructuralElement();
1024 } while (!eof());
1025}
1026
Nico Weber50767d82013-01-09 23:25:37 +00001027void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001028 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001029 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001030
1031 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001032 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001033 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001034 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001035 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001036 // Skip category, if present.
1037 parseParens();
1038
Manuel Klimek96e888b2013-05-28 11:55:06 +00001039 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001040 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001041
1042 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001043 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001044 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001045
1046 // With instance variables, this puts '}' on its own line. Without instance
1047 // variables, this ends the @interface line.
1048 addUnwrappedLine();
1049
Nico Weber1abe6ea2013-01-09 21:15:03 +00001050 parseObjCUntilAtEnd();
1051}
Nico Weber27d13672013-01-09 20:25:35 +00001052
Nico Weber1abe6ea2013-01-09 21:15:03 +00001053void UnwrappedLineParser::parseObjCProtocol() {
1054 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001055 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001056
Manuel Klimek96e888b2013-05-28 11:55:06 +00001057 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001058 parseObjCProtocolList();
1059
1060 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001061 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001062 nextToken();
1063 return addUnwrappedLine();
1064 }
1065
1066 addUnwrappedLine();
1067 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001068}
1069
Daniel Jasperbac016b2012-12-03 18:12:45 +00001070void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001071 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001072 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001073 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001074 llvm::dbgs() << "Line(" << Line->Level << ")"
1075 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001076 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1077 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001078 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001079 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001080 }
1081 llvm::dbgs() << "\n";
1082 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001083 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001084 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001085 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001086 for (std::vector<UnwrappedLine>::iterator
1087 I = PreprocessorDirectives.begin(),
1088 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001089 I != E; ++I) {
1090 CurrentLines->push_back(*I);
1091 }
1092 PreprocessorDirectives.clear();
1093 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001094}
1095
Manuel Klimek96e888b2013-05-28 11:55:06 +00001096bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001097
Manuel Klimek86721d22013-01-22 16:31:55 +00001098void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1099 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001100 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001101 I = CommentsBeforeNextToken.begin(),
1102 E = CommentsBeforeNextToken.end();
1103 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001104 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001105 addUnwrappedLine();
1106 }
1107 pushToken(*I);
1108 }
1109 if (NewlineBeforeNext && JustComments) {
1110 addUnwrappedLine();
1111 }
1112 CommentsBeforeNextToken.clear();
1113}
1114
Daniel Jasperbac016b2012-12-03 18:12:45 +00001115void UnwrappedLineParser::nextToken() {
1116 if (eof())
1117 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001118 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001119 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001120 readToken();
1121}
1122
1123void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001124 bool CommentsInCurrentLine = true;
1125 do {
1126 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001127 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1128 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001129 // If there is an unfinished unwrapped line, we flush the preprocessor
1130 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001131 bool SwitchToPreprocessorLines =
1132 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001133 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001134 // Comments stored before the preprocessor directive need to be output
1135 // before the preprocessor directive, at the same level as the
1136 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001137 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001138 parsePPDirective();
1139 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001140
1141 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1142 !Line->InPPDirective) {
1143 continue;
1144 }
1145
Manuel Klimek96e888b2013-05-28 11:55:06 +00001146 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001147 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001148 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001149 CommentsInCurrentLine = false;
1150 }
1151 if (CommentsInCurrentLine) {
1152 pushToken(FormatTok);
1153 } else {
1154 CommentsBeforeNextToken.push_back(FormatTok);
1155 }
1156 } while (!eof());
1157}
1158
Manuel Klimek96e888b2013-05-28 11:55:06 +00001159void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001160 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001161 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001162 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001163 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001164 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001165}
1166
Daniel Jaspercd162382013-01-07 13:26:07 +00001167} // end namespace format
1168} // end namespace clang