blob: e0b090f6abc99ae3a04f2c7a6fda6e9b9fec8022 [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
Craig Toppere50947f2013-07-01 04:21:54 +000033namespace {
34
Manuel Klimek70b03f42013-01-23 09:32:48 +000035class ScopedDeclarationState {
36public:
37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000040 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000041 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000042 }
43 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000044 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000045 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000049 }
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000050
Manuel Klimek70b03f42013-01-23 09:32:48 +000051private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54};
55
Manuel Klimekd4397b92013-01-04 23:34:14 +000056class ScopedMacroState : public FormatTokenSource {
57public:
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek96e888b2013-05-28 11:55:06 +000059 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimekd4397b92013-01-04 23:34:14 +000060 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek67d080d2013-04-12 14:13:36 +000061 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
Manuel Klimek96e888b2013-05-28 11:55:06 +000063 PreviousStructuralError(StructuralError), Token(NULL) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000065 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000066 Line.InPPDirective = true;
67 }
68
69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000073 Line.Level = PreviousLineLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +000074 StructuralError = PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +000075 }
76
Manuel Klimek96e888b2013-05-28 11:55:06 +000077 virtual FormatToken *getNextToken() {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek96e888b2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimekd4397b92013-01-04 23:34:14 +000084 return Token;
85 }
86
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000087 virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); }
Manuel Klimek80829bd2013-05-23 09:41:43 +000088
Manuel Klimek96e888b2013-05-28 11:55:06 +000089 virtual FormatToken *setPosition(unsigned Position) {
Manuel Klimek80829bd2013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimekd4397b92013-01-04 23:34:14 +000094private:
Manuel Klimek96e888b2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000096
Manuel Klimek96e888b2013-05-28 11:55:06 +000097 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000113 bool &StructuralError;
114 bool PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000115
Manuel Klimek96e888b2013-05-28 11:55:06 +0000116 FormatToken *Token;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000117};
118
Craig Toppere50947f2013-07-01 04:21:54 +0000119} // end anonymous namespace
120
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000121class ScopedLineState {
122public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
Daniel Jasper567dcf92013-09-05 09:29:45 +0000125 : Parser(Parser) {
126 OriginalLines = Parser.CurrentLines;
Manuel Klimek525fe162013-01-18 14:04:34 +0000127 if (SwitchToPreprocessorLines)
128 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000129 else if (!Parser.Line->Tokens.empty())
130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000131 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000132 Parser.Line.reset(new UnwrappedLine());
133 Parser.Line->Level = PreBlockLine->Level;
134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000135 }
136
137 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000138 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000139 Parser.addUnwrappedLine();
140 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000141 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000142 Parser.Line.reset(PreBlockLine);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144 Parser.MustBreakBeforeNextToken = true;
145 Parser.CurrentLines = OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000146 }
147
148private:
149 UnwrappedLineParser &Parser;
150
151 UnwrappedLine *PreBlockLine;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000152 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000153};
154
Craig Toppere50947f2013-07-01 04:21:54 +0000155namespace {
156
Manuel Klimek80829bd2013-05-23 09:41:43 +0000157class IndexedTokenSource : public FormatTokenSource {
158public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000159 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000160 : Tokens(Tokens), Position(-1) {}
161
Manuel Klimek96e888b2013-05-28 11:55:06 +0000162 virtual FormatToken *getNextToken() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000163 ++Position;
164 return Tokens[Position];
165 }
166
167 virtual unsigned getPosition() {
168 assert(Position >= 0);
169 return Position;
170 }
171
Manuel Klimek96e888b2013-05-28 11:55:06 +0000172 virtual FormatToken *setPosition(unsigned P) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000173 Position = P;
174 return Tokens[Position];
175 }
176
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000177 void reset() { Position = -1; }
178
Manuel Klimek80829bd2013-05-23 09:41:43 +0000179private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000180 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000181 int Position;
182};
183
Craig Toppere50947f2013-07-01 04:21:54 +0000184} // end anonymous namespace
185
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000186UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000187 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000188 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000189 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000190 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000191 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
192
193void UnwrappedLineParser::reset() {
194 PPBranchLevel = -1;
195 Line.reset(new UnwrappedLine);
196 CommentsBeforeNextToken.clear();
197 FormatTok = NULL;
198 MustBreakBeforeNextToken = false;
199 PreprocessorDirectives.clear();
200 CurrentLines = &Lines;
201 DeclarationScopeStack.clear();
202 StructuralError = false;
203 PPStack.clear();
204}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000205
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000206bool UnwrappedLineParser::parse() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000207 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000208 do {
209 DEBUG(llvm::dbgs() << "----\n");
210 reset();
211 Tokens = &TokenSource;
212 TokenSource.reset();
Daniel Jasper516fb312013-03-01 18:11:39 +0000213
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000214 readToken();
215 parseFile();
216 // Create line with eof token.
217 pushToken(FormatTok);
218 addUnwrappedLine();
219
220 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
221 E = Lines.end();
222 I != E; ++I) {
223 Callback.consumeUnwrappedLine(*I);
224 }
225 Callback.finishRun();
226 Lines.clear();
227 while (!PPLevelBranchIndex.empty() &&
Daniel Jasperac4d0182013-10-12 13:32:56 +0000228 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000229 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
230 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
231 }
232 if (!PPLevelBranchIndex.empty()) {
233 ++PPLevelBranchIndex.back();
234 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
235 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
236 }
237 } while (!PPLevelBranchIndex.empty());
238
Manuel Klimek67d080d2013-04-12 14:13:36 +0000239 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000240}
241
Manuel Klimek67d080d2013-04-12 14:13:36 +0000242void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000243 ScopedDeclarationState DeclarationState(
244 *Line, DeclarationScopeStack,
245 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000246 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000247 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000248 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000249 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000250}
251
Manuel Klimek67d080d2013-04-12 14:13:36 +0000252void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000253 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000254 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000255 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000256 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000257 nextToken();
258 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000259 break;
260 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000261 // FIXME: Add parameter whether this can happen - if this happens, we must
262 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000263 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000264 addUnwrappedLine();
265 break;
266 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000267 if (HasOpeningBrace)
268 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000269 StructuralError = true;
270 nextToken();
271 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000272 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000273 case tok::kw_default:
274 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000275 if (!SwitchLabelEncountered &&
276 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
277 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000278 SwitchLabelEncountered = true;
279 parseStructuralElement();
280 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000281 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000282 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000283 break;
284 }
285 } while (!eof());
286}
287
Manuel Klimek80829bd2013-05-23 09:41:43 +0000288void UnwrappedLineParser::calculateBraceTypes() {
289 // We'll parse forward through the tokens until we hit
290 // a closing brace or eof - note that getNextToken() will
291 // parse macros, so this will magically work inside macro
292 // definitions, too.
293 unsigned StoredPosition = Tokens->getPosition();
294 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000295 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000296 // Keep a stack of positions of lbrace tokens. We will
297 // update information about whether an lbrace starts a
298 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000299 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000300 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000301 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000302 // Get next none-comment token.
303 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000304 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000305 do {
306 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000307 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000308 } while (NextTok->is(tok::comment));
309
Manuel Klimek96e888b2013-05-28 11:55:06 +0000310 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000311 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000312 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000313 break;
314 case tok::r_brace:
315 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000316 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000317 // If there is a comma, semicolon or right paren after the closing
Manuel Klimek31e44f72013-09-04 08:20:47 +0000318 // brace, we assume this is a braced initializer list. Note that
319 // regardless how we mark inner braces here, we will overwrite the
320 // BlockKind later if we parse a braced list (where all blocks inside
321 // are by default braced lists), or when we explicitly detect blocks
322 // (for example while parsing lambdas).
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000323 //
324 // We exclude + and - as they can be ObjC visibility modifiers.
Daniel Jaspereb483662013-05-31 10:09:55 +0000325 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasperac885cd2013-09-13 10:55:31 +0000326 tok::r_square, tok::l_brace, tok::colon) ||
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000327 (NextTok->isBinaryOperator() &&
328 !NextTok->isOneOf(tok::plus, tok::minus))) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000329 Tok->BlockKind = BK_BracedInit;
330 LBraceStack.back()->BlockKind = BK_BracedInit;
331 } else {
332 Tok->BlockKind = BK_Block;
333 LBraceStack.back()->BlockKind = BK_Block;
334 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000335 }
336 LBraceStack.pop_back();
337 }
338 break;
339 case tok::semi:
340 case tok::kw_if:
341 case tok::kw_while:
342 case tok::kw_for:
343 case tok::kw_switch:
344 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000345 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000346 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000347 break;
348 default:
349 break;
350 }
351 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000352 Position += ReadTokens;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000353 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000354 // Assume other blocks for all unclosed opening braces.
355 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000356 if (LBraceStack[i]->BlockKind == BK_Unknown)
357 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000358 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000359
Manuel Klimek80829bd2013-05-23 09:41:43 +0000360 FormatTok = Tokens->setPosition(StoredPosition);
361}
362
Manuel Klimekaabfb272013-10-12 22:46:56 +0000363void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
364 bool MunchSemi) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000365 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000366 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000367 nextToken();
368
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000369 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000370
Manuel Klimek70b03f42013-01-23 09:32:48 +0000371 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
372 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000373 if (AddLevel)
374 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000375 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000376
Manuel Klimek96e888b2013-05-28 11:55:06 +0000377 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000378 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000379 StructuralError = true;
380 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000381 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000382
Daniel Jasperf9955d32013-03-20 12:37:50 +0000383 nextToken(); // Munch the closing brace.
Manuel Klimekaabfb272013-10-12 22:46:56 +0000384 if (MunchSemi && FormatTok->Tok.is(tok::semi))
385 nextToken();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000386 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000387}
388
Manuel Klimek753a5112013-09-04 13:25:30 +0000389void UnwrappedLineParser::parseChildBlock() {
390 FormatTok->BlockKind = BK_Block;
391 nextToken();
392 {
393 ScopedLineState LineState(*this);
394 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
395 /*MustBeDeclaration=*/false);
396 Line->Level += 1;
397 parseLevel(/*HasOpeningBrace=*/true);
398 Line->Level -= 1;
399 }
400 nextToken();
401}
402
Daniel Jasperbac016b2012-12-03 18:12:45 +0000403void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000404 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000405 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000406 nextToken();
407
Manuel Klimek96e888b2013-05-28 11:55:06 +0000408 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000409 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000410 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000411 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000412
Manuel Klimek96e888b2013-05-28 11:55:06 +0000413 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000414 case tok::pp_define:
415 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000416 return;
417 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000418 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000419 break;
420 case tok::pp_ifdef:
421 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000422 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000423 break;
424 case tok::pp_else:
425 parsePPElse();
426 break;
427 case tok::pp_elif:
428 parsePPElIf();
429 break;
430 case tok::pp_endif:
431 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000432 break;
433 default:
434 parsePPUnknown();
435 break;
436 }
437}
438
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000439void UnwrappedLineParser::pushPPConditional() {
440 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
441 PPStack.push_back(PP_Unreachable);
442 else
443 PPStack.push_back(PP_Conditional);
444}
445
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000446void UnwrappedLineParser::parsePPIf(bool IfDef) {
447 ++PPBranchLevel;
448 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
449 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
450 PPLevelBranchIndex.push_back(0);
451 PPLevelBranchCount.push_back(0);
452 }
453 PPChainBranchIndex.push(0);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000454 nextToken();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000455 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
456 StringRef(FormatTok->Tok.getLiteralData(),
457 FormatTok->Tok.getLength()) == "0") ||
458 FormatTok->Tok.is(tok::kw_false);
459 if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000460 PPStack.push_back(PP_Unreachable);
461 } else {
462 pushPPConditional();
463 }
464 parsePPUnknown();
465}
466
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000467void UnwrappedLineParser::parsePPElse() {
468 if (!PPStack.empty())
469 PPStack.pop_back();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000470 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
471 if (!PPChainBranchIndex.empty())
472 ++PPChainBranchIndex.top();
473 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
474 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) {
475 PPStack.push_back(PP_Unreachable);
476 } else {
477 pushPPConditional();
478 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000479 parsePPUnknown();
480}
481
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000482void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000483
484void UnwrappedLineParser::parsePPEndIf() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000485 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
486 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
487 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000488 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
489 }
490 }
491 --PPBranchLevel;
492 if (!PPChainBranchIndex.empty())
493 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000494 if (!PPStack.empty())
495 PPStack.pop_back();
496 parsePPUnknown();
497}
498
Manuel Klimekd4397b92013-01-04 23:34:14 +0000499void UnwrappedLineParser::parsePPDefine() {
500 nextToken();
501
Manuel Klimek96e888b2013-05-28 11:55:06 +0000502 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000503 parsePPUnknown();
504 return;
505 }
506 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000507 if (FormatTok->Tok.getKind() == tok::l_paren &&
508 FormatTok->WhitespaceRange.getBegin() ==
509 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000510 parseParens();
511 }
512 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000513 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000514
515 // Errors during a preprocessor directive can only affect the layout of the
516 // preprocessor directive, and thus we ignore them. An alternative approach
517 // would be to use the same approach we use on the file level (no
518 // re-indentation if there was a structural error) within the macro
519 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000520 parseFile();
521}
522
523void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000524 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000525 nextToken();
526 } while (!eof());
527 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000528}
529
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000530// Here we blacklist certain tokens that are not usually the first token in an
531// unwrapped line. This is used in attempt to distinguish macro calls without
532// trailing semicolons from other constructs split to several lines.
533bool tokenCanStartNewLine(clang::Token Tok) {
534 // Semicolon can be a null-statement, l_square can be a start of a macro or
535 // a C++11 attribute, but this doesn't seem to be common.
536 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
537 Tok.isNot(tok::l_square) &&
538 // Tokens that can only be used as binary operators and a part of
539 // overloaded operator names.
540 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
541 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
542 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
543 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
544 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
545 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
546 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
547 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
548 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
549 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
550 Tok.isNot(tok::lesslessequal) &&
551 // Colon is used in labels, base class lists, initializer lists,
552 // range-based for loops, ternary operator, but should never be the
553 // first token in an unwrapped line.
554 Tok.isNot(tok::colon);
555}
556
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000557void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000558 assert(!FormatTok->Tok.is(tok::l_brace));
559 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000560 case tok::at:
561 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000562 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000563 parseBracedList();
564 break;
565 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000566 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000567 case tok::objc_public:
568 case tok::objc_protected:
569 case tok::objc_package:
570 case tok::objc_private:
571 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000572 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000573 case tok::objc_implementation:
574 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000575 case tok::objc_protocol:
576 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000577 case tok::objc_end:
578 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000579 case tok::objc_optional:
580 case tok::objc_required:
581 nextToken();
582 addUnwrappedLine();
583 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000584 default:
585 break;
586 }
587 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000588 case tok::kw_namespace:
589 parseNamespace();
590 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000591 case tok::kw_inline:
592 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000593 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000594 parseNamespace();
595 return;
596 }
597 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000598 case tok::kw_public:
599 case tok::kw_protected:
600 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000601 parseAccessSpecifier();
602 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000603 case tok::kw_if:
604 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000605 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000606 case tok::kw_for:
607 case tok::kw_while:
608 parseForOrWhileLoop();
609 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000610 case tok::kw_do:
611 parseDoWhile();
612 return;
613 case tok::kw_switch:
614 parseSwitch();
615 return;
616 case tok::kw_default:
617 nextToken();
618 parseLabel();
619 return;
620 case tok::kw_case:
621 parseCaseLabel();
622 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000623 case tok::kw_return:
624 parseReturn();
625 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000626 case tok::kw_extern:
627 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000628 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000629 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000630 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000631 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000632 addUnwrappedLine();
633 return;
634 }
635 }
636 // In all other cases, parse the declaration.
637 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000638 default:
639 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000640 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000641 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000642 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000643 case tok::at:
644 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000645 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000646 parseBracedList();
647 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000648 case tok::kw_enum:
649 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000650 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000651 case tok::kw_struct:
652 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000653 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000654 parseRecord();
655 // A record declaration or definition is always the start of a structural
656 // element.
657 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000658 case tok::semi:
659 nextToken();
660 addUnwrappedLine();
661 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000662 case tok::r_brace:
663 addUnwrappedLine();
664 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000665 case tok::l_paren:
666 parseParens();
667 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000668 case tok::caret:
669 nextToken();
670 if (FormatTok->is(tok::l_brace)) {
671 parseChildBlock();
672 }
673 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000674 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000675 if (!tryToParseBracedList()) {
676 // A block outside of parentheses must be the last part of a
677 // structural element.
678 // FIXME: Figure out cases where this is not true, and add projections
679 // for them (the one we know is missing are lambdas).
680 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Manuel Klimeke4907052013-08-02 21:31:59 +0000681 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
682 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000683 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000684 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000685 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000686 return;
687 }
688 // Otherwise this was a braced init list, and the structural
689 // element continues.
690 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000691 case tok::identifier: {
692 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000693 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000694 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000695 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000696 parseLabel();
697 return;
698 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000699 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000700 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000701 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000702 if (FormatTok->HasUnescapedNewline &&
703 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000704 addUnwrappedLine();
705 return;
706 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000707 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
708 Text == Text.upper()) {
709 // Recognize free-standing macros like Q_OBJECT.
710 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000711 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000712 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000713 }
714 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000715 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000716 case tok::equal:
717 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000718 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000719 parseBracedList();
720 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000721 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000722 case tok::l_square:
723 tryToParseLambda();
724 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000725 default:
726 nextToken();
727 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000728 }
729 } while (!eof());
730}
731
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000732void UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000733 // FIXME: This is a dirty way to access the previous token. Find a better
734 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000735 if (!Line->Tokens.empty() &&
736 Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator)) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000737 nextToken();
738 return;
739 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000740 assert(FormatTok->is(tok::l_square));
741 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000742 if (!tryToParseLambdaIntroducer())
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000743 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000744
745 while (FormatTok->isNot(tok::l_brace)) {
746 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000747 case tok::l_brace:
748 break;
749 case tok::l_paren:
750 parseParens();
751 break;
752 case tok::identifier:
753 case tok::kw_mutable:
754 nextToken();
755 break;
756 default:
757 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000758 }
759 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000760 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000761 parseChildBlock();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000762}
763
764bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
765 nextToken();
766 if (FormatTok->is(tok::equal)) {
767 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000768 if (FormatTok->is(tok::r_square)) {
769 nextToken();
770 return true;
771 }
772 if (FormatTok->isNot(tok::comma))
773 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000774 nextToken();
775 } else if (FormatTok->is(tok::amp)) {
776 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000777 if (FormatTok->is(tok::r_square)) {
778 nextToken();
779 return true;
780 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000781 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
782 return false;
783 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000784 if (FormatTok->is(tok::comma))
785 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000786 } else if (FormatTok->is(tok::r_square)) {
787 nextToken();
788 return true;
789 }
790 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000791 if (FormatTok->is(tok::amp))
792 nextToken();
793 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
794 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000795 nextToken();
796 if (FormatTok->is(tok::comma)) {
797 nextToken();
798 } else if (FormatTok->is(tok::r_square)) {
799 nextToken();
800 return true;
801 } else {
802 return false;
803 }
804 } while (!eof());
805 return false;
806}
807
Manuel Klimek80829bd2013-05-23 09:41:43 +0000808bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000809 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000810 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000811 assert(FormatTok->BlockKind != BK_Unknown);
812 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000813 return false;
814 parseBracedList();
815 return true;
816}
817
Daniel Jasper57981202013-09-13 09:20:45 +0000818bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
819 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000820 nextToken();
821
Manuel Klimek423dd932013-04-10 09:52:05 +0000822 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
823 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000824 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000825 // FIXME: When we start to support lambdas, we'll want to parse them away
826 // here, otherwise our bail-out scenarios below break. The better solution
827 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000828 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +0000829 case tok::caret:
830 nextToken();
831 if (FormatTok->is(tok::l_brace)) {
832 parseChildBlock();
833 }
834 break;
835 case tok::l_square:
836 tryToParseLambda();
837 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000838 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +0000839 // Assume there are no blocks inside a braced init list apart
840 // from the ones we explicitly parse out (like lambdas).
841 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000842 parseBracedList();
843 break;
844 case tok::r_brace:
845 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +0000846 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +0000847 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +0000848 HasError = true;
849 if (!ContinueOnSemicolons)
850 return !HasError;
851 nextToken();
852 break;
Manuel Klimek423dd932013-04-10 09:52:05 +0000853 case tok::comma:
854 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000855 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000856 default:
857 nextToken();
858 break;
859 }
860 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +0000861 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000862}
863
Manuel Klimekc44ee892013-01-21 10:07:49 +0000864void UnwrappedLineParser::parseReturn() {
865 nextToken();
866
867 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000868 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000869 case tok::l_brace:
870 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000871 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000872 // Assume missing ';'.
873 addUnwrappedLine();
874 return;
875 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000876 break;
877 case tok::l_paren:
878 parseParens();
879 break;
880 case tok::r_brace:
881 // Assume missing ';'.
882 addUnwrappedLine();
883 return;
884 case tok::semi:
885 nextToken();
886 addUnwrappedLine();
887 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000888 case tok::l_square:
889 tryToParseLambda();
890 break;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000891 default:
892 nextToken();
893 break;
894 }
895 } while (!eof());
896}
897
Daniel Jasperbac016b2012-12-03 18:12:45 +0000898void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000899 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000900 nextToken();
901 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000902 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000903 case tok::l_paren:
904 parseParens();
905 break;
906 case tok::r_paren:
907 nextToken();
908 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000909 case tok::r_brace:
910 // A "}" inside parenthesis is an error if there wasn't a matching "{".
911 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000912 case tok::l_square:
913 tryToParseLambda();
914 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000915 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000916 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +0000917 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000918 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000919 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000920 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000921 case tok::at:
922 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000923 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000924 parseBracedList();
925 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000926 default:
927 nextToken();
928 break;
929 }
930 } while (!eof());
931}
932
933void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000934 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000935 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000936 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000937 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000938 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000939 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000940 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
941 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000942 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000943 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
944 addUnwrappedLine();
945 else
946 NeedsUnwrappedLine = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000947 } else {
948 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000949 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000950 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000951 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000952 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000953 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000954 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000955 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000956 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
957 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000958 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000959 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000960 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000961 parseIfThenElse();
962 } else {
963 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000964 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000965 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000966 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000967 }
968 } else if (NeedsUnwrappedLine) {
969 addUnwrappedLine();
970 }
971}
972
Alexander Kornienko15757312012-12-06 18:03:27 +0000973void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000974 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000975 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000976 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000977 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000978 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000979 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
980 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000981 addUnwrappedLine();
982
Daniel Jaspereff18b92013-07-31 23:16:02 +0000983 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
984 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
985 DeclarationScopeStack.size() > 1);
986 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000987 // Munch the semicolon after a namespace. This is more common than one would
988 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000989 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000990 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000991 addUnwrappedLine();
992 }
993 // FIXME: Add error handling.
994}
995
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000996void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000997 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000998 "'for' or 'while' expected");
999 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001000 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001001 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001002 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001003 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1004 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001005 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001006 addUnwrappedLine();
1007 } else {
1008 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001009 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001010 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001011 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001012 }
1013}
1014
Daniel Jasperbac016b2012-12-03 18:12:45 +00001015void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001016 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001017 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001018 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001019 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1020 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001021 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001022 } else {
1023 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001024 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001025 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001026 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001027 }
1028
Alexander Kornienko393b0082012-12-04 15:40:36 +00001029 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001030 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001031 addUnwrappedLine();
1032 return;
1033 }
1034
Daniel Jasperbac016b2012-12-03 18:12:45 +00001035 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001036 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001037}
1038
1039void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001040 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001041 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001042 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001043 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001044 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001045 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1046 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001047 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001048 if (FormatTok->Tok.is(tok::kw_break)) {
1049 // "break;" after "}" on its own line only for BS_Allman
1050 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1051 addUnwrappedLine();
1052 parseStructuralElement();
1053 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001054 }
1055 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001056 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001057}
1058
1059void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001060 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001061 // FIXME: fix handling of complex expressions here.
1062 do {
1063 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001064 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001065 parseLabel();
1066}
1067
1068void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001069 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001070 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001071 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001072 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001073 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001074 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1075 addUnwrappedLine();
Daniel Jaspereff18b92013-07-31 23:16:02 +00001076 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001077 addUnwrappedLine();
1078 } else {
1079 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001080 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001081 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001082 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001083 }
1084}
1085
1086void UnwrappedLineParser::parseAccessSpecifier() {
1087 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001088 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001089 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001090 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001091 addUnwrappedLine();
1092}
1093
1094void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +00001095 nextToken();
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001096 // Eat up enum class ...
1097 if (FormatTok->Tok.is(tok::kw_class) ||
1098 FormatTok->Tok.is(tok::kw_struct))
1099 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001100 while (FormatTok->Tok.getIdentifierInfo() ||
1101 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001102 nextToken();
1103 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001104 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001105 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001106 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001107 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001108 nextToken();
1109 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001110 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper57981202013-09-13 09:20:45 +00001111 FormatTok->BlockKind = BK_Block;
1112 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1113 if (HasError) {
1114 if (FormatTok->is(tok::semi))
1115 nextToken();
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001116 addUnwrappedLine();
Daniel Jasper57981202013-09-13 09:20:45 +00001117 }
Manuel Klimek308232c2013-01-21 19:17:52 +00001118 }
1119 // We fall through to parsing a structural element afterwards, so that in
1120 // enum A {} n, m;
1121 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001122}
1123
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001124void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +00001125 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001126 if (FormatTok->Tok.is(tok::identifier) ||
1127 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek3d712892013-10-07 09:15:41 +00001128 FormatTok->Tok.is(tok::kw___declspec) ||
1129 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001130 nextToken();
1131 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001132 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001133 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001134 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001135 // The actual identifier can be a nested name specifier, and in macros
1136 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001137 while (FormatTok->Tok.is(tok::identifier) ||
1138 FormatTok->Tok.is(tok::coloncolon) ||
1139 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001140 nextToken();
1141
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001142 // Note that parsing away template declarations here leads to incorrectly
1143 // accepting function declarations as record declarations.
1144 // In general, we cannot solve this problem. Consider:
1145 // class A<int> B() {}
1146 // which can be a function definition or a class definition when B() is a
1147 // macro. If we find enough real-world cases where this is a problem, we
1148 // can parse for the 'template' keyword in the beginning of the statement,
1149 // and thus rule out the record production in case there is no template
1150 // (this would still leave us with an ambiguity between template function
1151 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001152 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1153 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1154 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001155 return;
1156 nextToken();
1157 }
1158 }
1159 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001160 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001161 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1162 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +00001163 addUnwrappedLine();
1164
Manuel Klimekaabfb272013-10-12 22:46:56 +00001165 parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true,
1166 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001167 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001168 // We fall through to parsing a structural element afterwards, so
1169 // class A {} n, m;
1170 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001171}
1172
Nico Weber1abe6ea2013-01-09 21:15:03 +00001173void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001174 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001175 do
1176 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001177 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001178 nextToken(); // Skip '>'.
1179}
1180
1181void UnwrappedLineParser::parseObjCUntilAtEnd() {
1182 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001183 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001184 nextToken();
1185 addUnwrappedLine();
1186 break;
1187 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001188 if (FormatTok->is(tok::l_brace)) {
1189 parseBlock(/*MustBeDeclaration=*/false);
1190 // In ObjC interfaces, nothing should be following the "}".
1191 addUnwrappedLine();
1192 } else {
1193 parseStructuralElement();
1194 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001195 } while (!eof());
1196}
1197
Nico Weber50767d82013-01-09 23:25:37 +00001198void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001199 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001200 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001201
1202 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001203 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001204 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001205 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001206 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001207 // Skip category, if present.
1208 parseParens();
1209
Manuel Klimek96e888b2013-05-28 11:55:06 +00001210 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001211 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001212
1213 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001214 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001215 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001216
1217 // With instance variables, this puts '}' on its own line. Without instance
1218 // variables, this ends the @interface line.
1219 addUnwrappedLine();
1220
Nico Weber1abe6ea2013-01-09 21:15:03 +00001221 parseObjCUntilAtEnd();
1222}
Nico Weber27d13672013-01-09 20:25:35 +00001223
Nico Weber1abe6ea2013-01-09 21:15:03 +00001224void UnwrappedLineParser::parseObjCProtocol() {
1225 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001226 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001227
Manuel Klimek96e888b2013-05-28 11:55:06 +00001228 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001229 parseObjCProtocolList();
1230
1231 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001232 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001233 nextToken();
1234 return addUnwrappedLine();
1235 }
1236
1237 addUnwrappedLine();
1238 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001239}
1240
Daniel Jasperd98927d2013-09-05 16:05:56 +00001241LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1242 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001243 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1244 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1245 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1246 E = Line.Tokens.end();
1247 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001248 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001249 }
1250 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1251 E = Line.Tokens.end();
1252 I != E; ++I) {
1253 const UnwrappedLineNode &Node = *I;
1254 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1255 I = Node.Children.begin(),
1256 E = Node.Children.end();
1257 I != E; ++I) {
1258 printDebugInfo(*I, "\nChild: ");
1259 }
1260 }
1261 llvm::dbgs() << "\n";
1262}
1263
Daniel Jasperbac016b2012-12-03 18:12:45 +00001264void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001265 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001266 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001267 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001268 if (CurrentLines == &Lines)
1269 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001270 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001271 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001272 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001273 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001274 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001275 I = PreprocessorDirectives.begin(),
1276 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001277 I != E; ++I) {
1278 CurrentLines->push_back(*I);
1279 }
1280 PreprocessorDirectives.clear();
1281 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001282}
1283
Manuel Klimek96e888b2013-05-28 11:55:06 +00001284bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001285
Manuel Klimek86721d22013-01-22 16:31:55 +00001286void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1287 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001288 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001289 I = CommentsBeforeNextToken.begin(),
1290 E = CommentsBeforeNextToken.end();
1291 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001292 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001293 addUnwrappedLine();
1294 }
1295 pushToken(*I);
1296 }
1297 if (NewlineBeforeNext && JustComments) {
1298 addUnwrappedLine();
1299 }
1300 CommentsBeforeNextToken.clear();
1301}
1302
Daniel Jasperbac016b2012-12-03 18:12:45 +00001303void UnwrappedLineParser::nextToken() {
1304 if (eof())
1305 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001306 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001307 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001308 readToken();
1309}
1310
1311void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001312 bool CommentsInCurrentLine = true;
1313 do {
1314 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001315 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1316 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001317 // If there is an unfinished unwrapped line, we flush the preprocessor
1318 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001319 bool SwitchToPreprocessorLines =
1320 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001321 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001322 // Comments stored before the preprocessor directive need to be output
1323 // before the preprocessor directive, at the same level as the
1324 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001325 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001326 parsePPDirective();
1327 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001328
1329 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1330 !Line->InPPDirective) {
1331 continue;
1332 }
1333
Manuel Klimek96e888b2013-05-28 11:55:06 +00001334 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001335 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001336 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001337 CommentsInCurrentLine = false;
1338 }
1339 if (CommentsInCurrentLine) {
1340 pushToken(FormatTok);
1341 } else {
1342 CommentsBeforeNextToken.push_back(FormatTok);
1343 }
1344 } while (!eof());
1345}
1346
Manuel Klimek96e888b2013-05-28 11:55:06 +00001347void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001348 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001349 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001350 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001351 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001352 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001353}
1354
Daniel Jaspercd162382013-01-07 13:26:07 +00001355} // end namespace format
1356} // end namespace clang