blob: 1c2a8fe4889ceee8cf9a36333cff011f829c38e2 [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 {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000258 FormatToken *NextTok = Tokens->getNextToken();
259 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000260 case tok::l_brace:
261 LBraceStack.push_back(Position);
262 break;
263 case tok::r_brace:
264 if (!LBraceStack.empty()) {
265 if (LBraces[LBraceStack.back()] == BS_Unknown) {
266 // If there is a comma, semicolon or right paren after the closing
267 // brace, we assume this is a braced initializer list.
268
269 // FIXME: Note that this currently works only because we do not
270 // use the brace information while inside a braced init list.
271 // Thus, if the parent is a braced init list, we consider all
272 // brace blocks inside it braced init list. That works good enough
273 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jaspereb483662013-05-31 10:09:55 +0000274 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
275 tok::l_brace, tok::colon))
Manuel Klimek80829bd2013-05-23 09:41:43 +0000276 LBraces[LBraceStack.back()] = BS_BracedInit;
277 else
278 LBraces[LBraceStack.back()] = BS_Block;
279 }
280 LBraceStack.pop_back();
281 }
282 break;
283 case tok::semi:
284 case tok::kw_if:
285 case tok::kw_while:
286 case tok::kw_for:
287 case tok::kw_switch:
288 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000289 if (!LBraceStack.empty())
Manuel Klimek80829bd2013-05-23 09:41:43 +0000290 LBraces[LBraceStack.back()] = BS_Block;
291 break;
292 default:
293 break;
294 }
295 Tok = NextTok;
296 ++Position;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000297 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000298 // Assume other blocks for all unclosed opening braces.
299 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
300 if (LBraces[LBraceStack[i]] == BS_Unknown)
301 LBraces[LBraceStack[i]] = BS_Block;
302 }
303 FormatTok = Tokens->setPosition(StoredPosition);
304}
305
Manuel Klimek67d080d2013-04-12 14:13:36 +0000306void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
Nico Weberd74fcdb2013-02-10 20:35:35 +0000307 unsigned AddLevels) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000308 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000309 nextToken();
310
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000311 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000312
Manuel Klimek70b03f42013-01-23 09:32:48 +0000313 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
314 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000315 Line->Level += AddLevels;
Nico Weber27268772013-06-26 00:30:14 +0000316 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000317
Manuel Klimek96e888b2013-05-28 11:55:06 +0000318 if (!FormatTok->Tok.is(tok::r_brace)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000319 Line->Level -= AddLevels;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000320 StructuralError = true;
321 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000322 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000323
Daniel Jasperf9955d32013-03-20 12:37:50 +0000324 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000325 Line->Level -= AddLevels;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000326}
327
328void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000329 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000330 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000331 nextToken();
332
Manuel Klimek96e888b2013-05-28 11:55:06 +0000333 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000334 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000335 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000336 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000337
Manuel Klimek96e888b2013-05-28 11:55:06 +0000338 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000339 case tok::pp_define:
340 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000341 return;
342 case tok::pp_if:
343 parsePPIf();
344 break;
345 case tok::pp_ifdef:
346 case tok::pp_ifndef:
347 parsePPIfdef();
348 break;
349 case tok::pp_else:
350 parsePPElse();
351 break;
352 case tok::pp_elif:
353 parsePPElIf();
354 break;
355 case tok::pp_endif:
356 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000357 break;
358 default:
359 parsePPUnknown();
360 break;
361 }
362}
363
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000364void UnwrappedLineParser::pushPPConditional() {
365 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
366 PPStack.push_back(PP_Unreachable);
367 else
368 PPStack.push_back(PP_Conditional);
369}
370
371void UnwrappedLineParser::parsePPIf() {
372 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000373 if ((FormatTok->Tok.isLiteral() &&
374 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000375 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000376 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000377 PPStack.push_back(PP_Unreachable);
378 } else {
379 pushPPConditional();
380 }
381 parsePPUnknown();
382}
383
384void UnwrappedLineParser::parsePPIfdef() {
385 pushPPConditional();
386 parsePPUnknown();
387}
388
389void UnwrappedLineParser::parsePPElse() {
390 if (!PPStack.empty())
391 PPStack.pop_back();
392 pushPPConditional();
393 parsePPUnknown();
394}
395
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000396void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000397
398void UnwrappedLineParser::parsePPEndIf() {
399 if (!PPStack.empty())
400 PPStack.pop_back();
401 parsePPUnknown();
402}
403
Manuel Klimekd4397b92013-01-04 23:34:14 +0000404void UnwrappedLineParser::parsePPDefine() {
405 nextToken();
406
Manuel Klimek96e888b2013-05-28 11:55:06 +0000407 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000408 parsePPUnknown();
409 return;
410 }
411 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000412 if (FormatTok->Tok.getKind() == tok::l_paren &&
413 FormatTok->WhitespaceRange.getBegin() ==
414 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000415 parseParens();
416 }
417 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000418 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000419
420 // Errors during a preprocessor directive can only affect the layout of the
421 // preprocessor directive, and thus we ignore them. An alternative approach
422 // would be to use the same approach we use on the file level (no
423 // re-indentation if there was a structural error) within the macro
424 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000425 parseFile();
426}
427
428void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000429 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000430 nextToken();
431 } while (!eof());
432 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000433}
434
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000435// Here we blacklist certain tokens that are not usually the first token in an
436// unwrapped line. This is used in attempt to distinguish macro calls without
437// trailing semicolons from other constructs split to several lines.
438bool tokenCanStartNewLine(clang::Token Tok) {
439 // Semicolon can be a null-statement, l_square can be a start of a macro or
440 // a C++11 attribute, but this doesn't seem to be common.
441 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
442 Tok.isNot(tok::l_square) &&
443 // Tokens that can only be used as binary operators and a part of
444 // overloaded operator names.
445 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
446 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
447 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
448 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
449 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
450 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
451 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
452 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
453 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
454 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
455 Tok.isNot(tok::lesslessequal) &&
456 // Colon is used in labels, base class lists, initializer lists,
457 // range-based for loops, ternary operator, but should never be the
458 // first token in an unwrapped line.
459 Tok.isNot(tok::colon);
460}
461
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000462void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000463 assert(!FormatTok->Tok.is(tok::l_brace));
464 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000465 case tok::at:
466 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000467 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000468 parseBracedList();
469 break;
470 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000471 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000472 case tok::objc_public:
473 case tok::objc_protected:
474 case tok::objc_package:
475 case tok::objc_private:
476 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000477 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000478 case tok::objc_implementation:
479 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000480 case tok::objc_protocol:
481 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000482 case tok::objc_end:
483 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000484 case tok::objc_optional:
485 case tok::objc_required:
486 nextToken();
487 addUnwrappedLine();
488 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000489 default:
490 break;
491 }
492 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000493 case tok::kw_namespace:
494 parseNamespace();
495 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000496 case tok::kw_inline:
497 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000498 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000499 parseNamespace();
500 return;
501 }
502 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000503 case tok::kw_public:
504 case tok::kw_protected:
505 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000506 parseAccessSpecifier();
507 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000508 case tok::kw_if:
509 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000510 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000511 case tok::kw_for:
512 case tok::kw_while:
513 parseForOrWhileLoop();
514 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000515 case tok::kw_do:
516 parseDoWhile();
517 return;
518 case tok::kw_switch:
519 parseSwitch();
520 return;
521 case tok::kw_default:
522 nextToken();
523 parseLabel();
524 return;
525 case tok::kw_case:
526 parseCaseLabel();
527 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000528 case tok::kw_return:
529 parseReturn();
530 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000531 case tok::kw_extern:
532 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000533 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000534 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000535 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000536 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000537 addUnwrappedLine();
538 return;
539 }
540 }
541 // In all other cases, parse the declaration.
542 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000543 default:
544 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000545 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000546 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000547 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000548 case tok::at:
549 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000550 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000551 parseBracedList();
552 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000553 case tok::kw_enum:
554 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000555 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000556 case tok::kw_struct:
557 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000558 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000559 parseRecord();
560 // A record declaration or definition is always the start of a structural
561 // element.
562 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000563 case tok::semi:
564 nextToken();
565 addUnwrappedLine();
566 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000567 case tok::r_brace:
568 addUnwrappedLine();
569 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000570 case tok::l_paren:
571 parseParens();
572 break;
573 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000574 if (!tryToParseBracedList()) {
575 // A block outside of parentheses must be the last part of a
576 // structural element.
577 // FIXME: Figure out cases where this is not true, and add projections
578 // for them (the one we know is missing are lambdas).
579 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
580 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
581 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000582 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000583 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000584 return;
585 }
586 // Otherwise this was a braced init list, and the structural
587 // element continues.
588 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000589 case tok::identifier: {
590 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000591 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000592 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000593 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000594 parseLabel();
595 return;
596 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000597 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000598 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000599 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000600 if (FormatTok->HasUnescapedNewline &&
601 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000602 addUnwrappedLine();
603 return;
604 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000605 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
606 Text == Text.upper()) {
607 // Recognize free-standing macros like Q_OBJECT.
608 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000609 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000610 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000611 }
612 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000613 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000614 case tok::equal:
615 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000616 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000617 parseBracedList();
618 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000619 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000620 default:
621 nextToken();
622 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000623 }
624 } while (!eof());
625}
626
Manuel Klimek80829bd2013-05-23 09:41:43 +0000627bool UnwrappedLineParser::tryToParseBracedList() {
628 if (LBraces[Tokens->getPosition()] == BS_Unknown)
629 calculateBraceTypes();
630 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
631 if (LBraces[Tokens->getPosition()] == BS_Block)
632 return false;
633 parseBracedList();
634 return true;
635}
636
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000637void UnwrappedLineParser::parseBracedList() {
638 nextToken();
639
Manuel Klimek423dd932013-04-10 09:52:05 +0000640 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
641 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000642 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000643 // FIXME: When we start to support lambdas, we'll want to parse them away
644 // here, otherwise our bail-out scenarios below break. The better solution
645 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000646 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000647 case tok::l_brace:
648 parseBracedList();
649 break;
650 case tok::r_brace:
651 nextToken();
652 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000653 case tok::semi:
654 // Probably a missing closing brace. Bail out.
655 return;
656 case tok::comma:
657 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000658 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000659 default:
660 nextToken();
661 break;
662 }
663 } while (!eof());
664}
665
Manuel Klimekc44ee892013-01-21 10:07:49 +0000666void UnwrappedLineParser::parseReturn() {
667 nextToken();
668
669 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000670 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000671 case tok::l_brace:
672 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000673 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000674 // Assume missing ';'.
675 addUnwrappedLine();
676 return;
677 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000678 break;
679 case tok::l_paren:
680 parseParens();
681 break;
682 case tok::r_brace:
683 // Assume missing ';'.
684 addUnwrappedLine();
685 return;
686 case tok::semi:
687 nextToken();
688 addUnwrappedLine();
689 return;
690 default:
691 nextToken();
692 break;
693 }
694 } while (!eof());
695}
696
Daniel Jasperbac016b2012-12-03 18:12:45 +0000697void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000698 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000699 nextToken();
700 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000701 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000702 case tok::l_paren:
703 parseParens();
704 break;
705 case tok::r_paren:
706 nextToken();
707 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000708 case tok::r_brace:
709 // A "}" inside parenthesis is an error if there wasn't a matching "{".
710 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000711 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000712 if (!tryToParseBracedList()) {
713 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000714 {
715 ScopedLineState LineState(*this);
716 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000717 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000718 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000719 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000720 Line->Level -= 1;
721 }
722 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000723 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000724 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000725 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000726 case tok::at:
727 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000728 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000729 parseBracedList();
730 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000731 default:
732 nextToken();
733 break;
734 }
735 } while (!eof());
736}
737
738void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000739 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000740 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000741 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000742 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000743 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000744 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000745 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000746 NeedsUnwrappedLine = true;
747 } else {
748 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000749 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000750 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000751 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000752 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000753 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000754 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000755 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000756 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000757 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000758 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000759 parseIfThenElse();
760 } else {
761 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000762 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000763 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000764 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000765 }
766 } else if (NeedsUnwrappedLine) {
767 addUnwrappedLine();
768 }
769}
770
Alexander Kornienko15757312012-12-06 18:03:27 +0000771void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000772 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000773 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000774 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000775 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000776 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000777 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
778 addUnwrappedLine();
779
Nico Weber27268772013-06-26 00:30:14 +0000780 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000781 // Munch the semicolon after a namespace. This is more common than one would
782 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000783 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000784 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000785 addUnwrappedLine();
786 }
787 // FIXME: Add error handling.
788}
789
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000790void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000791 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000792 "'for' or 'while' expected");
793 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000794 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000795 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000796 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000797 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000798 addUnwrappedLine();
799 } else {
800 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000801 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000802 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000803 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000804 }
805}
806
Daniel Jasperbac016b2012-12-03 18:12:45 +0000807void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000808 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000809 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000810 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000811 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000812 } else {
813 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000814 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000815 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000816 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000817 }
818
Alexander Kornienko393b0082012-12-04 15:40:36 +0000819 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000820 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000821 addUnwrappedLine();
822 return;
823 }
824
Daniel Jasperbac016b2012-12-03 18:12:45 +0000825 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000826 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000827}
828
829void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000830 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000831 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000832 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000833 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000834 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000835 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000836 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000837 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000838 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000839 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000840 }
841 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000842 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843}
844
845void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000846 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000847 // FIXME: fix handling of complex expressions here.
848 do {
849 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000850 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000851 parseLabel();
852}
853
854void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000855 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000856 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000857 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000858 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000859 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000860 parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000861 addUnwrappedLine();
862 } else {
863 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000864 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000865 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000866 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000867 }
868}
869
870void UnwrappedLineParser::parseAccessSpecifier() {
871 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000872 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000873 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000874 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000875 addUnwrappedLine();
876}
877
878void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000879 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000880 if (FormatTok->Tok.is(tok::identifier) ||
881 FormatTok->Tok.is(tok::kw___attribute) ||
882 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000883 nextToken();
884 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000885 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000886 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000887 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000888 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000889 nextToken();
890 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000891 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000892 nextToken();
893 addUnwrappedLine();
894 ++Line->Level;
895 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000896 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000897 case tok::l_paren:
898 parseParens();
899 break;
900 case tok::r_brace:
901 addUnwrappedLine();
902 nextToken();
903 --Line->Level;
904 return;
905 case tok::comma:
906 nextToken();
907 addUnwrappedLine();
908 break;
909 default:
910 nextToken();
911 break;
912 }
913 } while (!eof());
914 }
915 // We fall through to parsing a structural element afterwards, so that in
916 // enum A {} n, m;
917 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000918}
919
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000920void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000921 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000922 if (FormatTok->Tok.is(tok::identifier) ||
923 FormatTok->Tok.is(tok::kw___attribute) ||
924 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000925 nextToken();
926 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000927 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000928 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000929 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000930 // The actual identifier can be a nested name specifier, and in macros
931 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000932 while (FormatTok->Tok.is(tok::identifier) ||
933 FormatTok->Tok.is(tok::coloncolon) ||
934 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000935 nextToken();
936
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000937 // Note that parsing away template declarations here leads to incorrectly
938 // accepting function declarations as record declarations.
939 // In general, we cannot solve this problem. Consider:
940 // class A<int> B() {}
941 // which can be a function definition or a class definition when B() is a
942 // macro. If we find enough real-world cases where this is a problem, we
943 // can parse for the 'template' keyword in the beginning of the statement,
944 // and thus rule out the record production in case there is no template
945 // (this would still leave us with an ambiguity between template function
946 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000947 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
948 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
949 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000950 return;
951 nextToken();
952 }
953 }
954 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000955 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000956 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
957 addUnwrappedLine();
958
Nico Weber27268772013-06-26 00:30:14 +0000959 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000960 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000961 // We fall through to parsing a structural element afterwards, so
962 // class A {} n, m;
963 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000964}
965
Nico Weber1abe6ea2013-01-09 21:15:03 +0000966void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000967 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000968 do
969 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000970 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000971 nextToken(); // Skip '>'.
972}
973
974void UnwrappedLineParser::parseObjCUntilAtEnd() {
975 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000976 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000977 nextToken();
978 addUnwrappedLine();
979 break;
980 }
981 parseStructuralElement();
982 } while (!eof());
983}
984
Nico Weber50767d82013-01-09 23:25:37 +0000985void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000986 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000987 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000988
989 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000990 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000991 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000992 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000993 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000994 // Skip category, if present.
995 parseParens();
996
Manuel Klimek96e888b2013-05-28 11:55:06 +0000997 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000998 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000999
1000 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001001 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001002 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001003
1004 // With instance variables, this puts '}' on its own line. Without instance
1005 // variables, this ends the @interface line.
1006 addUnwrappedLine();
1007
Nico Weber1abe6ea2013-01-09 21:15:03 +00001008 parseObjCUntilAtEnd();
1009}
Nico Weber27d13672013-01-09 20:25:35 +00001010
Nico Weber1abe6ea2013-01-09 21:15:03 +00001011void UnwrappedLineParser::parseObjCProtocol() {
1012 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001013 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001014
Manuel Klimek96e888b2013-05-28 11:55:06 +00001015 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001016 parseObjCProtocolList();
1017
1018 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001019 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001020 nextToken();
1021 return addUnwrappedLine();
1022 }
1023
1024 addUnwrappedLine();
1025 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001026}
1027
Daniel Jasperbac016b2012-12-03 18:12:45 +00001028void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001029 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001030 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001031 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001032 llvm::dbgs() << "Line(" << Line->Level << ")"
1033 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001034 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1035 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001036 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001037 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001038 }
1039 llvm::dbgs() << "\n";
1040 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001041 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001042 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001043 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001044 for (std::vector<UnwrappedLine>::iterator
1045 I = PreprocessorDirectives.begin(),
1046 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001047 I != E; ++I) {
1048 CurrentLines->push_back(*I);
1049 }
1050 PreprocessorDirectives.clear();
1051 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001052}
1053
Manuel Klimek96e888b2013-05-28 11:55:06 +00001054bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001055
Manuel Klimek86721d22013-01-22 16:31:55 +00001056void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1057 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001058 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001059 I = CommentsBeforeNextToken.begin(),
1060 E = CommentsBeforeNextToken.end();
1061 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001062 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001063 addUnwrappedLine();
1064 }
1065 pushToken(*I);
1066 }
1067 if (NewlineBeforeNext && JustComments) {
1068 addUnwrappedLine();
1069 }
1070 CommentsBeforeNextToken.clear();
1071}
1072
Daniel Jasperbac016b2012-12-03 18:12:45 +00001073void UnwrappedLineParser::nextToken() {
1074 if (eof())
1075 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001076 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001077 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001078 readToken();
1079}
1080
1081void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001082 bool CommentsInCurrentLine = true;
1083 do {
1084 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001085 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1086 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001087 // If there is an unfinished unwrapped line, we flush the preprocessor
1088 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001089 bool SwitchToPreprocessorLines =
1090 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001091 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001092 // Comments stored before the preprocessor directive need to be output
1093 // before the preprocessor directive, at the same level as the
1094 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001095 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001096 parsePPDirective();
1097 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001098
1099 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1100 !Line->InPPDirective) {
1101 continue;
1102 }
1103
Manuel Klimek96e888b2013-05-28 11:55:06 +00001104 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001105 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001106 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001107 CommentsInCurrentLine = false;
1108 }
1109 if (CommentsInCurrentLine) {
1110 pushToken(FormatTok);
1111 } else {
1112 CommentsBeforeNextToken.push_back(FormatTok);
1113 }
1114 } while (!eof());
1115}
1116
Manuel Klimek96e888b2013-05-28 11:55:06 +00001117void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001118 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001119 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001120 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001121 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001122 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001123}
1124
Daniel Jaspercd162382013-01-07 13:26:07 +00001125} // end namespace format
1126} // end namespace clang