blob: a149c185aa8a4e02c9df810a0cb5e12685e51867 [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;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000643 default:
644 nextToken();
645 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000646 }
647 } while (!eof());
648}
649
Manuel Klimek80829bd2013-05-23 09:41:43 +0000650bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000651 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000652 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000653 assert(FormatTok->BlockKind != BK_Unknown);
654 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000655 return false;
656 parseBracedList();
657 return true;
658}
659
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000660void UnwrappedLineParser::parseBracedList() {
661 nextToken();
662
Manuel Klimek423dd932013-04-10 09:52:05 +0000663 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
664 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000665 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000666 // FIXME: When we start to support lambdas, we'll want to parse them away
667 // here, otherwise our bail-out scenarios below break. The better solution
668 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000669 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000670 case tok::l_brace:
671 parseBracedList();
672 break;
673 case tok::r_brace:
674 nextToken();
675 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000676 case tok::semi:
677 // Probably a missing closing brace. Bail out.
678 return;
679 case tok::comma:
680 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000681 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000682 default:
683 nextToken();
684 break;
685 }
686 } while (!eof());
687}
688
Manuel Klimekc44ee892013-01-21 10:07:49 +0000689void UnwrappedLineParser::parseReturn() {
690 nextToken();
691
692 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000693 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000694 case tok::l_brace:
695 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000696 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000697 // Assume missing ';'.
698 addUnwrappedLine();
699 return;
700 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000701 break;
702 case tok::l_paren:
703 parseParens();
704 break;
705 case tok::r_brace:
706 // Assume missing ';'.
707 addUnwrappedLine();
708 return;
709 case tok::semi:
710 nextToken();
711 addUnwrappedLine();
712 return;
713 default:
714 nextToken();
715 break;
716 }
717 } while (!eof());
718}
719
Daniel Jasperbac016b2012-12-03 18:12:45 +0000720void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000721 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000722 nextToken();
723 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000724 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000725 case tok::l_paren:
726 parseParens();
727 break;
728 case tok::r_paren:
729 nextToken();
730 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000731 case tok::r_brace:
732 // A "}" inside parenthesis is an error if there wasn't a matching "{".
733 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000734 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000735 if (!tryToParseBracedList()) {
736 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000737 {
738 ScopedLineState LineState(*this);
739 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000740 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000741 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000742 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000743 Line->Level -= 1;
744 }
745 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000746 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000747 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000748 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000749 case tok::at:
750 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000751 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000752 parseBracedList();
753 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000754 default:
755 nextToken();
756 break;
757 }
758 } while (!eof());
759}
760
761void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000762 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000763 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000764 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000765 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000766 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000767 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000768 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
769 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000770 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000771 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
772 addUnwrappedLine();
773 else
774 NeedsUnwrappedLine = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000775 } else {
776 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000777 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000778 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000779 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000780 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000781 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000782 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000783 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000784 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
785 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000786 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000787 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000788 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000789 parseIfThenElse();
790 } else {
791 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000792 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000793 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000794 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000795 }
796 } else if (NeedsUnwrappedLine) {
797 addUnwrappedLine();
798 }
799}
800
Alexander Kornienko15757312012-12-06 18:03:27 +0000801void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000802 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000803 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000804 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000805 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000806 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000807 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
808 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +0000809 addUnwrappedLine();
810
Daniel Jaspereff18b92013-07-31 23:16:02 +0000811 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
812 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
813 DeclarationScopeStack.size() > 1);
814 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000815 // Munch the semicolon after a namespace. This is more common than one would
816 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000817 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000818 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000819 addUnwrappedLine();
820 }
821 // FIXME: Add error handling.
822}
823
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000824void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000825 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000826 "'for' or 'while' expected");
827 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000828 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000829 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000830 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000831 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
832 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000833 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000834 addUnwrappedLine();
835 } else {
836 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000837 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000838 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000839 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000840 }
841}
842
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000844 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000845 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000846 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000847 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
848 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000849 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000850 } else {
851 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000852 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000853 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000854 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000855 }
856
Alexander Kornienko393b0082012-12-04 15:40:36 +0000857 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000858 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000859 addUnwrappedLine();
860 return;
861 }
862
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000864 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000865}
866
867void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000868 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000869 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000870 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000871 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000872 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000873 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
874 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000875 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +0000876 if (FormatTok->Tok.is(tok::kw_break)) {
877 // "break;" after "}" on its own line only for BS_Allman
878 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
879 addUnwrappedLine();
880 parseStructuralElement();
881 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000882 }
883 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000884 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000885}
886
887void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000888 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000889 // FIXME: fix handling of complex expressions here.
890 do {
891 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000892 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000893 parseLabel();
894}
895
896void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000897 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000898 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000899 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000900 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000901 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000902 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
903 addUnwrappedLine();
Daniel Jaspereff18b92013-07-31 23:16:02 +0000904 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000905 addUnwrappedLine();
906 } else {
907 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000908 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000909 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000910 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000911 }
912}
913
914void UnwrappedLineParser::parseAccessSpecifier() {
915 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000916 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000917 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000918 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000919 addUnwrappedLine();
920}
921
922void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000923 nextToken();
Daniel Jaspercbeb1c62013-08-20 12:42:50 +0000924 // Eat up enum class ...
925 if (FormatTok->Tok.is(tok::kw_class) ||
926 FormatTok->Tok.is(tok::kw_struct))
927 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000928 if (FormatTok->Tok.is(tok::identifier) ||
929 FormatTok->Tok.is(tok::kw___attribute) ||
930 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000931 nextToken();
932 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000933 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000934 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000935 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000936 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000937 nextToken();
938 }
Daniel Jasper4b434cf2013-08-30 10:10:19 +0000939 bool HasError = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000940 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3a247c2013-08-07 19:20:45 +0000941 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
942 addUnwrappedLine();
Manuel Klimek308232c2013-01-21 19:17:52 +0000943 nextToken();
944 addUnwrappedLine();
945 ++Line->Level;
946 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000947 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000948 case tok::l_paren:
949 parseParens();
950 break;
951 case tok::r_brace:
952 addUnwrappedLine();
953 nextToken();
954 --Line->Level;
Daniel Jasper4b434cf2013-08-30 10:10:19 +0000955 if (HasError) {
956 if (FormatTok->is(tok::semi))
957 nextToken();
958 addUnwrappedLine();
959 }
Manuel Klimek308232c2013-01-21 19:17:52 +0000960 return;
Daniel Jasper4b434cf2013-08-30 10:10:19 +0000961 case tok::semi:
962 HasError = true;
963 nextToken();
964 addUnwrappedLine();
965 break;
Manuel Klimek308232c2013-01-21 19:17:52 +0000966 case tok::comma:
967 nextToken();
968 addUnwrappedLine();
969 break;
970 default:
971 nextToken();
972 break;
973 }
974 } while (!eof());
975 }
976 // We fall through to parsing a structural element afterwards, so that in
977 // enum A {} n, m;
978 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000979}
980
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000981void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000982 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000983 if (FormatTok->Tok.is(tok::identifier) ||
984 FormatTok->Tok.is(tok::kw___attribute) ||
985 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000986 nextToken();
987 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000988 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000989 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000990 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000991 // The actual identifier can be a nested name specifier, and in macros
992 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000993 while (FormatTok->Tok.is(tok::identifier) ||
994 FormatTok->Tok.is(tok::coloncolon) ||
995 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000996 nextToken();
997
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000998 // Note that parsing away template declarations here leads to incorrectly
999 // accepting function declarations as record declarations.
1000 // In general, we cannot solve this problem. Consider:
1001 // class A<int> B() {}
1002 // which can be a function definition or a class definition when B() is a
1003 // macro. If we find enough real-world cases where this is a problem, we
1004 // can parse for the 'template' keyword in the beginning of the statement,
1005 // and thus rule out the record production in case there is no template
1006 // (this would still leave us with an ambiguity between template function
1007 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001008 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1009 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1010 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001011 return;
1012 nextToken();
1013 }
1014 }
1015 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001016 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001017 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1018 Style.BreakBeforeBraces == FormatStyle::BS_Allman)
Manuel Klimek44135b82013-05-13 12:51:40 +00001019 addUnwrappedLine();
1020
Nico Weber27268772013-06-26 00:30:14 +00001021 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +00001022 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001023 // We fall through to parsing a structural element afterwards, so
1024 // class A {} n, m;
1025 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001026}
1027
Nico Weber1abe6ea2013-01-09 21:15:03 +00001028void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001029 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001030 do
1031 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001032 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001033 nextToken(); // Skip '>'.
1034}
1035
1036void UnwrappedLineParser::parseObjCUntilAtEnd() {
1037 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001038 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001039 nextToken();
1040 addUnwrappedLine();
1041 break;
1042 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001043 if (FormatTok->is(tok::l_brace)) {
1044 parseBlock(/*MustBeDeclaration=*/false);
1045 // In ObjC interfaces, nothing should be following the "}".
1046 addUnwrappedLine();
1047 } else {
1048 parseStructuralElement();
1049 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001050 } while (!eof());
1051}
1052
Nico Weber50767d82013-01-09 23:25:37 +00001053void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001054 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001055 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001056
1057 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001058 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001059 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001060 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001061 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001062 // Skip category, if present.
1063 parseParens();
1064
Manuel Klimek96e888b2013-05-28 11:55:06 +00001065 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001066 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001067
1068 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001069 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001070 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001071
1072 // With instance variables, this puts '}' on its own line. Without instance
1073 // variables, this ends the @interface line.
1074 addUnwrappedLine();
1075
Nico Weber1abe6ea2013-01-09 21:15:03 +00001076 parseObjCUntilAtEnd();
1077}
Nico Weber27d13672013-01-09 20:25:35 +00001078
Nico Weber1abe6ea2013-01-09 21:15:03 +00001079void UnwrappedLineParser::parseObjCProtocol() {
1080 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001081 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001082
Manuel Klimek96e888b2013-05-28 11:55:06 +00001083 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001084 parseObjCProtocolList();
1085
1086 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001087 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001088 nextToken();
1089 return addUnwrappedLine();
1090 }
1091
1092 addUnwrappedLine();
1093 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001094}
1095
Daniel Jasperbac016b2012-12-03 18:12:45 +00001096void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001097 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001098 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001099 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001100 llvm::dbgs() << "Line(" << Line->Level << ")"
1101 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001102 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1103 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001104 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001105 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001106 }
1107 llvm::dbgs() << "\n";
1108 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001109 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001110 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001111 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001112 for (std::vector<UnwrappedLine>::iterator
1113 I = PreprocessorDirectives.begin(),
1114 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001115 I != E; ++I) {
1116 CurrentLines->push_back(*I);
1117 }
1118 PreprocessorDirectives.clear();
1119 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001120}
1121
Manuel Klimek96e888b2013-05-28 11:55:06 +00001122bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001123
Manuel Klimek86721d22013-01-22 16:31:55 +00001124void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1125 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001126 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001127 I = CommentsBeforeNextToken.begin(),
1128 E = CommentsBeforeNextToken.end();
1129 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001130 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001131 addUnwrappedLine();
1132 }
1133 pushToken(*I);
1134 }
1135 if (NewlineBeforeNext && JustComments) {
1136 addUnwrappedLine();
1137 }
1138 CommentsBeforeNextToken.clear();
1139}
1140
Daniel Jasperbac016b2012-12-03 18:12:45 +00001141void UnwrappedLineParser::nextToken() {
1142 if (eof())
1143 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001144 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001145 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001146 readToken();
1147}
1148
1149void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001150 bool CommentsInCurrentLine = true;
1151 do {
1152 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001153 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1154 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001155 // If there is an unfinished unwrapped line, we flush the preprocessor
1156 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001157 bool SwitchToPreprocessorLines =
1158 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001159 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001160 // Comments stored before the preprocessor directive need to be output
1161 // before the preprocessor directive, at the same level as the
1162 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001163 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001164 parsePPDirective();
1165 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001166
1167 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1168 !Line->InPPDirective) {
1169 continue;
1170 }
1171
Manuel Klimek96e888b2013-05-28 11:55:06 +00001172 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001173 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001174 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001175 CommentsInCurrentLine = false;
1176 }
1177 if (CommentsInCurrentLine) {
1178 pushToken(FormatTok);
1179 } else {
1180 CommentsBeforeNextToken.push_back(FormatTok);
1181 }
1182 } while (!eof());
1183}
1184
Manuel Klimek96e888b2013-05-28 11:55:06 +00001185void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001186 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001187 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001188 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001189 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001190 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001191}
1192
Daniel Jaspercd162382013-01-07 13:26:07 +00001193} // end namespace format
1194} // end namespace clang