blob: f58ee823032d6b47c62fb91225c5e66d0d1c311c [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:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000238 if (!SwitchLabelEncountered &&
239 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
240 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000241 SwitchLabelEncountered = true;
242 parseStructuralElement();
243 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000244 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000245 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000246 break;
247 }
248 } while (!eof());
249}
250
Manuel Klimek80829bd2013-05-23 09:41:43 +0000251void UnwrappedLineParser::calculateBraceTypes() {
252 // We'll parse forward through the tokens until we hit
253 // a closing brace or eof - note that getNextToken() will
254 // parse macros, so this will magically work inside macro
255 // definitions, too.
256 unsigned StoredPosition = Tokens->getPosition();
257 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000258 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000259 // Keep a stack of positions of lbrace tokens. We will
260 // update information about whether an lbrace starts a
261 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000262 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000263 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000264 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000265 // Get next none-comment token.
266 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000267 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000268 do {
269 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000270 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000271 } while (NextTok->is(tok::comment));
272
Manuel Klimek96e888b2013-05-28 11:55:06 +0000273 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000274 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000275 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000276 break;
277 case tok::r_brace:
278 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000279 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000280 // If there is a comma, semicolon or right paren after the closing
Manuel Klimek31e44f72013-09-04 08:20:47 +0000281 // brace, we assume this is a braced initializer list. Note that
282 // regardless how we mark inner braces here, we will overwrite the
283 // BlockKind later if we parse a braced list (where all blocks inside
284 // are by default braced lists), or when we explicitly detect blocks
285 // (for example while parsing lambdas).
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000286 //
287 // We exclude + and - as they can be ObjC visibility modifiers.
Daniel Jaspereb483662013-05-31 10:09:55 +0000288 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000289 tok::l_brace, tok::colon) ||
290 (NextTok->isBinaryOperator() &&
291 !NextTok->isOneOf(tok::plus, tok::minus))) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000292 Tok->BlockKind = BK_BracedInit;
293 LBraceStack.back()->BlockKind = BK_BracedInit;
294 } else {
295 Tok->BlockKind = BK_Block;
296 LBraceStack.back()->BlockKind = BK_Block;
297 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000298 }
299 LBraceStack.pop_back();
300 }
301 break;
302 case tok::semi:
303 case tok::kw_if:
304 case tok::kw_while:
305 case tok::kw_for:
306 case tok::kw_switch:
307 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000308 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000309 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000310 break;
311 default:
312 break;
313 }
314 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000315 Position += ReadTokens;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000316 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000317 // Assume other blocks for all unclosed opening braces.
318 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000319 if (LBraceStack[i]->BlockKind == BK_Unknown)
320 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000321 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000322
Manuel Klimek80829bd2013-05-23 09:41:43 +0000323 FormatTok = Tokens->setPosition(StoredPosition);
324}
325
Daniel Jaspereff18b92013-07-31 23:16:02 +0000326void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000327 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000328 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000329 nextToken();
330
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000331 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000332
Manuel Klimek70b03f42013-01-23 09:32:48 +0000333 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
334 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000335 if (AddLevel)
336 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000337 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000338
Manuel Klimek96e888b2013-05-28 11:55:06 +0000339 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000340 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000341 StructuralError = true;
342 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000343 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000344
Daniel Jasperf9955d32013-03-20 12:37:50 +0000345 nextToken(); // Munch the closing brace.
Daniel Jaspere865cc52013-07-25 11:31:57 +0000346 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000347}
348
349void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000350 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000351 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000352 nextToken();
353
Manuel Klimek96e888b2013-05-28 11:55:06 +0000354 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000355 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000356 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000357 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000358
Manuel Klimek96e888b2013-05-28 11:55:06 +0000359 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000360 case tok::pp_define:
361 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000362 return;
363 case tok::pp_if:
364 parsePPIf();
365 break;
366 case tok::pp_ifdef:
367 case tok::pp_ifndef:
368 parsePPIfdef();
369 break;
370 case tok::pp_else:
371 parsePPElse();
372 break;
373 case tok::pp_elif:
374 parsePPElIf();
375 break;
376 case tok::pp_endif:
377 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000378 break;
379 default:
380 parsePPUnknown();
381 break;
382 }
383}
384
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000385void UnwrappedLineParser::pushPPConditional() {
386 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
387 PPStack.push_back(PP_Unreachable);
388 else
389 PPStack.push_back(PP_Conditional);
390}
391
392void UnwrappedLineParser::parsePPIf() {
393 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000394 if ((FormatTok->Tok.isLiteral() &&
395 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000396 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000397 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000398 PPStack.push_back(PP_Unreachable);
399 } else {
400 pushPPConditional();
401 }
402 parsePPUnknown();
403}
404
405void UnwrappedLineParser::parsePPIfdef() {
406 pushPPConditional();
407 parsePPUnknown();
408}
409
410void UnwrappedLineParser::parsePPElse() {
411 if (!PPStack.empty())
412 PPStack.pop_back();
413 pushPPConditional();
414 parsePPUnknown();
415}
416
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000417void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000418
419void UnwrappedLineParser::parsePPEndIf() {
420 if (!PPStack.empty())
421 PPStack.pop_back();
422 parsePPUnknown();
423}
424
Manuel Klimekd4397b92013-01-04 23:34:14 +0000425void UnwrappedLineParser::parsePPDefine() {
426 nextToken();
427
Manuel Klimek96e888b2013-05-28 11:55:06 +0000428 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000429 parsePPUnknown();
430 return;
431 }
432 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000433 if (FormatTok->Tok.getKind() == tok::l_paren &&
434 FormatTok->WhitespaceRange.getBegin() ==
435 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000436 parseParens();
437 }
438 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000439 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000440
441 // Errors during a preprocessor directive can only affect the layout of the
442 // preprocessor directive, and thus we ignore them. An alternative approach
443 // would be to use the same approach we use on the file level (no
444 // re-indentation if there was a structural error) within the macro
445 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000446 parseFile();
447}
448
449void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000450 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000451 nextToken();
452 } while (!eof());
453 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000454}
455
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000456// Here we blacklist certain tokens that are not usually the first token in an
457// unwrapped line. This is used in attempt to distinguish macro calls without
458// trailing semicolons from other constructs split to several lines.
459bool tokenCanStartNewLine(clang::Token Tok) {
460 // Semicolon can be a null-statement, l_square can be a start of a macro or
461 // a C++11 attribute, but this doesn't seem to be common.
462 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
463 Tok.isNot(tok::l_square) &&
464 // Tokens that can only be used as binary operators and a part of
465 // overloaded operator names.
466 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
467 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
468 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
469 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
470 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
471 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
472 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
473 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
474 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
475 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
476 Tok.isNot(tok::lesslessequal) &&
477 // Colon is used in labels, base class lists, initializer lists,
478 // range-based for loops, ternary operator, but should never be the
479 // first token in an unwrapped line.
480 Tok.isNot(tok::colon);
481}
482
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000483void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000484 assert(!FormatTok->Tok.is(tok::l_brace));
485 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000486 case tok::at:
487 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000488 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000489 parseBracedList();
490 break;
491 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000492 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000493 case tok::objc_public:
494 case tok::objc_protected:
495 case tok::objc_package:
496 case tok::objc_private:
497 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000498 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000499 case tok::objc_implementation:
500 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000501 case tok::objc_protocol:
502 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000503 case tok::objc_end:
504 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000505 case tok::objc_optional:
506 case tok::objc_required:
507 nextToken();
508 addUnwrappedLine();
509 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000510 default:
511 break;
512 }
513 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000514 case tok::kw_namespace:
515 parseNamespace();
516 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000517 case tok::kw_inline:
518 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000519 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000520 parseNamespace();
521 return;
522 }
523 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000524 case tok::kw_public:
525 case tok::kw_protected:
526 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000527 parseAccessSpecifier();
528 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000529 case tok::kw_if:
530 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000531 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000532 case tok::kw_for:
533 case tok::kw_while:
534 parseForOrWhileLoop();
535 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000536 case tok::kw_do:
537 parseDoWhile();
538 return;
539 case tok::kw_switch:
540 parseSwitch();
541 return;
542 case tok::kw_default:
543 nextToken();
544 parseLabel();
545 return;
546 case tok::kw_case:
547 parseCaseLabel();
548 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000549 case tok::kw_return:
550 parseReturn();
551 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000552 case tok::kw_extern:
553 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000554 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000555 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000556 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000557 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000558 addUnwrappedLine();
559 return;
560 }
561 }
562 // In all other cases, parse the declaration.
563 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000564 default:
565 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000566 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000567 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000568 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000569 case tok::at:
570 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000571 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000572 parseBracedList();
573 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000574 case tok::kw_enum:
575 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000576 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000577 case tok::kw_struct:
578 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000579 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000580 parseRecord();
581 // A record declaration or definition is always the start of a structural
582 // element.
583 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000584 case tok::semi:
585 nextToken();
586 addUnwrappedLine();
587 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000588 case tok::r_brace:
589 addUnwrappedLine();
590 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000591 case tok::l_paren:
592 parseParens();
593 break;
594 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000595 if (!tryToParseBracedList()) {
596 // A block outside of parentheses must be the last part of a
597 // structural element.
598 // FIXME: Figure out cases where this is not true, and add projections
599 // for them (the one we know is missing are lambdas).
600 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Manuel Klimeke4907052013-08-02 21:31:59 +0000601 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
602 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000603 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000604 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000605 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000606 return;
607 }
608 // Otherwise this was a braced init list, and the structural
609 // element continues.
610 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000611 case tok::identifier: {
612 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000613 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000614 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000615 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000616 parseLabel();
617 return;
618 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000619 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000620 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000621 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000622 if (FormatTok->HasUnescapedNewline &&
623 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000624 addUnwrappedLine();
625 return;
626 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000627 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
628 Text == Text.upper()) {
629 // Recognize free-standing macros like Q_OBJECT.
630 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000631 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000632 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000633 }
634 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000635 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000636 case tok::equal:
637 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000638 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000639 parseBracedList();
640 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000641 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000642 case tok::l_square:
643 tryToParseLambda();
644 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000645 default:
646 nextToken();
647 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000648 }
649 } while (!eof());
650}
651
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000652void UnwrappedLineParser::tryToParseLambda() {
653 if (!tryToParseLambdaIntroducer()) {
654 return;
655 }
656 if (FormatTok->is(tok::l_paren)) {
657 parseParens();
658 }
659
660 while (FormatTok->isNot(tok::l_brace)) {
661 switch (FormatTok->Tok.getKind()) {
662 case tok::l_brace:
663 break;
664 return;
665 case tok::l_paren:
666 parseParens();
667 break;
668 case tok::semi:
669 case tok::equal:
670 case tok::eof:
671 return;
672 default:
673 nextToken();
674 break;
675 }
676 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000677 FormatTok->BlockKind = BK_Block;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000678 nextToken();
679 {
680 ScopedLineState LineState(*this);
681 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
682 /*MustBeDeclaration=*/false);
683 Line->Level += 1;
684 parseLevel(/*HasOpeningBrace=*/true);
685 Line->Level -= 1;
686 }
687 nextToken();
688}
689
690bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
691 nextToken();
692 if (FormatTok->is(tok::equal)) {
693 nextToken();
694 if (FormatTok->is(tok::r_square)) return true;
695 if (FormatTok->isNot(tok::comma)) return false;
696 nextToken();
697 } else if (FormatTok->is(tok::amp)) {
698 nextToken();
699 if (FormatTok->is(tok::r_square)) return true;
700 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
701 return false;
702 }
703 if (FormatTok->is(tok::comma)) nextToken();
704 } else if (FormatTok->is(tok::r_square)) {
705 nextToken();
706 return true;
707 }
708 do {
709 if (FormatTok->is(tok::amp)) nextToken();
710 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) return false;
711 nextToken();
712 if (FormatTok->is(tok::comma)) {
713 nextToken();
714 } else if (FormatTok->is(tok::r_square)) {
715 nextToken();
716 return true;
717 } else {
718 return false;
719 }
720 } while (!eof());
721 return false;
722}
723
Manuel Klimek80829bd2013-05-23 09:41:43 +0000724bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000725 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000726 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000727 assert(FormatTok->BlockKind != BK_Unknown);
728 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000729 return false;
730 parseBracedList();
731 return true;
732}
733
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000734void UnwrappedLineParser::parseBracedList() {
735 nextToken();
736
Manuel Klimek423dd932013-04-10 09:52:05 +0000737 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
738 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000739 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000740 // FIXME: When we start to support lambdas, we'll want to parse them away
741 // here, otherwise our bail-out scenarios below break. The better solution
742 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000743 switch (FormatTok->Tok.getKind()) {
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000744 case tok::l_square:
745 tryToParseLambda();
746 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000747 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +0000748 // Assume there are no blocks inside a braced init list apart
749 // from the ones we explicitly parse out (like lambdas).
750 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000751 parseBracedList();
752 break;
753 case tok::r_brace:
754 nextToken();
755 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000756 case tok::semi:
757 // Probably a missing closing brace. Bail out.
758 return;
759 case tok::comma:
760 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000761 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000762 default:
763 nextToken();
764 break;
765 }
766 } while (!eof());
767}
768
Manuel Klimekc44ee892013-01-21 10:07:49 +0000769void UnwrappedLineParser::parseReturn() {
770 nextToken();
771
772 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000773 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000774 case tok::l_brace:
775 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000776 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000777 // Assume missing ';'.
778 addUnwrappedLine();
779 return;
780 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000781 break;
782 case tok::l_paren:
783 parseParens();
784 break;
785 case tok::r_brace:
786 // Assume missing ';'.
787 addUnwrappedLine();
788 return;
789 case tok::semi:
790 nextToken();
791 addUnwrappedLine();
792 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000793 case tok::l_square:
794 tryToParseLambda();
795 break;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000796 default:
797 nextToken();
798 break;
799 }
800 } while (!eof());
801}
802
Daniel Jasperbac016b2012-12-03 18:12:45 +0000803void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000804 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000805 nextToken();
806 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000807 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000808 case tok::l_paren:
809 parseParens();
810 break;
811 case tok::r_paren:
812 nextToken();
813 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000814 case tok::r_brace:
815 // A "}" inside parenthesis is an error if there wasn't a matching "{".
816 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000817 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000818 if (!tryToParseBracedList()) {
819 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000820 {
821 ScopedLineState LineState(*this);
822 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000823 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000824 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000825 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000826 Line->Level -= 1;
827 }
828 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000829 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000830 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000831 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000832 case tok::at:
833 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000834 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000835 parseBracedList();
836 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000837 default:
838 nextToken();
839 break;
840 }
841 } while (!eof());
842}
843
844void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000845 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000846 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000847 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000848 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000849 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000850 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000851 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
852 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000853 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000854 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
855 addUnwrappedLine();
856 else
857 NeedsUnwrappedLine = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000858 } else {
859 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000860 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000861 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000862 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000864 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000865 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000866 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000867 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
868 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000869 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000870 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000871 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000872 parseIfThenElse();
873 } else {
874 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000875 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000876 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000877 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000878 }
879 } else if (NeedsUnwrappedLine) {
880 addUnwrappedLine();
881 }
882}
883
Alexander Kornienko15757312012-12-06 18:03:27 +0000884void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000885 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000886 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000887 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000888 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000889 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000890 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
891 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000892 addUnwrappedLine();
893
Daniel Jaspereff18b92013-07-31 23:16:02 +0000894 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
895 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
896 DeclarationScopeStack.size() > 1);
897 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000898 // Munch the semicolon after a namespace. This is more common than one would
899 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000900 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000901 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000902 addUnwrappedLine();
903 }
904 // FIXME: Add error handling.
905}
906
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000907void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000908 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000909 "'for' or 'while' expected");
910 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000911 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000912 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000913 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000914 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
915 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000916 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000917 addUnwrappedLine();
918 } else {
919 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000920 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000921 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000922 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000923 }
924}
925
Daniel Jasperbac016b2012-12-03 18:12:45 +0000926void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000927 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000928 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000929 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000930 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
931 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000932 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000933 } else {
934 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000935 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000936 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000937 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000938 }
939
Alexander Kornienko393b0082012-12-04 15:40:36 +0000940 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000941 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000942 addUnwrappedLine();
943 return;
944 }
945
Daniel Jasperbac016b2012-12-03 18:12:45 +0000946 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000947 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000948}
949
950void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000951 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000952 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000953 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000954 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000955 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000956 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
957 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000958 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000959 if (FormatTok->Tok.is(tok::kw_break)) {
960 // "break;" after "}" on its own line only for BS_Allman
961 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
962 addUnwrappedLine();
963 parseStructuralElement();
964 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000965 }
966 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000967 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000968}
969
970void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000971 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000972 // FIXME: fix handling of complex expressions here.
973 do {
974 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000975 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000976 parseLabel();
977}
978
979void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000980 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000981 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000982 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000983 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000984 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000985 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
986 addUnwrappedLine();
Daniel Jaspereff18b92013-07-31 23:16:02 +0000987 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000988 addUnwrappedLine();
989 } else {
990 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000991 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000992 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000993 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000994 }
995}
996
997void UnwrappedLineParser::parseAccessSpecifier() {
998 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000999 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001000 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001001 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001002 addUnwrappedLine();
1003}
1004
1005void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +00001006 nextToken();
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001007 // Eat up enum class ...
1008 if (FormatTok->Tok.is(tok::kw_class) ||
1009 FormatTok->Tok.is(tok::kw_struct))
1010 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001011 if (FormatTok->Tok.is(tok::identifier) ||
1012 FormatTok->Tok.is(tok::kw___attribute) ||
1013 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001014 nextToken();
1015 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001016 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001017 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001018 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001019 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001020 nextToken();
1021 }
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001022 bool HasError = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001023 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001024 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1025 addUnwrappedLine();
Manuel Klimek308232c2013-01-21 19:17:52 +00001026 nextToken();
1027 addUnwrappedLine();
1028 ++Line->Level;
1029 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001030 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001031 case tok::l_paren:
1032 parseParens();
1033 break;
1034 case tok::r_brace:
1035 addUnwrappedLine();
1036 nextToken();
1037 --Line->Level;
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001038 if (HasError) {
1039 if (FormatTok->is(tok::semi))
1040 nextToken();
1041 addUnwrappedLine();
1042 }
Manuel Klimek308232c2013-01-21 19:17:52 +00001043 return;
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001044 case tok::semi:
1045 HasError = true;
1046 nextToken();
1047 addUnwrappedLine();
1048 break;
Manuel Klimek308232c2013-01-21 19:17:52 +00001049 case tok::comma:
1050 nextToken();
1051 addUnwrappedLine();
1052 break;
1053 default:
1054 nextToken();
1055 break;
1056 }
1057 } while (!eof());
1058 }
1059 // We fall through to parsing a structural element afterwards, so that in
1060 // enum A {} n, m;
1061 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001062}
1063
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001064void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +00001065 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001066 if (FormatTok->Tok.is(tok::identifier) ||
1067 FormatTok->Tok.is(tok::kw___attribute) ||
1068 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001069 nextToken();
1070 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001071 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001072 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001073 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001074 // The actual identifier can be a nested name specifier, and in macros
1075 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001076 while (FormatTok->Tok.is(tok::identifier) ||
1077 FormatTok->Tok.is(tok::coloncolon) ||
1078 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001079 nextToken();
1080
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001081 // Note that parsing away template declarations here leads to incorrectly
1082 // accepting function declarations as record declarations.
1083 // In general, we cannot solve this problem. Consider:
1084 // class A<int> B() {}
1085 // which can be a function definition or a class definition when B() is a
1086 // macro. If we find enough real-world cases where this is a problem, we
1087 // can parse for the 'template' keyword in the beginning of the statement,
1088 // and thus rule out the record production in case there is no template
1089 // (this would still leave us with an ambiguity between template function
1090 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001091 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1092 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1093 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001094 return;
1095 nextToken();
1096 }
1097 }
1098 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001099 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001100 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1101 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +00001102 addUnwrappedLine();
1103
Nico Weber27268772013-06-26 00:30:14 +00001104 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +00001105 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001106 // We fall through to parsing a structural element afterwards, so
1107 // class A {} n, m;
1108 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001109}
1110
Nico Weber1abe6ea2013-01-09 21:15:03 +00001111void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001112 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001113 do
1114 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001115 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001116 nextToken(); // Skip '>'.
1117}
1118
1119void UnwrappedLineParser::parseObjCUntilAtEnd() {
1120 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001121 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001122 nextToken();
1123 addUnwrappedLine();
1124 break;
1125 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001126 if (FormatTok->is(tok::l_brace)) {
1127 parseBlock(/*MustBeDeclaration=*/false);
1128 // In ObjC interfaces, nothing should be following the "}".
1129 addUnwrappedLine();
1130 } else {
1131 parseStructuralElement();
1132 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001133 } while (!eof());
1134}
1135
Nico Weber50767d82013-01-09 23:25:37 +00001136void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001137 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001138 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001139
1140 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001141 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001142 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001143 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001144 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001145 // Skip category, if present.
1146 parseParens();
1147
Manuel Klimek96e888b2013-05-28 11:55:06 +00001148 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001149 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001150
1151 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001152 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001153 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001154
1155 // With instance variables, this puts '}' on its own line. Without instance
1156 // variables, this ends the @interface line.
1157 addUnwrappedLine();
1158
Nico Weber1abe6ea2013-01-09 21:15:03 +00001159 parseObjCUntilAtEnd();
1160}
Nico Weber27d13672013-01-09 20:25:35 +00001161
Nico Weber1abe6ea2013-01-09 21:15:03 +00001162void UnwrappedLineParser::parseObjCProtocol() {
1163 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001164 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001165
Manuel Klimek96e888b2013-05-28 11:55:06 +00001166 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001167 parseObjCProtocolList();
1168
1169 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001170 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001171 nextToken();
1172 return addUnwrappedLine();
1173 }
1174
1175 addUnwrappedLine();
1176 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001177}
1178
Daniel Jasperbac016b2012-12-03 18:12:45 +00001179void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001180 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001181 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001182 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001183 llvm::dbgs() << "Line(" << Line->Level << ")"
1184 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001185 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1186 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001187 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001188 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001189 }
1190 llvm::dbgs() << "\n";
1191 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001192 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001193 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001194 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001195 for (std::vector<UnwrappedLine>::iterator
1196 I = PreprocessorDirectives.begin(),
1197 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001198 I != E; ++I) {
1199 CurrentLines->push_back(*I);
1200 }
1201 PreprocessorDirectives.clear();
1202 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001203}
1204
Manuel Klimek96e888b2013-05-28 11:55:06 +00001205bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001206
Manuel Klimek86721d22013-01-22 16:31:55 +00001207void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1208 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001209 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001210 I = CommentsBeforeNextToken.begin(),
1211 E = CommentsBeforeNextToken.end();
1212 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001213 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001214 addUnwrappedLine();
1215 }
1216 pushToken(*I);
1217 }
1218 if (NewlineBeforeNext && JustComments) {
1219 addUnwrappedLine();
1220 }
1221 CommentsBeforeNextToken.clear();
1222}
1223
Daniel Jasperbac016b2012-12-03 18:12:45 +00001224void UnwrappedLineParser::nextToken() {
1225 if (eof())
1226 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001227 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001228 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001229 readToken();
1230}
1231
1232void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001233 bool CommentsInCurrentLine = true;
1234 do {
1235 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001236 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1237 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001238 // If there is an unfinished unwrapped line, we flush the preprocessor
1239 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001240 bool SwitchToPreprocessorLines =
1241 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001242 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001243 // Comments stored before the preprocessor directive need to be output
1244 // before the preprocessor directive, at the same level as the
1245 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001246 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001247 parsePPDirective();
1248 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001249
1250 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1251 !Line->InPPDirective) {
1252 continue;
1253 }
1254
Manuel Klimek96e888b2013-05-28 11:55:06 +00001255 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001256 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001257 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001258 CommentsInCurrentLine = false;
1259 }
1260 if (CommentsInCurrentLine) {
1261 pushToken(FormatTok);
1262 } else {
1263 CommentsBeforeNextToken.push_back(FormatTok);
1264 }
1265 } while (!eof());
1266}
1267
Manuel Klimek96e888b2013-05-28 11:55:06 +00001268void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001269 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001270 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001271 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001272 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001273 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001274}
1275
Daniel Jaspercd162382013-01-07 13:26:07 +00001276} // end namespace format
1277} // end namespace clang