blob: 94fbf078dcfd4c94bcfee7d14e2845a46ef52d81 [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:
238 if (!SwitchLabelEncountered)
239 Line->Level += Style.IndentCaseLabels;
240 SwitchLabelEncountered = true;
241 parseStructuralElement();
242 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000243 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000244 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000245 break;
246 }
247 } while (!eof());
248}
249
Manuel Klimek80829bd2013-05-23 09:41:43 +0000250void UnwrappedLineParser::calculateBraceTypes() {
251 // We'll parse forward through the tokens until we hit
252 // a closing brace or eof - note that getNextToken() will
253 // parse macros, so this will magically work inside macro
254 // definitions, too.
255 unsigned StoredPosition = Tokens->getPosition();
256 unsigned Position = StoredPosition;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000257 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000258 // Keep a stack of positions of lbrace tokens. We will
259 // update information about whether an lbrace starts a
260 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000261 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000262 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000263 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000264 // Get next none-comment token.
265 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000266 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000267 do {
268 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000269 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000270 } while (NextTok->is(tok::comment));
271
Manuel Klimek96e888b2013-05-28 11:55:06 +0000272 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000273 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000274 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000275 break;
276 case tok::r_brace:
277 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000278 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000279 // If there is a comma, semicolon or right paren after the closing
280 // brace, we assume this is a braced initializer list.
281
282 // FIXME: Note that this currently works only because we do not
283 // use the brace information while inside a braced init list.
284 // Thus, if the parent is a braced init list, we consider all
285 // brace blocks inside it braced init list. That works good enough
286 // for now, but we will need to fix it to correctly handle lambdas.
Daniel Jaspereb483662013-05-31 10:09:55 +0000287 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000288 tok::l_brace, tok::colon)) {
289 Tok->BlockKind = BK_BracedInit;
290 LBraceStack.back()->BlockKind = BK_BracedInit;
291 } else {
292 Tok->BlockKind = BK_Block;
293 LBraceStack.back()->BlockKind = BK_Block;
294 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000295 }
296 LBraceStack.pop_back();
297 }
298 break;
299 case tok::semi:
300 case tok::kw_if:
301 case tok::kw_while:
302 case tok::kw_for:
303 case tok::kw_switch:
304 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000305 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000306 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000307 break;
308 default:
309 break;
310 }
311 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000312 Position += ReadTokens;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000313 } while (Tok->Tok.isNot(tok::eof));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000314 // Assume other blocks for all unclosed opening braces.
315 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000316 if (LBraceStack[i]->BlockKind == BK_Unknown)
317 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000318 }
319 FormatTok = Tokens->setPosition(StoredPosition);
320}
321
Daniel Jaspereff18b92013-07-31 23:16:02 +0000322void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000323 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000324 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000325 nextToken();
326
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000327 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000328
Manuel Klimek70b03f42013-01-23 09:32:48 +0000329 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
330 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000331 if (AddLevel)
332 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000333 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000334
Manuel Klimek96e888b2013-05-28 11:55:06 +0000335 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000336 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000337 StructuralError = true;
338 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000339 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000340
Daniel Jasperf9955d32013-03-20 12:37:50 +0000341 nextToken(); // Munch the closing brace.
Daniel Jaspere865cc52013-07-25 11:31:57 +0000342 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000343}
344
345void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000346 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000347 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000348 nextToken();
349
Manuel Klimek96e888b2013-05-28 11:55:06 +0000350 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000351 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000352 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000353 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000354
Manuel Klimek96e888b2013-05-28 11:55:06 +0000355 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000356 case tok::pp_define:
357 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000358 return;
359 case tok::pp_if:
360 parsePPIf();
361 break;
362 case tok::pp_ifdef:
363 case tok::pp_ifndef:
364 parsePPIfdef();
365 break;
366 case tok::pp_else:
367 parsePPElse();
368 break;
369 case tok::pp_elif:
370 parsePPElIf();
371 break;
372 case tok::pp_endif:
373 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000374 break;
375 default:
376 parsePPUnknown();
377 break;
378 }
379}
380
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000381void UnwrappedLineParser::pushPPConditional() {
382 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
383 PPStack.push_back(PP_Unreachable);
384 else
385 PPStack.push_back(PP_Conditional);
386}
387
388void UnwrappedLineParser::parsePPIf() {
389 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000390 if ((FormatTok->Tok.isLiteral() &&
391 StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) ==
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000392 "0") ||
Manuel Klimek96e888b2013-05-28 11:55:06 +0000393 FormatTok->Tok.is(tok::kw_false)) {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000394 PPStack.push_back(PP_Unreachable);
395 } else {
396 pushPPConditional();
397 }
398 parsePPUnknown();
399}
400
401void UnwrappedLineParser::parsePPIfdef() {
402 pushPPConditional();
403 parsePPUnknown();
404}
405
406void UnwrappedLineParser::parsePPElse() {
407 if (!PPStack.empty())
408 PPStack.pop_back();
409 pushPPConditional();
410 parsePPUnknown();
411}
412
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000413void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000414
415void UnwrappedLineParser::parsePPEndIf() {
416 if (!PPStack.empty())
417 PPStack.pop_back();
418 parsePPUnknown();
419}
420
Manuel Klimekd4397b92013-01-04 23:34:14 +0000421void UnwrappedLineParser::parsePPDefine() {
422 nextToken();
423
Manuel Klimek96e888b2013-05-28 11:55:06 +0000424 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000425 parsePPUnknown();
426 return;
427 }
428 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000429 if (FormatTok->Tok.getKind() == tok::l_paren &&
430 FormatTok->WhitespaceRange.getBegin() ==
431 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000432 parseParens();
433 }
434 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000435 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000436
437 // Errors during a preprocessor directive can only affect the layout of the
438 // preprocessor directive, and thus we ignore them. An alternative approach
439 // would be to use the same approach we use on the file level (no
440 // re-indentation if there was a structural error) within the macro
441 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000442 parseFile();
443}
444
445void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000446 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000447 nextToken();
448 } while (!eof());
449 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000450}
451
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000452// Here we blacklist certain tokens that are not usually the first token in an
453// unwrapped line. This is used in attempt to distinguish macro calls without
454// trailing semicolons from other constructs split to several lines.
455bool tokenCanStartNewLine(clang::Token Tok) {
456 // Semicolon can be a null-statement, l_square can be a start of a macro or
457 // a C++11 attribute, but this doesn't seem to be common.
458 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
459 Tok.isNot(tok::l_square) &&
460 // Tokens that can only be used as binary operators and a part of
461 // overloaded operator names.
462 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
463 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
464 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
465 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
466 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
467 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
468 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
469 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
470 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
471 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
472 Tok.isNot(tok::lesslessequal) &&
473 // Colon is used in labels, base class lists, initializer lists,
474 // range-based for loops, ternary operator, but should never be the
475 // first token in an unwrapped line.
476 Tok.isNot(tok::colon);
477}
478
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000479void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000480 assert(!FormatTok->Tok.is(tok::l_brace));
481 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000482 case tok::at:
483 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000484 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000485 parseBracedList();
486 break;
487 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000488 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000489 case tok::objc_public:
490 case tok::objc_protected:
491 case tok::objc_package:
492 case tok::objc_private:
493 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000494 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000495 case tok::objc_implementation:
496 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000497 case tok::objc_protocol:
498 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000499 case tok::objc_end:
500 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000501 case tok::objc_optional:
502 case tok::objc_required:
503 nextToken();
504 addUnwrappedLine();
505 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000506 default:
507 break;
508 }
509 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000510 case tok::kw_namespace:
511 parseNamespace();
512 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000513 case tok::kw_inline:
514 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000515 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000516 parseNamespace();
517 return;
518 }
519 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000520 case tok::kw_public:
521 case tok::kw_protected:
522 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000523 parseAccessSpecifier();
524 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000525 case tok::kw_if:
526 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000527 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000528 case tok::kw_for:
529 case tok::kw_while:
530 parseForOrWhileLoop();
531 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000532 case tok::kw_do:
533 parseDoWhile();
534 return;
535 case tok::kw_switch:
536 parseSwitch();
537 return;
538 case tok::kw_default:
539 nextToken();
540 parseLabel();
541 return;
542 case tok::kw_case:
543 parseCaseLabel();
544 return;
Manuel Klimekc44ee892013-01-21 10:07:49 +0000545 case tok::kw_return:
546 parseReturn();
547 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000548 case tok::kw_extern:
549 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000550 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000551 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000552 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000553 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000554 addUnwrappedLine();
555 return;
556 }
557 }
558 // In all other cases, parse the declaration.
559 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000560 default:
561 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000562 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000563 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000564 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000565 case tok::at:
566 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000567 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000568 parseBracedList();
569 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000570 case tok::kw_enum:
571 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000572 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000573 case tok::kw_struct:
574 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000575 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000576 parseRecord();
577 // A record declaration or definition is always the start of a structural
578 // element.
579 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000580 case tok::semi:
581 nextToken();
582 addUnwrappedLine();
583 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000584 case tok::r_brace:
585 addUnwrappedLine();
586 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000587 case tok::l_paren:
588 parseParens();
589 break;
590 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000591 if (!tryToParseBracedList()) {
592 // A block outside of parentheses must be the last part of a
593 // structural element.
594 // FIXME: Figure out cases where this is not true, and add projections
595 // for them (the one we know is missing are lambdas).
596 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
597 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
598 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +0000599 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000600 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000601 return;
602 }
603 // Otherwise this was a braced init list, and the structural
604 // element continues.
605 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000606 case tok::identifier: {
607 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000608 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000609 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000610 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000611 parseLabel();
612 return;
613 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000614 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000615 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000616 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000617 if (FormatTok->HasUnescapedNewline &&
618 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000619 addUnwrappedLine();
620 return;
621 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000622 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
623 Text == Text.upper()) {
624 // Recognize free-standing macros like Q_OBJECT.
625 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000626 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000627 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000628 }
629 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000630 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000631 case tok::equal:
632 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000633 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000634 parseBracedList();
635 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000636 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000637 default:
638 nextToken();
639 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000640 }
641 } while (!eof());
642}
643
Manuel Klimek80829bd2013-05-23 09:41:43 +0000644bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000645 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000646 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000647 assert(FormatTok->BlockKind != BK_Unknown);
648 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000649 return false;
650 parseBracedList();
651 return true;
652}
653
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000654void UnwrappedLineParser::parseBracedList() {
655 nextToken();
656
Manuel Klimek423dd932013-04-10 09:52:05 +0000657 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
658 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000659 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000660 // FIXME: When we start to support lambdas, we'll want to parse them away
661 // here, otherwise our bail-out scenarios below break. The better solution
662 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000663 switch (FormatTok->Tok.getKind()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000664 case tok::l_brace:
665 parseBracedList();
666 break;
667 case tok::r_brace:
668 nextToken();
669 return;
Manuel Klimek423dd932013-04-10 09:52:05 +0000670 case tok::semi:
671 // Probably a missing closing brace. Bail out.
672 return;
673 case tok::comma:
674 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000675 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000676 default:
677 nextToken();
678 break;
679 }
680 } while (!eof());
681}
682
Manuel Klimekc44ee892013-01-21 10:07:49 +0000683void UnwrappedLineParser::parseReturn() {
684 nextToken();
685
686 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000687 switch (FormatTok->Tok.getKind()) {
Manuel Klimekc44ee892013-01-21 10:07:49 +0000688 case tok::l_brace:
689 parseBracedList();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000690 if (FormatTok->Tok.isNot(tok::semi)) {
Manuel Klimek423dd932013-04-10 09:52:05 +0000691 // Assume missing ';'.
692 addUnwrappedLine();
693 return;
694 }
Manuel Klimekc44ee892013-01-21 10:07:49 +0000695 break;
696 case tok::l_paren:
697 parseParens();
698 break;
699 case tok::r_brace:
700 // Assume missing ';'.
701 addUnwrappedLine();
702 return;
703 case tok::semi:
704 nextToken();
705 addUnwrappedLine();
706 return;
707 default:
708 nextToken();
709 break;
710 }
711 } while (!eof());
712}
713
Daniel Jasperbac016b2012-12-03 18:12:45 +0000714void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000715 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000716 nextToken();
717 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000718 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000719 case tok::l_paren:
720 parseParens();
721 break;
722 case tok::r_paren:
723 nextToken();
724 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000725 case tok::r_brace:
726 // A "}" inside parenthesis is an error if there wasn't a matching "{".
727 return;
Nico Weber2afbe522013-02-10 04:38:23 +0000728 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000729 if (!tryToParseBracedList()) {
730 nextToken();
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000731 {
732 ScopedLineState LineState(*this);
733 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
Nico Weber27268772013-06-26 00:30:14 +0000734 /*MustBeDeclaration=*/false);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000735 Line->Level += 1;
Nico Weber27268772013-06-26 00:30:14 +0000736 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000737 Line->Level -= 1;
738 }
739 nextToken();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000740 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000741 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000742 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000743 case tok::at:
744 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000745 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000746 parseBracedList();
747 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000748 default:
749 nextToken();
750 break;
751 }
752 } while (!eof());
753}
754
755void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000756 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000757 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000758 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000759 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000760 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000761 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000762 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000763 NeedsUnwrappedLine = true;
764 } else {
765 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000766 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000767 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000768 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000769 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000770 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000771 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000772 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000773 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000774 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000775 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000776 parseIfThenElse();
777 } else {
778 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000779 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000780 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000781 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000782 }
783 } else if (NeedsUnwrappedLine) {
784 addUnwrappedLine();
785 }
786}
787
Alexander Kornienko15757312012-12-06 18:03:27 +0000788void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000789 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +0000790 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000791 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +0000792 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000793 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000794 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
795 addUnwrappedLine();
796
Daniel Jaspereff18b92013-07-31 23:16:02 +0000797 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
798 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
799 DeclarationScopeStack.size() > 1);
800 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000801 // Munch the semicolon after a namespace. This is more common than one would
802 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000803 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +0000804 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +0000805 addUnwrappedLine();
806 }
807 // FIXME: Add error handling.
808}
809
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000810void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000811 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000812 "'for' or 'while' expected");
813 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000814 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000815 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000816 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000817 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000818 addUnwrappedLine();
819 } else {
820 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000821 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000822 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000823 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000824 }
825}
826
Daniel Jasperbac016b2012-12-03 18:12:45 +0000827void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000828 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000829 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000830 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000831 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000832 } else {
833 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000834 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000835 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +0000836 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000837 }
838
Alexander Kornienko393b0082012-12-04 15:40:36 +0000839 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000840 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +0000841 addUnwrappedLine();
842 return;
843 }
844
Daniel Jasperbac016b2012-12-03 18:12:45 +0000845 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000846 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000847}
848
849void UnwrappedLineParser::parseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000850 if (FormatTok->Tok.isNot(tok::colon))
Daniel Jasper89a0daa2013-02-12 20:17:17 +0000851 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000852 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +0000853 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +0000854 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +0000855 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000856 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Nico Weber27268772013-06-26 00:30:14 +0000857 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000858 if (FormatTok->Tok.is(tok::kw_break))
Nico Weber94fb7292013-01-18 05:50:57 +0000859 parseStructuralElement(); // "break;" after "}" goes on the same line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000860 }
861 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000862 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863}
864
865void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000866 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000867 // FIXME: fix handling of complex expressions here.
868 do {
869 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000870 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +0000871 parseLabel();
872}
873
874void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000875 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000876 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000877 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +0000878 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000879 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000880 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000881 addUnwrappedLine();
882 } else {
883 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000884 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000885 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000886 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000887 }
888}
889
890void UnwrappedLineParser::parseAccessSpecifier() {
891 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000892 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000893 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +0000894 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000895 addUnwrappedLine();
896}
897
898void UnwrappedLineParser::parseEnum() {
Manuel Klimek308232c2013-01-21 19:17:52 +0000899 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000900 if (FormatTok->Tok.is(tok::identifier) ||
901 FormatTok->Tok.is(tok::kw___attribute) ||
902 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000903 nextToken();
904 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000905 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000906 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000907 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000908 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +0000909 nextToken();
910 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000911 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000912 nextToken();
913 addUnwrappedLine();
914 ++Line->Level;
915 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000916 switch (FormatTok->Tok.getKind()) {
Manuel Klimek308232c2013-01-21 19:17:52 +0000917 case tok::l_paren:
918 parseParens();
919 break;
920 case tok::r_brace:
921 addUnwrappedLine();
922 nextToken();
923 --Line->Level;
924 return;
925 case tok::comma:
926 nextToken();
927 addUnwrappedLine();
928 break;
929 default:
930 nextToken();
931 break;
932 }
933 } while (!eof());
934 }
935 // We fall through to parsing a structural element afterwards, so that in
936 // enum A {} n, m;
937 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000938}
939
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000940void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +0000941 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000942 if (FormatTok->Tok.is(tok::identifier) ||
943 FormatTok->Tok.is(tok::kw___attribute) ||
944 FormatTok->Tok.is(tok::kw___declspec)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000945 nextToken();
946 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000947 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000948 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +0000949 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +0000950 // The actual identifier can be a nested name specifier, and in macros
951 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000952 while (FormatTok->Tok.is(tok::identifier) ||
953 FormatTok->Tok.is(tok::coloncolon) ||
954 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000955 nextToken();
956
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000957 // Note that parsing away template declarations here leads to incorrectly
958 // accepting function declarations as record declarations.
959 // In general, we cannot solve this problem. Consider:
960 // class A<int> B() {}
961 // which can be a function definition or a class definition when B() is a
962 // macro. If we find enough real-world cases where this is a problem, we
963 // can parse for the 'template' keyword in the beginning of the statement,
964 // and thus rule out the record production in case there is no template
965 // (this would still leave us with an ambiguity between template function
966 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +0000967 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
968 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
969 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000970 return;
971 nextToken();
972 }
973 }
974 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000975 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek44135b82013-05-13 12:51:40 +0000976 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
977 addUnwrappedLine();
978
Nico Weber27268772013-06-26 00:30:14 +0000979 parseBlock(/*MustBeDeclaration=*/true);
Manuel Klimek44135b82013-05-13 12:51:40 +0000980 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +0000981 // We fall through to parsing a structural element afterwards, so
982 // class A {} n, m;
983 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +0000984}
985
Nico Weber1abe6ea2013-01-09 21:15:03 +0000986void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000987 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +0000988 do
989 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000990 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +0000991 nextToken(); // Skip '>'.
992}
993
994void UnwrappedLineParser::parseObjCUntilAtEnd() {
995 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000996 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +0000997 nextToken();
998 addUnwrappedLine();
999 break;
1000 }
1001 parseStructuralElement();
1002 } while (!eof());
1003}
1004
Nico Weber50767d82013-01-09 23:25:37 +00001005void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001006 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001007 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001008
1009 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001010 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001011 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001012 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001013 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001014 // Skip category, if present.
1015 parseParens();
1016
Manuel Klimek96e888b2013-05-28 11:55:06 +00001017 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001018 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001019
1020 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001021 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001022 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001023
1024 // With instance variables, this puts '}' on its own line. Without instance
1025 // variables, this ends the @interface line.
1026 addUnwrappedLine();
1027
Nico Weber1abe6ea2013-01-09 21:15:03 +00001028 parseObjCUntilAtEnd();
1029}
Nico Weber27d13672013-01-09 20:25:35 +00001030
Nico Weber1abe6ea2013-01-09 21:15:03 +00001031void UnwrappedLineParser::parseObjCProtocol() {
1032 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001033 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001034
Manuel Klimek96e888b2013-05-28 11:55:06 +00001035 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001036 parseObjCProtocolList();
1037
1038 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001039 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001040 nextToken();
1041 return addUnwrappedLine();
1042 }
1043
1044 addUnwrappedLine();
1045 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001046}
1047
Daniel Jasperbac016b2012-12-03 18:12:45 +00001048void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001049 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001050 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001051 DEBUG({
Manuel Klimeka28fc062013-02-11 12:33:24 +00001052 llvm::dbgs() << "Line(" << Line->Level << ")"
1053 << (Line->InPPDirective ? " MACRO" : "") << ": ";
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001054 for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(),
1055 E = Line->Tokens.end();
Manuel Klimek8fa37992013-01-16 12:31:12 +00001056 I != E; ++I) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001057 llvm::dbgs() << (*I)->Tok.getName() << " ";
Manuel Klimek8fa37992013-01-16 12:31:12 +00001058 }
1059 llvm::dbgs() << "\n";
1060 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001061 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001062 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001063 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper516fb312013-03-01 18:11:39 +00001064 for (std::vector<UnwrappedLine>::iterator
1065 I = PreprocessorDirectives.begin(),
1066 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001067 I != E; ++I) {
1068 CurrentLines->push_back(*I);
1069 }
1070 PreprocessorDirectives.clear();
1071 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001072}
1073
Manuel Klimek96e888b2013-05-28 11:55:06 +00001074bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001075
Manuel Klimek86721d22013-01-22 16:31:55 +00001076void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1077 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001078 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001079 I = CommentsBeforeNextToken.begin(),
1080 E = CommentsBeforeNextToken.end();
1081 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001082 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001083 addUnwrappedLine();
1084 }
1085 pushToken(*I);
1086 }
1087 if (NewlineBeforeNext && JustComments) {
1088 addUnwrappedLine();
1089 }
1090 CommentsBeforeNextToken.clear();
1091}
1092
Daniel Jasperbac016b2012-12-03 18:12:45 +00001093void UnwrappedLineParser::nextToken() {
1094 if (eof())
1095 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001096 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001097 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001098 readToken();
1099}
1100
1101void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001102 bool CommentsInCurrentLine = true;
1103 do {
1104 FormatTok = Tokens->getNextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001105 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1106 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001107 // If there is an unfinished unwrapped line, we flush the preprocessor
1108 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001109 bool SwitchToPreprocessorLines =
1110 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001111 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001112 // Comments stored before the preprocessor directive need to be output
1113 // before the preprocessor directive, at the same level as the
1114 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001115 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001116 parsePPDirective();
1117 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001118
1119 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1120 !Line->InPPDirective) {
1121 continue;
1122 }
1123
Manuel Klimek96e888b2013-05-28 11:55:06 +00001124 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001125 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001126 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001127 CommentsInCurrentLine = false;
1128 }
1129 if (CommentsInCurrentLine) {
1130 pushToken(FormatTok);
1131 } else {
1132 CommentsBeforeNextToken.push_back(FormatTok);
1133 }
1134 } while (!eof());
1135}
1136
Manuel Klimek96e888b2013-05-28 11:55:06 +00001137void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001138 Line->Tokens.push_back(Tok);
Manuel Klimek86721d22013-01-22 16:31:55 +00001139 if (MustBreakBeforeNextToken) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +00001140 Line->Tokens.back()->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001141 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001142 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001143}
1144
Daniel Jaspercd162382013-01-07 13:26:07 +00001145} // end namespace format
1146} // end namespace clang