blob: 618f54a5184f587195d6c614e30376bfc57bba84 [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.
Daniel Jaspereb483662013-05-31 10:09:55 +0000267 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
268 tok::l_brace, tok::colon))
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;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000584 case tok::identifier: {
585 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000586 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000587 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000588 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000589 parseLabel();
590 return;
591 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000592 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000593 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000594 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000595 if (FormatTok->HasUnescapedNewline &&
596 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000597 addUnwrappedLine();
598 return;
599 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000600 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
601 Text == Text.upper()) {
602 // Recognize free-standing macros like Q_OBJECT.
603 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000604 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000605 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000606 }
607 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000608 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000609 case tok::equal:
610 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000611 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000612 parseBracedList();
613 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000614 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000615 default:
616 nextToken();
617 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000618 }
619 } while (!eof());
620}
621
Manuel Klimek80829bd2013-05-23 09:41:43 +0000622bool UnwrappedLineParser::tryToParseBracedList() {
623 if (LBraces[Tokens->getPosition()] == BS_Unknown)
624 calculateBraceTypes();
625 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
626 if (LBraces[Tokens->getPosition()] == BS_Block)
627 return false;
628 parseBracedList();
629 return true;
630}
631
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000632void UnwrappedLineParser::parseBracedList() {
633 nextToken();
634
Manuel Klimek423dd932013-04-10 09:52:05 +0000635 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
636 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000637 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000638 // FIXME: When we start to support lambdas, we'll want to parse them away
639 // here, otherwise our bail-out scenarios below break. The better solution
640 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000641 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000642 case tok::l_brace:
643 parseBracedList();
644 break;
645 case tok::r_brace:
646 nextToken();
647 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000648 case tok::semi:
649 // Probably a missing closing brace. Bail out.
650 return;
651 case tok::comma:
652 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000653 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000654 default:
655 nextToken();
656 break;
657 }
658 } while (!eof());
659}
660
Manuel Klimekc44ee892013-01-21 10:07:49 +0000661void UnwrappedLineParser::parseReturn() {
662 nextToken();
663
664 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000665 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000666 case tok::l_brace:
667 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000668 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000669 // Assume missing ';'.
670 addUnwrappedLine();
671 return;
672 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000673 break;
674 case tok::l_paren:
675 parseParens();
676 break;
677 case tok::r_brace:
678 // Assume missing ';'.
679 addUnwrappedLine();
680 return;
681 case tok::semi:
682 nextToken();
683 addUnwrappedLine();
684 return;
685 default:
686 nextToken();
687 break;
688 }
689 } while (!eof());
690}
691
Daniel Jasperbac016b2012-12-03 18:12:45 +0000692void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000693 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000694 nextToken();
695 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000696 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000697 case tok::l_paren:
698 parseParens();
699 break;
700 case tok::r_paren:
701 nextToken();
702 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000703 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000704 if (!tryToParseBracedList()) {
705 nextToken();
706 ScopedLineState LineState(*this);
707 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
708 /*MustBeDeclaration=*/ false);
709 Line->Level += 1;
710 parseLevel(/*HasOpeningBrace=*/ true);
711 Line->Level -= 1;
712 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000713 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000714 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000715 case tok::at:
716 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000717 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000718 parseBracedList();
719 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000720 default:
721 nextToken();
722 break;
723 }
724 } while (!eof());
725}
726
727void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000728 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000729 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000730 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000731 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000732 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000733 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000734 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000735 NeedsUnwrappedLine = true;
736 } else {
737 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000738 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000739 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000740 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000741 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000742 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000743 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000744 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000745 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000746 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000747 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000748 parseIfThenElse();
749 } else {
750 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000751 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000752 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000753 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000754 }
755 } else if (NeedsUnwrappedLine) {
756 addUnwrappedLine();
757 }
758}
759
Alexander Kornienko15757312012-12-06 18:03:27 +0000760void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000761 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000762 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000763 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000764 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000765 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000766 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
767 addUnwrappedLine();
768
Manuel Klimek70b03f42013-01-23 09:32:48 +0000769 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000770 // Munch the semicolon after a namespace. This is more common than one would
771 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000772 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000773 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000774 addUnwrappedLine();
775 }
776 // FIXME: Add error handling.
777}
778
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000779void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000780 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000781 "'for' or 'while' expected");
782 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000783 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000784 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000785 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000786 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000787 addUnwrappedLine();
788 } else {
789 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000790 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000791 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000792 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000793 }
794}
795
Daniel Jasperbac016b2012-12-03 18:12:45 +0000796void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000797 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000798 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000799 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000800 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000801 } else {
802 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000803 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000804 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000805 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000806 }
807
Alexander Kornienko393b0082012-12-04 15:40:36 +0000808 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000809 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000810 addUnwrappedLine();
811 return;
812 }
813
Daniel Jasperbac016b2012-12-03 18:12:45 +0000814 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000815 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000816}
817
818void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000819 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000820 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000821 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000822 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000823 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000824 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000825 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000826 parseBlock(/*MustBeDeclaration=*/ false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000827 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000828 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000829 }
830 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000831 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000832}
833
834void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000835 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000836 // FIXME: fix handling of complex expressions here.
837 do {
838 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000839 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000840 parseLabel();
841}
842
843void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000844 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000845 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000846 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000847 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000848 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000849 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000850 addUnwrappedLine();
851 } else {
852 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000853 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000854 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000855 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000856 }
857}
858
859void UnwrappedLineParser::parseAccessSpecifier() {
860 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000861 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000862 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000863 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000864 addUnwrappedLine();
865}
866
867void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000868 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000869 if (FormatTok->Tok.is(tok::identifier) ||
870 FormatTok->Tok.is(tok::kw___attribute) ||
871 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000872 nextToken();
873 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000874 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000875 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000876 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000877 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000878 nextToken();
879 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000880 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000881 nextToken();
882 addUnwrappedLine();
883 ++Line->Level;
884 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000885 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000886 case tok::l_paren:
887 parseParens();
888 break;
889 case tok::r_brace:
890 addUnwrappedLine();
891 nextToken();
892 --Line->Level;
893 return;
894 case tok::comma:
895 nextToken();
896 addUnwrappedLine();
897 break;
898 default:
899 nextToken();
900 break;
901 }
902 } while (!eof());
903 }
904 // We fall through to parsing a structural element afterwards, so that in
905 // enum A {} n, m;
906 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000907}
908
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000909void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000910 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000911 if (FormatTok->Tok.is(tok::identifier) ||
912 FormatTok->Tok.is(tok::kw___attribute) ||
913 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000914 nextToken();
915 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000916 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000917 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000918 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000919 // The actual identifier can be a nested name specifier, and in macros
920 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000921 while (FormatTok->Tok.is(tok::identifier) ||
922 FormatTok->Tok.is(tok::coloncolon) ||
923 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000924 nextToken();
925
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000926 // Note that parsing away template declarations here leads to incorrectly
927 // accepting function declarations as record declarations.
928 // In general, we cannot solve this problem. Consider:
929 // class A<int> B() {}
930 // which can be a function definition or a class definition when B() is a
931 // macro. If we find enough real-world cases where this is a problem, we
932 // can parse for the 'template' keyword in the beginning of the statement,
933 // and thus rule out the record production in case there is no template
934 // (this would still leave us with an ambiguity between template function
935 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000936 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
937 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
938 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000939 return;
940 nextToken();
941 }
942 }
943 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000944 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000945 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
946 addUnwrappedLine();
947
Manuel Klimek70b03f42013-01-23 09:32:48 +0000948 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000949 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000950 // We fall through to parsing a structural element afterwards, so
951 // class A {} n, m;
952 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000953}
954
Nico Weber1abe6ea2013-01-09 21:15:03 +0000955void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000956 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000957 do
958 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000959 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000960 nextToken(); // Skip '>'.
961}
962
963void UnwrappedLineParser::parseObjCUntilAtEnd() {
964 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000965 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000966 nextToken();
967 addUnwrappedLine();
968 break;
969 }
970 parseStructuralElement();
971 } while (!eof());
972}
973
Nico Weber50767d82013-01-09 23:25:37 +0000974void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000975 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000976 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000977
978 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000979 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000980 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000981 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000982 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000983 // Skip category, if present.
984 parseParens();
985
Manuel Klimek96e888b2013-05-28 11:55:06 +0000986 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000987 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000988
989 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000990 if (FormatTok->Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000991 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000992
993 // With instance variables, this puts '}' on its own line. Without instance
994 // variables, this ends the @interface line.
995 addUnwrappedLine();
996
Nico Weber1abe6ea2013-01-09 21:15:03 +0000997 parseObjCUntilAtEnd();
998}
Nico Weber27d13672013-01-09 20:25:35 +0000999
Nico Weber1abe6ea2013-01-09 21:15:03 +00001000void UnwrappedLineParser::parseObjCProtocol() {
1001 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001002 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001003
Manuel Klimek96e888b2013-05-28 11:55:06 +00001004 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001005 parseObjCProtocolList();
1006
1007 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001008 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001009 nextToken();
1010 return addUnwrappedLine();
1011 }
1012
1013 addUnwrappedLine();
1014 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001015}
1016
Daniel Jasperbac016b2012-12-03 18:12:45 +00001017void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001018 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001019 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001020 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001021 llvm::dbgs() << "Line(" << Line->Level << ")"
1022 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001023 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1024 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001025 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001026 llvm::dbgs() << (*I)->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001027
Manuel Klimek8fa37992013-01-16 12:31:12 +00001028 }
1029 llvm::dbgs() << "\n";
1030 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001031 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001032 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001033 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001034 for (std::vector<UnwrappedLine>::iterator
1035 I = PreprocessorDirectives.begin(),
1036 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001037 I != E; ++I) {
1038 CurrentLines->push_back(*I);
1039 }
1040 PreprocessorDirectives.clear();
1041 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001042}
1043
Manuel Klimek96e888b2013-05-28 11:55:06 +00001044bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001045
Manuel Klimek86721d22013-01-22 16:31:55 +00001046void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1047 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001048 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001049 I = CommentsBeforeNextToken.begin(),
1050 E = CommentsBeforeNextToken.end();
1051 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001052 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001053 addUnwrappedLine();
1054 }
1055 pushToken(*I);
1056 }
1057 if (NewlineBeforeNext && JustComments) {
1058 addUnwrappedLine();
1059 }
1060 CommentsBeforeNextToken.clear();
1061}
1062
Daniel Jasperbac016b2012-12-03 18:12:45 +00001063void UnwrappedLineParser::nextToken() {
1064 if (eof())
1065 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001066 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001067 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001068 readToken();
1069}
1070
1071void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001072 bool CommentsInCurrentLine = true;
1073 do {
1074 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001075 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1076 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001077 // If there is an unfinished unwrapped line, we flush the preprocessor
1078 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001079 bool SwitchToPreprocessorLines =
1080 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001081 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001082 // Comments stored before the preprocessor directive need to be output
1083 // before the preprocessor directive, at the same level as the
1084 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001085 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001086 parsePPDirective();
1087 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001088
1089 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1090 !Line->InPPDirective) {
1091 continue;
1092 }
1093
Manuel Klimek96e888b2013-05-28 11:55:06 +00001094 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001095 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001096 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001097 CommentsInCurrentLine = false;
1098 }
1099 if (CommentsInCurrentLine) {
1100 pushToken(FormatTok);
1101 } else {
1102 CommentsBeforeNextToken.push_back(FormatTok);
1103 }
1104 } while (!eof());
1105}
1106
Manuel Klimek96e888b2013-05-28 11:55:06 +00001107void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001108 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001109 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001110 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001111 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001112 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001113}
1114
Daniel Jaspercd162382013-01-07 13:26:07 +00001115} // end namespace format
1116} // end namespace clang