blob: 316d324f7a4a2f075dd19ae824f6001b0775c2df [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;
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();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000604 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000605 }
606 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000607 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000608 case tok::equal:
609 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000610 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000611 parseBracedList();
612 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000613 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000614 default:
615 nextToken();
616 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000617 }
618 } while (!eof());
619}
620
Manuel Klimek80829bd2013-05-23 09:41:43 +0000621bool UnwrappedLineParser::tryToParseBracedList() {
622 if (LBraces[Tokens->getPosition()] == BS_Unknown)
623 calculateBraceTypes();
624 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
625 if (LBraces[Tokens->getPosition()] == BS_Block)
626 return false;
627 parseBracedList();
628 return true;
629}
630
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000631void UnwrappedLineParser::parseBracedList() {
632 nextToken();
633
Manuel Klimek423dd932013-04-10 09:52:05 +0000634 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
635 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000636 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000637 // FIXME: When we start to support lambdas, we'll want to parse them away
638 // here, otherwise our bail-out scenarios below break. The better solution
639 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000640 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000641 case tok::l_brace:
642 parseBracedList();
643 break;
644 case tok::r_brace:
645 nextToken();
646 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000647 case tok::semi:
648 // Probably a missing closing brace. Bail out.
649 return;
650 case tok::comma:
651 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000652 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000653 default:
654 nextToken();
655 break;
656 }
657 } while (!eof());
658}
659
Manuel Klimekc44ee892013-01-21 10:07:49 +0000660void UnwrappedLineParser::parseReturn() {
661 nextToken();
662
663 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000664 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000665 case tok::l_brace:
666 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000667 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000668 // Assume missing ';'.
669 addUnwrappedLine();
670 return;
671 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000672 break;
673 case tok::l_paren:
674 parseParens();
675 break;
676 case tok::r_brace:
677 // Assume missing ';'.
678 addUnwrappedLine();
679 return;
680 case tok::semi:
681 nextToken();
682 addUnwrappedLine();
683 return;
684 default:
685 nextToken();
686 break;
687 }
688 } while (!eof());
689}
690
Daniel Jasperbac016b2012-12-03 18:12:45 +0000691void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000692 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000693 nextToken();
694 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000695 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000696 case tok::l_paren:
697 parseParens();
698 break;
699 case tok::r_paren:
700 nextToken();
701 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000702 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000703 if (!tryToParseBracedList()) {
704 nextToken();
705 ScopedLineState LineState(*this);
706 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
707 /*MustBeDeclaration=*/ false);
708 Line->Level += 1;
709 parseLevel(/*HasOpeningBrace=*/ true);
710 Line->Level -= 1;
711 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000712 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000713 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000714 case tok::at:
715 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000716 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000717 parseBracedList();
718 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000719 default:
720 nextToken();
721 break;
722 }
723 } while (!eof());
724}
725
726void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000727 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000728 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000729 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000730 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000731 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000732 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000733 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000734 NeedsUnwrappedLine = true;
735 } else {
736 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000737 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000738 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000739 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000740 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000741 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000742 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000743 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000744 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000745 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000746 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000747 parseIfThenElse();
748 } else {
749 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000750 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000751 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000752 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000753 }
754 } else if (NeedsUnwrappedLine) {
755 addUnwrappedLine();
756 }
757}
758
Alexander Kornienko15757312012-12-06 18:03:27 +0000759void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000760 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000761 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000762 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000763 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000764 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000765 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
766 addUnwrappedLine();
767
Manuel Klimek70b03f42013-01-23 09:32:48 +0000768 parseBlock(/*MustBeDeclaration=*/ true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000769 // Munch the semicolon after a namespace. This is more common than one would
770 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000771 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000772 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000773 addUnwrappedLine();
774 }
775 // FIXME: Add error handling.
776}
777
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000778void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000779 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000780 "'for' or 'while' expected");
781 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000782 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000783 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000784 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000785 parseBlock(/*MustBeDeclaration=*/ false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000786 addUnwrappedLine();
787 } else {
788 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000789 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000790 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000791 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000792 }
793}
794
Daniel Jasperbac016b2012-12-03 18:12:45 +0000795void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000796 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000797 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000798 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000799 parseBlock(/*MustBeDeclaration=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000800 } else {
801 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000802 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000803 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000804 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000805 }
806
Alexander Kornienko393b0082012-12-04 15:40:36 +0000807 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000808 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000809 addUnwrappedLine();
810 return;
811 }
812
Daniel Jasperbac016b2012-12-03 18:12:45 +0000813 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000814 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000815}
816
817void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000818 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000819 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000820 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000821 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000822 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000823 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000824 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000825 parseBlock(/*MustBeDeclaration=*/ false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000826 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000827 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000828 }
829 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000830 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000831}
832
833void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000834 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000835 // FIXME: fix handling of complex expressions here.
836 do {
837 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000838 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000839 parseLabel();
840}
841
842void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000843 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000844 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000845 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000846 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000847 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek70b03f42013-01-23 09:32:48 +0000848 parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000849 addUnwrappedLine();
850 } else {
851 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000852 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000853 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000854 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000855 }
856}
857
858void UnwrappedLineParser::parseAccessSpecifier() {
859 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000860 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000861 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000862 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863 addUnwrappedLine();
864}
865
866void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000867 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000868 if (FormatTok->Tok.is(tok::identifier) ||
869 FormatTok->Tok.is(tok::kw___attribute) ||
870 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000871 nextToken();
872 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000873 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000874 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000875 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000876 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000877 nextToken();
878 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000879 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000880 nextToken();
881 addUnwrappedLine();
882 ++Line->Level;
883 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000884 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000885 case tok::l_paren:
886 parseParens();
887 break;
888 case tok::r_brace:
889 addUnwrappedLine();
890 nextToken();
891 --Line->Level;
892 return;
893 case tok::comma:
894 nextToken();
895 addUnwrappedLine();
896 break;
897 default:
898 nextToken();
899 break;
900 }
901 } while (!eof());
902 }
903 // We fall through to parsing a structural element afterwards, so that in
904 // enum A {} n, m;
905 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000906}
907
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000908void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000909 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000910 if (FormatTok->Tok.is(tok::identifier) ||
911 FormatTok->Tok.is(tok::kw___attribute) ||
912 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000913 nextToken();
914 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000915 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000916 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000917 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000918 // The actual identifier can be a nested name specifier, and in macros
919 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000920 while (FormatTok->Tok.is(tok::identifier) ||
921 FormatTok->Tok.is(tok::coloncolon) ||
922 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000923 nextToken();
924
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000925 // Note that parsing away template declarations here leads to incorrectly
926 // accepting function declarations as record declarations.
927 // In general, we cannot solve this problem. Consider:
928 // class A<int> B() {}
929 // which can be a function definition or a class definition when B() is a
930 // macro. If we find enough real-world cases where this is a problem, we
931 // can parse for the 'template' keyword in the beginning of the statement,
932 // and thus rule out the record production in case there is no template
933 // (this would still leave us with an ambiguity between template function
934 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000935 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
936 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
937 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000938 return;
939 nextToken();
940 }
941 }
942 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000943 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000944 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
945 addUnwrappedLine();
946
Manuel Klimek70b03f42013-01-23 09:32:48 +0000947 parseBlock(/*MustBeDeclaration=*/ true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000948 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000949 // We fall through to parsing a structural element afterwards, so
950 // class A {} n, m;
951 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000952}
953
Nico Weber1abe6ea2013-01-09 21:15:03 +0000954void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000955 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000956 do
957 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000958 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000959 nextToken(); // Skip '>'.
960}
961
962void UnwrappedLineParser::parseObjCUntilAtEnd() {
963 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000964 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000965 nextToken();
966 addUnwrappedLine();
967 break;
968 }
969 parseStructuralElement();
970 } while (!eof());
971}
972
Nico Weber50767d82013-01-09 23:25:37 +0000973void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000974 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000975 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000976
977 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000978 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000979 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000980 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000981 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000982 // Skip category, if present.
983 parseParens();
984
Manuel Klimek96e888b2013-05-28 11:55:06 +0000985 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000986 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000987
988 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000989 if (FormatTok->Tok.is(tok::l_brace))
Manuel Klimek70b03f42013-01-23 09:32:48 +0000990 parseBlock(/*MustBeDeclaration=*/ true);
Nico Weber27d13672013-01-09 20:25:35 +0000991
992 // With instance variables, this puts '}' on its own line. Without instance
993 // variables, this ends the @interface line.
994 addUnwrappedLine();
995
Nico Weber1abe6ea2013-01-09 21:15:03 +0000996 parseObjCUntilAtEnd();
997}
Nico Weber27d13672013-01-09 20:25:35 +0000998
Nico Weber1abe6ea2013-01-09 21:15:03 +0000999void UnwrappedLineParser::parseObjCProtocol() {
1000 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001001 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001002
Manuel Klimek96e888b2013-05-28 11:55:06 +00001003 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001004 parseObjCProtocolList();
1005
1006 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001007 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001008 nextToken();
1009 return addUnwrappedLine();
1010 }
1011
1012 addUnwrappedLine();
1013 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001014}
1015
Daniel Jasperbac016b2012-12-03 18:12:45 +00001016void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001017 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001018 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001019 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001020 llvm::dbgs() << "Line(" << Line->Level << ")"
1021 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001022 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1023 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001024 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001025 llvm::dbgs() << (*I)->Tok.getName() << " ";
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001026
Manuel Klimek8fa37992013-01-16 12:31:12 +00001027 }
1028 llvm::dbgs() << "\n";
1029 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001030 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001031 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001032 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001033 for (std::vector<UnwrappedLine>::iterator
1034 I = PreprocessorDirectives.begin(),
1035 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001036 I != E; ++I) {
1037 CurrentLines->push_back(*I);
1038 }
1039 PreprocessorDirectives.clear();
1040 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001041}
1042
Manuel Klimek96e888b2013-05-28 11:55:06 +00001043bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001044
Manuel Klimek86721d22013-01-22 16:31:55 +00001045void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1046 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001047 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001048 I = CommentsBeforeNextToken.begin(),
1049 E = CommentsBeforeNextToken.end();
1050 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001051 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001052 addUnwrappedLine();
1053 }
1054 pushToken(*I);
1055 }
1056 if (NewlineBeforeNext && JustComments) {
1057 addUnwrappedLine();
1058 }
1059 CommentsBeforeNextToken.clear();
1060}
1061
Daniel Jasperbac016b2012-12-03 18:12:45 +00001062void UnwrappedLineParser::nextToken() {
1063 if (eof())
1064 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001065 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001066 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001067 readToken();
1068}
1069
1070void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001071 bool CommentsInCurrentLine = true;
1072 do {
1073 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001074 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1075 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001076 // If there is an unfinished unwrapped line, we flush the preprocessor
1077 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001078 bool SwitchToPreprocessorLines =
1079 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001080 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001081 // Comments stored before the preprocessor directive need to be output
1082 // before the preprocessor directive, at the same level as the
1083 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001084 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001085 parsePPDirective();
1086 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001087
1088 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1089 !Line->InPPDirective) {
1090 continue;
1091 }
1092
Manuel Klimek96e888b2013-05-28 11:55:06 +00001093 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001094 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001095 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001096 CommentsInCurrentLine = false;
1097 }
1098 if (CommentsInCurrentLine) {
1099 pushToken(FormatTok);
1100 } else {
1101 CommentsBeforeNextToken.push_back(FormatTok);
1102 }
1103 } while (!eof());
1104}
1105
Manuel Klimek96e888b2013-05-28 11:55:06 +00001106void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001107 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001108 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001109 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001110 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001111 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001112}
1113
Daniel Jaspercd162382013-01-07 13:26:07 +00001114} // end namespace format
1115} // end namespace clang