blob: 77f98bffda227254ff3ee8a643963451bbcd826c [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 Jasperbac016b2012-12-03 18:12:45 +0000216 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000217 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000218 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000219 nextToken();
220 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221 break;
222 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000223 // FIXME: Add parameter whether this can happen - if this happens, we must
224 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000225 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000226 addUnwrappedLine();
227 break;
228 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000229 if (HasOpeningBrace)
230 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000231 StructuralError = true;
232 nextToken();
233 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000234 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000235 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000236 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000237 break;
238 }
239 } while (!eof());
240}
241
Manuel Klimek80829bd2013-05-23 09:41:43 +0000242void UnwrappedLineParser::calculateBraceTypes() {
243 // We'll parse forward through the tokens until we hit
244 // a closing brace or eof - note that getNextToken() will
245 // parse macros, so this will magically work inside macro
246 // definitions, too.
247 unsigned StoredPosition = Tokens->getPosition();
248 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000249 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000250 // Keep a stack of positions of lbrace tokens. We will
251 // update information about whether an lbrace starts a
252 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000253 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000254 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000255 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000256 // Get next none-comment token.
257 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000258 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000259 do {
260 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000261 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000262 } while (NextTok->is(tok::comment));
263
Manuel Klimek96e888b2013-05-28 11:55:06 +0000264 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000265 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000266 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000267 break;
268 case tok::r_brace:
269 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000270 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000271 // If there is a comma, semicolon or right paren after the closing
272 // brace, we assume this is a braced initializer list.
273
274 // FIXME: Note that this currently works only because we do not
275 // use the brace information while inside a braced init list.
276 // Thus, if the parent is a braced init list, we consider all
277 // brace blocks inside it braced init list. That works good enough
278 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jaspereb483662013-05-31 10:09:55 +0000279 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000280 tok::l_brace, tok::colon)) {
281 Tok->BlockKind = BK_BracedInit;
282 LBraceStack.back()->BlockKind = BK_BracedInit;
283 } else {
284 Tok->BlockKind = BK_Block;
285 LBraceStack.back()->BlockKind = BK_Block;
286 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000287 }
288 LBraceStack.pop_back();
289 }
290 break;
291 case tok::semi:
292 case tok::kw_if:
293 case tok::kw_while:
294 case tok::kw_for:
295 case tok::kw_switch:
296 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000297 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000298 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000299 break;
300 default:
301 break;
302 }
303 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000304 Position += ReadTokens;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000305 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000306 // Assume other blocks for all unclosed opening braces.
307 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000308 if (LBraceStack[i]->BlockKind == BK_Unknown)
309 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000310 }
311 FormatTok = Tokens->setPosition(StoredPosition);
312}
313
Manuel Klimek67d080d2013-04-12 14:13:36 +0000314void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
Nico Weberd74fcdb2013-02-10 20:35:35 +0000315 unsigned AddLevels) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000316 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000317 nextToken();
318
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000319 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000320
Manuel Klimek70b03f42013-01-23 09:32:48 +0000321 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
322 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000323 Line->Level += AddLevels;
Nico Weber27268772013-06-26 00:30:14 +0000324 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000325
Manuel Klimek96e888b2013-05-28 11:55:06 +0000326 if (!FormatTok->Tok.is(tok::r_brace)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000327 Line->Level -= AddLevels;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000328 StructuralError = true;
329 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000330 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000331
Daniel Jasperf9955d32013-03-20 12:37:50 +0000332 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000333 Line->Level -= AddLevels;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000334}
335
336void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000337 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000338 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000339 nextToken();
340
Manuel Klimek96e888b2013-05-28 11:55:06 +0000341 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000342 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000343 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000344 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000345
Manuel Klimek96e888b2013-05-28 11:55:06 +0000346 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000347 case tok::pp_define:
348 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000349 return;
350 case tok::pp_if:
351 parsePPIf();
352 break;
353 case tok::pp_ifdef:
354 case tok::pp_ifndef:
355 parsePPIfdef();
356 break;
357 case tok::pp_else:
358 parsePPElse();
359 break;
360 case tok::pp_elif:
361 parsePPElIf();
362 break;
363 case tok::pp_endif:
364 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000365 break;
366 default:
367 parsePPUnknown();
368 break;
369 }
370}
371
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000372void UnwrappedLineParser::pushPPConditional() {
373 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
374 PPStack.push_back(PP_Unreachable);
375 else
376 PPStack.push_back(PP_Conditional);
377}
378
379void UnwrappedLineParser::parsePPIf() {
380 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000381 if ((FormatTok->Tok.isLiteral() &&
382 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000383 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000384 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000385 PPStack.push_back(PP_Unreachable);
386 } else {
387 pushPPConditional();
388 }
389 parsePPUnknown();
390}
391
392void UnwrappedLineParser::parsePPIfdef() {
393 pushPPConditional();
394 parsePPUnknown();
395}
396
397void UnwrappedLineParser::parsePPElse() {
398 if (!PPStack.empty())
399 PPStack.pop_back();
400 pushPPConditional();
401 parsePPUnknown();
402}
403
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000404void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000405
406void UnwrappedLineParser::parsePPEndIf() {
407 if (!PPStack.empty())
408 PPStack.pop_back();
409 parsePPUnknown();
410}
411
Manuel Klimekd4397b92013-01-04 23:34:14 +0000412void UnwrappedLineParser::parsePPDefine() {
413 nextToken();
414
Manuel Klimek96e888b2013-05-28 11:55:06 +0000415 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000416 parsePPUnknown();
417 return;
418 }
419 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000420 if (FormatTok->Tok.getKind() == tok::l_paren &&
421 FormatTok->WhitespaceRange.getBegin() ==
422 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000423 parseParens();
424 }
425 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000426 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000427
428 // Errors during a preprocessor directive can only affect the layout of the
429 // preprocessor directive, and thus we ignore them. An alternative approach
430 // would be to use the same approach we use on the file level (no
431 // re-indentation if there was a structural error) within the macro
432 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000433 parseFile();
434}
435
436void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000437 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000438 nextToken();
439 } while (!eof());
440 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000441}
442
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000443// Here we blacklist certain tokens that are not usually the first token in an
444// unwrapped line. This is used in attempt to distinguish macro calls without
445// trailing semicolons from other constructs split to several lines.
446bool tokenCanStartNewLine(clang::Token Tok) {
447 // Semicolon can be a null-statement, l_square can be a start of a macro or
448 // a C++11 attribute, but this doesn't seem to be common.
449 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
450 Tok.isNot(tok::l_square) &&
451 // Tokens that can only be used as binary operators and a part of
452 // overloaded operator names.
453 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
454 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
455 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
456 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
457 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
458 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
459 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
460 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
461 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
462 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
463 Tok.isNot(tok::lesslessequal) &&
464 // Colon is used in labels, base class lists, initializer lists,
465 // range-based for loops, ternary operator, but should never be the
466 // first token in an unwrapped line.
467 Tok.isNot(tok::colon);
468}
469
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000470void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000471 assert(!FormatTok->Tok.is(tok::l_brace));
472 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000473 case tok::at:
474 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000475 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000476 parseBracedList();
477 break;
478 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000479 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000480 case tok::objc_public:
481 case tok::objc_protected:
482 case tok::objc_package:
483 case tok::objc_private:
484 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000485 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000486 case tok::objc_implementation:
487 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000488 case tok::objc_protocol:
489 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000490 case tok::objc_end:
491 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000492 case tok::objc_optional:
493 case tok::objc_required:
494 nextToken();
495 addUnwrappedLine();
496 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000497 default:
498 break;
499 }
500 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000501 case tok::kw_namespace:
502 parseNamespace();
503 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000504 case tok::kw_inline:
505 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000506 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000507 parseNamespace();
508 return;
509 }
510 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000511 case tok::kw_public:
512 case tok::kw_protected:
513 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000514 parseAccessSpecifier();
515 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000516 case tok::kw_if:
517 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000518 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000519 case tok::kw_for:
520 case tok::kw_while:
521 parseForOrWhileLoop();
522 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000523 case tok::kw_do:
524 parseDoWhile();
525 return;
526 case tok::kw_switch:
527 parseSwitch();
528 return;
529 case tok::kw_default:
530 nextToken();
531 parseLabel();
532 return;
533 case tok::kw_case:
534 parseCaseLabel();
535 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000536 case tok::kw_return:
537 parseReturn();
538 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000539 case tok::kw_extern:
540 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000541 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000542 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000543 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000544 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000545 addUnwrappedLine();
546 return;
547 }
548 }
549 // In all other cases, parse the declaration.
550 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000551 default:
552 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000553 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000554 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000555 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000556 case tok::at:
557 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000558 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000559 parseBracedList();
560 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000561 case tok::kw_enum:
562 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000563 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000564 case tok::kw_struct:
565 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000566 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000567 parseRecord();
568 // A record declaration or definition is always the start of a structural
569 // element.
570 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000571 case tok::semi:
572 nextToken();
573 addUnwrappedLine();
574 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000575 case tok::r_brace:
576 addUnwrappedLine();
577 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000578 case tok::l_paren:
579 parseParens();
580 break;
581 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000582 if (!tryToParseBracedList()) {
583 // A block outside of parentheses must be the last part of a
584 // structural element.
585 // FIXME: Figure out cases where this is not true, and add projections
586 // for them (the one we know is missing are lambdas).
587 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
588 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
589 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000590 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000591 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000592 return;
593 }
594 // Otherwise this was a braced init list, and the structural
595 // element continues.
596 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000597 case tok::identifier: {
598 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000599 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000600 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000601 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000602 parseLabel();
603 return;
604 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000605 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000606 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000607 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000608 if (FormatTok->HasUnescapedNewline &&
609 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000610 addUnwrappedLine();
611 return;
612 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000613 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
614 Text == Text.upper()) {
615 // Recognize free-standing macros like Q_OBJECT.
616 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000617 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000618 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000619 }
620 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000621 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000622 case tok::equal:
623 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000624 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000625 parseBracedList();
626 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000627 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000628 default:
629 nextToken();
630 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000631 }
632 } while (!eof());
633}
634
Manuel Klimek80829bd2013-05-23 09:41:43 +0000635bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000636 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000637 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000638 assert(FormatTok->BlockKind != BK_Unknown);
639 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000640 return false;
641 parseBracedList();
642 return true;
643}
644
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000645void UnwrappedLineParser::parseBracedList() {
646 nextToken();
647
Manuel Klimek423dd932013-04-10 09:52:05 +0000648 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
649 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000650 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000651 // FIXME: When we start to support lambdas, we'll want to parse them away
652 // here, otherwise our bail-out scenarios below break. The better solution
653 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000654 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000655 case tok::l_brace:
656 parseBracedList();
657 break;
658 case tok::r_brace:
659 nextToken();
660 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000661 case tok::semi:
662 // Probably a missing closing brace. Bail out.
663 return;
664 case tok::comma:
665 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000666 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000667 default:
668 nextToken();
669 break;
670 }
671 } while (!eof());
672}
673
Manuel Klimekc44ee892013-01-21 10:07:49 +0000674void UnwrappedLineParser::parseReturn() {
675 nextToken();
676
677 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000678 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000679 case tok::l_brace:
680 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000681 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000682 // Assume missing ';'.
683 addUnwrappedLine();
684 return;
685 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000686 break;
687 case tok::l_paren:
688 parseParens();
689 break;
690 case tok::r_brace:
691 // Assume missing ';'.
692 addUnwrappedLine();
693 return;
694 case tok::semi:
695 nextToken();
696 addUnwrappedLine();
697 return;
698 default:
699 nextToken();
700 break;
701 }
702 } while (!eof());
703}
704
Daniel Jasperbac016b2012-12-03 18:12:45 +0000705void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000706 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000707 nextToken();
708 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000709 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000710 case tok::l_paren:
711 parseParens();
712 break;
713 case tok::r_paren:
714 nextToken();
715 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000716 case tok::r_brace:
717 // A "}" inside parenthesis is an error if there wasn't a matching "{".
718 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000719 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000720 if (!tryToParseBracedList()) {
721 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000722 {
723 ScopedLineState LineState(*this);
724 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000725 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000726 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000727 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000728 Line->Level -= 1;
729 }
730 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000731 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000732 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000733 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000734 case tok::at:
735 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000736 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000737 parseBracedList();
738 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000739 default:
740 nextToken();
741 break;
742 }
743 } while (!eof());
744}
745
746void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000747 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000748 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000749 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000750 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000751 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000752 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000753 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000754 NeedsUnwrappedLine = true;
755 } else {
756 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000757 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000758 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000759 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000760 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000761 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000762 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000763 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000764 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000765 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000766 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000767 parseIfThenElse();
768 } else {
769 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000770 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000771 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000772 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000773 }
774 } else if (NeedsUnwrappedLine) {
775 addUnwrappedLine();
776 }
777}
778
Alexander Kornienko15757312012-12-06 18:03:27 +0000779void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000780 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000781 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000782 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000783 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000784 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000785 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
786 addUnwrappedLine();
787
Nico Weber27268772013-06-26 00:30:14 +0000788 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000789 // Munch the semicolon after a namespace. This is more common than one would
790 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000791 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000792 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000793 addUnwrappedLine();
794 }
795 // FIXME: Add error handling.
796}
797
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000798void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000799 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000800 "'for' or 'while' expected");
801 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000802 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000803 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000804 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000805 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000806 addUnwrappedLine();
807 } else {
808 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000809 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000810 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000811 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000812 }
813}
814
Daniel Jasperbac016b2012-12-03 18:12:45 +0000815void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000816 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000817 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000818 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000819 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000820 } else {
821 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000822 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000823 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000824 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000825 }
826
Alexander Kornienko393b0082012-12-04 15:40:36 +0000827 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000828 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000829 addUnwrappedLine();
830 return;
831 }
832
Daniel Jasperbac016b2012-12-03 18:12:45 +0000833 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000834 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000835}
836
837void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000838 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000839 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000840 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000841 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000842 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000843 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000844 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000845 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000846 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000847 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000848 }
849 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000850 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000851}
852
853void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000854 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000855 // FIXME: fix handling of complex expressions here.
856 do {
857 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000858 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000859 parseLabel();
860}
861
862void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000863 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000864 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000865 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000866 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000867 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000868 parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000869 addUnwrappedLine();
870 } else {
871 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000872 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000873 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000874 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000875 }
876}
877
878void UnwrappedLineParser::parseAccessSpecifier() {
879 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000880 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000881 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000882 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000883 addUnwrappedLine();
884}
885
886void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000887 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000888 if (FormatTok->Tok.is(tok::identifier) ||
889 FormatTok->Tok.is(tok::kw___attribute) ||
890 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000891 nextToken();
892 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000893 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000894 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000895 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000896 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000897 nextToken();
898 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000899 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000900 nextToken();
901 addUnwrappedLine();
902 ++Line->Level;
903 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000904 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000905 case tok::l_paren:
906 parseParens();
907 break;
908 case tok::r_brace:
909 addUnwrappedLine();
910 nextToken();
911 --Line->Level;
912 return;
913 case tok::comma:
914 nextToken();
915 addUnwrappedLine();
916 break;
917 default:
918 nextToken();
919 break;
920 }
921 } while (!eof());
922 }
923 // We fall through to parsing a structural element afterwards, so that in
924 // enum A {} n, m;
925 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000926}
927
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000928void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000929 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000930 if (FormatTok->Tok.is(tok::identifier) ||
931 FormatTok->Tok.is(tok::kw___attribute) ||
932 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000933 nextToken();
934 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000935 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000936 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000937 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000938 // The actual identifier can be a nested name specifier, and in macros
939 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000940 while (FormatTok->Tok.is(tok::identifier) ||
941 FormatTok->Tok.is(tok::coloncolon) ||
942 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000943 nextToken();
944
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000945 // Note that parsing away template declarations here leads to incorrectly
946 // accepting function declarations as record declarations.
947 // In general, we cannot solve this problem. Consider:
948 // class A<int> B() {}
949 // which can be a function definition or a class definition when B() is a
950 // macro. If we find enough real-world cases where this is a problem, we
951 // can parse for the 'template' keyword in the beginning of the statement,
952 // and thus rule out the record production in case there is no template
953 // (this would still leave us with an ambiguity between template function
954 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000955 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
956 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
957 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000958 return;
959 nextToken();
960 }
961 }
962 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000963 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000964 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
965 addUnwrappedLine();
966
Nico Weber27268772013-06-26 00:30:14 +0000967 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000968 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000969 // We fall through to parsing a structural element afterwards, so
970 // class A {} n, m;
971 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000972}
973
Nico Weber1abe6ea2013-01-09 21:15:03 +0000974void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000975 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000976 do
977 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000978 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000979 nextToken(); // Skip '>'.
980}
981
982void UnwrappedLineParser::parseObjCUntilAtEnd() {
983 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000984 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000985 nextToken();
986 addUnwrappedLine();
987 break;
988 }
989 parseStructuralElement();
990 } while (!eof());
991}
992
Nico Weber50767d82013-01-09 23:25:37 +0000993void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000994 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000995 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000996
997 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000998 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000999 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001000 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001001 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001002 // Skip category, if present.
1003 parseParens();
1004
Manuel Klimek96e888b2013-05-28 11:55:06 +00001005 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001006 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001007
1008 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001009 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001010 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001011
1012 // With instance variables, this puts '}' on its own line. Without instance
1013 // variables, this ends the @interface line.
1014 addUnwrappedLine();
1015
Nico Weber1abe6ea2013-01-09 21:15:03 +00001016 parseObjCUntilAtEnd();
1017}
Nico Weber27d13672013-01-09 20:25:35 +00001018
Nico Weber1abe6ea2013-01-09 21:15:03 +00001019void UnwrappedLineParser::parseObjCProtocol() {
1020 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001021 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001022
Manuel Klimek96e888b2013-05-28 11:55:06 +00001023 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001024 parseObjCProtocolList();
1025
1026 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001027 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001028 nextToken();
1029 return addUnwrappedLine();
1030 }
1031
1032 addUnwrappedLine();
1033 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001034}
1035
Daniel Jasperbac016b2012-12-03 18:12:45 +00001036void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001037 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001038 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001039 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001040 llvm::dbgs() << "Line(" << Line->Level << ")"
1041 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001042 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1043 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001044 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001045 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001046 }
1047 llvm::dbgs() << "\n";
1048 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001049 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001050 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001051 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001052 for (std::vector<UnwrappedLine>::iterator
1053 I = PreprocessorDirectives.begin(),
1054 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001055 I != E; ++I) {
1056 CurrentLines->push_back(*I);
1057 }
1058 PreprocessorDirectives.clear();
1059 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001060}
1061
Manuel Klimek96e888b2013-05-28 11:55:06 +00001062bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001063
Manuel Klimek86721d22013-01-22 16:31:55 +00001064void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1065 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001066 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001067 I = CommentsBeforeNextToken.begin(),
1068 E = CommentsBeforeNextToken.end();
1069 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001070 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001071 addUnwrappedLine();
1072 }
1073 pushToken(*I);
1074 }
1075 if (NewlineBeforeNext && JustComments) {
1076 addUnwrappedLine();
1077 }
1078 CommentsBeforeNextToken.clear();
1079}
1080
Daniel Jasperbac016b2012-12-03 18:12:45 +00001081void UnwrappedLineParser::nextToken() {
1082 if (eof())
1083 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001084 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001085 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001086 readToken();
1087}
1088
1089void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001090 bool CommentsInCurrentLine = true;
1091 do {
1092 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001093 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1094 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001095 // If there is an unfinished unwrapped line, we flush the preprocessor
1096 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001097 bool SwitchToPreprocessorLines =
1098 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001099 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001100 // Comments stored before the preprocessor directive need to be output
1101 // before the preprocessor directive, at the same level as the
1102 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001103 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001104 parsePPDirective();
1105 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001106
1107 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1108 !Line->InPPDirective) {
1109 continue;
1110 }
1111
Manuel Klimek96e888b2013-05-28 11:55:06 +00001112 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001113 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001114 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001115 CommentsInCurrentLine = false;
1116 }
1117 if (CommentsInCurrentLine) {
1118 pushToken(FormatTok);
1119 } else {
1120 CommentsBeforeNextToken.push_back(FormatTok);
1121 }
1122 } while (!eof());
1123}
1124
Manuel Klimek96e888b2013-05-28 11:55:06 +00001125void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001126 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001127 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001128 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001129 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001130 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001131}
1132
Daniel Jaspercd162382013-01-07 13:26:07 +00001133} // end namespace format
1134} // end namespace clang