blob: 2d44cdb12f47e22376e9b4523acdbb96a6e12f36 [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.
630 bool StartOfExpression = true;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000631 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000632 // FIXME: When we start to support lambdas, we'll want to parse them away
633 // here, otherwise our bail-out scenarios below break. The better solution
634 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000635 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000636 case tok::l_brace:
Manuel Klimek423dd932013-04-10 09:52:05 +0000637 if (!StartOfExpression) {
638 // Probably a missing closing brace. Bail out.
639 addUnwrappedLine();
640 return;
641 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000642 parseBracedList();
Manuel Klimek423dd932013-04-10 09:52:05 +0000643 StartOfExpression = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000644 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();
653 StartOfExpression = true;
654 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000655 default:
656 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000657 StartOfExpression = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000658 break;
659 }
660 } while (!eof());
661}
662
Manuel Klimekc44ee892013-01-21 10:07:49 +0000663void UnwrappedLineParser::parseReturn() {
664 nextToken();
665
666 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000667 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000668 case tok::l_brace:
669 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000670 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000671 // Assume missing ';'.
672 addUnwrappedLine();
673 return;
674 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000675 break;
676 case tok::l_paren:
677 parseParens();
678 break;
679 case tok::r_brace:
680 // Assume missing ';'.
681 addUnwrappedLine();
682 return;
683 case tok::semi:
684 nextToken();
685 addUnwrappedLine();
686 return;
687 default:
688 nextToken();
689 break;
690 }
691 } while (!eof());
692}
693
Daniel Jasperbac016b2012-12-03 18:12:45 +0000694void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000695 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000696 nextToken();
697 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000698 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000699 case tok::l_paren:
700 parseParens();
701 break;
702 case tok::r_paren:
703 nextToken();
704 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000705 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000706 if (!tryToParseBracedList()) {
707 nextToken();
708 ScopedLineState LineState(*this);
709 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
710 /*MustBeDeclaration=*/ false);
711 Line->Level += 1;
712 parseLevel(/*HasOpeningBrace=*/ true);
713 Line->Level -= 1;
714 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000715 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000716 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000717 case tok::at:
718 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000719 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000720 parseBracedList();
721 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000722 default:
723 nextToken();
724 break;
725 }
726 } while (!eof());
727}
728
729void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000730 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000731 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000732 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000733 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000734 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000735 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000736 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000737 NeedsUnwrappedLine = true;
738 } else {
739 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000740 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000741 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000742 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000743 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000744 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000745 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000746 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000747 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000748 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000749 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000750 parseIfThenElse();
751 } else {
752 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000753 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000754 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000755 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000756 }
757 } else if (NeedsUnwrappedLine) {
758 addUnwrappedLine();
759 }
760}
761
Alexander Kornienko15757312012-12-06 18:03:27 +0000762void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000763 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000764 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000765 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000766 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000767 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000768 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
769 addUnwrappedLine();
770
Manuel Klimek70b03f42013-01-23 09:32:48 +0000771 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000772 // Munch the semicolon after a namespace. This is more common than one would
773 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000774 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000775 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000776 addUnwrappedLine();
777 }
778 // FIXME: Add error handling.
779}
780
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000781void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000782 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000783 "'for' or 'while' expected");
784 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000785 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000786 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000787 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000788 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000789 addUnwrappedLine();
790 } else {
791 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000792 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000793 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000794 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000795 }
796}
797
Daniel Jasperbac016b2012-12-03 18:12:45 +0000798void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000799 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000800 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000801 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000802 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000803 } else {
804 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000805 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000806 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000807 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000808 }
809
Alexander Kornienko393b0082012-12-04 15:40:36 +0000810 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000811 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000812 addUnwrappedLine();
813 return;
814 }
815
Daniel Jasperbac016b2012-12-03 18:12:45 +0000816 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000817 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000818}
819
820void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000821 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000822 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000823 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000824 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000825 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000826 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000827 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000828 parseBlock(/*MustBeDeclaration=*/ false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000829 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000830 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000831 }
832 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000833 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000834}
835
836void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000837 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000838 // FIXME: fix handling of complex expressions here.
839 do {
840 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000841 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000842 parseLabel();
843}
844
845void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000846 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000847 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000848 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000849 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000850 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000851 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000852 addUnwrappedLine();
853 } else {
854 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000855 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000856 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000857 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000858 }
859}
860
861void UnwrappedLineParser::parseAccessSpecifier() {
862 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000863 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000864 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000865 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000866 addUnwrappedLine();
867}
868
869void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000870 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000871 if (FormatTok->Tok.is(tok::identifier) ||
872 FormatTok->Tok.is(tok::kw___attribute) ||
873 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000874 nextToken();
875 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000876 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000877 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000878 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000879 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000880 nextToken();
881 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000882 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000883 nextToken();
884 addUnwrappedLine();
885 ++Line->Level;
886 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000887 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000888 case tok::l_paren:
889 parseParens();
890 break;
891 case tok::r_brace:
892 addUnwrappedLine();
893 nextToken();
894 --Line->Level;
895 return;
896 case tok::comma:
897 nextToken();
898 addUnwrappedLine();
899 break;
900 default:
901 nextToken();
902 break;
903 }
904 } while (!eof());
905 }
906 // We fall through to parsing a structural element afterwards, so that in
907 // enum A {} n, m;
908 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000909}
910
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000911void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000912 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000913 if (FormatTok->Tok.is(tok::identifier) ||
914 FormatTok->Tok.is(tok::kw___attribute) ||
915 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000916 nextToken();
917 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000918 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000919 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000920 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000921 // The actual identifier can be a nested name specifier, and in macros
922 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000923 while (FormatTok->Tok.is(tok::identifier) ||
924 FormatTok->Tok.is(tok::coloncolon) ||
925 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000926 nextToken();
927
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000928 // Note that parsing away template declarations here leads to incorrectly
929 // accepting function declarations as record declarations.
930 // In general, we cannot solve this problem. Consider:
931 // class A<int> B() {}
932 // which can be a function definition or a class definition when B() is a
933 // macro. If we find enough real-world cases where this is a problem, we
934 // can parse for the 'template' keyword in the beginning of the statement,
935 // and thus rule out the record production in case there is no template
936 // (this would still leave us with an ambiguity between template function
937 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000938 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
939 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
940 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000941 return;
942 nextToken();
943 }
944 }
945 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000946 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000947 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
948 addUnwrappedLine();
949
Manuel Klimek70b03f42013-01-23 09:32:48 +0000950 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000951 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000952 // We fall through to parsing a structural element afterwards, so
953 // class A {} n, m;
954 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000955}
956
Nico Weber1abe6ea2013-01-09 21:15:03 +0000957void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000958 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000959 do
960 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000961 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000962 nextToken(); // Skip '>'.
963}
964
965void UnwrappedLineParser::parseObjCUntilAtEnd() {
966 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000967 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000968 nextToken();
969 addUnwrappedLine();
970 break;
971 }
972 parseStructuralElement();
973 } while (!eof());
974}
975
Nico Weber50767d82013-01-09 23:25:37 +0000976void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000977 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000978 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000979
980 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000981 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000982 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000983 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000984 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000985 // Skip category, if present.
986 parseParens();
987
Manuel Klimek96e888b2013-05-28 11:55:06 +0000988 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000989 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000990
991 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000992 if (FormatTok->Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000993 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000994
995 // With instance variables, this puts '}' on its own line. Without instance
996 // variables, this ends the @interface line.
997 addUnwrappedLine();
998
Nico Weber1abe6ea2013-01-09 21:15:03 +0000999 parseObjCUntilAtEnd();
1000}
Nico Weber27d13672013-01-09 20:25:35 +00001001
Nico Weber1abe6ea2013-01-09 21:15:03 +00001002void UnwrappedLineParser::parseObjCProtocol() {
1003 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001004 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001005
Manuel Klimek96e888b2013-05-28 11:55:06 +00001006 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001007 parseObjCProtocolList();
1008
1009 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001010 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001011 nextToken();
1012 return addUnwrappedLine();
1013 }
1014
1015 addUnwrappedLine();
1016 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001017}
1018
Daniel Jasperbac016b2012-12-03 18:12:45 +00001019void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001020 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001021 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001022 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001023 llvm::dbgs() << "Line(" << Line->Level << ")"
1024 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001025 for (std::list<FormatToken>::iterator I = Line->Tokens.begin(),
1026 E = Line->Tokens.end();
1027 I != E; ++I) {
1028 llvm::dbgs() << I->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001029
Manuel Klimek8fa37992013-01-16 12:31:12 +00001030 }
1031 llvm::dbgs() << "\n";
1032 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001033 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001034 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001035 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001036 for (std::vector<UnwrappedLine>::iterator
1037 I = PreprocessorDirectives.begin(),
1038 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001039 I != E; ++I) {
1040 CurrentLines->push_back(*I);
1041 }
1042 PreprocessorDirectives.clear();
1043 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001044}
1045
Manuel Klimek96e888b2013-05-28 11:55:06 +00001046bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001047
Manuel Klimek86721d22013-01-22 16:31:55 +00001048void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1049 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001050 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001051 I = CommentsBeforeNextToken.begin(),
1052 E = CommentsBeforeNextToken.end();
1053 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001054 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001055 addUnwrappedLine();
1056 }
1057 pushToken(*I);
1058 }
1059 if (NewlineBeforeNext && JustComments) {
1060 addUnwrappedLine();
1061 }
1062 CommentsBeforeNextToken.clear();
1063}
1064
Daniel Jasperbac016b2012-12-03 18:12:45 +00001065void UnwrappedLineParser::nextToken() {
1066 if (eof())
1067 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001068 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001069 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001070 readToken();
1071}
1072
1073void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001074 bool CommentsInCurrentLine = true;
1075 do {
1076 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001077 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1078 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001079 // If there is an unfinished unwrapped line, we flush the preprocessor
1080 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001081 bool SwitchToPreprocessorLines =
1082 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001083 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001084 // Comments stored before the preprocessor directive need to be output
1085 // before the preprocessor directive, at the same level as the
1086 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001087 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001088 parsePPDirective();
1089 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001090
1091 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1092 !Line->InPPDirective) {
1093 continue;
1094 }
1095
Manuel Klimek96e888b2013-05-28 11:55:06 +00001096 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001097 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001098 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001099 CommentsInCurrentLine = false;
1100 }
1101 if (CommentsInCurrentLine) {
1102 pushToken(FormatTok);
1103 } else {
1104 CommentsBeforeNextToken.push_back(FormatTok);
1105 }
1106 } while (!eof());
1107}
1108
Manuel Klimek96e888b2013-05-28 11:55:06 +00001109void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1110 Line->Tokens.push_back(*Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001111 if (MustBreakBeforeNextToken) {
1112 Line->Tokens.back().MustBreakBefore = true;
1113 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001114 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001115}
1116
Daniel Jaspercd162382013-01-07 13:26:07 +00001117} // end namespace format
1118} // end namespace clang