blob: 98a5a8ae075cc8c2b3e6fbf8dbb230bd8a293476 [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;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000260 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000261 do {
262 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000263 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000264 } while (NextTok->is(tok::comment));
265
Manuel Klimek96e888b2013-05-28 11:55:06 +0000266 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000267 case tok::l_brace:
268 LBraceStack.push_back(Position);
269 break;
270 case tok::r_brace:
271 if (!LBraceStack.empty()) {
272 if (LBraces[LBraceStack.back()] == BS_Unknown) {
273 // If there is a comma, semicolon or right paren after the closing
274 // brace, we assume this is a braced initializer list.
275
276 // FIXME: Note that this currently works only because we do not
277 // use the brace information while inside a braced init list.
278 // Thus, if the parent is a braced init list, we consider all
279 // brace blocks inside it braced init list. That works good enough
280 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jaspereb483662013-05-31 10:09:55 +0000281 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
282 tok::l_brace, tok::colon))
Manuel Klimek80829bd2013-05-23 09:41:43 +0000283 LBraces[LBraceStack.back()] = BS_BracedInit;
284 else
285 LBraces[LBraceStack.back()] = BS_Block;
286 }
287 LBraceStack.pop_back();
288 }
289 break;
290 case tok::semi:
291 case tok::kw_if:
292 case tok::kw_while:
293 case tok::kw_for:
294 case tok::kw_switch:
295 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000296 if (!LBraceStack.empty())
Manuel Klimek80829bd2013-05-23 09:41:43 +0000297 LBraces[LBraceStack.back()] = BS_Block;
298 break;
299 default:
300 break;
301 }
302 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000303 Position += ReadTokens;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000304 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000305 // Assume other blocks for all unclosed opening braces.
306 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
307 if (LBraces[LBraceStack[i]] == BS_Unknown)
308 LBraces[LBraceStack[i]] = BS_Block;
309 }
310 FormatTok = Tokens->setPosition(StoredPosition);
311}
312
Manuel Klimek67d080d2013-04-12 14:13:36 +0000313void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
Nico Weberd74fcdb2013-02-10 20:35:35 +0000314 unsigned AddLevels) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000315 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000316 nextToken();
317
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000318 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000319
Manuel Klimek70b03f42013-01-23 09:32:48 +0000320 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
321 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000322 Line->Level += AddLevels;
Nico Weber27268772013-06-26 00:30:14 +0000323 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000324
Manuel Klimek96e888b2013-05-28 11:55:06 +0000325 if (!FormatTok->Tok.is(tok::r_brace)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000326 Line->Level -= AddLevels;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000327 StructuralError = true;
328 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000329 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000330
Daniel Jasperf9955d32013-03-20 12:37:50 +0000331 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000332 Line->Level -= AddLevels;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000333}
334
335void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000336 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000337 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000338 nextToken();
339
Manuel Klimek96e888b2013-05-28 11:55:06 +0000340 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000341 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000342 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000343 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000344
Manuel Klimek96e888b2013-05-28 11:55:06 +0000345 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000346 case tok::pp_define:
347 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000348 return;
349 case tok::pp_if:
350 parsePPIf();
351 break;
352 case tok::pp_ifdef:
353 case tok::pp_ifndef:
354 parsePPIfdef();
355 break;
356 case tok::pp_else:
357 parsePPElse();
358 break;
359 case tok::pp_elif:
360 parsePPElIf();
361 break;
362 case tok::pp_endif:
363 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000364 break;
365 default:
366 parsePPUnknown();
367 break;
368 }
369}
370
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000371void UnwrappedLineParser::pushPPConditional() {
372 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
373 PPStack.push_back(PP_Unreachable);
374 else
375 PPStack.push_back(PP_Conditional);
376}
377
378void UnwrappedLineParser::parsePPIf() {
379 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000380 if ((FormatTok->Tok.isLiteral() &&
381 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000382 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000383 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000384 PPStack.push_back(PP_Unreachable);
385 } else {
386 pushPPConditional();
387 }
388 parsePPUnknown();
389}
390
391void UnwrappedLineParser::parsePPIfdef() {
392 pushPPConditional();
393 parsePPUnknown();
394}
395
396void UnwrappedLineParser::parsePPElse() {
397 if (!PPStack.empty())
398 PPStack.pop_back();
399 pushPPConditional();
400 parsePPUnknown();
401}
402
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000403void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000404
405void UnwrappedLineParser::parsePPEndIf() {
406 if (!PPStack.empty())
407 PPStack.pop_back();
408 parsePPUnknown();
409}
410
Manuel Klimekd4397b92013-01-04 23:34:14 +0000411void UnwrappedLineParser::parsePPDefine() {
412 nextToken();
413
Manuel Klimek96e888b2013-05-28 11:55:06 +0000414 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000415 parsePPUnknown();
416 return;
417 }
418 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000419 if (FormatTok->Tok.getKind() == tok::l_paren &&
420 FormatTok->WhitespaceRange.getBegin() ==
421 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000422 parseParens();
423 }
424 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000425 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000426
427 // Errors during a preprocessor directive can only affect the layout of the
428 // preprocessor directive, and thus we ignore them. An alternative approach
429 // would be to use the same approach we use on the file level (no
430 // re-indentation if there was a structural error) within the macro
431 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000432 parseFile();
433}
434
435void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000436 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000437 nextToken();
438 } while (!eof());
439 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000440}
441
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000442// Here we blacklist certain tokens that are not usually the first token in an
443// unwrapped line. This is used in attempt to distinguish macro calls without
444// trailing semicolons from other constructs split to several lines.
445bool tokenCanStartNewLine(clang::Token Tok) {
446 // Semicolon can be a null-statement, l_square can be a start of a macro or
447 // a C++11 attribute, but this doesn't seem to be common.
448 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
449 Tok.isNot(tok::l_square) &&
450 // Tokens that can only be used as binary operators and a part of
451 // overloaded operator names.
452 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
453 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
454 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
455 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
456 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
457 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
458 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
459 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
460 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
461 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
462 Tok.isNot(tok::lesslessequal) &&
463 // Colon is used in labels, base class lists, initializer lists,
464 // range-based for loops, ternary operator, but should never be the
465 // first token in an unwrapped line.
466 Tok.isNot(tok::colon);
467}
468
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000469void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000470 assert(!FormatTok->Tok.is(tok::l_brace));
471 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000472 case tok::at:
473 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000474 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000475 parseBracedList();
476 break;
477 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000478 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000479 case tok::objc_public:
480 case tok::objc_protected:
481 case tok::objc_package:
482 case tok::objc_private:
483 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000484 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000485 case tok::objc_implementation:
486 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000487 case tok::objc_protocol:
488 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000489 case tok::objc_end:
490 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000491 case tok::objc_optional:
492 case tok::objc_required:
493 nextToken();
494 addUnwrappedLine();
495 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000496 default:
497 break;
498 }
499 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000500 case tok::kw_namespace:
501 parseNamespace();
502 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000503 case tok::kw_inline:
504 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000505 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000506 parseNamespace();
507 return;
508 }
509 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000510 case tok::kw_public:
511 case tok::kw_protected:
512 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000513 parseAccessSpecifier();
514 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000515 case tok::kw_if:
516 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000517 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000518 case tok::kw_for:
519 case tok::kw_while:
520 parseForOrWhileLoop();
521 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000522 case tok::kw_do:
523 parseDoWhile();
524 return;
525 case tok::kw_switch:
526 parseSwitch();
527 return;
528 case tok::kw_default:
529 nextToken();
530 parseLabel();
531 return;
532 case tok::kw_case:
533 parseCaseLabel();
534 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000535 case tok::kw_return:
536 parseReturn();
537 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000538 case tok::kw_extern:
539 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000540 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000541 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000542 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000543 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000544 addUnwrappedLine();
545 return;
546 }
547 }
548 // In all other cases, parse the declaration.
549 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000550 default:
551 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000552 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000553 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000554 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000555 case tok::at:
556 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000557 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000558 parseBracedList();
559 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000560 case tok::kw_enum:
561 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000562 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000563 case tok::kw_struct:
564 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000565 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000566 parseRecord();
567 // A record declaration or definition is always the start of a structural
568 // element.
569 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000570 case tok::semi:
571 nextToken();
572 addUnwrappedLine();
573 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000574 case tok::r_brace:
575 addUnwrappedLine();
576 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000577 case tok::l_paren:
578 parseParens();
579 break;
580 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000581 if (!tryToParseBracedList()) {
582 // A block outside of parentheses must be the last part of a
583 // structural element.
584 // FIXME: Figure out cases where this is not true, and add projections
585 // for them (the one we know is missing are lambdas).
586 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
587 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
588 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000589 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000590 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000591 return;
592 }
593 // Otherwise this was a braced init list, and the structural
594 // element continues.
595 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000596 case tok::identifier: {
597 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000598 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000599 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000600 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000601 parseLabel();
602 return;
603 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000604 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000605 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000606 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000607 if (FormatTok->HasUnescapedNewline &&
608 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000609 addUnwrappedLine();
610 return;
611 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000612 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
613 Text == Text.upper()) {
614 // Recognize free-standing macros like Q_OBJECT.
615 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000616 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000617 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000618 }
619 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000620 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000621 case tok::equal:
622 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000623 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000624 parseBracedList();
625 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000626 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000627 default:
628 nextToken();
629 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000630 }
631 } while (!eof());
632}
633
Manuel Klimek80829bd2013-05-23 09:41:43 +0000634bool UnwrappedLineParser::tryToParseBracedList() {
635 if (LBraces[Tokens->getPosition()] == BS_Unknown)
636 calculateBraceTypes();
637 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
638 if (LBraces[Tokens->getPosition()] == BS_Block)
639 return false;
640 parseBracedList();
641 return true;
642}
643
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000644void UnwrappedLineParser::parseBracedList() {
645 nextToken();
646
Manuel Klimek423dd932013-04-10 09:52:05 +0000647 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
648 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000649 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000650 // FIXME: When we start to support lambdas, we'll want to parse them away
651 // here, otherwise our bail-out scenarios below break. The better solution
652 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000653 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000654 case tok::l_brace:
655 parseBracedList();
656 break;
657 case tok::r_brace:
658 nextToken();
659 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000660 case tok::semi:
661 // Probably a missing closing brace. Bail out.
662 return;
663 case tok::comma:
664 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000665 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000666 default:
667 nextToken();
668 break;
669 }
670 } while (!eof());
671}
672
Manuel Klimekc44ee892013-01-21 10:07:49 +0000673void UnwrappedLineParser::parseReturn() {
674 nextToken();
675
676 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000677 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000678 case tok::l_brace:
679 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000680 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000681 // Assume missing ';'.
682 addUnwrappedLine();
683 return;
684 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000685 break;
686 case tok::l_paren:
687 parseParens();
688 break;
689 case tok::r_brace:
690 // Assume missing ';'.
691 addUnwrappedLine();
692 return;
693 case tok::semi:
694 nextToken();
695 addUnwrappedLine();
696 return;
697 default:
698 nextToken();
699 break;
700 }
701 } while (!eof());
702}
703
Daniel Jasperbac016b2012-12-03 18:12:45 +0000704void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000705 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000706 nextToken();
707 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000708 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000709 case tok::l_paren:
710 parseParens();
711 break;
712 case tok::r_paren:
713 nextToken();
714 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000715 case tok::r_brace:
716 // A "}" inside parenthesis is an error if there wasn't a matching "{".
717 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000718 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000719 if (!tryToParseBracedList()) {
720 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000721 {
722 ScopedLineState LineState(*this);
723 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000724 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000725 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000726 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000727 Line->Level -= 1;
728 }
729 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000730 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000731 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000732 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000733 case tok::at:
734 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000735 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000736 parseBracedList();
737 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000738 default:
739 nextToken();
740 break;
741 }
742 } while (!eof());
743}
744
745void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000746 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000747 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000748 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000749 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000750 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000751 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000752 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000753 NeedsUnwrappedLine = true;
754 } else {
755 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000756 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000757 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000758 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000759 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000760 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000761 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000762 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000763 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000764 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000765 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000766 parseIfThenElse();
767 } else {
768 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000769 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000770 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000771 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000772 }
773 } else if (NeedsUnwrappedLine) {
774 addUnwrappedLine();
775 }
776}
777
Alexander Kornienko15757312012-12-06 18:03:27 +0000778void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000779 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000780 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000781 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000782 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000783 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000784 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
785 addUnwrappedLine();
786
Nico Weber27268772013-06-26 00:30:14 +0000787 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000788 // Munch the semicolon after a namespace. This is more common than one would
789 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000790 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000791 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000792 addUnwrappedLine();
793 }
794 // FIXME: Add error handling.
795}
796
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000797void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000798 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000799 "'for' or 'while' expected");
800 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000801 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000802 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000803 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000804 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000805 addUnwrappedLine();
806 } else {
807 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000808 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000809 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000810 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000811 }
812}
813
Daniel Jasperbac016b2012-12-03 18:12:45 +0000814void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000815 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000816 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000817 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000818 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000819 } else {
820 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000821 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000822 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000823 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000824 }
825
Alexander Kornienko393b0082012-12-04 15:40:36 +0000826 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000827 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000828 addUnwrappedLine();
829 return;
830 }
831
Daniel Jasperbac016b2012-12-03 18:12:45 +0000832 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000833 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000834}
835
836void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000837 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000838 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000839 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000840 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000841 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000842 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000843 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000844 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000845 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000846 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000847 }
848 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000849 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000850}
851
852void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000853 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000854 // FIXME: fix handling of complex expressions here.
855 do {
856 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000857 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000858 parseLabel();
859}
860
861void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000862 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000864 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000865 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000866 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000867 parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000868 addUnwrappedLine();
869 } else {
870 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000871 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000872 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000873 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000874 }
875}
876
877void UnwrappedLineParser::parseAccessSpecifier() {
878 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000879 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000880 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000881 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000882 addUnwrappedLine();
883}
884
885void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000886 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000887 if (FormatTok->Tok.is(tok::identifier) ||
888 FormatTok->Tok.is(tok::kw___attribute) ||
889 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000890 nextToken();
891 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000892 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000893 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000894 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000895 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000896 nextToken();
897 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000898 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000899 nextToken();
900 addUnwrappedLine();
901 ++Line->Level;
902 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000903 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000904 case tok::l_paren:
905 parseParens();
906 break;
907 case tok::r_brace:
908 addUnwrappedLine();
909 nextToken();
910 --Line->Level;
911 return;
912 case tok::comma:
913 nextToken();
914 addUnwrappedLine();
915 break;
916 default:
917 nextToken();
918 break;
919 }
920 } while (!eof());
921 }
922 // We fall through to parsing a structural element afterwards, so that in
923 // enum A {} n, m;
924 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000925}
926
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000927void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000928 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000929 if (FormatTok->Tok.is(tok::identifier) ||
930 FormatTok->Tok.is(tok::kw___attribute) ||
931 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000932 nextToken();
933 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000934 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000935 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000936 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000937 // The actual identifier can be a nested name specifier, and in macros
938 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000939 while (FormatTok->Tok.is(tok::identifier) ||
940 FormatTok->Tok.is(tok::coloncolon) ||
941 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000942 nextToken();
943
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000944 // Note that parsing away template declarations here leads to incorrectly
945 // accepting function declarations as record declarations.
946 // In general, we cannot solve this problem. Consider:
947 // class A<int> B() {}
948 // which can be a function definition or a class definition when B() is a
949 // macro. If we find enough real-world cases where this is a problem, we
950 // can parse for the 'template' keyword in the beginning of the statement,
951 // and thus rule out the record production in case there is no template
952 // (this would still leave us with an ambiguity between template function
953 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000954 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
955 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
956 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000957 return;
958 nextToken();
959 }
960 }
961 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000962 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000963 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
964 addUnwrappedLine();
965
Nico Weber27268772013-06-26 00:30:14 +0000966 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000967 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000968 // We fall through to parsing a structural element afterwards, so
969 // class A {} n, m;
970 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000971}
972
Nico Weber1abe6ea2013-01-09 21:15:03 +0000973void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000974 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000975 do
976 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000977 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000978 nextToken(); // Skip '>'.
979}
980
981void UnwrappedLineParser::parseObjCUntilAtEnd() {
982 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000983 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000984 nextToken();
985 addUnwrappedLine();
986 break;
987 }
988 parseStructuralElement();
989 } while (!eof());
990}
991
Nico Weber50767d82013-01-09 23:25:37 +0000992void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000993 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000994 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000995
996 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000997 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000998 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000999 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001000 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001001 // Skip category, if present.
1002 parseParens();
1003
Manuel Klimek96e888b2013-05-28 11:55:06 +00001004 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001005 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001006
1007 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001008 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001009 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001010
1011 // With instance variables, this puts '}' on its own line. Without instance
1012 // variables, this ends the @interface line.
1013 addUnwrappedLine();
1014
Nico Weber1abe6ea2013-01-09 21:15:03 +00001015 parseObjCUntilAtEnd();
1016}
Nico Weber27d13672013-01-09 20:25:35 +00001017
Nico Weber1abe6ea2013-01-09 21:15:03 +00001018void UnwrappedLineParser::parseObjCProtocol() {
1019 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001020 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001021
Manuel Klimek96e888b2013-05-28 11:55:06 +00001022 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001023 parseObjCProtocolList();
1024
1025 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001026 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001027 nextToken();
1028 return addUnwrappedLine();
1029 }
1030
1031 addUnwrappedLine();
1032 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001033}
1034
Daniel Jasperbac016b2012-12-03 18:12:45 +00001035void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001036 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001037 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001038 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001039 llvm::dbgs() << "Line(" << Line->Level << ")"
1040 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001041 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1042 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001043 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001044 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001045 }
1046 llvm::dbgs() << "\n";
1047 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001048 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001049 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001050 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001051 for (std::vector<UnwrappedLine>::iterator
1052 I = PreprocessorDirectives.begin(),
1053 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001054 I != E; ++I) {
1055 CurrentLines->push_back(*I);
1056 }
1057 PreprocessorDirectives.clear();
1058 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001059}
1060
Manuel Klimek96e888b2013-05-28 11:55:06 +00001061bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001062
Manuel Klimek86721d22013-01-22 16:31:55 +00001063void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1064 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001065 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001066 I = CommentsBeforeNextToken.begin(),
1067 E = CommentsBeforeNextToken.end();
1068 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001069 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001070 addUnwrappedLine();
1071 }
1072 pushToken(*I);
1073 }
1074 if (NewlineBeforeNext && JustComments) {
1075 addUnwrappedLine();
1076 }
1077 CommentsBeforeNextToken.clear();
1078}
1079
Daniel Jasperbac016b2012-12-03 18:12:45 +00001080void UnwrappedLineParser::nextToken() {
1081 if (eof())
1082 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001083 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001084 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001085 readToken();
1086}
1087
1088void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001089 bool CommentsInCurrentLine = true;
1090 do {
1091 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001092 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1093 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001094 // If there is an unfinished unwrapped line, we flush the preprocessor
1095 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001096 bool SwitchToPreprocessorLines =
1097 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001098 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001099 // Comments stored before the preprocessor directive need to be output
1100 // before the preprocessor directive, at the same level as the
1101 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001102 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001103 parsePPDirective();
1104 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001105
1106 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1107 !Line->InPPDirective) {
1108 continue;
1109 }
1110
Manuel Klimek96e888b2013-05-28 11:55:06 +00001111 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001112 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001113 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001114 CommentsInCurrentLine = false;
1115 }
1116 if (CommentsInCurrentLine) {
1117 pushToken(FormatTok);
1118 } else {
1119 CommentsBeforeNextToken.push_back(FormatTok);
1120 }
1121 } while (!eof());
1122}
1123
Manuel Klimek96e888b2013-05-28 11:55:06 +00001124void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001125 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001126 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001127 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001128 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001129 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001130}
1131
Daniel Jaspercd162382013-01-07 13:26:07 +00001132} // end namespace format
1133} // end namespace clang