blob: f70de5f6d88e981264c031a819e43b02befe0f4d [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
177private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000178 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000179 int Position;
180};
181
Craig Toppere50947f2013-07-01 04:21:54 +0000182} // end anonymous namespace
183
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000184UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000185 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000186 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000187 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000188 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000189 Callback(Callback), AllTokens(Tokens) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000190
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000191bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000192 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek80829bd2013-05-23 09:41:43 +0000193 IndexedTokenSource TokenSource(AllTokens);
194 Tokens = &TokenSource;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000195 readToken();
Manuel Klimek67d080d2013-04-12 14:13:36 +0000196 parseFile();
Daniel Jasper567dcf92013-09-05 09:29:45 +0000197 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
198 E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000199 I != E; ++I) {
200 Callback.consumeUnwrappedLine(*I);
201 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000202
203 // Create line with eof token.
204 pushToken(FormatTok);
205 Callback.consumeUnwrappedLine(*Line);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000206 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000207}
208
Manuel Klimek67d080d2013-04-12 14:13:36 +0000209void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000210 ScopedDeclarationState DeclarationState(
211 *Line, DeclarationScopeStack,
212 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000213 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000214 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000215 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000216 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217}
218
Manuel Klimek67d080d2013-04-12 14:13:36 +0000219void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000220 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000222 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000223 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000224 nextToken();
225 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000226 break;
227 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000228 // FIXME: Add parameter whether this can happen - if this happens, we must
229 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000230 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000231 addUnwrappedLine();
232 break;
233 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000234 if (HasOpeningBrace)
235 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000236 StructuralError = true;
237 nextToken();
238 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000239 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000240 case tok::kw_default:
241 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000242 if (!SwitchLabelEncountered &&
243 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
244 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000245 SwitchLabelEncountered = true;
246 parseStructuralElement();
247 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000248 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000249 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000250 break;
251 }
252 } while (!eof());
253}
254
Manuel Klimek80829bd2013-05-23 09:41:43 +0000255void UnwrappedLineParser::calculateBraceTypes() {
256 // We'll parse forward through the tokens until we hit
257 // a closing brace or eof - note that getNextToken() will
258 // parse macros, so this will magically work inside macro
259 // definitions, too.
260 unsigned StoredPosition = Tokens->getPosition();
261 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000262 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000263 // Keep a stack of positions of lbrace tokens. We will
264 // update information about whether an lbrace starts a
265 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000266 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000267 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000268 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000269 // Get next none-comment token.
270 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000271 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000272 do {
273 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000274 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000275 } while (NextTok->is(tok::comment));
276
Manuel Klimek96e888b2013-05-28 11:55:06 +0000277 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000278 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000279 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000280 break;
281 case tok::r_brace:
282 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000283 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000284 // If there is a comma, semicolon or right paren after the closing
Manuel Klimek31e44f72013-09-04 08:20:47 +0000285 // brace, we assume this is a braced initializer list. Note that
286 // regardless how we mark inner braces here, we will overwrite the
287 // BlockKind later if we parse a braced list (where all blocks inside
288 // are by default braced lists), or when we explicitly detect blocks
289 // (for example while parsing lambdas).
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000290 //
291 // We exclude + and - as they can be ObjC visibility modifiers.
Daniel Jaspereb483662013-05-31 10:09:55 +0000292 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000293 tok::l_brace, tok::colon) ||
294 (NextTok->isBinaryOperator() &&
295 !NextTok->isOneOf(tok::plus, tok::minus))) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000296 Tok->BlockKind = BK_BracedInit;
297 LBraceStack.back()->BlockKind = BK_BracedInit;
298 } else {
299 Tok->BlockKind = BK_Block;
300 LBraceStack.back()->BlockKind = BK_Block;
301 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000302 }
303 LBraceStack.pop_back();
304 }
305 break;
306 case tok::semi:
307 case tok::kw_if:
308 case tok::kw_while:
309 case tok::kw_for:
310 case tok::kw_switch:
311 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000312 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000313 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000314 break;
315 default:
316 break;
317 }
318 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000319 Position += ReadTokens;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000320 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000321 // Assume other blocks for all unclosed opening braces.
322 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000323 if (LBraceStack[i]->BlockKind == BK_Unknown)
324 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000325 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000326
Manuel Klimek80829bd2013-05-23 09:41:43 +0000327 FormatTok = Tokens->setPosition(StoredPosition);
328}
329
Daniel Jaspereff18b92013-07-31 23:16:02 +0000330void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000331 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000332 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000333 nextToken();
334
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000335 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000336
Manuel Klimek70b03f42013-01-23 09:32:48 +0000337 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
338 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000339 if (AddLevel)
340 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000341 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000342
Manuel Klimek96e888b2013-05-28 11:55:06 +0000343 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000344 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000345 StructuralError = true;
346 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000347 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000348
Daniel Jasperf9955d32013-03-20 12:37:50 +0000349 nextToken(); // Munch the closing brace.
Daniel Jaspere865cc52013-07-25 11:31:57 +0000350 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000351}
352
Manuel Klimek753a5112013-09-04 13:25:30 +0000353void UnwrappedLineParser::parseChildBlock() {
354 FormatTok->BlockKind = BK_Block;
355 nextToken();
356 {
357 ScopedLineState LineState(*this);
358 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
359 /*MustBeDeclaration=*/false);
360 Line->Level += 1;
361 parseLevel(/*HasOpeningBrace=*/true);
362 Line->Level -= 1;
363 }
364 nextToken();
365}
366
Daniel Jasperbac016b2012-12-03 18:12:45 +0000367void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000368 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000369 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000370 nextToken();
371
Manuel Klimek96e888b2013-05-28 11:55:06 +0000372 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000373 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000374 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000375 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000376
Manuel Klimek96e888b2013-05-28 11:55:06 +0000377 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000378 case tok::pp_define:
379 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000380 return;
381 case tok::pp_if:
382 parsePPIf();
383 break;
384 case tok::pp_ifdef:
385 case tok::pp_ifndef:
386 parsePPIfdef();
387 break;
388 case tok::pp_else:
389 parsePPElse();
390 break;
391 case tok::pp_elif:
392 parsePPElIf();
393 break;
394 case tok::pp_endif:
395 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000396 break;
397 default:
398 parsePPUnknown();
399 break;
400 }
401}
402
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000403void UnwrappedLineParser::pushPPConditional() {
404 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
405 PPStack.push_back(PP_Unreachable);
406 else
407 PPStack.push_back(PP_Conditional);
408}
409
410void UnwrappedLineParser::parsePPIf() {
411 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000412 if ((FormatTok->Tok.isLiteral() &&
413 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000414 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000415 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000416 PPStack.push_back(PP_Unreachable);
417 } else {
418 pushPPConditional();
419 }
420 parsePPUnknown();
421}
422
423void UnwrappedLineParser::parsePPIfdef() {
424 pushPPConditional();
425 parsePPUnknown();
426}
427
428void UnwrappedLineParser::parsePPElse() {
429 if (!PPStack.empty())
430 PPStack.pop_back();
431 pushPPConditional();
432 parsePPUnknown();
433}
434
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000435void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000436
437void UnwrappedLineParser::parsePPEndIf() {
438 if (!PPStack.empty())
439 PPStack.pop_back();
440 parsePPUnknown();
441}
442
Manuel Klimekd4397b92013-01-04 23:34:14 +0000443void UnwrappedLineParser::parsePPDefine() {
444 nextToken();
445
Manuel Klimek96e888b2013-05-28 11:55:06 +0000446 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000447 parsePPUnknown();
448 return;
449 }
450 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000451 if (FormatTok->Tok.getKind() == tok::l_paren &&
452 FormatTok->WhitespaceRange.getBegin() ==
453 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000454 parseParens();
455 }
456 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000457 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000458
459 // Errors during a preprocessor directive can only affect the layout of the
460 // preprocessor directive, and thus we ignore them. An alternative approach
461 // would be to use the same approach we use on the file level (no
462 // re-indentation if there was a structural error) within the macro
463 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000464 parseFile();
465}
466
467void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000468 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000469 nextToken();
470 } while (!eof());
471 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000472}
473
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000474// Here we blacklist certain tokens that are not usually the first token in an
475// unwrapped line. This is used in attempt to distinguish macro calls without
476// trailing semicolons from other constructs split to several lines.
477bool tokenCanStartNewLine(clang::Token Tok) {
478 // Semicolon can be a null-statement, l_square can be a start of a macro or
479 // a C++11 attribute, but this doesn't seem to be common.
480 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
481 Tok.isNot(tok::l_square) &&
482 // Tokens that can only be used as binary operators and a part of
483 // overloaded operator names.
484 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
485 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
486 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
487 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
488 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
489 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
490 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
491 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
492 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
493 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
494 Tok.isNot(tok::lesslessequal) &&
495 // Colon is used in labels, base class lists, initializer lists,
496 // range-based for loops, ternary operator, but should never be the
497 // first token in an unwrapped line.
498 Tok.isNot(tok::colon);
499}
500
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000501void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000502 assert(!FormatTok->Tok.is(tok::l_brace));
503 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000504 case tok::at:
505 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000506 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000507 parseBracedList();
508 break;
509 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000510 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000511 case tok::objc_public:
512 case tok::objc_protected:
513 case tok::objc_package:
514 case tok::objc_private:
515 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000516 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000517 case tok::objc_implementation:
518 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000519 case tok::objc_protocol:
520 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000521 case tok::objc_end:
522 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000523 case tok::objc_optional:
524 case tok::objc_required:
525 nextToken();
526 addUnwrappedLine();
527 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000528 default:
529 break;
530 }
531 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000532 case tok::kw_namespace:
533 parseNamespace();
534 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000535 case tok::kw_inline:
536 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000537 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000538 parseNamespace();
539 return;
540 }
541 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000542 case tok::kw_public:
543 case tok::kw_protected:
544 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000545 parseAccessSpecifier();
546 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000547 case tok::kw_if:
548 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000549 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000550 case tok::kw_for:
551 case tok::kw_while:
552 parseForOrWhileLoop();
553 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000554 case tok::kw_do:
555 parseDoWhile();
556 return;
557 case tok::kw_switch:
558 parseSwitch();
559 return;
560 case tok::kw_default:
561 nextToken();
562 parseLabel();
563 return;
564 case tok::kw_case:
565 parseCaseLabel();
566 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000567 case tok::kw_return:
568 parseReturn();
569 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000570 case tok::kw_extern:
571 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000572 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000573 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000574 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000575 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000576 addUnwrappedLine();
577 return;
578 }
579 }
580 // In all other cases, parse the declaration.
581 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000582 default:
583 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000584 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000585 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000586 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000587 case tok::at:
588 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000589 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000590 parseBracedList();
591 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000592 case tok::kw_enum:
593 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000594 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000595 case tok::kw_struct:
596 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000597 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000598 parseRecord();
599 // A record declaration or definition is always the start of a structural
600 // element.
601 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000602 case tok::semi:
603 nextToken();
604 addUnwrappedLine();
605 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000606 case tok::r_brace:
607 addUnwrappedLine();
608 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000609 case tok::l_paren:
610 parseParens();
611 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000612 case tok::caret:
613 nextToken();
614 if (FormatTok->is(tok::l_brace)) {
615 parseChildBlock();
616 }
617 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000618 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000619 if (!tryToParseBracedList()) {
620 // A block outside of parentheses must be the last part of a
621 // structural element.
622 // FIXME: Figure out cases where this is not true, and add projections
623 // for them (the one we know is missing are lambdas).
624 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Manuel Klimeke4907052013-08-02 21:31:59 +0000625 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
626 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000627 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000628 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000629 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000630 return;
631 }
632 // Otherwise this was a braced init list, and the structural
633 // element continues.
634 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000635 case tok::identifier: {
636 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000637 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000638 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000639 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000640 parseLabel();
641 return;
642 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000643 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000644 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000645 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000646 if (FormatTok->HasUnescapedNewline &&
647 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000648 addUnwrappedLine();
649 return;
650 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000651 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
652 Text == Text.upper()) {
653 // Recognize free-standing macros like Q_OBJECT.
654 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000655 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000656 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000657 }
658 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000659 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000660 case tok::equal:
661 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000662 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000663 parseBracedList();
664 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000665 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000666 case tok::l_square:
667 tryToParseLambda();
668 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000669 default:
670 nextToken();
671 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000672 }
673 } while (!eof());
674}
675
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000676void UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000677 // FIXME: This is a dirty way to access the previous token. Find a better
678 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000679 if (!Line->Tokens.empty() &&
680 Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator)) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000681 nextToken();
682 return;
683 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000684 assert(FormatTok->is(tok::l_square));
685 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000686 if (!tryToParseLambdaIntroducer())
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000687 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000688
689 while (FormatTok->isNot(tok::l_brace)) {
690 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000691 case tok::l_brace:
692 break;
693 case tok::l_paren:
694 parseParens();
695 break;
696 case tok::identifier:
697 case tok::kw_mutable:
698 nextToken();
699 break;
700 default:
701 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000702 }
703 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000704 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000705 parseChildBlock();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000706}
707
708bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
709 nextToken();
710 if (FormatTok->is(tok::equal)) {
711 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000712 if (FormatTok->is(tok::r_square)) {
713 nextToken();
714 return true;
715 }
716 if (FormatTok->isNot(tok::comma))
717 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000718 nextToken();
719 } else if (FormatTok->is(tok::amp)) {
720 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000721 if (FormatTok->is(tok::r_square)) {
722 nextToken();
723 return true;
724 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000725 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
726 return false;
727 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000728 if (FormatTok->is(tok::comma))
729 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000730 } else if (FormatTok->is(tok::r_square)) {
731 nextToken();
732 return true;
733 }
734 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000735 if (FormatTok->is(tok::amp))
736 nextToken();
737 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
738 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000739 nextToken();
740 if (FormatTok->is(tok::comma)) {
741 nextToken();
742 } else if (FormatTok->is(tok::r_square)) {
743 nextToken();
744 return true;
745 } else {
746 return false;
747 }
748 } while (!eof());
749 return false;
750}
751
Manuel Klimek80829bd2013-05-23 09:41:43 +0000752bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000753 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000754 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000755 assert(FormatTok->BlockKind != BK_Unknown);
756 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000757 return false;
758 parseBracedList();
759 return true;
760}
761
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000762void UnwrappedLineParser::parseBracedList() {
763 nextToken();
764
Manuel Klimek423dd932013-04-10 09:52:05 +0000765 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
766 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000767 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000768 // FIXME: When we start to support lambdas, we'll want to parse them away
769 // here, otherwise our bail-out scenarios below break. The better solution
770 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000771 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +0000772 case tok::caret:
773 nextToken();
774 if (FormatTok->is(tok::l_brace)) {
775 parseChildBlock();
776 }
777 break;
778 case tok::l_square:
779 tryToParseLambda();
780 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000781 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +0000782 // Assume there are no blocks inside a braced init list apart
783 // from the ones we explicitly parse out (like lambdas).
784 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000785 parseBracedList();
786 break;
787 case tok::r_brace:
788 nextToken();
789 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000790 case tok::semi:
791 // Probably a missing closing brace. Bail out.
792 return;
793 case tok::comma:
794 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000795 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000796 default:
797 nextToken();
798 break;
799 }
800 } while (!eof());
801}
802
Manuel Klimekc44ee892013-01-21 10:07:49 +0000803void UnwrappedLineParser::parseReturn() {
804 nextToken();
805
806 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000807 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000808 case tok::l_brace:
809 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000810 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000811 // Assume missing ';'.
812 addUnwrappedLine();
813 return;
814 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000815 break;
816 case tok::l_paren:
817 parseParens();
818 break;
819 case tok::r_brace:
820 // Assume missing ';'.
821 addUnwrappedLine();
822 return;
823 case tok::semi:
824 nextToken();
825 addUnwrappedLine();
826 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000827 case tok::l_square:
828 tryToParseLambda();
829 break;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000830 default:
831 nextToken();
832 break;
833 }
834 } while (!eof());
835}
836
Daniel Jasperbac016b2012-12-03 18:12:45 +0000837void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000838 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000839 nextToken();
840 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000841 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000842 case tok::l_paren:
843 parseParens();
844 break;
845 case tok::r_paren:
846 nextToken();
847 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000848 case tok::r_brace:
849 // A "}" inside parenthesis is an error if there wasn't a matching "{".
850 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000851 case tok::l_square:
852 tryToParseLambda();
853 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000854 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000855 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +0000856 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000857 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000858 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000859 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000860 case tok::at:
861 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000862 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000863 parseBracedList();
864 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000865 default:
866 nextToken();
867 break;
868 }
869 } while (!eof());
870}
871
872void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000873 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000874 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000875 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000876 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000877 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000878 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000879 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
880 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000881 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000882 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
883 addUnwrappedLine();
884 else
885 NeedsUnwrappedLine = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000886 } else {
887 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000888 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000889 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000890 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000891 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000892 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000893 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000894 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000895 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
896 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000897 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000898 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000899 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000900 parseIfThenElse();
901 } else {
902 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000903 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000904 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000905 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000906 }
907 } else if (NeedsUnwrappedLine) {
908 addUnwrappedLine();
909 }
910}
911
Alexander Kornienko15757312012-12-06 18:03:27 +0000912void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000913 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000914 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000915 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000916 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000917 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000918 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
919 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000920 addUnwrappedLine();
921
Daniel Jaspereff18b92013-07-31 23:16:02 +0000922 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
923 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
924 DeclarationScopeStack.size() > 1);
925 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000926 // Munch the semicolon after a namespace. This is more common than one would
927 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000928 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000929 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000930 addUnwrappedLine();
931 }
932 // FIXME: Add error handling.
933}
934
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000935void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000936 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000937 "'for' or 'while' expected");
938 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000939 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000940 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000941 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000942 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
943 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000944 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000945 addUnwrappedLine();
946 } else {
947 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000948 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000949 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000950 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000951 }
952}
953
Daniel Jasperbac016b2012-12-03 18:12:45 +0000954void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000955 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000956 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000957 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000958 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
959 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000960 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000961 } else {
962 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000963 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000964 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000965 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000966 }
967
Alexander Kornienko393b0082012-12-04 15:40:36 +0000968 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000969 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000970 addUnwrappedLine();
971 return;
972 }
973
Daniel Jasperbac016b2012-12-03 18:12:45 +0000974 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000975 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000976}
977
978void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000979 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000980 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000981 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000982 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000983 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000984 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
985 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000986 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000987 if (FormatTok->Tok.is(tok::kw_break)) {
988 // "break;" after "}" on its own line only for BS_Allman
989 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
990 addUnwrappedLine();
991 parseStructuralElement();
992 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000993 }
994 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000995 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000996}
997
998void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000999 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001000 // FIXME: fix handling of complex expressions here.
1001 do {
1002 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001003 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001004 parseLabel();
1005}
1006
1007void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001008 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001009 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001010 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001011 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001012 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001013 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1014 addUnwrappedLine();
Daniel Jaspereff18b92013-07-31 23:16:02 +00001015 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001016 addUnwrappedLine();
1017 } else {
1018 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001019 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001020 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001021 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001022 }
1023}
1024
1025void UnwrappedLineParser::parseAccessSpecifier() {
1026 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001027 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001028 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001029 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001030 addUnwrappedLine();
1031}
1032
1033void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +00001034 nextToken();
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001035 // Eat up enum class ...
1036 if (FormatTok->Tok.is(tok::kw_class) ||
1037 FormatTok->Tok.is(tok::kw_struct))
1038 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001039 while (FormatTok->Tok.getIdentifierInfo() ||
1040 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001041 nextToken();
1042 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001043 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001044 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001045 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001046 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001047 nextToken();
1048 }
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001049 bool HasError = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001050 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001051 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1052 addUnwrappedLine();
Manuel Klimek308232c2013-01-21 19:17:52 +00001053 nextToken();
1054 addUnwrappedLine();
1055 ++Line->Level;
1056 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001057 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001058 case tok::l_paren:
1059 parseParens();
1060 break;
1061 case tok::r_brace:
1062 addUnwrappedLine();
1063 nextToken();
1064 --Line->Level;
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001065 if (HasError) {
1066 if (FormatTok->is(tok::semi))
1067 nextToken();
1068 addUnwrappedLine();
1069 }
Manuel Klimek308232c2013-01-21 19:17:52 +00001070 return;
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001071 case tok::semi:
1072 HasError = true;
1073 nextToken();
1074 addUnwrappedLine();
1075 break;
Manuel Klimek308232c2013-01-21 19:17:52 +00001076 case tok::comma:
1077 nextToken();
1078 addUnwrappedLine();
1079 break;
1080 default:
1081 nextToken();
1082 break;
1083 }
1084 } while (!eof());
1085 }
1086 // We fall through to parsing a structural element afterwards, so that in
1087 // enum A {} n, m;
1088 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001089}
1090
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001091void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +00001092 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001093 if (FormatTok->Tok.is(tok::identifier) ||
1094 FormatTok->Tok.is(tok::kw___attribute) ||
1095 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001096 nextToken();
1097 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001098 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001099 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001100 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001101 // The actual identifier can be a nested name specifier, and in macros
1102 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001103 while (FormatTok->Tok.is(tok::identifier) ||
1104 FormatTok->Tok.is(tok::coloncolon) ||
1105 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001106 nextToken();
1107
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001108 // Note that parsing away template declarations here leads to incorrectly
1109 // accepting function declarations as record declarations.
1110 // In general, we cannot solve this problem. Consider:
1111 // class A<int> B() {}
1112 // which can be a function definition or a class definition when B() is a
1113 // macro. If we find enough real-world cases where this is a problem, we
1114 // can parse for the 'template' keyword in the beginning of the statement,
1115 // and thus rule out the record production in case there is no template
1116 // (this would still leave us with an ambiguity between template function
1117 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001118 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1119 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1120 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001121 return;
1122 nextToken();
1123 }
1124 }
1125 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001126 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001127 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1128 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +00001129 addUnwrappedLine();
1130
Nico Weber27268772013-06-26 00:30:14 +00001131 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +00001132 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001133 // We fall through to parsing a structural element afterwards, so
1134 // class A {} n, m;
1135 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001136}
1137
Nico Weber1abe6ea2013-01-09 21:15:03 +00001138void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001139 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001140 do
1141 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001142 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001143 nextToken(); // Skip '>'.
1144}
1145
1146void UnwrappedLineParser::parseObjCUntilAtEnd() {
1147 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001148 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001149 nextToken();
1150 addUnwrappedLine();
1151 break;
1152 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001153 if (FormatTok->is(tok::l_brace)) {
1154 parseBlock(/*MustBeDeclaration=*/false);
1155 // In ObjC interfaces, nothing should be following the "}".
1156 addUnwrappedLine();
1157 } else {
1158 parseStructuralElement();
1159 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001160 } while (!eof());
1161}
1162
Nico Weber50767d82013-01-09 23:25:37 +00001163void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001164 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001165 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001166
1167 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001168 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001169 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001170 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001171 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001172 // Skip category, if present.
1173 parseParens();
1174
Manuel Klimek96e888b2013-05-28 11:55:06 +00001175 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001176 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001177
1178 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001179 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001180 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001181
1182 // With instance variables, this puts '}' on its own line. Without instance
1183 // variables, this ends the @interface line.
1184 addUnwrappedLine();
1185
Nico Weber1abe6ea2013-01-09 21:15:03 +00001186 parseObjCUntilAtEnd();
1187}
Nico Weber27d13672013-01-09 20:25:35 +00001188
Nico Weber1abe6ea2013-01-09 21:15:03 +00001189void UnwrappedLineParser::parseObjCProtocol() {
1190 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001191 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001192
Manuel Klimek96e888b2013-05-28 11:55:06 +00001193 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001194 parseObjCProtocolList();
1195
1196 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001197 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001198 nextToken();
1199 return addUnwrappedLine();
1200 }
1201
1202 addUnwrappedLine();
1203 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001204}
1205
Daniel Jasperd98927d2013-09-05 16:05:56 +00001206LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1207 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001208 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1209 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1210 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1211 E = Line.Tokens.end();
1212 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001213 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001214 }
1215 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1216 E = Line.Tokens.end();
1217 I != E; ++I) {
1218 const UnwrappedLineNode &Node = *I;
1219 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1220 I = Node.Children.begin(),
1221 E = Node.Children.end();
1222 I != E; ++I) {
1223 printDebugInfo(*I, "\nChild: ");
1224 }
1225 }
1226 llvm::dbgs() << "\n";
1227}
1228
Daniel Jasperbac016b2012-12-03 18:12:45 +00001229void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001230 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001231 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001232 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001233 if (CurrentLines == &Lines)
1234 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001235 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001236 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001237 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001238 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001239 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001240 I = PreprocessorDirectives.begin(),
1241 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001242 I != E; ++I) {
1243 CurrentLines->push_back(*I);
1244 }
1245 PreprocessorDirectives.clear();
1246 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001247}
1248
Manuel Klimek96e888b2013-05-28 11:55:06 +00001249bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001250
Manuel Klimek86721d22013-01-22 16:31:55 +00001251void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1252 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001253 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001254 I = CommentsBeforeNextToken.begin(),
1255 E = CommentsBeforeNextToken.end();
1256 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001257 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001258 addUnwrappedLine();
1259 }
1260 pushToken(*I);
1261 }
1262 if (NewlineBeforeNext && JustComments) {
1263 addUnwrappedLine();
1264 }
1265 CommentsBeforeNextToken.clear();
1266}
1267
Daniel Jasperbac016b2012-12-03 18:12:45 +00001268void UnwrappedLineParser::nextToken() {
1269 if (eof())
1270 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001271 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001272 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001273 readToken();
1274}
1275
1276void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001277 bool CommentsInCurrentLine = true;
1278 do {
1279 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001280 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1281 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001282 // If there is an unfinished unwrapped line, we flush the preprocessor
1283 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001284 bool SwitchToPreprocessorLines =
1285 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001286 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001287 // Comments stored before the preprocessor directive need to be output
1288 // before the preprocessor directive, at the same level as the
1289 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001290 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001291 parsePPDirective();
1292 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001293
1294 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1295 !Line->InPPDirective) {
1296 continue;
1297 }
1298
Manuel Klimek96e888b2013-05-28 11:55:06 +00001299 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001300 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001301 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001302 CommentsInCurrentLine = false;
1303 }
1304 if (CommentsInCurrentLine) {
1305 pushToken(FormatTok);
1306 } else {
1307 CommentsBeforeNextToken.push_back(FormatTok);
1308 }
1309 } while (!eof());
1310}
1311
Manuel Klimek96e888b2013-05-28 11:55:06 +00001312void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001313 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001314 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001315 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001316 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001317 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001318}
1319
Daniel Jaspercd162382013-01-07 13:26:07 +00001320} // end namespace format
1321} // end namespace clang