blob: 253dbf97fdc6ce693dbfd39cd4d8488c6694b7b4 [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)
125 : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000128 PreBlockLine = Parser.Line.take();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000129 Parser.Line.reset(new UnwrappedLine());
130 Parser.Line->Level = PreBlockLine->Level;
131 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000132 }
133
134 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000135 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000136 Parser.addUnwrappedLine();
137 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000138 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000139 Parser.Line.reset(PreBlockLine);
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000140 Parser.MustBreakBeforeNextToken = true;
Manuel Klimek525fe162013-01-18 14:04:34 +0000141 if (SwitchToPreprocessorLines)
142 Parser.CurrentLines = &Parser.Lines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000143 }
144
145private:
146 UnwrappedLineParser &Parser;
Manuel Klimek525fe162013-01-18 14:04:34 +0000147 const bool SwitchToPreprocessorLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000148
149 UnwrappedLine *PreBlockLine;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000150};
151
Craig Toppere50947f2013-07-01 04:21:54 +0000152namespace {
153
Manuel Klimek80829bd2013-05-23 09:41:43 +0000154class IndexedTokenSource : public FormatTokenSource {
155public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000156 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000157 : Tokens(Tokens), Position(-1) {}
158
Manuel Klimek96e888b2013-05-28 11:55:06 +0000159 virtual FormatToken *getNextToken() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000160 ++Position;
161 return Tokens[Position];
162 }
163
164 virtual unsigned getPosition() {
165 assert(Position >= 0);
166 return Position;
167 }
168
Manuel Klimek96e888b2013-05-28 11:55:06 +0000169 virtual FormatToken *setPosition(unsigned P) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000170 Position = P;
171 return Tokens[Position];
172 }
173
174private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000175 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000176 int Position;
177};
178
Craig Toppere50947f2013-07-01 04:21:54 +0000179} // end anonymous namespace
180
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000181UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000182 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000183 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000184 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000185 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000186 Callback(Callback), AllTokens(Tokens) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000187
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000188bool UnwrappedLineParser::parse() {
Manuel Klimek8fa37992013-01-16 12:31:12 +0000189 DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek80829bd2013-05-23 09:41:43 +0000190 IndexedTokenSource TokenSource(AllTokens);
191 Tokens = &TokenSource;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000192 readToken();
Manuel Klimek67d080d2013-04-12 14:13:36 +0000193 parseFile();
Daniel Jasperf9955d32013-03-20 12:37:50 +0000194 for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
Manuel Klimek525fe162013-01-18 14:04:34 +0000195 I != E; ++I) {
196 Callback.consumeUnwrappedLine(*I);
197 }
Daniel Jasper516fb312013-03-01 18:11:39 +0000198
199 // Create line with eof token.
200 pushToken(FormatTok);
201 Callback.consumeUnwrappedLine(*Line);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000202 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000203}
204
Manuel Klimek67d080d2013-04-12 14:13:36 +0000205void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000206 ScopedDeclarationState DeclarationState(
207 *Line, DeclarationScopeStack,
208 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000209 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000210 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000211 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000212 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000213}
214
Manuel Klimek67d080d2013-04-12 14:13:36 +0000215void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000216 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000217 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000218 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000219 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000220 nextToken();
221 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000222 break;
223 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000224 // FIXME: Add parameter whether this can happen - if this happens, we must
225 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000226 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000227 addUnwrappedLine();
228 break;
229 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000230 if (HasOpeningBrace)
231 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000232 StructuralError = true;
233 nextToken();
234 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000235 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000236 case tok::kw_default:
237 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000238 if (!SwitchLabelEncountered &&
239 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
240 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000241 SwitchLabelEncountered = true;
242 parseStructuralElement();
243 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000244 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000245 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000246 break;
247 }
248 } while (!eof());
249}
250
Manuel Klimek80829bd2013-05-23 09:41:43 +0000251void UnwrappedLineParser::calculateBraceTypes() {
252 // We'll parse forward through the tokens until we hit
253 // a closing brace or eof - note that getNextToken() will
254 // parse macros, so this will magically work inside macro
255 // definitions, too.
256 unsigned StoredPosition = Tokens->getPosition();
257 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000258 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000259 // Keep a stack of positions of lbrace tokens. We will
260 // update information about whether an lbrace starts a
261 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000262 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000263 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000264 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000265 // Get next none-comment token.
266 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000267 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000268 do {
269 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000270 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000271 } while (NextTok->is(tok::comment));
272
Manuel Klimek96e888b2013-05-28 11:55:06 +0000273 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000274 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000275 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000276 break;
277 case tok::r_brace:
278 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000279 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000280 // If there is a comma, semicolon or right paren after the closing
281 // brace, we assume this is a braced initializer list.
282
283 // FIXME: Note that this currently works only because we do not
284 // use the brace information while inside a braced init list.
285 // Thus, if the parent is a braced init list, we consider all
286 // brace blocks inside it braced init list. That works good enough
287 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000288 //
289 // We exclude + and - as they can be ObjC visibility modifiers.
Daniel Jaspereb483662013-05-31 10:09:55 +0000290 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000291 tok::l_brace, tok::colon) ||
292 (NextTok->isBinaryOperator() &&
293 !NextTok->isOneOf(tok::plus, tok::minus))) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000294 Tok->BlockKind = BK_BracedInit;
295 LBraceStack.back()->BlockKind = BK_BracedInit;
296 } else {
297 Tok->BlockKind = BK_Block;
298 LBraceStack.back()->BlockKind = BK_Block;
299 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000300 }
301 LBraceStack.pop_back();
302 }
303 break;
304 case tok::semi:
305 case tok::kw_if:
306 case tok::kw_while:
307 case tok::kw_for:
308 case tok::kw_switch:
309 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000310 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000311 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000312 break;
313 default:
314 break;
315 }
316 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000317 Position += ReadTokens;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000318 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000319 // Assume other blocks for all unclosed opening braces.
320 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000321 if (LBraceStack[i]->BlockKind == BK_Unknown)
322 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000323 }
324 FormatTok = Tokens->setPosition(StoredPosition);
325}
326
Daniel Jaspereff18b92013-07-31 23:16:02 +0000327void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000328 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000329 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000330 nextToken();
331
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000332 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000333
Manuel Klimek70b03f42013-01-23 09:32:48 +0000334 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
335 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000336 if (AddLevel)
337 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000338 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000339
Manuel Klimek96e888b2013-05-28 11:55:06 +0000340 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000341 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000342 StructuralError = true;
343 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000344 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000345
Daniel Jasperf9955d32013-03-20 12:37:50 +0000346 nextToken(); // Munch the closing brace.
Daniel Jaspere865cc52013-07-25 11:31:57 +0000347 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000348}
349
350void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000351 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000352 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000353 nextToken();
354
Manuel Klimek96e888b2013-05-28 11:55:06 +0000355 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000356 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000357 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000358 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000359
Manuel Klimek96e888b2013-05-28 11:55:06 +0000360 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000361 case tok::pp_define:
362 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000363 return;
364 case tok::pp_if:
365 parsePPIf();
366 break;
367 case tok::pp_ifdef:
368 case tok::pp_ifndef:
369 parsePPIfdef();
370 break;
371 case tok::pp_else:
372 parsePPElse();
373 break;
374 case tok::pp_elif:
375 parsePPElIf();
376 break;
377 case tok::pp_endif:
378 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000379 break;
380 default:
381 parsePPUnknown();
382 break;
383 }
384}
385
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000386void UnwrappedLineParser::pushPPConditional() {
387 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
388 PPStack.push_back(PP_Unreachable);
389 else
390 PPStack.push_back(PP_Conditional);
391}
392
393void UnwrappedLineParser::parsePPIf() {
394 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000395 if ((FormatTok->Tok.isLiteral() &&
396 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000397 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000398 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000399 PPStack.push_back(PP_Unreachable);
400 } else {
401 pushPPConditional();
402 }
403 parsePPUnknown();
404}
405
406void UnwrappedLineParser::parsePPIfdef() {
407 pushPPConditional();
408 parsePPUnknown();
409}
410
411void UnwrappedLineParser::parsePPElse() {
412 if (!PPStack.empty())
413 PPStack.pop_back();
414 pushPPConditional();
415 parsePPUnknown();
416}
417
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000418void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000419
420void UnwrappedLineParser::parsePPEndIf() {
421 if (!PPStack.empty())
422 PPStack.pop_back();
423 parsePPUnknown();
424}
425
Manuel Klimekd4397b92013-01-04 23:34:14 +0000426void UnwrappedLineParser::parsePPDefine() {
427 nextToken();
428
Manuel Klimek96e888b2013-05-28 11:55:06 +0000429 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000430 parsePPUnknown();
431 return;
432 }
433 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000434 if (FormatTok->Tok.getKind() == tok::l_paren &&
435 FormatTok->WhitespaceRange.getBegin() ==
436 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000437 parseParens();
438 }
439 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000440 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000441
442 // Errors during a preprocessor directive can only affect the layout of the
443 // preprocessor directive, and thus we ignore them. An alternative approach
444 // would be to use the same approach we use on the file level (no
445 // re-indentation if there was a structural error) within the macro
446 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000447 parseFile();
448}
449
450void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000451 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000452 nextToken();
453 } while (!eof());
454 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000455}
456
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000457// Here we blacklist certain tokens that are not usually the first token in an
458// unwrapped line. This is used in attempt to distinguish macro calls without
459// trailing semicolons from other constructs split to several lines.
460bool tokenCanStartNewLine(clang::Token Tok) {
461 // Semicolon can be a null-statement, l_square can be a start of a macro or
462 // a C++11 attribute, but this doesn't seem to be common.
463 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
464 Tok.isNot(tok::l_square) &&
465 // Tokens that can only be used as binary operators and a part of
466 // overloaded operator names.
467 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
468 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
469 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
470 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
471 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
472 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
473 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
474 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
475 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
476 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
477 Tok.isNot(tok::lesslessequal) &&
478 // Colon is used in labels, base class lists, initializer lists,
479 // range-based for loops, ternary operator, but should never be the
480 // first token in an unwrapped line.
481 Tok.isNot(tok::colon);
482}
483
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000484void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000485 assert(!FormatTok->Tok.is(tok::l_brace));
486 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000487 case tok::at:
488 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000489 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000490 parseBracedList();
491 break;
492 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000493 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000494 case tok::objc_public:
495 case tok::objc_protected:
496 case tok::objc_package:
497 case tok::objc_private:
498 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000499 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000500 case tok::objc_implementation:
501 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000502 case tok::objc_protocol:
503 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000504 case tok::objc_end:
505 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000506 case tok::objc_optional:
507 case tok::objc_required:
508 nextToken();
509 addUnwrappedLine();
510 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000511 default:
512 break;
513 }
514 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000515 case tok::kw_namespace:
516 parseNamespace();
517 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000518 case tok::kw_inline:
519 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000520 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000521 parseNamespace();
522 return;
523 }
524 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000525 case tok::kw_public:
526 case tok::kw_protected:
527 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000528 parseAccessSpecifier();
529 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000530 case tok::kw_if:
531 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000532 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000533 case tok::kw_for:
534 case tok::kw_while:
535 parseForOrWhileLoop();
536 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000537 case tok::kw_do:
538 parseDoWhile();
539 return;
540 case tok::kw_switch:
541 parseSwitch();
542 return;
543 case tok::kw_default:
544 nextToken();
545 parseLabel();
546 return;
547 case tok::kw_case:
548 parseCaseLabel();
549 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000550 case tok::kw_return:
551 parseReturn();
552 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000553 case tok::kw_extern:
554 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000555 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000556 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000557 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000558 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000559 addUnwrappedLine();
560 return;
561 }
562 }
563 // In all other cases, parse the declaration.
564 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000565 default:
566 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000567 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000568 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000569 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000570 case tok::at:
571 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000572 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000573 parseBracedList();
574 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000575 case tok::kw_enum:
576 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000577 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000578 case tok::kw_struct:
579 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000580 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000581 parseRecord();
582 // A record declaration or definition is always the start of a structural
583 // element.
584 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000585 case tok::semi:
586 nextToken();
587 addUnwrappedLine();
588 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000589 case tok::r_brace:
590 addUnwrappedLine();
591 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000592 case tok::l_paren:
593 parseParens();
594 break;
595 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000596 if (!tryToParseBracedList()) {
597 // A block outside of parentheses must be the last part of a
598 // structural element.
599 // FIXME: Figure out cases where this is not true, and add projections
600 // for them (the one we know is missing are lambdas).
601 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Manuel Klimeke4907052013-08-02 21:31:59 +0000602 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
603 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000604 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000605 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000606 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000607 return;
608 }
609 // Otherwise this was a braced init list, and the structural
610 // element continues.
611 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000612 case tok::identifier: {
613 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000614 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000615 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000616 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000617 parseLabel();
618 return;
619 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000620 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000621 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000622 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000623 if (FormatTok->HasUnescapedNewline &&
624 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000625 addUnwrappedLine();
626 return;
627 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000628 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
629 Text == Text.upper()) {
630 // Recognize free-standing macros like Q_OBJECT.
631 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000632 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000633 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000634 }
635 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000636 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000637 case tok::equal:
638 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000639 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000640 parseBracedList();
641 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000642 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000643 case tok::l_square:
644 tryToParseLambda();
645 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000646 default:
647 nextToken();
648 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000649 }
650 } while (!eof());
651}
652
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000653void UnwrappedLineParser::tryToParseLambda() {
654 if (!tryToParseLambdaIntroducer()) {
655 return;
656 }
657 if (FormatTok->is(tok::l_paren)) {
658 parseParens();
659 }
660
661 while (FormatTok->isNot(tok::l_brace)) {
662 switch (FormatTok->Tok.getKind()) {
663 case tok::l_brace:
664 break;
665 return;
666 case tok::l_paren:
667 parseParens();
668 break;
669 case tok::semi:
670 case tok::equal:
671 case tok::eof:
672 return;
673 default:
674 nextToken();
675 break;
676 }
677 }
678 nextToken();
679 {
680 ScopedLineState LineState(*this);
681 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
682 /*MustBeDeclaration=*/false);
683 Line->Level += 1;
684 parseLevel(/*HasOpeningBrace=*/true);
685 Line->Level -= 1;
686 }
687 nextToken();
688}
689
690bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
691 nextToken();
692 if (FormatTok->is(tok::equal)) {
693 nextToken();
694 if (FormatTok->is(tok::r_square)) return true;
695 if (FormatTok->isNot(tok::comma)) return false;
696 nextToken();
697 } else if (FormatTok->is(tok::amp)) {
698 nextToken();
699 if (FormatTok->is(tok::r_square)) return true;
700 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
701 return false;
702 }
703 if (FormatTok->is(tok::comma)) nextToken();
704 } else if (FormatTok->is(tok::r_square)) {
705 nextToken();
706 return true;
707 }
708 do {
709 if (FormatTok->is(tok::amp)) nextToken();
710 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) return false;
711 nextToken();
712 if (FormatTok->is(tok::comma)) {
713 nextToken();
714 } else if (FormatTok->is(tok::r_square)) {
715 nextToken();
716 return true;
717 } else {
718 return false;
719 }
720 } while (!eof());
721 return false;
722}
723
Manuel Klimek80829bd2013-05-23 09:41:43 +0000724bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000725 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000726 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000727 assert(FormatTok->BlockKind != BK_Unknown);
728 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000729 return false;
730 parseBracedList();
731 return true;
732}
733
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000734void UnwrappedLineParser::parseBracedList() {
735 nextToken();
736
Manuel Klimek423dd932013-04-10 09:52:05 +0000737 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
738 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000739 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000740 // FIXME: When we start to support lambdas, we'll want to parse them away
741 // here, otherwise our bail-out scenarios below break. The better solution
742 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000743 switch (FormatTok->Tok.getKind()) {
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000744 case tok::l_square:
745 tryToParseLambda();
746 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000747 case tok::l_brace:
748 parseBracedList();
749 break;
750 case tok::r_brace:
751 nextToken();
752 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000753 case tok::semi:
754 // Probably a missing closing brace. Bail out.
755 return;
756 case tok::comma:
757 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000758 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000759 default:
760 nextToken();
761 break;
762 }
763 } while (!eof());
764}
765
Manuel Klimekc44ee892013-01-21 10:07:49 +0000766void UnwrappedLineParser::parseReturn() {
767 nextToken();
768
769 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000770 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000771 case tok::l_brace:
772 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000773 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000774 // Assume missing ';'.
775 addUnwrappedLine();
776 return;
777 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000778 break;
779 case tok::l_paren:
780 parseParens();
781 break;
782 case tok::r_brace:
783 // Assume missing ';'.
784 addUnwrappedLine();
785 return;
786 case tok::semi:
787 nextToken();
788 addUnwrappedLine();
789 return;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000790 case tok::l_square:
791 tryToParseLambda();
792 break;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000793 default:
794 nextToken();
795 break;
796 }
797 } while (!eof());
798}
799
Daniel Jasperbac016b2012-12-03 18:12:45 +0000800void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000801 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000802 nextToken();
803 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000804 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000805 case tok::l_paren:
806 parseParens();
807 break;
808 case tok::r_paren:
809 nextToken();
810 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000811 case tok::r_brace:
812 // A "}" inside parenthesis is an error if there wasn't a matching "{".
813 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000814 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000815 if (!tryToParseBracedList()) {
816 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000817 {
818 ScopedLineState LineState(*this);
819 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000820 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000821 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000822 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000823 Line->Level -= 1;
824 }
825 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000826 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000827 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000828 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000829 case tok::at:
830 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000831 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000832 parseBracedList();
833 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000834 default:
835 nextToken();
836 break;
837 }
838 } while (!eof());
839}
840
841void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000842 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000844 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000845 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000846 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000847 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000848 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
849 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000850 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000851 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
852 addUnwrappedLine();
853 else
854 NeedsUnwrappedLine = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000855 } else {
856 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000857 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000858 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000859 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000860 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000861 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000862 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000863 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000864 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
865 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000866 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000867 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000868 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000869 parseIfThenElse();
870 } else {
871 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000872 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000873 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000874 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000875 }
876 } else if (NeedsUnwrappedLine) {
877 addUnwrappedLine();
878 }
879}
880
Alexander Kornienko15757312012-12-06 18:03:27 +0000881void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000882 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000883 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000884 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000885 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000886 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000887 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
888 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000889 addUnwrappedLine();
890
Daniel Jaspereff18b92013-07-31 23:16:02 +0000891 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
892 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
893 DeclarationScopeStack.size() > 1);
894 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000895 // Munch the semicolon after a namespace. This is more common than one would
896 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000897 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000898 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000899 addUnwrappedLine();
900 }
901 // FIXME: Add error handling.
902}
903
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000904void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000905 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000906 "'for' or 'while' expected");
907 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000908 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000909 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000910 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000911 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
912 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000913 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000914 addUnwrappedLine();
915 } else {
916 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000917 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000918 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000919 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000920 }
921}
922
Daniel Jasperbac016b2012-12-03 18:12:45 +0000923void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000924 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000925 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000926 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000927 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
928 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000929 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000930 } else {
931 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000932 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000933 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000934 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000935 }
936
Alexander Kornienko393b0082012-12-04 15:40:36 +0000937 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000938 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000939 addUnwrappedLine();
940 return;
941 }
942
Daniel Jasperbac016b2012-12-03 18:12:45 +0000943 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000944 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000945}
946
947void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000948 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000949 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000950 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000951 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000952 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000953 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
954 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000955 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000956 if (FormatTok->Tok.is(tok::kw_break)) {
957 // "break;" after "}" on its own line only for BS_Allman
958 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
959 addUnwrappedLine();
960 parseStructuralElement();
961 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000962 }
963 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000964 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000965}
966
967void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000968 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000969 // FIXME: fix handling of complex expressions here.
970 do {
971 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000972 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000973 parseLabel();
974}
975
976void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000977 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000978 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000979 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000980 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000981 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000982 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
983 addUnwrappedLine();
Daniel Jaspereff18b92013-07-31 23:16:02 +0000984 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000985 addUnwrappedLine();
986 } else {
987 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000988 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000989 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000990 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000991 }
992}
993
994void UnwrappedLineParser::parseAccessSpecifier() {
995 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000996 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000997 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000998 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000999 addUnwrappedLine();
1000}
1001
1002void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +00001003 nextToken();
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001004 // Eat up enum class ...
1005 if (FormatTok->Tok.is(tok::kw_class) ||
1006 FormatTok->Tok.is(tok::kw_struct))
1007 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001008 if (FormatTok->Tok.is(tok::identifier) ||
1009 FormatTok->Tok.is(tok::kw___attribute) ||
1010 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001011 nextToken();
1012 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001013 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001014 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001015 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001016 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001017 nextToken();
1018 }
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001019 bool HasError = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001020 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001021 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
1022 addUnwrappedLine();
Manuel Klimek308232c2013-01-21 19:17:52 +00001023 nextToken();
1024 addUnwrappedLine();
1025 ++Line->Level;
1026 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001027 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001028 case tok::l_paren:
1029 parseParens();
1030 break;
1031 case tok::r_brace:
1032 addUnwrappedLine();
1033 nextToken();
1034 --Line->Level;
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001035 if (HasError) {
1036 if (FormatTok->is(tok::semi))
1037 nextToken();
1038 addUnwrappedLine();
1039 }
Manuel Klimek308232c2013-01-21 19:17:52 +00001040 return;
Daniel Jasper4b434cf2013-08-30 10:10:19 +00001041 case tok::semi:
1042 HasError = true;
1043 nextToken();
1044 addUnwrappedLine();
1045 break;
Manuel Klimek308232c2013-01-21 19:17:52 +00001046 case tok::comma:
1047 nextToken();
1048 addUnwrappedLine();
1049 break;
1050 default:
1051 nextToken();
1052 break;
1053 }
1054 } while (!eof());
1055 }
1056 // We fall through to parsing a structural element afterwards, so that in
1057 // enum A {} n, m;
1058 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001059}
1060
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001061void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +00001062 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001063 if (FormatTok->Tok.is(tok::identifier) ||
1064 FormatTok->Tok.is(tok::kw___attribute) ||
1065 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001066 nextToken();
1067 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001068 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001069 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001070 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001071 // The actual identifier can be a nested name specifier, and in macros
1072 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001073 while (FormatTok->Tok.is(tok::identifier) ||
1074 FormatTok->Tok.is(tok::coloncolon) ||
1075 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001076 nextToken();
1077
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001078 // Note that parsing away template declarations here leads to incorrectly
1079 // accepting function declarations as record declarations.
1080 // In general, we cannot solve this problem. Consider:
1081 // class A<int> B() {}
1082 // which can be a function definition or a class definition when B() is a
1083 // macro. If we find enough real-world cases where this is a problem, we
1084 // can parse for the 'template' keyword in the beginning of the statement,
1085 // and thus rule out the record production in case there is no template
1086 // (this would still leave us with an ambiguity between template function
1087 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001088 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1089 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1090 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001091 return;
1092 nextToken();
1093 }
1094 }
1095 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001096 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001097 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1098 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +00001099 addUnwrappedLine();
1100
Nico Weber27268772013-06-26 00:30:14 +00001101 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +00001102 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001103 // We fall through to parsing a structural element afterwards, so
1104 // class A {} n, m;
1105 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001106}
1107
Nico Weber1abe6ea2013-01-09 21:15:03 +00001108void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001109 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001110 do
1111 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001112 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001113 nextToken(); // Skip '>'.
1114}
1115
1116void UnwrappedLineParser::parseObjCUntilAtEnd() {
1117 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001118 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001119 nextToken();
1120 addUnwrappedLine();
1121 break;
1122 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001123 if (FormatTok->is(tok::l_brace)) {
1124 parseBlock(/*MustBeDeclaration=*/false);
1125 // In ObjC interfaces, nothing should be following the "}".
1126 addUnwrappedLine();
1127 } else {
1128 parseStructuralElement();
1129 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001130 } while (!eof());
1131}
1132
Nico Weber50767d82013-01-09 23:25:37 +00001133void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001134 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001135 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001136
1137 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001138 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001139 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001140 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001141 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001142 // Skip category, if present.
1143 parseParens();
1144
Manuel Klimek96e888b2013-05-28 11:55:06 +00001145 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001146 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001147
1148 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001149 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001150 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001151
1152 // With instance variables, this puts '}' on its own line. Without instance
1153 // variables, this ends the @interface line.
1154 addUnwrappedLine();
1155
Nico Weber1abe6ea2013-01-09 21:15:03 +00001156 parseObjCUntilAtEnd();
1157}
Nico Weber27d13672013-01-09 20:25:35 +00001158
Nico Weber1abe6ea2013-01-09 21:15:03 +00001159void UnwrappedLineParser::parseObjCProtocol() {
1160 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001161 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001162
Manuel Klimek96e888b2013-05-28 11:55:06 +00001163 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001164 parseObjCProtocolList();
1165
1166 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001167 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001168 nextToken();
1169 return addUnwrappedLine();
1170 }
1171
1172 addUnwrappedLine();
1173 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001174}
1175
Daniel Jasperbac016b2012-12-03 18:12:45 +00001176void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001177 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001178 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001179 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001180 llvm::dbgs() << "Line(" << Line->Level << ")"
1181 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001182 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1183 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001184 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001185 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001186 }
1187 llvm::dbgs() << "\n";
1188 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001189 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001190 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001191 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001192 for (std::vector<UnwrappedLine>::iterator
1193 I = PreprocessorDirectives.begin(),
1194 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001195 I != E; ++I) {
1196 CurrentLines->push_back(*I);
1197 }
1198 PreprocessorDirectives.clear();
1199 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001200}
1201
Manuel Klimek96e888b2013-05-28 11:55:06 +00001202bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001203
Manuel Klimek86721d22013-01-22 16:31:55 +00001204void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1205 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001206 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001207 I = CommentsBeforeNextToken.begin(),
1208 E = CommentsBeforeNextToken.end();
1209 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001210 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001211 addUnwrappedLine();
1212 }
1213 pushToken(*I);
1214 }
1215 if (NewlineBeforeNext && JustComments) {
1216 addUnwrappedLine();
1217 }
1218 CommentsBeforeNextToken.clear();
1219}
1220
Daniel Jasperbac016b2012-12-03 18:12:45 +00001221void UnwrappedLineParser::nextToken() {
1222 if (eof())
1223 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001224 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001225 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001226 readToken();
1227}
1228
1229void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001230 bool CommentsInCurrentLine = true;
1231 do {
1232 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001233 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1234 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001235 // If there is an unfinished unwrapped line, we flush the preprocessor
1236 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001237 bool SwitchToPreprocessorLines =
1238 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001239 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001240 // Comments stored before the preprocessor directive need to be output
1241 // before the preprocessor directive, at the same level as the
1242 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001243 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001244 parsePPDirective();
1245 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001246
1247 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1248 !Line->InPPDirective) {
1249 continue;
1250 }
1251
Manuel Klimek96e888b2013-05-28 11:55:06 +00001252 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001253 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001254 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001255 CommentsInCurrentLine = false;
1256 }
1257 if (CommentsInCurrentLine) {
1258 pushToken(FormatTok);
1259 } else {
1260 CommentsBeforeNextToken.push_back(FormatTok);
1261 }
1262 } while (!eof());
1263}
1264
Manuel Klimek96e888b2013-05-28 11:55:06 +00001265void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001266 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001267 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001268 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001269 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001270 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001271}
1272
Daniel Jaspercd162382013-01-07 13:26:07 +00001273} // end namespace format
1274} // end namespace clang