blob: 170c89260917dd271184eb500b0fb10a7310eae2 [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),
186 Callback(Callback), AllTokens(Tokens) {
187 LBraces.resize(Tokens.size(), BS_Unknown);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000188}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000189
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000190bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000191 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek80829bd2013-05-23 09:41:43 +0000192 IndexedTokenSource TokenSource(AllTokens);
193 Tokens = &TokenSource;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000194 readToken();
Manuel Klimek67d080d2013-04-12 14:13:36 +0000195 parseFile();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000196 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000197 I != E; ++I) {
198 Callback.consumeUnwrappedLine(*I);
199 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000200
201 // Create line with eof token.
202 pushToken(FormatTok);
203 Callback.consumeUnwrappedLine(*Line);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000204 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000205}
206
Manuel Klimek67d080d2013-04-12 14:13:36 +0000207void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000208 ScopedDeclarationState DeclarationState(
209 *Line, DeclarationScopeStack,
210 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000211 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000212 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000213 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000214 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000215}
216
Manuel Klimek67d080d2013-04-12 14:13:36 +0000217void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000218 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000219 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000220 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000221 nextToken();
222 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000223 break;
224 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000225 // FIXME: Add parameter whether this can happen - if this happens, we must
226 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000227 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000228 addUnwrappedLine();
229 break;
230 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000231 if (HasOpeningBrace)
232 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000233 StructuralError = true;
234 nextToken();
235 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000236 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000237 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000238 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000239 break;
240 }
241 } while (!eof());
242}
243
Manuel Klimek80829bd2013-05-23 09:41:43 +0000244void UnwrappedLineParser::calculateBraceTypes() {
245 // We'll parse forward through the tokens until we hit
246 // a closing brace or eof - note that getNextToken() will
247 // parse macros, so this will magically work inside macro
248 // definitions, too.
249 unsigned StoredPosition = Tokens->getPosition();
250 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000251 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000252 // Keep a stack of positions of lbrace tokens. We will
253 // update information about whether an lbrace starts a
254 // braced init list or a different block during the loop.
255 SmallVector<unsigned, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000256 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000257 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000258 // Get next none-comment token.
259 FormatToken *NextTok;
260 do {
261 NextTok = Tokens->getNextToken();
262 } 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:
266 LBraceStack.push_back(Position);
267 break;
268 case tok::r_brace:
269 if (!LBraceStack.empty()) {
270 if (LBraces[LBraceStack.back()] == BS_Unknown) {
271 // 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,
280 tok::l_brace, tok::colon))
Manuel Klimek80829bd2013-05-23 09:41:43 +0000281 LBraces[LBraceStack.back()] = BS_BracedInit;
282 else
283 LBraces[LBraceStack.back()] = BS_Block;
284 }
285 LBraceStack.pop_back();
286 }
287 break;
288 case tok::semi:
289 case tok::kw_if:
290 case tok::kw_while:
291 case tok::kw_for:
292 case tok::kw_switch:
293 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000294 if (!LBraceStack.empty())
Manuel Klimek80829bd2013-05-23 09:41:43 +0000295 LBraces[LBraceStack.back()] = BS_Block;
296 break;
297 default:
298 break;
299 }
300 Tok = NextTok;
301 ++Position;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000302 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000303 // Assume other blocks for all unclosed opening braces.
304 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
305 if (LBraces[LBraceStack[i]] == BS_Unknown)
306 LBraces[LBraceStack[i]] = BS_Block;
307 }
308 FormatTok = Tokens->setPosition(StoredPosition);
309}
310
Manuel Klimek67d080d2013-04-12 14:13:36 +0000311void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
Nico Weberd74fcdb2013-02-10 20:35:35 +0000312 unsigned AddLevels) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000313 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000314 nextToken();
315
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000316 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000317
Manuel Klimek70b03f42013-01-23 09:32:48 +0000318 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
319 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000320 Line->Level += AddLevels;
Nico Weber27268772013-06-26 00:30:14 +0000321 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000322
Manuel Klimek96e888b2013-05-28 11:55:06 +0000323 if (!FormatTok->Tok.is(tok::r_brace)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000324 Line->Level -= AddLevels;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000325 StructuralError = true;
326 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000327 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000328
Daniel Jasperf9955d32013-03-20 12:37:50 +0000329 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000330 Line->Level -= AddLevels;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000331}
332
333void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000334 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000335 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000336 nextToken();
337
Manuel Klimek96e888b2013-05-28 11:55:06 +0000338 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000339 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000340 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000341 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000342
Manuel Klimek96e888b2013-05-28 11:55:06 +0000343 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000344 case tok::pp_define:
345 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000346 return;
347 case tok::pp_if:
348 parsePPIf();
349 break;
350 case tok::pp_ifdef:
351 case tok::pp_ifndef:
352 parsePPIfdef();
353 break;
354 case tok::pp_else:
355 parsePPElse();
356 break;
357 case tok::pp_elif:
358 parsePPElIf();
359 break;
360 case tok::pp_endif:
361 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000362 break;
363 default:
364 parsePPUnknown();
365 break;
366 }
367}
368
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000369void UnwrappedLineParser::pushPPConditional() {
370 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
371 PPStack.push_back(PP_Unreachable);
372 else
373 PPStack.push_back(PP_Conditional);
374}
375
376void UnwrappedLineParser::parsePPIf() {
377 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000378 if ((FormatTok->Tok.isLiteral() &&
379 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000380 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000381 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000382 PPStack.push_back(PP_Unreachable);
383 } else {
384 pushPPConditional();
385 }
386 parsePPUnknown();
387}
388
389void UnwrappedLineParser::parsePPIfdef() {
390 pushPPConditional();
391 parsePPUnknown();
392}
393
394void UnwrappedLineParser::parsePPElse() {
395 if (!PPStack.empty())
396 PPStack.pop_back();
397 pushPPConditional();
398 parsePPUnknown();
399}
400
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000401void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000402
403void UnwrappedLineParser::parsePPEndIf() {
404 if (!PPStack.empty())
405 PPStack.pop_back();
406 parsePPUnknown();
407}
408
Manuel Klimekd4397b92013-01-04 23:34:14 +0000409void UnwrappedLineParser::parsePPDefine() {
410 nextToken();
411
Manuel Klimek96e888b2013-05-28 11:55:06 +0000412 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000413 parsePPUnknown();
414 return;
415 }
416 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000417 if (FormatTok->Tok.getKind() == tok::l_paren &&
418 FormatTok->WhitespaceRange.getBegin() ==
419 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000420 parseParens();
421 }
422 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000423 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000424
425 // Errors during a preprocessor directive can only affect the layout of the
426 // preprocessor directive, and thus we ignore them. An alternative approach
427 // would be to use the same approach we use on the file level (no
428 // re-indentation if there was a structural error) within the macro
429 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000430 parseFile();
431}
432
433void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000434 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000435 nextToken();
436 } while (!eof());
437 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000438}
439
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000440// Here we blacklist certain tokens that are not usually the first token in an
441// unwrapped line. This is used in attempt to distinguish macro calls without
442// trailing semicolons from other constructs split to several lines.
443bool tokenCanStartNewLine(clang::Token Tok) {
444 // Semicolon can be a null-statement, l_square can be a start of a macro or
445 // a C++11 attribute, but this doesn't seem to be common.
446 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
447 Tok.isNot(tok::l_square) &&
448 // Tokens that can only be used as binary operators and a part of
449 // overloaded operator names.
450 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
451 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
452 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
453 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
454 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
455 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
456 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
457 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
458 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
459 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
460 Tok.isNot(tok::lesslessequal) &&
461 // Colon is used in labels, base class lists, initializer lists,
462 // range-based for loops, ternary operator, but should never be the
463 // first token in an unwrapped line.
464 Tok.isNot(tok::colon);
465}
466
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000467void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000468 assert(!FormatTok->Tok.is(tok::l_brace));
469 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000470 case tok::at:
471 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000472 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000473 parseBracedList();
474 break;
475 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000476 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000477 case tok::objc_public:
478 case tok::objc_protected:
479 case tok::objc_package:
480 case tok::objc_private:
481 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000482 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000483 case tok::objc_implementation:
484 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000485 case tok::objc_protocol:
486 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000487 case tok::objc_end:
488 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000489 case tok::objc_optional:
490 case tok::objc_required:
491 nextToken();
492 addUnwrappedLine();
493 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000494 default:
495 break;
496 }
497 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000498 case tok::kw_namespace:
499 parseNamespace();
500 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000501 case tok::kw_inline:
502 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000503 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000504 parseNamespace();
505 return;
506 }
507 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000508 case tok::kw_public:
509 case tok::kw_protected:
510 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000511 parseAccessSpecifier();
512 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000513 case tok::kw_if:
514 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000515 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000516 case tok::kw_for:
517 case tok::kw_while:
518 parseForOrWhileLoop();
519 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000520 case tok::kw_do:
521 parseDoWhile();
522 return;
523 case tok::kw_switch:
524 parseSwitch();
525 return;
526 case tok::kw_default:
527 nextToken();
528 parseLabel();
529 return;
530 case tok::kw_case:
531 parseCaseLabel();
532 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000533 case tok::kw_return:
534 parseReturn();
535 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000536 case tok::kw_extern:
537 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000538 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000539 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000540 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000541 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000542 addUnwrappedLine();
543 return;
544 }
545 }
546 // In all other cases, parse the declaration.
547 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000548 default:
549 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000550 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000551 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000552 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000553 case tok::at:
554 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000555 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000556 parseBracedList();
557 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000558 case tok::kw_enum:
559 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000560 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000561 case tok::kw_struct:
562 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000563 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000564 parseRecord();
565 // A record declaration or definition is always the start of a structural
566 // element.
567 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000568 case tok::semi:
569 nextToken();
570 addUnwrappedLine();
571 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000572 case tok::r_brace:
573 addUnwrappedLine();
574 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000575 case tok::l_paren:
576 parseParens();
577 break;
578 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000579 if (!tryToParseBracedList()) {
580 // A block outside of parentheses must be the last part of a
581 // structural element.
582 // FIXME: Figure out cases where this is not true, and add projections
583 // for them (the one we know is missing are lambdas).
584 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
585 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
586 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000587 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000588 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000589 return;
590 }
591 // Otherwise this was a braced init list, and the structural
592 // element continues.
593 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000594 case tok::identifier: {
595 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000596 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000597 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000598 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000599 parseLabel();
600 return;
601 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000602 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000603 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000604 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000605 if (FormatTok->HasUnescapedNewline &&
606 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000607 addUnwrappedLine();
608 return;
609 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000610 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
611 Text == Text.upper()) {
612 // Recognize free-standing macros like Q_OBJECT.
613 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000614 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000615 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000616 }
617 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000618 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000619 case tok::equal:
620 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000621 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000622 parseBracedList();
623 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000624 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000625 default:
626 nextToken();
627 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000628 }
629 } while (!eof());
630}
631
Manuel Klimek80829bd2013-05-23 09:41:43 +0000632bool UnwrappedLineParser::tryToParseBracedList() {
633 if (LBraces[Tokens->getPosition()] == BS_Unknown)
634 calculateBraceTypes();
635 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
636 if (LBraces[Tokens->getPosition()] == BS_Block)
637 return false;
638 parseBracedList();
639 return true;
640}
641
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000642void UnwrappedLineParser::parseBracedList() {
643 nextToken();
644
Manuel Klimek423dd932013-04-10 09:52:05 +0000645 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
646 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000647 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000648 // FIXME: When we start to support lambdas, we'll want to parse them away
649 // here, otherwise our bail-out scenarios below break. The better solution
650 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000651 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000652 case tok::l_brace:
653 parseBracedList();
654 break;
655 case tok::r_brace:
656 nextToken();
657 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000658 case tok::semi:
659 // Probably a missing closing brace. Bail out.
660 return;
661 case tok::comma:
662 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000663 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000664 default:
665 nextToken();
666 break;
667 }
668 } while (!eof());
669}
670
Manuel Klimekc44ee892013-01-21 10:07:49 +0000671void UnwrappedLineParser::parseReturn() {
672 nextToken();
673
674 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000675 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000676 case tok::l_brace:
677 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000678 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000679 // Assume missing ';'.
680 addUnwrappedLine();
681 return;
682 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000683 break;
684 case tok::l_paren:
685 parseParens();
686 break;
687 case tok::r_brace:
688 // Assume missing ';'.
689 addUnwrappedLine();
690 return;
691 case tok::semi:
692 nextToken();
693 addUnwrappedLine();
694 return;
695 default:
696 nextToken();
697 break;
698 }
699 } while (!eof());
700}
701
Daniel Jasperbac016b2012-12-03 18:12:45 +0000702void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000703 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000704 nextToken();
705 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000706 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000707 case tok::l_paren:
708 parseParens();
709 break;
710 case tok::r_paren:
711 nextToken();
712 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000713 case tok::r_brace:
714 // A "}" inside parenthesis is an error if there wasn't a matching "{".
715 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000716 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000717 if (!tryToParseBracedList()) {
718 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000719 {
720 ScopedLineState LineState(*this);
721 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000722 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000723 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000724 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000725 Line->Level -= 1;
726 }
727 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000728 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000729 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000730 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000731 case tok::at:
732 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000733 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000734 parseBracedList();
735 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000736 default:
737 nextToken();
738 break;
739 }
740 } while (!eof());
741}
742
743void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000744 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000745 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000746 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000747 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000748 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000749 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000750 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000751 NeedsUnwrappedLine = true;
752 } else {
753 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000754 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000755 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000756 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000757 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000758 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000759 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000760 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000761 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000762 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000763 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000764 parseIfThenElse();
765 } else {
766 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000767 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000768 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000769 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000770 }
771 } else if (NeedsUnwrappedLine) {
772 addUnwrappedLine();
773 }
774}
775
Alexander Kornienko15757312012-12-06 18:03:27 +0000776void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000777 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000778 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000779 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000780 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000781 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000782 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
783 addUnwrappedLine();
784
Nico Weber27268772013-06-26 00:30:14 +0000785 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000786 // Munch the semicolon after a namespace. This is more common than one would
787 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000788 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000789 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000790 addUnwrappedLine();
791 }
792 // FIXME: Add error handling.
793}
794
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000795void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000796 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000797 "'for' or 'while' expected");
798 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000799 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000800 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000801 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000802 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000803 addUnwrappedLine();
804 } else {
805 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000806 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000807 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000808 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000809 }
810}
811
Daniel Jasperbac016b2012-12-03 18:12:45 +0000812void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000813 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000814 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000815 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000816 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000817 } else {
818 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000819 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000820 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000821 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000822 }
823
Alexander Kornienko393b0082012-12-04 15:40:36 +0000824 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000825 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000826 addUnwrappedLine();
827 return;
828 }
829
Daniel Jasperbac016b2012-12-03 18:12:45 +0000830 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000831 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000832}
833
834void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000835 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000836 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000837 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000838 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000839 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000840 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000841 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000842 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000843 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000844 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000845 }
846 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000847 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000848}
849
850void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000851 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000852 // FIXME: fix handling of complex expressions here.
853 do {
854 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000855 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000856 parseLabel();
857}
858
859void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000860 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000861 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000862 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000863 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000864 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000865 parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000866 addUnwrappedLine();
867 } else {
868 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000869 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000870 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000871 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000872 }
873}
874
875void UnwrappedLineParser::parseAccessSpecifier() {
876 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000877 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000878 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000879 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000880 addUnwrappedLine();
881}
882
883void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000884 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000885 if (FormatTok->Tok.is(tok::identifier) ||
886 FormatTok->Tok.is(tok::kw___attribute) ||
887 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000888 nextToken();
889 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000890 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000891 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000892 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000893 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000894 nextToken();
895 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000896 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000897 nextToken();
898 addUnwrappedLine();
899 ++Line->Level;
900 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000901 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000902 case tok::l_paren:
903 parseParens();
904 break;
905 case tok::r_brace:
906 addUnwrappedLine();
907 nextToken();
908 --Line->Level;
909 return;
910 case tok::comma:
911 nextToken();
912 addUnwrappedLine();
913 break;
914 default:
915 nextToken();
916 break;
917 }
918 } while (!eof());
919 }
920 // We fall through to parsing a structural element afterwards, so that in
921 // enum A {} n, m;
922 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000923}
924
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000925void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000926 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000927 if (FormatTok->Tok.is(tok::identifier) ||
928 FormatTok->Tok.is(tok::kw___attribute) ||
929 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000930 nextToken();
931 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000932 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000933 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000934 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000935 // The actual identifier can be a nested name specifier, and in macros
936 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000937 while (FormatTok->Tok.is(tok::identifier) ||
938 FormatTok->Tok.is(tok::coloncolon) ||
939 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000940 nextToken();
941
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000942 // Note that parsing away template declarations here leads to incorrectly
943 // accepting function declarations as record declarations.
944 // In general, we cannot solve this problem. Consider:
945 // class A<int> B() {}
946 // which can be a function definition or a class definition when B() is a
947 // macro. If we find enough real-world cases where this is a problem, we
948 // can parse for the 'template' keyword in the beginning of the statement,
949 // and thus rule out the record production in case there is no template
950 // (this would still leave us with an ambiguity between template function
951 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000952 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
953 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
954 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000955 return;
956 nextToken();
957 }
958 }
959 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000960 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000961 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
962 addUnwrappedLine();
963
Nico Weber27268772013-06-26 00:30:14 +0000964 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000965 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000966 // We fall through to parsing a structural element afterwards, so
967 // class A {} n, m;
968 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000969}
970
Nico Weber1abe6ea2013-01-09 21:15:03 +0000971void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000972 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000973 do
974 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000975 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000976 nextToken(); // Skip '>'.
977}
978
979void UnwrappedLineParser::parseObjCUntilAtEnd() {
980 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000981 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000982 nextToken();
983 addUnwrappedLine();
984 break;
985 }
986 parseStructuralElement();
987 } while (!eof());
988}
989
Nico Weber50767d82013-01-09 23:25:37 +0000990void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000991 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000992 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000993
994 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000995 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000996 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000997 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000998 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000999 // Skip category, if present.
1000 parseParens();
1001
Manuel Klimek96e888b2013-05-28 11:55:06 +00001002 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001003 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001004
1005 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001006 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001007 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001008
1009 // With instance variables, this puts '}' on its own line. Without instance
1010 // variables, this ends the @interface line.
1011 addUnwrappedLine();
1012
Nico Weber1abe6ea2013-01-09 21:15:03 +00001013 parseObjCUntilAtEnd();
1014}
Nico Weber27d13672013-01-09 20:25:35 +00001015
Nico Weber1abe6ea2013-01-09 21:15:03 +00001016void UnwrappedLineParser::parseObjCProtocol() {
1017 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001018 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001019
Manuel Klimek96e888b2013-05-28 11:55:06 +00001020 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001021 parseObjCProtocolList();
1022
1023 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001024 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001025 nextToken();
1026 return addUnwrappedLine();
1027 }
1028
1029 addUnwrappedLine();
1030 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001031}
1032
Daniel Jasperbac016b2012-12-03 18:12:45 +00001033void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001034 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001035 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001036 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001037 llvm::dbgs() << "Line(" << Line->Level << ")"
1038 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001039 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1040 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001041 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001042 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001043 }
1044 llvm::dbgs() << "\n";
1045 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001046 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001047 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001048 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001049 for (std::vector<UnwrappedLine>::iterator
1050 I = PreprocessorDirectives.begin(),
1051 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001052 I != E; ++I) {
1053 CurrentLines->push_back(*I);
1054 }
1055 PreprocessorDirectives.clear();
1056 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001057}
1058
Manuel Klimek96e888b2013-05-28 11:55:06 +00001059bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001060
Manuel Klimek86721d22013-01-22 16:31:55 +00001061void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1062 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001063 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001064 I = CommentsBeforeNextToken.begin(),
1065 E = CommentsBeforeNextToken.end();
1066 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001067 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001068 addUnwrappedLine();
1069 }
1070 pushToken(*I);
1071 }
1072 if (NewlineBeforeNext && JustComments) {
1073 addUnwrappedLine();
1074 }
1075 CommentsBeforeNextToken.clear();
1076}
1077
Daniel Jasperbac016b2012-12-03 18:12:45 +00001078void UnwrappedLineParser::nextToken() {
1079 if (eof())
1080 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001081 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001082 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001083 readToken();
1084}
1085
1086void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001087 bool CommentsInCurrentLine = true;
1088 do {
1089 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001090 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1091 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001092 // If there is an unfinished unwrapped line, we flush the preprocessor
1093 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001094 bool SwitchToPreprocessorLines =
1095 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001096 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001097 // Comments stored before the preprocessor directive need to be output
1098 // before the preprocessor directive, at the same level as the
1099 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001100 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001101 parsePPDirective();
1102 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001103
1104 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1105 !Line->InPPDirective) {
1106 continue;
1107 }
1108
Manuel Klimek96e888b2013-05-28 11:55:06 +00001109 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001110 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001111 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001112 CommentsInCurrentLine = false;
1113 }
1114 if (CommentsInCurrentLine) {
1115 pushToken(FormatTok);
1116 } else {
1117 CommentsBeforeNextToken.push_back(FormatTok);
1118 }
1119 } while (!eof());
1120}
1121
Manuel Klimek96e888b2013-05-28 11:55:06 +00001122void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001123 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001124 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001125 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001126 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001127 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001128}
1129
Daniel Jaspercd162382013-01-07 13:26:07 +00001130} // end namespace format
1131} // end namespace clang