blob: 341952bb726d94abac63242b30d5d6ebe7f26e71 [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
Manuel Klimek70b03f42013-01-23 09:32:48 +000033class ScopedDeclarationState {
34public:
35 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
36 bool MustBeDeclaration)
37 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000038 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000039 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000040 }
41 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000042 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000043 if (!Stack.empty())
44 Line.MustBeDeclaration = Stack.back();
45 else
46 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000047 }
48private:
49 UnwrappedLine &Line;
50 std::vector<bool> &Stack;
51};
52
Manuel Klimekd4397b92013-01-04 23:34:14 +000053class ScopedMacroState : public FormatTokenSource {
54public:
55 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek96e888b2013-05-28 11:55:06 +000056 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimekd4397b92013-01-04 23:34:14 +000057 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek67d080d2013-04-12 14:13:36 +000058 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
59 StructuralError(StructuralError),
Manuel Klimek96e888b2013-05-28 11:55:06 +000060 PreviousStructuralError(StructuralError), Token(NULL) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000061 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000062 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000063 Line.InPPDirective = true;
64 }
65
66 ~ScopedMacroState() {
67 TokenSource = PreviousTokenSource;
68 ResetToken = Token;
69 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000070 Line.Level = PreviousLineLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +000071 StructuralError = PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +000072 }
73
Manuel Klimek96e888b2013-05-28 11:55:06 +000074 virtual FormatToken *getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000075 // The \c UnwrappedLineParser guards against this by never calling
76 // \c getNextToken() after it has encountered the first eof token.
77 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000078 Token = PreviousTokenSource->getNextToken();
79 if (eof())
Manuel Klimek96e888b2013-05-28 11:55:06 +000080 return getFakeEOF();
Manuel Klimekd4397b92013-01-04 23:34:14 +000081 return Token;
82 }
83
Manuel Klimek80829bd2013-05-23 09:41:43 +000084 virtual unsigned getPosition() {
85 return PreviousTokenSource->getPosition();
86 }
87
Manuel Klimek96e888b2013-05-28 11:55:06 +000088 virtual FormatToken *setPosition(unsigned Position) {
Manuel Klimek80829bd2013-05-23 09:41:43 +000089 Token = PreviousTokenSource->setPosition(Position);
90 return Token;
91 }
92
Manuel Klimekd4397b92013-01-04 23:34:14 +000093private:
Manuel Klimek96e888b2013-05-28 11:55:06 +000094 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000095
Manuel Klimek96e888b2013-05-28 11:55:06 +000096 FormatToken *getFakeEOF() {
97 static bool EOFInitialized = false;
98 static FormatToken FormatTok;
99 if (!EOFInitialized) {
100 FormatTok.Tok.startToken();
101 FormatTok.Tok.setKind(tok::eof);
102 EOFInitialized = true;
103 }
104 return &FormatTok;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000105 }
106
107 UnwrappedLine &Line;
108 FormatTokenSource *&TokenSource;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000109 FormatToken *&ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000110 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000111 FormatTokenSource *PreviousTokenSource;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000112 bool &StructuralError;
113 bool PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000114
Manuel Klimek96e888b2013-05-28 11:55:06 +0000115 FormatToken *Token;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000116};
117
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000118class ScopedLineState {
119public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000120 ScopedLineState(UnwrappedLineParser &Parser,
121 bool SwitchToPreprocessorLines = false)
122 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
123 if (SwitchToPreprocessorLines)
124 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000125 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000126 Parser.Line.reset(new UnwrappedLine());
127 Parser.Line->Level = PreBlockLine->Level;
128 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000129 }
130
131 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000132 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000133 Parser.addUnwrappedLine();
134 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000135 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000136 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000137 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000138 if (SwitchToPreprocessorLines)
139 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000140 }
141
142private:
143 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000144 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000145
146 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000147};
148
Manuel Klimek80829bd2013-05-23 09:41:43 +0000149class IndexedTokenSource : public FormatTokenSource {
150public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000151 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000152 : Tokens(Tokens), Position(-1) {}
153
Manuel Klimek96e888b2013-05-28 11:55:06 +0000154 virtual FormatToken *getNextToken() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000155 ++Position;
156 return Tokens[Position];
157 }
158
159 virtual unsigned getPosition() {
160 assert(Position >= 0);
161 return Position;
162 }
163
Manuel Klimek96e888b2013-05-28 11:55:06 +0000164 virtual FormatToken *setPosition(unsigned P) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000165 Position = P;
166 return Tokens[Position];
167 }
168
169private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000170 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000171 int Position;
172};
173
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000174UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000175 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000176 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000177 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000178 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
179 Callback(Callback), AllTokens(Tokens) {
180 LBraces.resize(Tokens.size(), BS_Unknown);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000181}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000182
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000183bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000184 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek80829bd2013-05-23 09:41:43 +0000185 IndexedTokenSource TokenSource(AllTokens);
186 Tokens = &TokenSource;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000187 readToken();
Manuel Klimek67d080d2013-04-12 14:13:36 +0000188 parseFile();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000189 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000190 I != E; ++I) {
191 Callback.consumeUnwrappedLine(*I);
192 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000193
194 // Create line with eof token.
195 pushToken(FormatTok);
196 Callback.consumeUnwrappedLine(*Line);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000197 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000198}
199
Manuel Klimek67d080d2013-04-12 14:13:36 +0000200void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000201 ScopedDeclarationState DeclarationState(
202 *Line, DeclarationScopeStack,
203 /*MustBeDeclaration=*/ !Line->InPPDirective);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000204 parseLevel(/*HasOpeningBrace=*/ false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000205 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000206 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000207 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000208}
209
Manuel Klimek67d080d2013-04-12 14:13:36 +0000210void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000211 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000212 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000213 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000214 nextToken();
215 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000216 break;
217 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000218 // FIXME: Add parameter whether this can happen - if this happens, we must
219 // be in a non-declaration context.
Manuel Klimek67d080d2013-04-12 14:13:36 +0000220 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221 addUnwrappedLine();
222 break;
223 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000224 if (HasOpeningBrace)
225 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000226 StructuralError = true;
227 nextToken();
228 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000229 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000230 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000231 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000232 break;
233 }
234 } while (!eof());
235}
236
Manuel Klimek80829bd2013-05-23 09:41:43 +0000237void UnwrappedLineParser::calculateBraceTypes() {
238 // We'll parse forward through the tokens until we hit
239 // a closing brace or eof - note that getNextToken() will
240 // parse macros, so this will magically work inside macro
241 // definitions, too.
242 unsigned StoredPosition = Tokens->getPosition();
243 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000244 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000245 // Keep a stack of positions of lbrace tokens. We will
246 // update information about whether an lbrace starts a
247 // braced init list or a different block during the loop.
248 SmallVector<unsigned, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000249 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000250 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000251 FormatToken *NextTok = Tokens->getNextToken();
252 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000253 case tok::l_brace:
254 LBraceStack.push_back(Position);
255 break;
256 case tok::r_brace:
257 if (!LBraceStack.empty()) {
258 if (LBraces[LBraceStack.back()] == BS_Unknown) {
259 // If there is a comma, semicolon or right paren after the closing
260 // brace, we assume this is a braced initializer list.
261
262 // FIXME: Note that this currently works only because we do not
263 // use the brace information while inside a braced init list.
264 // Thus, if the parent is a braced init list, we consider all
265 // brace blocks inside it braced init list. That works good enough
266 // for now, but we will need to fix it to correctly handle lambdas.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000267 if (NextTok->Tok.is(tok::comma) || NextTok->Tok.is(tok::semi) ||
268 NextTok->Tok.is(tok::r_paren) || NextTok->Tok.is(tok::l_brace))
Manuel Klimek80829bd2013-05-23 09:41:43 +0000269 LBraces[LBraceStack.back()] = BS_BracedInit;
270 else
271 LBraces[LBraceStack.back()] = BS_Block;
272 }
273 LBraceStack.pop_back();
274 }
275 break;
276 case tok::semi:
277 case tok::kw_if:
278 case tok::kw_while:
279 case tok::kw_for:
280 case tok::kw_switch:
281 case tok::kw_try:
282 if (!LBraceStack.empty())
283 LBraces[LBraceStack.back()] = BS_Block;
284 break;
285 default:
286 break;
287 }
288 Tok = NextTok;
289 ++Position;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000290 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000291 // Assume other blocks for all unclosed opening braces.
292 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
293 if (LBraces[LBraceStack[i]] == BS_Unknown)
294 LBraces[LBraceStack[i]] = BS_Block;
295 }
296 FormatTok = Tokens->setPosition(StoredPosition);
297}
298
Manuel Klimek67d080d2013-04-12 14:13:36 +0000299void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
Nico Weberd74fcdb2013-02-10 20:35:35 +0000300 unsigned AddLevels) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000301 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000302 nextToken();
303
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000304 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000305
Manuel Klimek70b03f42013-01-23 09:32:48 +0000306 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
307 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000308 Line->Level += AddLevels;
Daniel Jasperf9955d32013-03-20 12:37:50 +0000309 parseLevel(/*HasOpeningBrace=*/ true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000310
Manuel Klimek96e888b2013-05-28 11:55:06 +0000311 if (!FormatTok->Tok.is(tok::r_brace)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000312 Line->Level -= AddLevels;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000313 StructuralError = true;
314 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000315 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000316
Daniel Jasperf9955d32013-03-20 12:37:50 +0000317 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000318 Line->Level -= AddLevels;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000319}
320
321void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000322 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000323 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000324 nextToken();
325
Manuel Klimek96e888b2013-05-28 11:55:06 +0000326 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000327 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000328 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000329 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000330
Manuel Klimek96e888b2013-05-28 11:55:06 +0000331 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000332 case tok::pp_define:
333 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000334 return;
335 case tok::pp_if:
336 parsePPIf();
337 break;
338 case tok::pp_ifdef:
339 case tok::pp_ifndef:
340 parsePPIfdef();
341 break;
342 case tok::pp_else:
343 parsePPElse();
344 break;
345 case tok::pp_elif:
346 parsePPElIf();
347 break;
348 case tok::pp_endif:
349 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000350 break;
351 default:
352 parsePPUnknown();
353 break;
354 }
355}
356
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000357void UnwrappedLineParser::pushPPConditional() {
358 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
359 PPStack.push_back(PP_Unreachable);
360 else
361 PPStack.push_back(PP_Conditional);
362}
363
364void UnwrappedLineParser::parsePPIf() {
365 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000366 if ((FormatTok->Tok.isLiteral() &&
367 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000368 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000369 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000370 PPStack.push_back(PP_Unreachable);
371 } else {
372 pushPPConditional();
373 }
374 parsePPUnknown();
375}
376
377void UnwrappedLineParser::parsePPIfdef() {
378 pushPPConditional();
379 parsePPUnknown();
380}
381
382void UnwrappedLineParser::parsePPElse() {
383 if (!PPStack.empty())
384 PPStack.pop_back();
385 pushPPConditional();
386 parsePPUnknown();
387}
388
389void UnwrappedLineParser::parsePPElIf() {
390 parsePPElse();
391}
392
393void UnwrappedLineParser::parsePPEndIf() {
394 if (!PPStack.empty())
395 PPStack.pop_back();
396 parsePPUnknown();
397}
398
Manuel Klimekd4397b92013-01-04 23:34:14 +0000399void UnwrappedLineParser::parsePPDefine() {
400 nextToken();
401
Manuel Klimek96e888b2013-05-28 11:55:06 +0000402 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000403 parsePPUnknown();
404 return;
405 }
406 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000407 if (FormatTok->Tok.getKind() == tok::l_paren &&
408 FormatTok->WhitespaceRange.getBegin() ==
409 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000410 parseParens();
411 }
412 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000413 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000414
415 // Errors during a preprocessor directive can only affect the layout of the
416 // preprocessor directive, and thus we ignore them. An alternative approach
417 // would be to use the same approach we use on the file level (no
418 // re-indentation if there was a structural error) within the macro
419 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000420 parseFile();
421}
422
423void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000424 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000425 nextToken();
426 } while (!eof());
427 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000428}
429
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000430// Here we blacklist certain tokens that are not usually the first token in an
431// unwrapped line. This is used in attempt to distinguish macro calls without
432// trailing semicolons from other constructs split to several lines.
433bool tokenCanStartNewLine(clang::Token Tok) {
434 // Semicolon can be a null-statement, l_square can be a start of a macro or
435 // a C++11 attribute, but this doesn't seem to be common.
436 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
437 Tok.isNot(tok::l_square) &&
438 // Tokens that can only be used as binary operators and a part of
439 // overloaded operator names.
440 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
441 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
442 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
443 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
444 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
445 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
446 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
447 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
448 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
449 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
450 Tok.isNot(tok::lesslessequal) &&
451 // Colon is used in labels, base class lists, initializer lists,
452 // range-based for loops, ternary operator, but should never be the
453 // first token in an unwrapped line.
454 Tok.isNot(tok::colon);
455}
456
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000457void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000458 assert(!FormatTok->Tok.is(tok::l_brace));
459 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000460 case tok::at:
461 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000462 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000463 parseBracedList();
464 break;
465 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000466 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000467 case tok::objc_public:
468 case tok::objc_protected:
469 case tok::objc_package:
470 case tok::objc_private:
471 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000472 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000473 case tok::objc_implementation:
474 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000475 case tok::objc_protocol:
476 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000477 case tok::objc_end:
478 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000479 case tok::objc_optional:
480 case tok::objc_required:
481 nextToken();
482 addUnwrappedLine();
483 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000484 default:
485 break;
486 }
487 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000488 case tok::kw_namespace:
489 parseNamespace();
490 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000491 case tok::kw_inline:
492 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000493 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000494 parseNamespace();
495 return;
496 }
497 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000498 case tok::kw_public:
499 case tok::kw_protected:
500 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000501 parseAccessSpecifier();
502 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000503 case tok::kw_if:
504 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000505 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000506 case tok::kw_for:
507 case tok::kw_while:
508 parseForOrWhileLoop();
509 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000510 case tok::kw_do:
511 parseDoWhile();
512 return;
513 case tok::kw_switch:
514 parseSwitch();
515 return;
516 case tok::kw_default:
517 nextToken();
518 parseLabel();
519 return;
520 case tok::kw_case:
521 parseCaseLabel();
522 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000523 case tok::kw_return:
524 parseReturn();
525 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000526 case tok::kw_extern:
527 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000528 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000529 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000530 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000531 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000532 addUnwrappedLine();
533 return;
534 }
535 }
536 // In all other cases, parse the declaration.
537 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000538 default:
539 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000540 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000541 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000542 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000543 case tok::at:
544 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000545 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000546 parseBracedList();
547 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000548 case tok::kw_enum:
549 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000550 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000551 case tok::kw_struct:
552 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000553 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000554 parseRecord();
555 // A record declaration or definition is always the start of a structural
556 // element.
557 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000558 case tok::semi:
559 nextToken();
560 addUnwrappedLine();
561 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000562 case tok::r_brace:
563 addUnwrappedLine();
564 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000565 case tok::l_paren:
566 parseParens();
567 break;
568 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000569 if (!tryToParseBracedList()) {
570 // A block outside of parentheses must be the last part of a
571 // structural element.
572 // FIXME: Figure out cases where this is not true, and add projections
573 // for them (the one we know is missing are lambdas).
574 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
575 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
576 addUnwrappedLine();
577 parseBlock(/*MustBeDeclaration=*/ false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000578 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000579 return;
580 }
581 // Otherwise this was a braced init list, and the structural
582 // element continues.
583 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000584 case tok::identifier:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000585 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000586 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000587 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000588 parseLabel();
589 return;
590 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000591 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000592 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000593 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000594 if (FormatTok->HasUnescapedNewline &&
595 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000596 addUnwrappedLine();
597 return;
598 }
599 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000600 }
601 break;
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000602 case tok::equal:
603 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000604 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000605 parseBracedList();
606 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000607 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000608 default:
609 nextToken();
610 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000611 }
612 } while (!eof());
613}
614
Manuel Klimek80829bd2013-05-23 09:41:43 +0000615bool UnwrappedLineParser::tryToParseBracedList() {
616 if (LBraces[Tokens->getPosition()] == BS_Unknown)
617 calculateBraceTypes();
618 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
619 if (LBraces[Tokens->getPosition()] == BS_Block)
620 return false;
621 parseBracedList();
622 return true;
623}
624
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000625void UnwrappedLineParser::parseBracedList() {
626 nextToken();
627
Manuel Klimek423dd932013-04-10 09:52:05 +0000628 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
629 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000630 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000631 // FIXME: When we start to support lambdas, we'll want to parse them away
632 // here, otherwise our bail-out scenarios below break. The better solution
633 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000634 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000635 case tok::l_brace:
636 parseBracedList();
637 break;
638 case tok::r_brace:
639 nextToken();
640 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000641 case tok::semi:
642 // Probably a missing closing brace. Bail out.
643 return;
644 case tok::comma:
645 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000646 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000647 default:
648 nextToken();
649 break;
650 }
651 } while (!eof());
652}
653
Manuel Klimekc44ee892013-01-21 10:07:49 +0000654void UnwrappedLineParser::parseReturn() {
655 nextToken();
656
657 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000658 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000659 case tok::l_brace:
660 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000661 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000662 // Assume missing ';'.
663 addUnwrappedLine();
664 return;
665 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000666 break;
667 case tok::l_paren:
668 parseParens();
669 break;
670 case tok::r_brace:
671 // Assume missing ';'.
672 addUnwrappedLine();
673 return;
674 case tok::semi:
675 nextToken();
676 addUnwrappedLine();
677 return;
678 default:
679 nextToken();
680 break;
681 }
682 } while (!eof());
683}
684
Daniel Jasperbac016b2012-12-03 18:12:45 +0000685void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000686 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000687 nextToken();
688 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000689 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000690 case tok::l_paren:
691 parseParens();
692 break;
693 case tok::r_paren:
694 nextToken();
695 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000696 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000697 if (!tryToParseBracedList()) {
698 nextToken();
699 ScopedLineState LineState(*this);
700 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
701 /*MustBeDeclaration=*/ false);
702 Line->Level += 1;
703 parseLevel(/*HasOpeningBrace=*/ true);
704 Line->Level -= 1;
705 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000706 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000707 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000708 case tok::at:
709 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000710 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000711 parseBracedList();
712 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000713 default:
714 nextToken();
715 break;
716 }
717 } while (!eof());
718}
719
720void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000721 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000722 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000723 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000724 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000725 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000726 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000727 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000728 NeedsUnwrappedLine = true;
729 } else {
730 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000731 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000732 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000733 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000734 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000735 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000736 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000737 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000738 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000739 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000740 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000741 parseIfThenElse();
742 } else {
743 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000744 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000745 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000746 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000747 }
748 } else if (NeedsUnwrappedLine) {
749 addUnwrappedLine();
750 }
751}
752
Alexander Kornienko15757312012-12-06 18:03:27 +0000753void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000754 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000755 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000756 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000757 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000758 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000759 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
760 addUnwrappedLine();
761
Manuel Klimek70b03f42013-01-23 09:32:48 +0000762 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000763 // Munch the semicolon after a namespace. This is more common than one would
764 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000765 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000766 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000767 addUnwrappedLine();
768 }
769 // FIXME: Add error handling.
770}
771
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000772void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000773 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000774 "'for' or 'while' expected");
775 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000776 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000777 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000778 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000779 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000780 addUnwrappedLine();
781 } else {
782 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000783 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000784 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000785 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000786 }
787}
788
Daniel Jasperbac016b2012-12-03 18:12:45 +0000789void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000790 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000791 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000792 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000793 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000794 } else {
795 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000796 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000797 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000798 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000799 }
800
Alexander Kornienko393b0082012-12-04 15:40:36 +0000801 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000802 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000803 addUnwrappedLine();
804 return;
805 }
806
Daniel Jasperbac016b2012-12-03 18:12:45 +0000807 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000808 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000809}
810
811void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000812 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000813 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000814 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000815 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000816 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000817 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000818 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000819 parseBlock(/*MustBeDeclaration=*/ false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000820 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000821 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000822 }
823 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000824 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000825}
826
827void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000828 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000829 // FIXME: fix handling of complex expressions here.
830 do {
831 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000832 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000833 parseLabel();
834}
835
836void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000837 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000838 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000839 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000840 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000841 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000842 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843 addUnwrappedLine();
844 } else {
845 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000846 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000847 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000848 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000849 }
850}
851
852void UnwrappedLineParser::parseAccessSpecifier() {
853 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000854 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000855 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000856 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000857 addUnwrappedLine();
858}
859
860void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000861 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000862 if (FormatTok->Tok.is(tok::identifier) ||
863 FormatTok->Tok.is(tok::kw___attribute) ||
864 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000865 nextToken();
866 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000867 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000868 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000869 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000870 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000871 nextToken();
872 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000873 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000874 nextToken();
875 addUnwrappedLine();
876 ++Line->Level;
877 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000878 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000879 case tok::l_paren:
880 parseParens();
881 break;
882 case tok::r_brace:
883 addUnwrappedLine();
884 nextToken();
885 --Line->Level;
886 return;
887 case tok::comma:
888 nextToken();
889 addUnwrappedLine();
890 break;
891 default:
892 nextToken();
893 break;
894 }
895 } while (!eof());
896 }
897 // We fall through to parsing a structural element afterwards, so that in
898 // enum A {} n, m;
899 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000900}
901
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000902void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000903 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000904 if (FormatTok->Tok.is(tok::identifier) ||
905 FormatTok->Tok.is(tok::kw___attribute) ||
906 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000907 nextToken();
908 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000909 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000910 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000911 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000912 // The actual identifier can be a nested name specifier, and in macros
913 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000914 while (FormatTok->Tok.is(tok::identifier) ||
915 FormatTok->Tok.is(tok::coloncolon) ||
916 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000917 nextToken();
918
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000919 // Note that parsing away template declarations here leads to incorrectly
920 // accepting function declarations as record declarations.
921 // In general, we cannot solve this problem. Consider:
922 // class A<int> B() {}
923 // which can be a function definition or a class definition when B() is a
924 // macro. If we find enough real-world cases where this is a problem, we
925 // can parse for the 'template' keyword in the beginning of the statement,
926 // and thus rule out the record production in case there is no template
927 // (this would still leave us with an ambiguity between template function
928 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000929 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
930 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
931 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000932 return;
933 nextToken();
934 }
935 }
936 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000937 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000938 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
939 addUnwrappedLine();
940
Manuel Klimek70b03f42013-01-23 09:32:48 +0000941 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000942 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000943 // We fall through to parsing a structural element afterwards, so
944 // class A {} n, m;
945 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000946}
947
Nico Weber1abe6ea2013-01-09 21:15:03 +0000948void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000949 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000950 do
951 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000952 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000953 nextToken(); // Skip '>'.
954}
955
956void UnwrappedLineParser::parseObjCUntilAtEnd() {
957 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000958 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000959 nextToken();
960 addUnwrappedLine();
961 break;
962 }
963 parseStructuralElement();
964 } while (!eof());
965}
966
Nico Weber50767d82013-01-09 23:25:37 +0000967void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000968 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000969 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000970
971 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000972 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000973 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000974 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000975 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000976 // Skip category, if present.
977 parseParens();
978
Manuel Klimek96e888b2013-05-28 11:55:06 +0000979 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000980 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000981
982 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000983 if (FormatTok->Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000984 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000985
986 // With instance variables, this puts '}' on its own line. Without instance
987 // variables, this ends the @interface line.
988 addUnwrappedLine();
989
Nico Weber1abe6ea2013-01-09 21:15:03 +0000990 parseObjCUntilAtEnd();
991}
Nico Weber27d13672013-01-09 20:25:35 +0000992
Nico Weber1abe6ea2013-01-09 21:15:03 +0000993void UnwrappedLineParser::parseObjCProtocol() {
994 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000995 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +0000996
Manuel Klimek96e888b2013-05-28 11:55:06 +0000997 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000998 parseObjCProtocolList();
999
1000 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001001 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001002 nextToken();
1003 return addUnwrappedLine();
1004 }
1005
1006 addUnwrappedLine();
1007 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001008}
1009
Daniel Jasperbac016b2012-12-03 18:12:45 +00001010void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001011 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001012 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001013 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001014 llvm::dbgs() << "Line(" << Line->Level << ")"
1015 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001016 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1017 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001018 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001019 llvm::dbgs() << (*I)->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001020
Manuel Klimek8fa37992013-01-16 12:31:12 +00001021 }
1022 llvm::dbgs() << "\n";
1023 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001024 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001025 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001026 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001027 for (std::vector<UnwrappedLine>::iterator
1028 I = PreprocessorDirectives.begin(),
1029 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001030 I != E; ++I) {
1031 CurrentLines->push_back(*I);
1032 }
1033 PreprocessorDirectives.clear();
1034 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001035}
1036
Manuel Klimek96e888b2013-05-28 11:55:06 +00001037bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001038
Manuel Klimek86721d22013-01-22 16:31:55 +00001039void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1040 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001041 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001042 I = CommentsBeforeNextToken.begin(),
1043 E = CommentsBeforeNextToken.end();
1044 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001045 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001046 addUnwrappedLine();
1047 }
1048 pushToken(*I);
1049 }
1050 if (NewlineBeforeNext && JustComments) {
1051 addUnwrappedLine();
1052 }
1053 CommentsBeforeNextToken.clear();
1054}
1055
Daniel Jasperbac016b2012-12-03 18:12:45 +00001056void UnwrappedLineParser::nextToken() {
1057 if (eof())
1058 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001059 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001060 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001061 readToken();
1062}
1063
1064void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001065 bool CommentsInCurrentLine = true;
1066 do {
1067 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001068 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1069 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001070 // If there is an unfinished unwrapped line, we flush the preprocessor
1071 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001072 bool SwitchToPreprocessorLines =
1073 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001074 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001075 // Comments stored before the preprocessor directive need to be output
1076 // before the preprocessor directive, at the same level as the
1077 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001078 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001079 parsePPDirective();
1080 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001081
1082 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1083 !Line->InPPDirective) {
1084 continue;
1085 }
1086
Manuel Klimek96e888b2013-05-28 11:55:06 +00001087 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001088 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001089 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001090 CommentsInCurrentLine = false;
1091 }
1092 if (CommentsInCurrentLine) {
1093 pushToken(FormatTok);
1094 } else {
1095 CommentsBeforeNextToken.push_back(FormatTok);
1096 }
1097 } while (!eof());
1098}
1099
Manuel Klimek96e888b2013-05-28 11:55:06 +00001100void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001101 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001102 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001103 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001104 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001105 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001106}
1107
Daniel Jaspercd162382013-01-07 13:26:07 +00001108} // end namespace format
1109} // end namespace clang