blob: e1c1468bc13f5160a402773173f0e29d91f08def [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 }
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000048
Manuel Klimek70b03f42013-01-23 09:32:48 +000049private:
50 UnwrappedLine &Line;
51 std::vector<bool> &Stack;
52};
53
Manuel Klimekd4397b92013-01-04 23:34:14 +000054class ScopedMacroState : public FormatTokenSource {
55public:
56 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek96e888b2013-05-28 11:55:06 +000057 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimekd4397b92013-01-04 23:34:14 +000058 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek67d080d2013-04-12 14:13:36 +000059 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
60 StructuralError(StructuralError),
Manuel Klimek96e888b2013-05-28 11:55:06 +000061 PreviousStructuralError(StructuralError), Token(NULL) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000062 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000063 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 Line.InPPDirective = true;
65 }
66
67 ~ScopedMacroState() {
68 TokenSource = PreviousTokenSource;
69 ResetToken = Token;
70 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000071 Line.Level = PreviousLineLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +000072 StructuralError = PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +000073 }
74
Manuel Klimek96e888b2013-05-28 11:55:06 +000075 virtual FormatToken *getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000076 // The \c UnwrappedLineParser guards against this by never calling
77 // \c getNextToken() after it has encountered the first eof token.
78 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000079 Token = PreviousTokenSource->getNextToken();
80 if (eof())
Manuel Klimek96e888b2013-05-28 11:55:06 +000081 return getFakeEOF();
Manuel Klimekd4397b92013-01-04 23:34:14 +000082 return Token;
83 }
84
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000085 virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); }
Manuel Klimek80829bd2013-05-23 09:41:43 +000086
Manuel Klimek96e888b2013-05-28 11:55:06 +000087 virtual FormatToken *setPosition(unsigned Position) {
Manuel Klimek80829bd2013-05-23 09:41:43 +000088 Token = PreviousTokenSource->setPosition(Position);
89 return Token;
90 }
91
Manuel Klimekd4397b92013-01-04 23:34:14 +000092private:
Manuel Klimek96e888b2013-05-28 11:55:06 +000093 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000094
Manuel Klimek96e888b2013-05-28 11:55:06 +000095 FormatToken *getFakeEOF() {
96 static bool EOFInitialized = false;
97 static FormatToken FormatTok;
98 if (!EOFInitialized) {
99 FormatTok.Tok.startToken();
100 FormatTok.Tok.setKind(tok::eof);
101 EOFInitialized = true;
102 }
103 return &FormatTok;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000104 }
105
106 UnwrappedLine &Line;
107 FormatTokenSource *&TokenSource;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000108 FormatToken *&ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000109 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000110 FormatTokenSource *PreviousTokenSource;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000111 bool &StructuralError;
112 bool PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000113
Manuel Klimek96e888b2013-05-28 11:55:06 +0000114 FormatToken *Token;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000115};
116
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000117class ScopedLineState {
118public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000119 ScopedLineState(UnwrappedLineParser &Parser,
120 bool SwitchToPreprocessorLines = false)
121 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
122 if (SwitchToPreprocessorLines)
123 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000124 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000125 Parser.Line.reset(new UnwrappedLine());
126 Parser.Line->Level = PreBlockLine->Level;
127 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000128 }
129
130 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000131 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000132 Parser.addUnwrappedLine();
133 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000134 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000135 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000136 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000137 if (SwitchToPreprocessorLines)
138 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000139 }
140
141private:
142 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000143 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000144
145 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000146};
147
Manuel Klimek80829bd2013-05-23 09:41:43 +0000148class IndexedTokenSource : public FormatTokenSource {
149public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000150 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000151 : Tokens(Tokens), Position(-1) {}
152
Manuel Klimek96e888b2013-05-28 11:55:06 +0000153 virtual FormatToken *getNextToken() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000154 ++Position;
155 return Tokens[Position];
156 }
157
158 virtual unsigned getPosition() {
159 assert(Position >= 0);
160 return Position;
161 }
162
Manuel Klimek96e888b2013-05-28 11:55:06 +0000163 virtual FormatToken *setPosition(unsigned P) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000164 Position = P;
165 return Tokens[Position];
166 }
167
168private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000169 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000170 int Position;
171};
172
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000173UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000174 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000175 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000176 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000177 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
178 Callback(Callback), AllTokens(Tokens) {
179 LBraces.resize(Tokens.size(), BS_Unknown);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000180}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000181
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000182bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000183 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek80829bd2013-05-23 09:41:43 +0000184 IndexedTokenSource TokenSource(AllTokens);
185 Tokens = &TokenSource;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000186 readToken();
Manuel Klimek67d080d2013-04-12 14:13:36 +0000187 parseFile();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000188 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000189 I != E; ++I) {
190 Callback.consumeUnwrappedLine(*I);
191 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000192
193 // Create line with eof token.
194 pushToken(FormatTok);
195 Callback.consumeUnwrappedLine(*Line);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000196 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000197}
198
Manuel Klimek67d080d2013-04-12 14:13:36 +0000199void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000200 ScopedDeclarationState DeclarationState(
201 *Line, DeclarationScopeStack,
202 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000203 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000204 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000205 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000206 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000207}
208
Manuel Klimek67d080d2013-04-12 14:13:36 +0000209void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000210 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000211 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000212 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000213 nextToken();
214 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000215 break;
216 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000217 // FIXME: Add parameter whether this can happen - if this happens, we must
218 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000219 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000220 addUnwrappedLine();
221 break;
222 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000223 if (HasOpeningBrace)
224 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000225 StructuralError = true;
226 nextToken();
227 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000228 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000229 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000230 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000231 break;
232 }
233 } while (!eof());
234}
235
Manuel Klimek80829bd2013-05-23 09:41:43 +0000236void UnwrappedLineParser::calculateBraceTypes() {
237 // We'll parse forward through the tokens until we hit
238 // a closing brace or eof - note that getNextToken() will
239 // parse macros, so this will magically work inside macro
240 // definitions, too.
241 unsigned StoredPosition = Tokens->getPosition();
242 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000243 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000244 // Keep a stack of positions of lbrace tokens. We will
245 // update information about whether an lbrace starts a
246 // braced init list or a different block during the loop.
247 SmallVector<unsigned, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000248 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000249 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000250 FormatToken *NextTok = Tokens->getNextToken();
251 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000252 case tok::l_brace:
253 LBraceStack.push_back(Position);
254 break;
255 case tok::r_brace:
256 if (!LBraceStack.empty()) {
257 if (LBraces[LBraceStack.back()] == BS_Unknown) {
258 // If there is a comma, semicolon or right paren after the closing
259 // brace, we assume this is a braced initializer list.
260
261 // FIXME: Note that this currently works only because we do not
262 // use the brace information while inside a braced init list.
263 // Thus, if the parent is a braced init list, we consider all
264 // brace blocks inside it braced init list. That works good enough
265 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jaspereb483662013-05-31 10:09:55 +0000266 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
267 tok::l_brace, tok::colon))
Manuel Klimek80829bd2013-05-23 09:41:43 +0000268 LBraces[LBraceStack.back()] = BS_BracedInit;
269 else
270 LBraces[LBraceStack.back()] = BS_Block;
271 }
272 LBraceStack.pop_back();
273 }
274 break;
275 case tok::semi:
276 case tok::kw_if:
277 case tok::kw_while:
278 case tok::kw_for:
279 case tok::kw_switch:
280 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000281 if (!LBraceStack.empty())
Manuel Klimek80829bd2013-05-23 09:41:43 +0000282 LBraces[LBraceStack.back()] = BS_Block;
283 break;
284 default:
285 break;
286 }
287 Tok = NextTok;
288 ++Position;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000289 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000290 // Assume other blocks for all unclosed opening braces.
291 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
292 if (LBraces[LBraceStack[i]] == BS_Unknown)
293 LBraces[LBraceStack[i]] = BS_Block;
294 }
295 FormatTok = Tokens->setPosition(StoredPosition);
296}
297
Manuel Klimek67d080d2013-04-12 14:13:36 +0000298void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
Nico Weberd74fcdb2013-02-10 20:35:35 +0000299 unsigned AddLevels) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000300 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000301 nextToken();
302
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000303 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000304
Manuel Klimek70b03f42013-01-23 09:32:48 +0000305 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
306 MustBeDeclaration);
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000307 Line->Level += AddLevels;
Nico Weber27268772013-06-26 00:30:14 +0000308 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000309
Manuel Klimek96e888b2013-05-28 11:55:06 +0000310 if (!FormatTok->Tok.is(tok::r_brace)) {
Manuel Klimek86721d22013-01-22 16:31:55 +0000311 Line->Level -= AddLevels;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000312 StructuralError = true;
313 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000314 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000315
Daniel Jasperf9955d32013-03-20 12:37:50 +0000316 nextToken(); // Munch the closing brace.
Manuel Klimek86721d22013-01-22 16:31:55 +0000317 Line->Level -= AddLevels;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000318}
319
320void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000321 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000322 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000323 nextToken();
324
Manuel Klimek96e888b2013-05-28 11:55:06 +0000325 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000326 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000327 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000328 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000329
Manuel Klimek96e888b2013-05-28 11:55:06 +0000330 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000331 case tok::pp_define:
332 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000333 return;
334 case tok::pp_if:
335 parsePPIf();
336 break;
337 case tok::pp_ifdef:
338 case tok::pp_ifndef:
339 parsePPIfdef();
340 break;
341 case tok::pp_else:
342 parsePPElse();
343 break;
344 case tok::pp_elif:
345 parsePPElIf();
346 break;
347 case tok::pp_endif:
348 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000349 break;
350 default:
351 parsePPUnknown();
352 break;
353 }
354}
355
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000356void UnwrappedLineParser::pushPPConditional() {
357 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
358 PPStack.push_back(PP_Unreachable);
359 else
360 PPStack.push_back(PP_Conditional);
361}
362
363void UnwrappedLineParser::parsePPIf() {
364 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000365 if ((FormatTok->Tok.isLiteral() &&
366 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000367 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000368 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000369 PPStack.push_back(PP_Unreachable);
370 } else {
371 pushPPConditional();
372 }
373 parsePPUnknown();
374}
375
376void UnwrappedLineParser::parsePPIfdef() {
377 pushPPConditional();
378 parsePPUnknown();
379}
380
381void UnwrappedLineParser::parsePPElse() {
382 if (!PPStack.empty())
383 PPStack.pop_back();
384 pushPPConditional();
385 parsePPUnknown();
386}
387
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000388void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000389
390void UnwrappedLineParser::parsePPEndIf() {
391 if (!PPStack.empty())
392 PPStack.pop_back();
393 parsePPUnknown();
394}
395
Manuel Klimekd4397b92013-01-04 23:34:14 +0000396void UnwrappedLineParser::parsePPDefine() {
397 nextToken();
398
Manuel Klimek96e888b2013-05-28 11:55:06 +0000399 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000400 parsePPUnknown();
401 return;
402 }
403 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000404 if (FormatTok->Tok.getKind() == tok::l_paren &&
405 FormatTok->WhitespaceRange.getBegin() ==
406 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000407 parseParens();
408 }
409 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000410 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000411
412 // Errors during a preprocessor directive can only affect the layout of the
413 // preprocessor directive, and thus we ignore them. An alternative approach
414 // would be to use the same approach we use on the file level (no
415 // re-indentation if there was a structural error) within the macro
416 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000417 parseFile();
418}
419
420void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000421 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000422 nextToken();
423 } while (!eof());
424 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000425}
426
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000427// Here we blacklist certain tokens that are not usually the first token in an
428// unwrapped line. This is used in attempt to distinguish macro calls without
429// trailing semicolons from other constructs split to several lines.
430bool tokenCanStartNewLine(clang::Token Tok) {
431 // Semicolon can be a null-statement, l_square can be a start of a macro or
432 // a C++11 attribute, but this doesn't seem to be common.
433 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
434 Tok.isNot(tok::l_square) &&
435 // Tokens that can only be used as binary operators and a part of
436 // overloaded operator names.
437 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
438 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
439 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
440 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
441 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
442 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
443 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
444 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
445 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
446 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
447 Tok.isNot(tok::lesslessequal) &&
448 // Colon is used in labels, base class lists, initializer lists,
449 // range-based for loops, ternary operator, but should never be the
450 // first token in an unwrapped line.
451 Tok.isNot(tok::colon);
452}
453
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000454void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000455 assert(!FormatTok->Tok.is(tok::l_brace));
456 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000457 case tok::at:
458 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000459 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000460 parseBracedList();
461 break;
462 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000463 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000464 case tok::objc_public:
465 case tok::objc_protected:
466 case tok::objc_package:
467 case tok::objc_private:
468 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000469 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000470 case tok::objc_implementation:
471 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000472 case tok::objc_protocol:
473 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000474 case tok::objc_end:
475 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000476 case tok::objc_optional:
477 case tok::objc_required:
478 nextToken();
479 addUnwrappedLine();
480 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000481 default:
482 break;
483 }
484 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000485 case tok::kw_namespace:
486 parseNamespace();
487 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000488 case tok::kw_inline:
489 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000490 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000491 parseNamespace();
492 return;
493 }
494 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000495 case tok::kw_public:
496 case tok::kw_protected:
497 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000498 parseAccessSpecifier();
499 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000500 case tok::kw_if:
501 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000502 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000503 case tok::kw_for:
504 case tok::kw_while:
505 parseForOrWhileLoop();
506 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000507 case tok::kw_do:
508 parseDoWhile();
509 return;
510 case tok::kw_switch:
511 parseSwitch();
512 return;
513 case tok::kw_default:
514 nextToken();
515 parseLabel();
516 return;
517 case tok::kw_case:
518 parseCaseLabel();
519 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000520 case tok::kw_return:
521 parseReturn();
522 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000523 case tok::kw_extern:
524 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000525 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000526 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000527 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000528 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000529 addUnwrappedLine();
530 return;
531 }
532 }
533 // In all other cases, parse the declaration.
534 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000535 default:
536 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000537 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000538 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000539 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000540 case tok::at:
541 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000542 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000543 parseBracedList();
544 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000545 case tok::kw_enum:
546 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000547 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000548 case tok::kw_struct:
549 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000550 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000551 parseRecord();
552 // A record declaration or definition is always the start of a structural
553 // element.
554 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000555 case tok::semi:
556 nextToken();
557 addUnwrappedLine();
558 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000559 case tok::r_brace:
560 addUnwrappedLine();
561 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000562 case tok::l_paren:
563 parseParens();
564 break;
565 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000566 if (!tryToParseBracedList()) {
567 // A block outside of parentheses must be the last part of a
568 // structural element.
569 // FIXME: Figure out cases where this is not true, and add projections
570 // for them (the one we know is missing are lambdas).
571 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
572 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
573 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000574 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000575 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000576 return;
577 }
578 // Otherwise this was a braced init list, and the structural
579 // element continues.
580 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000581 case tok::identifier: {
582 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000583 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000584 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000585 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000586 parseLabel();
587 return;
588 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000589 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000590 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000591 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000592 if (FormatTok->HasUnescapedNewline &&
593 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000594 addUnwrappedLine();
595 return;
596 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000597 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
598 Text == Text.upper()) {
599 // Recognize free-standing macros like Q_OBJECT.
600 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000601 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000602 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000603 }
604 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000605 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000606 case tok::equal:
607 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000608 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000609 parseBracedList();
610 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000611 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000612 default:
613 nextToken();
614 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000615 }
616 } while (!eof());
617}
618
Manuel Klimek80829bd2013-05-23 09:41:43 +0000619bool UnwrappedLineParser::tryToParseBracedList() {
620 if (LBraces[Tokens->getPosition()] == BS_Unknown)
621 calculateBraceTypes();
622 assert(LBraces[Tokens->getPosition()] != BS_Unknown);
623 if (LBraces[Tokens->getPosition()] == BS_Block)
624 return false;
625 parseBracedList();
626 return true;
627}
628
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000629void UnwrappedLineParser::parseBracedList() {
630 nextToken();
631
Manuel Klimek423dd932013-04-10 09:52:05 +0000632 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
633 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000634 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000635 // FIXME: When we start to support lambdas, we'll want to parse them away
636 // here, otherwise our bail-out scenarios below break. The better solution
637 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000638 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000639 case tok::l_brace:
640 parseBracedList();
641 break;
642 case tok::r_brace:
643 nextToken();
644 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000645 case tok::semi:
646 // Probably a missing closing brace. Bail out.
647 return;
648 case tok::comma:
649 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000650 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000651 default:
652 nextToken();
653 break;
654 }
655 } while (!eof());
656}
657
Manuel Klimekc44ee892013-01-21 10:07:49 +0000658void UnwrappedLineParser::parseReturn() {
659 nextToken();
660
661 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000662 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000663 case tok::l_brace:
664 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000665 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000666 // Assume missing ';'.
667 addUnwrappedLine();
668 return;
669 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000670 break;
671 case tok::l_paren:
672 parseParens();
673 break;
674 case tok::r_brace:
675 // Assume missing ';'.
676 addUnwrappedLine();
677 return;
678 case tok::semi:
679 nextToken();
680 addUnwrappedLine();
681 return;
682 default:
683 nextToken();
684 break;
685 }
686 } while (!eof());
687}
688
Daniel Jasperbac016b2012-12-03 18:12:45 +0000689void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000690 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000691 nextToken();
692 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000693 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000694 case tok::l_paren:
695 parseParens();
696 break;
697 case tok::r_paren:
698 nextToken();
699 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000700 case tok::r_brace:
701 // A "}" inside parenthesis is an error if there wasn't a matching "{".
702 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000703 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000704 if (!tryToParseBracedList()) {
705 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000706 {
707 ScopedLineState LineState(*this);
708 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000709 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000710 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000711 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000712 Line->Level -= 1;
713 }
714 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000715 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000716 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000717 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000718 case tok::at:
719 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000720 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000721 parseBracedList();
722 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000723 default:
724 nextToken();
725 break;
726 }
727 } while (!eof());
728}
729
730void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000731 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000732 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000733 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000734 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000735 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000736 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000737 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000738 NeedsUnwrappedLine = true;
739 } else {
740 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000741 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000742 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000743 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000744 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000745 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000746 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000747 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000748 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000749 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000750 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000751 parseIfThenElse();
752 } else {
753 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000754 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000755 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000756 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000757 }
758 } else if (NeedsUnwrappedLine) {
759 addUnwrappedLine();
760 }
761}
762
Alexander Kornienko15757312012-12-06 18:03:27 +0000763void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000764 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000765 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000766 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000767 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000768 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000769 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
770 addUnwrappedLine();
771
Nico Weber27268772013-06-26 00:30:14 +0000772 parseBlock(/*MustBeDeclaration=*/true, 0);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000773 // Munch the semicolon after a namespace. This is more common than one would
774 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000775 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000776 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000777 addUnwrappedLine();
778 }
779 // FIXME: Add error handling.
780}
781
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000782void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000783 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000784 "'for' or 'while' expected");
785 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000786 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000787 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000788 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000789 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000790 addUnwrappedLine();
791 } else {
792 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000793 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000794 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000795 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000796 }
797}
798
Daniel Jasperbac016b2012-12-03 18:12:45 +0000799void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000800 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000801 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000802 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000803 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000804 } else {
805 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000806 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000807 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000808 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000809 }
810
Alexander Kornienko393b0082012-12-04 15:40:36 +0000811 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000812 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000813 addUnwrappedLine();
814 return;
815 }
816
Daniel Jasperbac016b2012-12-03 18:12:45 +0000817 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000818 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000819}
820
821void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000822 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000823 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000824 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000825 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000826 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000827 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000828 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000829 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000830 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000831 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000832 }
833 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000834 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000835}
836
837void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000838 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000839 // FIXME: fix handling of complex expressions here.
840 do {
841 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000842 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843 parseLabel();
844}
845
846void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000847 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000848 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000849 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000850 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000851 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000852 parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000853 addUnwrappedLine();
854 } else {
855 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000856 Line->Level += (Style.IndentCaseLabels ? 2 : 1);
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000857 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000858 Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000859 }
860}
861
862void UnwrappedLineParser::parseAccessSpecifier() {
863 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000864 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000865 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000866 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000867 addUnwrappedLine();
868}
869
870void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000871 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000872 if (FormatTok->Tok.is(tok::identifier) ||
873 FormatTok->Tok.is(tok::kw___attribute) ||
874 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000875 nextToken();
876 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000877 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000878 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000879 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000880 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000881 nextToken();
882 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000883 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000884 nextToken();
885 addUnwrappedLine();
886 ++Line->Level;
887 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000888 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000889 case tok::l_paren:
890 parseParens();
891 break;
892 case tok::r_brace:
893 addUnwrappedLine();
894 nextToken();
895 --Line->Level;
896 return;
897 case tok::comma:
898 nextToken();
899 addUnwrappedLine();
900 break;
901 default:
902 nextToken();
903 break;
904 }
905 } while (!eof());
906 }
907 // We fall through to parsing a structural element afterwards, so that in
908 // enum A {} n, m;
909 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000910}
911
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000912void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000913 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000914 if (FormatTok->Tok.is(tok::identifier) ||
915 FormatTok->Tok.is(tok::kw___attribute) ||
916 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000917 nextToken();
918 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000919 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000920 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000921 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000922 // The actual identifier can be a nested name specifier, and in macros
923 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000924 while (FormatTok->Tok.is(tok::identifier) ||
925 FormatTok->Tok.is(tok::coloncolon) ||
926 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000927 nextToken();
928
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000929 // Note that parsing away template declarations here leads to incorrectly
930 // accepting function declarations as record declarations.
931 // In general, we cannot solve this problem. Consider:
932 // class A<int> B() {}
933 // which can be a function definition or a class definition when B() is a
934 // macro. If we find enough real-world cases where this is a problem, we
935 // can parse for the 'template' keyword in the beginning of the statement,
936 // and thus rule out the record production in case there is no template
937 // (this would still leave us with an ambiguity between template function
938 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000939 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
940 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
941 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000942 return;
943 nextToken();
944 }
945 }
946 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000947 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000948 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
949 addUnwrappedLine();
950
Nico Weber27268772013-06-26 00:30:14 +0000951 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000952 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000953 // We fall through to parsing a structural element afterwards, so
954 // class A {} n, m;
955 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000956}
957
Nico Weber1abe6ea2013-01-09 21:15:03 +0000958void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000959 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000960 do
961 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000962 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000963 nextToken(); // Skip '>'.
964}
965
966void UnwrappedLineParser::parseObjCUntilAtEnd() {
967 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000968 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000969 nextToken();
970 addUnwrappedLine();
971 break;
972 }
973 parseStructuralElement();
974 } while (!eof());
975}
976
Nico Weber50767d82013-01-09 23:25:37 +0000977void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +0000978 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000979 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +0000980
981 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000982 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +0000983 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000984 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +0000985 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +0000986 // Skip category, if present.
987 parseParens();
988
Manuel Klimek96e888b2013-05-28 11:55:06 +0000989 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +0000990 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +0000991
992 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000993 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +0000994 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +0000995
996 // With instance variables, this puts '}' on its own line. Without instance
997 // variables, this ends the @interface line.
998 addUnwrappedLine();
999
Nico Weber1abe6ea2013-01-09 21:15:03 +00001000 parseObjCUntilAtEnd();
1001}
Nico Weber27d13672013-01-09 20:25:35 +00001002
Nico Weber1abe6ea2013-01-09 21:15:03 +00001003void UnwrappedLineParser::parseObjCProtocol() {
1004 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001005 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001006
Manuel Klimek96e888b2013-05-28 11:55:06 +00001007 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001008 parseObjCProtocolList();
1009
1010 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001011 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001012 nextToken();
1013 return addUnwrappedLine();
1014 }
1015
1016 addUnwrappedLine();
1017 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001018}
1019
Daniel Jasperbac016b2012-12-03 18:12:45 +00001020void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001021 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001022 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001023 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001024 llvm::dbgs() << "Line(" << Line->Level << ")"
1025 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001026 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1027 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001028 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001029 llvm::dbgs() << (*I)->Tok.getName() << " ";
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) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001110 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001111 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001112 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001113 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