blob: 905f9c1ba5fdf792c4a8df97ae357fedba4f5734 [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
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070017#include "llvm/ADT/STLExtras.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070019#include "llvm/Support/raw_ostream.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000020
Stephen Hines6bcf27b2014-05-29 04:14:42 -070021#define DEBUG_TYPE "format-parser"
22
Daniel Jasperbac016b2012-12-03 18:12:45 +000023namespace clang {
24namespace format {
25
Manuel Klimek96e888b2013-05-28 11:55:06 +000026class FormatTokenSource {
27public:
28 virtual ~FormatTokenSource() {}
29 virtual FormatToken *getNextToken() = 0;
30
31 virtual unsigned getPosition() = 0;
32 virtual FormatToken *setPosition(unsigned Position) = 0;
33};
34
Craig Toppere50947f2013-07-01 04:21:54 +000035namespace {
36
Manuel Klimek70b03f42013-01-23 09:32:48 +000037class ScopedDeclarationState {
38public:
39 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
40 bool MustBeDeclaration)
41 : Line(Line), Stack(Stack) {
Manuel Klimek70b03f42013-01-23 09:32:48 +000042 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000043 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000044 }
45 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000046 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000047 if (!Stack.empty())
48 Line.MustBeDeclaration = Stack.back();
49 else
50 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000051 }
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000052
Manuel Klimek70b03f42013-01-23 09:32:48 +000053private:
54 UnwrappedLine &Line;
55 std::vector<bool> &Stack;
56};
57
Manuel Klimekd4397b92013-01-04 23:34:14 +000058class ScopedMacroState : public FormatTokenSource {
59public:
60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek96e888b2013-05-28 11:55:06 +000061 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimekd4397b92013-01-04 23:34:14 +000062 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek67d080d2013-04-12 14:13:36 +000063 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
64 StructuralError(StructuralError),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070065 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000066 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000067 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000068 Line.InPPDirective = true;
69 }
70
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070071 ~ScopedMacroState() override {
Manuel Klimekd4397b92013-01-04 23:34:14 +000072 TokenSource = PreviousTokenSource;
73 ResetToken = Token;
74 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000075 Line.Level = PreviousLineLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +000076 StructuralError = PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +000077 }
78
Stephen Hines651f13c2014-04-23 16:59:28 -070079 FormatToken *getNextToken() override {
Manuel Klimekdd5b1012013-01-07 10:03:37 +000080 // The \c UnwrappedLineParser guards against this by never calling
81 // \c getNextToken() after it has encountered the first eof token.
82 assert(!eof());
Manuel Klimekd4397b92013-01-04 23:34:14 +000083 Token = PreviousTokenSource->getNextToken();
84 if (eof())
Manuel Klimek96e888b2013-05-28 11:55:06 +000085 return getFakeEOF();
Manuel Klimekd4397b92013-01-04 23:34:14 +000086 return Token;
87 }
88
Stephen Hines651f13c2014-04-23 16:59:28 -070089 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimek80829bd2013-05-23 09:41:43 +000090
Stephen Hines651f13c2014-04-23 16:59:28 -070091 FormatToken *setPosition(unsigned Position) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +000092 Token = PreviousTokenSource->setPosition(Position);
93 return Token;
94 }
95
Manuel Klimekd4397b92013-01-04 23:34:14 +000096private:
Manuel Klimek96e888b2013-05-28 11:55:06 +000097 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000098
Manuel Klimek96e888b2013-05-28 11:55:06 +000099 FormatToken *getFakeEOF() {
100 static bool EOFInitialized = false;
101 static FormatToken FormatTok;
102 if (!EOFInitialized) {
103 FormatTok.Tok.startToken();
104 FormatTok.Tok.setKind(tok::eof);
105 EOFInitialized = true;
106 }
107 return &FormatTok;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000108 }
109
110 UnwrappedLine &Line;
111 FormatTokenSource *&TokenSource;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000112 FormatToken *&ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000113 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000114 FormatTokenSource *PreviousTokenSource;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000115 bool &StructuralError;
116 bool PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000117
Manuel Klimek96e888b2013-05-28 11:55:06 +0000118 FormatToken *Token;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000119};
120
Craig Toppere50947f2013-07-01 04:21:54 +0000121} // end anonymous namespace
122
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000123class ScopedLineState {
124public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000125 ScopedLineState(UnwrappedLineParser &Parser,
126 bool SwitchToPreprocessorLines = false)
Stephen Hines176edba2014-12-01 14:53:08 -0800127 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimek525fe162013-01-18 14:04:34 +0000128 if (SwitchToPreprocessorLines)
129 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000130 else if (!Parser.Line->Tokens.empty())
131 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Stephen Hines176edba2014-12-01 14:53:08 -0800132 PreBlockLine = std::move(Parser.Line);
133 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000134 Parser.Line->Level = PreBlockLine->Level;
135 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000136 }
137
138 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000139 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000140 Parser.addUnwrappedLine();
141 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000142 assert(Parser.Line->Tokens.empty());
Stephen Hines176edba2014-12-01 14:53:08 -0800143 Parser.Line = std::move(PreBlockLine);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000144 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
145 Parser.MustBreakBeforeNextToken = true;
146 Parser.CurrentLines = OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000147 }
148
149private:
150 UnwrappedLineParser &Parser;
151
Stephen Hines176edba2014-12-01 14:53:08 -0800152 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000153 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000154};
155
Stephen Hines651f13c2014-04-23 16:59:28 -0700156class CompoundStatementIndenter {
157public:
158 CompoundStatementIndenter(UnwrappedLineParser *Parser,
159 const FormatStyle &Style, unsigned &LineLevel)
160 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
161 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
162 Parser->addUnwrappedLine();
163 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
164 Parser->addUnwrappedLine();
165 ++LineLevel;
166 }
167 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700168 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Stephen Hines651f13c2014-04-23 16:59:28 -0700169
170private:
171 unsigned &LineLevel;
172 unsigned OldLineLevel;
173};
174
Craig Toppere50947f2013-07-01 04:21:54 +0000175namespace {
176
Manuel Klimek80829bd2013-05-23 09:41:43 +0000177class IndexedTokenSource : public FormatTokenSource {
178public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000179 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000180 : Tokens(Tokens), Position(-1) {}
181
Stephen Hines651f13c2014-04-23 16:59:28 -0700182 FormatToken *getNextToken() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000183 ++Position;
184 return Tokens[Position];
185 }
186
Stephen Hines651f13c2014-04-23 16:59:28 -0700187 unsigned getPosition() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000188 assert(Position >= 0);
189 return Position;
190 }
191
Stephen Hines651f13c2014-04-23 16:59:28 -0700192 FormatToken *setPosition(unsigned P) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000193 Position = P;
194 return Tokens[Position];
195 }
196
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000197 void reset() { Position = -1; }
198
Manuel Klimek80829bd2013-05-23 09:41:43 +0000199private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000200 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000201 int Position;
202};
203
Craig Toppere50947f2013-07-01 04:21:54 +0000204} // end anonymous namespace
205
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000206UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Stephen Hines176edba2014-12-01 14:53:08 -0800207 const AdditionalKeywords &Keywords,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000208 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000209 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700211 CurrentLines(&Lines), StructuralError(false), Style(Style),
Stephen Hines176edba2014-12-01 14:53:08 -0800212 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
213 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000214
215void UnwrappedLineParser::reset() {
216 PPBranchLevel = -1;
217 Line.reset(new UnwrappedLine);
218 CommentsBeforeNextToken.clear();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700219 FormatTok = nullptr;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000220 MustBreakBeforeNextToken = false;
221 PreprocessorDirectives.clear();
222 CurrentLines = &Lines;
223 DeclarationScopeStack.clear();
224 StructuralError = false;
225 PPStack.clear();
226}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000227
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000228bool UnwrappedLineParser::parse() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000229 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000230 do {
231 DEBUG(llvm::dbgs() << "----\n");
232 reset();
233 Tokens = &TokenSource;
234 TokenSource.reset();
Daniel Jasper516fb312013-03-01 18:11:39 +0000235
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000236 readToken();
237 parseFile();
238 // Create line with eof token.
239 pushToken(FormatTok);
240 addUnwrappedLine();
241
242 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
243 E = Lines.end();
244 I != E; ++I) {
245 Callback.consumeUnwrappedLine(*I);
246 }
247 Callback.finishRun();
248 Lines.clear();
249 while (!PPLevelBranchIndex.empty() &&
Daniel Jasperac4d0182013-10-12 13:32:56 +0000250 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000251 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
252 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
253 }
254 if (!PPLevelBranchIndex.empty()) {
255 ++PPLevelBranchIndex.back();
256 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
257 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
258 }
259 } while (!PPLevelBranchIndex.empty());
260
Manuel Klimek67d080d2013-04-12 14:13:36 +0000261 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000262}
263
Manuel Klimek67d080d2013-04-12 14:13:36 +0000264void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000265 ScopedDeclarationState DeclarationState(
266 *Line, DeclarationScopeStack,
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700267 /*MustBeDeclaration=*/!Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000268 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000269 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000270 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000271 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000272}
273
Manuel Klimek67d080d2013-04-12 14:13:36 +0000274void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000275 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000276 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000277 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000278 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000279 nextToken();
280 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000281 break;
282 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000283 // FIXME: Add parameter whether this can happen - if this happens, we must
284 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000285 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000286 addUnwrappedLine();
287 break;
288 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000289 if (HasOpeningBrace)
290 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000291 StructuralError = true;
292 nextToken();
293 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000294 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000295 case tok::kw_default:
296 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000297 if (!SwitchLabelEncountered &&
298 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
299 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000300 SwitchLabelEncountered = true;
301 parseStructuralElement();
302 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000303 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000304 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000305 break;
306 }
307 } while (!eof());
308}
309
Manuel Klimek80829bd2013-05-23 09:41:43 +0000310void UnwrappedLineParser::calculateBraceTypes() {
311 // We'll parse forward through the tokens until we hit
312 // a closing brace or eof - note that getNextToken() will
313 // parse macros, so this will magically work inside macro
314 // definitions, too.
315 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000316 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000317 // Keep a stack of positions of lbrace tokens. We will
318 // update information about whether an lbrace starts a
319 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000320 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000321 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000322 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000323 // Get next none-comment token.
324 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000325 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000326 do {
327 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000328 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000329 } while (NextTok->is(tok::comment));
330
Manuel Klimek96e888b2013-05-28 11:55:06 +0000331 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000332 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000333 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000334 break;
335 case tok::r_brace:
336 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000337 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700338 bool ProbablyBracedList = false;
339 if (Style.Language == FormatStyle::LK_Proto) {
340 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
341 } else {
342 // Using OriginalColumn to distinguish between ObjC methods and
343 // binary operators is a bit hacky.
344 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
345 NextTok->OriginalColumn == 0;
346
347 // If there is a comma, semicolon or right paren after the closing
348 // brace, we assume this is a braced initializer list. Note that
349 // regardless how we mark inner braces here, we will overwrite the
350 // BlockKind later if we parse a braced list (where all blocks
351 // inside are by default braced lists), or when we explicitly detect
352 // blocks (for example while parsing lambdas).
353 //
354 // We exclude + and - as they can be ObjC visibility modifiers.
355 ProbablyBracedList =
356 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
357 tok::r_paren, tok::r_square, tok::l_brace,
Stephen Hines176edba2014-12-01 14:53:08 -0800358 tok::l_paren, tok::ellipsis) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700359 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
360 }
361 if (ProbablyBracedList) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000362 Tok->BlockKind = BK_BracedInit;
363 LBraceStack.back()->BlockKind = BK_BracedInit;
364 } else {
365 Tok->BlockKind = BK_Block;
366 LBraceStack.back()->BlockKind = BK_Block;
367 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000368 }
369 LBraceStack.pop_back();
370 }
371 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700372 case tok::at:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000373 case tok::semi:
374 case tok::kw_if:
375 case tok::kw_while:
376 case tok::kw_for:
377 case tok::kw_switch:
378 case tok::kw_try:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700379 case tok::kw___try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000380 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000381 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000382 break;
383 default:
384 break;
385 }
386 Tok = NextTok;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000387 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000388 // Assume other blocks for all unclosed opening braces.
389 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000390 if (LBraceStack[i]->BlockKind == BK_Unknown)
391 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000392 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000393
Manuel Klimek80829bd2013-05-23 09:41:43 +0000394 FormatTok = Tokens->setPosition(StoredPosition);
395}
396
Manuel Klimekaabfb272013-10-12 22:46:56 +0000397void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
398 bool MunchSemi) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000399 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000400 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000401 nextToken();
402
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000403 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000404
Manuel Klimek70b03f42013-01-23 09:32:48 +0000405 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
406 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000407 if (AddLevel)
408 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000409 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000410
Manuel Klimek96e888b2013-05-28 11:55:06 +0000411 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000412 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000413 StructuralError = true;
414 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000415 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000416
Daniel Jasperf9955d32013-03-20 12:37:50 +0000417 nextToken(); // Munch the closing brace.
Manuel Klimekaabfb272013-10-12 22:46:56 +0000418 if (MunchSemi && FormatTok->Tok.is(tok::semi))
419 nextToken();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000420 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000421}
422
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700423static bool isGoogScope(const UnwrappedLine &Line) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700424 // FIXME: Closure-library specific stuff should not be hard-coded but be
425 // configurable.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700426 if (Line.Tokens.size() < 4)
427 return false;
428 auto I = Line.Tokens.begin();
429 if (I->Tok->TokenText != "goog")
430 return false;
431 ++I;
432 if (I->Tok->isNot(tok::period))
433 return false;
434 ++I;
435 if (I->Tok->TokenText != "scope")
436 return false;
437 ++I;
438 return I->Tok->is(tok::l_paren);
439}
440
Stephen Hines176edba2014-12-01 14:53:08 -0800441static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
442 const FormatToken &InitialToken) {
443 switch (Style.BreakBeforeBraces) {
444 case FormatStyle::BS_Linux:
445 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
446 case FormatStyle::BS_Allman:
447 case FormatStyle::BS_GNU:
448 return true;
449 default:
450 return false;
451 }
452}
453
Manuel Klimek753a5112013-09-04 13:25:30 +0000454void UnwrappedLineParser::parseChildBlock() {
455 FormatTok->BlockKind = BK_Block;
456 nextToken();
457 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700458 bool GoogScope =
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700459 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek753a5112013-09-04 13:25:30 +0000460 ScopedLineState LineState(*this);
461 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
462 /*MustBeDeclaration=*/false);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700463 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000464 parseLevel(/*HasOpeningBrace=*/true);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700465 flushComments(isOnNewLine(*FormatTok));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700466 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000467 }
468 nextToken();
469}
470
Daniel Jasperbac016b2012-12-03 18:12:45 +0000471void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000472 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000473 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000474 nextToken();
475
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700476 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000477 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000478 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000479 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000480
Manuel Klimek96e888b2013-05-28 11:55:06 +0000481 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000482 case tok::pp_define:
483 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000484 return;
485 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000486 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000487 break;
488 case tok::pp_ifdef:
489 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000490 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000491 break;
492 case tok::pp_else:
493 parsePPElse();
494 break;
495 case tok::pp_elif:
496 parsePPElIf();
497 break;
498 case tok::pp_endif:
499 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000500 break;
501 default:
502 parsePPUnknown();
503 break;
504 }
505}
506
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700507void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
508 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000509 PPStack.push_back(PP_Unreachable);
510 else
511 PPStack.push_back(PP_Conditional);
512}
513
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700514void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000515 ++PPBranchLevel;
516 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
517 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
518 PPLevelBranchIndex.push_back(0);
519 PPLevelBranchCount.push_back(0);
520 }
521 PPChainBranchIndex.push(0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700522 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
523 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000524}
525
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700526void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000527 if (!PPStack.empty())
528 PPStack.pop_back();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000529 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
530 if (!PPChainBranchIndex.empty())
531 ++PPChainBranchIndex.top();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700532 conditionalCompilationCondition(
533 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
534 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000535}
536
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700537void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000538 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
539 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
540 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000541 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
542 }
543 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700544 // Guard against #endif's without #if.
545 if (PPBranchLevel > 0)
546 --PPBranchLevel;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000547 if (!PPChainBranchIndex.empty())
548 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000549 if (!PPStack.empty())
550 PPStack.pop_back();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700551}
552
553void UnwrappedLineParser::parsePPIf(bool IfDef) {
554 nextToken();
555 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700556 FormatTok->Tok.getLiteralData() != nullptr &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700557 StringRef(FormatTok->Tok.getLiteralData(),
558 FormatTok->Tok.getLength()) == "0") ||
559 FormatTok->Tok.is(tok::kw_false);
560 conditionalCompilationStart(!IfDef && IsLiteralFalse);
561 parsePPUnknown();
562}
563
564void UnwrappedLineParser::parsePPElse() {
565 conditionalCompilationAlternative();
566 parsePPUnknown();
567}
568
569void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
570
571void UnwrappedLineParser::parsePPEndIf() {
572 conditionalCompilationEnd();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000573 parsePPUnknown();
574}
575
Manuel Klimekd4397b92013-01-04 23:34:14 +0000576void UnwrappedLineParser::parsePPDefine() {
577 nextToken();
578
Manuel Klimek96e888b2013-05-28 11:55:06 +0000579 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000580 parsePPUnknown();
581 return;
582 }
583 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000584 if (FormatTok->Tok.getKind() == tok::l_paren &&
585 FormatTok->WhitespaceRange.getBegin() ==
586 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000587 parseParens();
588 }
589 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000590 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000591
592 // Errors during a preprocessor directive can only affect the layout of the
593 // preprocessor directive, and thus we ignore them. An alternative approach
594 // would be to use the same approach we use on the file level (no
595 // re-indentation if there was a structural error) within the macro
596 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000597 parseFile();
598}
599
600void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000601 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000602 nextToken();
603 } while (!eof());
604 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000605}
606
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000607// Here we blacklist certain tokens that are not usually the first token in an
608// unwrapped line. This is used in attempt to distinguish macro calls without
609// trailing semicolons from other constructs split to several lines.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700610static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000611 // Semicolon can be a null-statement, l_square can be a start of a macro or
612 // a C++11 attribute, but this doesn't seem to be common.
613 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
614 Tok.isNot(tok::l_square) &&
615 // Tokens that can only be used as binary operators and a part of
616 // overloaded operator names.
617 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
618 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
619 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
620 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
621 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
622 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
623 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
624 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
625 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
626 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
627 Tok.isNot(tok::lesslessequal) &&
628 // Colon is used in labels, base class lists, initializer lists,
629 // range-based for loops, ternary operator, but should never be the
630 // first token in an unwrapped line.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700631 Tok.isNot(tok::colon) &&
632 // 'noexcept' is a trailing annotation.
633 Tok.isNot(tok::kw_noexcept);
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000634}
635
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000636void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000637 assert(!FormatTok->Tok.is(tok::l_brace));
638 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000639 case tok::at:
640 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000641 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000642 parseBracedList();
643 break;
644 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000645 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000646 case tok::objc_public:
647 case tok::objc_protected:
648 case tok::objc_package:
649 case tok::objc_private:
650 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000651 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000652 case tok::objc_implementation:
653 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000654 case tok::objc_protocol:
655 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000656 case tok::objc_end:
657 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000658 case tok::objc_optional:
659 case tok::objc_required:
660 nextToken();
661 addUnwrappedLine();
662 return;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700663 case tok::objc_try:
664 // This branch isn't strictly necessary (the kw_try case below would
665 // do this too after the tok::at is parsed above). But be explicit.
666 parseTryCatch();
667 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000668 default:
669 break;
670 }
671 break;
Stephen Hines176edba2014-12-01 14:53:08 -0800672 case tok::kw_asm:
Stephen Hines176edba2014-12-01 14:53:08 -0800673 nextToken();
674 if (FormatTok->is(tok::l_brace)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700675 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -0800676 while (FormatTok && FormatTok->isNot(tok::eof)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800677 if (FormatTok->is(tok::r_brace)) {
678 nextToken();
679 break;
680 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700681 FormatTok->Finalized = true;
Stephen Hines176edba2014-12-01 14:53:08 -0800682 nextToken();
683 }
684 }
685 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000686 case tok::kw_namespace:
687 parseNamespace();
688 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000689 case tok::kw_inline:
690 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000691 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000692 parseNamespace();
693 return;
694 }
695 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000696 case tok::kw_public:
697 case tok::kw_protected:
698 case tok::kw_private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700699 if (Style.Language == FormatStyle::LK_Java ||
700 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -0800701 nextToken();
702 else
703 parseAccessSpecifier();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000704 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000705 case tok::kw_if:
706 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000707 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000708 case tok::kw_for:
709 case tok::kw_while:
710 parseForOrWhileLoop();
711 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000712 case tok::kw_do:
713 parseDoWhile();
714 return;
715 case tok::kw_switch:
716 parseSwitch();
717 return;
718 case tok::kw_default:
719 nextToken();
720 parseLabel();
721 return;
722 case tok::kw_case:
723 parseCaseLabel();
724 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700725 case tok::kw_try:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700726 case tok::kw___try:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700727 parseTryCatch();
728 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000729 case tok::kw_extern:
730 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000731 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000732 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000733 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000734 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000735 addUnwrappedLine();
736 return;
737 }
738 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700739 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700740 case tok::kw_export:
741 if (Style.Language == FormatStyle::LK_JavaScript) {
742 parseJavaScriptEs6ImportExport();
743 return;
744 }
745 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700746 case tok::identifier:
747 if (FormatTok->IsForEachMacro) {
748 parseForOrWhileLoop();
749 return;
750 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700751 if (Style.Language == FormatStyle::LK_JavaScript &&
752 FormatTok->is(Keywords.kw_import)) {
753 parseJavaScriptEs6ImportExport();
754 return;
755 }
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700756 if (FormatTok->is(Keywords.kw_signals)) {
757 parseAccessSpecifier();
758 return;
759 }
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000760 // In all other cases, parse the declaration.
761 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000762 default:
763 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000764 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000765 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000766 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000767 case tok::at:
768 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000769 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000770 parseBracedList();
771 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000772 case tok::kw_enum:
773 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000774 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700775 case tok::kw_typedef:
776 nextToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700777 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
778 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Stephen Hines651f13c2014-04-23 16:59:28 -0700779 parseEnum();
780 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000781 case tok::kw_struct:
782 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000783 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000784 parseRecord();
785 // A record declaration or definition is always the start of a structural
786 // element.
787 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700788 case tok::period:
789 nextToken();
790 // In Java, classes have an implicit static member "class".
791 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
792 FormatTok->is(tok::kw_class))
793 nextToken();
794 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000795 case tok::semi:
796 nextToken();
797 addUnwrappedLine();
798 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000799 case tok::r_brace:
800 addUnwrappedLine();
801 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000802 case tok::l_paren:
803 parseParens();
804 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000805 case tok::caret:
806 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700807 if (FormatTok->Tok.isAnyIdentifier() ||
808 FormatTok->isSimpleTypeSpecifier())
809 nextToken();
810 if (FormatTok->is(tok::l_paren))
811 parseParens();
812 if (FormatTok->is(tok::l_brace))
Manuel Klimek753a5112013-09-04 13:25:30 +0000813 parseChildBlock();
Manuel Klimek753a5112013-09-04 13:25:30 +0000814 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000815 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000816 if (!tryToParseBracedList()) {
817 // A block outside of parentheses must be the last part of a
818 // structural element.
819 // FIXME: Figure out cases where this is not true, and add projections
820 // for them (the one we know is missing are lambdas).
Stephen Hines651f13c2014-04-23 16:59:28 -0700821 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000822 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700823 FormatTok->Type = TT_FunctionLBrace;
Nico Weber27268772013-06-26 00:30:14 +0000824 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000825 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000826 return;
827 }
828 // Otherwise this was a braced init list, and the structural
829 // element continues.
830 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700831 case tok::kw_try:
832 // We arrive here when parsing function-try blocks.
833 parseTryCatch();
834 return;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000835 case tok::identifier: {
836 StringRef Text = FormatTok->TokenText;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700837 // Parse function literal unless 'function' is the first token in a line
838 // in which case this should be treated as a free-standing function.
839 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
840 Line->Tokens.size() > 0) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700841 tryToParseJSFunction();
842 break;
843 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000844 nextToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700845 if (Line->Tokens.size() == 1 &&
846 // JS doesn't have macros, and within classes colons indicate fields,
847 // not labels.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700848 Style.Language != FormatStyle::LK_JavaScript) {
849 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000850 parseLabel();
851 return;
852 }
Stephen Hines176edba2014-12-01 14:53:08 -0800853 // Recognize function-like macro usages without trailing semicolon as
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700854 // well as free-standing macros like Q_OBJECT.
Stephen Hines176edba2014-12-01 14:53:08 -0800855 bool FunctionLike = FormatTok->is(tok::l_paren);
856 if (FunctionLike)
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000857 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -0800858 if (FormatTok->NewlinesBefore > 0 &&
859 (Text.size() >= 5 || FunctionLike) &&
860 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000861 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000862 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000863 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000864 }
865 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000866 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000867 case tok::equal:
868 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000869 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000870 parseBracedList();
871 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000872 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000873 case tok::l_square:
Stephen Hines651f13c2014-04-23 16:59:28 -0700874 parseSquare();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000875 break;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700876 case tok::kw_new:
877 parseNew();
878 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000879 default:
880 nextToken();
881 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000882 }
883 } while (!eof());
884}
885
Stephen Hines651f13c2014-04-23 16:59:28 -0700886bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000887 // FIXME: This is a dirty way to access the previous token. Find a better
888 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000889 if (!Line->Tokens.empty() &&
Stephen Hines176edba2014-12-01 14:53:08 -0800890 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
891 tok::kw_new, tok::kw_delete) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700892 Line->Tokens.back().Tok->closesScope() ||
893 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000894 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700895 return false;
Daniel Jasper2d657052013-09-05 11:49:39 +0000896 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000897 assert(FormatTok->is(tok::l_square));
898 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000899 if (!tryToParseLambdaIntroducer())
Stephen Hines651f13c2014-04-23 16:59:28 -0700900 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000901
902 while (FormatTok->isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700903 if (FormatTok->isSimpleTypeSpecifier()) {
904 nextToken();
905 continue;
906 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000907 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000908 case tok::l_brace:
909 break;
910 case tok::l_paren:
911 parseParens();
912 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700913 case tok::amp:
914 case tok::star:
915 case tok::kw_const:
916 case tok::comma:
Stephen Hines651f13c2014-04-23 16:59:28 -0700917 case tok::less:
918 case tok::greater:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000919 case tok::identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700920 case tok::coloncolon:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000921 case tok::kw_mutable:
922 nextToken();
923 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700924 case tok::arrow:
925 FormatTok->Type = TT_TrailingReturnArrow;
926 nextToken();
927 break;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000928 default:
Stephen Hines651f13c2014-04-23 16:59:28 -0700929 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000930 }
931 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000932 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000933 parseChildBlock();
Stephen Hines651f13c2014-04-23 16:59:28 -0700934 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000935}
936
937bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
938 nextToken();
939 if (FormatTok->is(tok::equal)) {
940 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000941 if (FormatTok->is(tok::r_square)) {
942 nextToken();
943 return true;
944 }
945 if (FormatTok->isNot(tok::comma))
946 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000947 nextToken();
948 } else if (FormatTok->is(tok::amp)) {
949 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000950 if (FormatTok->is(tok::r_square)) {
951 nextToken();
952 return true;
953 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000954 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
955 return false;
956 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000957 if (FormatTok->is(tok::comma))
958 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000959 } else if (FormatTok->is(tok::r_square)) {
960 nextToken();
961 return true;
962 }
963 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000964 if (FormatTok->is(tok::amp))
965 nextToken();
966 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
967 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000968 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700969 if (FormatTok->is(tok::ellipsis))
970 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000971 if (FormatTok->is(tok::comma)) {
972 nextToken();
973 } else if (FormatTok->is(tok::r_square)) {
974 nextToken();
975 return true;
976 } else {
977 return false;
978 }
979 } while (!eof());
980 return false;
981}
982
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700983void UnwrappedLineParser::tryToParseJSFunction() {
984 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700985
986 // Consume function name.
987 if (FormatTok->is(tok::identifier))
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700988 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700989
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700990 if (FormatTok->isNot(tok::l_paren))
991 return;
992 nextToken();
993 while (FormatTok->isNot(tok::l_brace)) {
994 // Err on the side of caution in order to avoid consuming the full file in
995 // case of incomplete code.
996 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
997 tok::comment))
998 return;
999 nextToken();
1000 }
1001 parseChildBlock();
1002}
1003
Manuel Klimek80829bd2013-05-23 09:41:43 +00001004bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +00001005 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +00001006 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +00001007 assert(FormatTok->BlockKind != BK_Unknown);
1008 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +00001009 return false;
1010 parseBracedList();
1011 return true;
1012}
1013
Daniel Jasper57981202013-09-13 09:20:45 +00001014bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1015 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001016 nextToken();
1017
Manuel Klimek423dd932013-04-10 09:52:05 +00001018 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1019 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001020 do {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001021 if (Style.Language == FormatStyle::LK_JavaScript &&
Stephen Hines176edba2014-12-01 14:53:08 -08001022 FormatTok->is(Keywords.kw_function)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001023 tryToParseJSFunction();
1024 continue;
1025 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001026 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +00001027 case tok::caret:
1028 nextToken();
1029 if (FormatTok->is(tok::l_brace)) {
1030 parseChildBlock();
1031 }
1032 break;
1033 case tok::l_square:
1034 tryToParseLambda();
1035 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001036 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +00001037 // Assume there are no blocks inside a braced init list apart
1038 // from the ones we explicitly parse out (like lambdas).
1039 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001040 parseBracedList();
1041 break;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001042 case tok::r_paren:
1043 // JavaScript can just have free standing methods and getters/setters in
1044 // object literals. Detect them by a "{" following ")".
1045 if (Style.Language == FormatStyle::LK_JavaScript) {
1046 nextToken();
1047 if (FormatTok->is(tok::l_brace))
1048 parseChildBlock();
1049 break;
1050 }
1051 nextToken();
1052 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001053 case tok::r_brace:
1054 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +00001055 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +00001056 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +00001057 HasError = true;
1058 if (!ContinueOnSemicolons)
1059 return !HasError;
1060 nextToken();
1061 break;
Manuel Klimek423dd932013-04-10 09:52:05 +00001062 case tok::comma:
1063 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +00001064 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001065 default:
1066 nextToken();
1067 break;
1068 }
1069 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +00001070 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001071}
1072
Daniel Jasperbac016b2012-12-03 18:12:45 +00001073void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001074 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001075 nextToken();
1076 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001077 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001078 case tok::l_paren:
1079 parseParens();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001080 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1081 parseChildBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001082 break;
1083 case tok::r_paren:
1084 nextToken();
1085 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +00001086 case tok::r_brace:
1087 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1088 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +00001089 case tok::l_square:
1090 tryToParseLambda();
1091 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001092 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +00001093 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +00001094 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +00001095 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001096 break;
Nico Weberd74fcdb2013-02-10 20:35:35 +00001097 case tok::at:
1098 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001099 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +00001100 parseBracedList();
1101 break;
Stephen Hines176edba2014-12-01 14:53:08 -08001102 case tok::identifier:
1103 if (Style.Language == FormatStyle::LK_JavaScript &&
1104 FormatTok->is(Keywords.kw_function))
1105 tryToParseJSFunction();
1106 else
1107 nextToken();
1108 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001109 default:
1110 nextToken();
1111 break;
1112 }
1113 } while (!eof());
1114}
1115
Stephen Hines651f13c2014-04-23 16:59:28 -07001116void UnwrappedLineParser::parseSquare() {
1117 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1118 if (tryToParseLambda())
1119 return;
1120 do {
1121 switch (FormatTok->Tok.getKind()) {
1122 case tok::l_paren:
1123 parseParens();
1124 break;
1125 case tok::r_square:
1126 nextToken();
1127 return;
1128 case tok::r_brace:
1129 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1130 return;
1131 case tok::l_square:
1132 parseSquare();
1133 break;
1134 case tok::l_brace: {
1135 if (!tryToParseBracedList()) {
1136 parseChildBlock();
1137 }
1138 break;
1139 }
1140 case tok::at:
1141 nextToken();
1142 if (FormatTok->Tok.is(tok::l_brace))
1143 parseBracedList();
1144 break;
1145 default:
1146 nextToken();
1147 break;
1148 }
1149 } while (!eof());
1150}
1151
Daniel Jasperbac016b2012-12-03 18:12:45 +00001152void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001153 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001154 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001155 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +00001156 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001157 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001158 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001159 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001160 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001161 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1162 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001163 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001164 } else {
Manuel Klimeke4907052013-08-02 21:31:59 +00001165 NeedsUnwrappedLine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001166 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001167 } else {
1168 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001169 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001170 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001171 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001172 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001173 if (FormatTok->Tok.is(tok::kw_else)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001174 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1175 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001176 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001177 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001178 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001179 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001180 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001181 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001182 parseIfThenElse();
1183 } else {
1184 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001185 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001186 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001187 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001188 }
1189 } else if (NeedsUnwrappedLine) {
1190 addUnwrappedLine();
1191 }
1192}
1193
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001194void UnwrappedLineParser::parseTryCatch() {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001195 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001196 nextToken();
1197 bool NeedsUnwrappedLine = false;
1198 if (FormatTok->is(tok::colon)) {
1199 // We are in a function try block, what comes is an initializer list.
1200 nextToken();
1201 while (FormatTok->is(tok::identifier)) {
1202 nextToken();
1203 if (FormatTok->is(tok::l_paren))
1204 parseParens();
1205 else
1206 StructuralError = true;
1207 if (FormatTok->is(tok::comma))
1208 nextToken();
1209 }
1210 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001211 // Parse try with resource.
1212 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1213 parseParens();
1214 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001215 if (FormatTok->is(tok::l_brace)) {
1216 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1217 parseBlock(/*MustBeDeclaration=*/false);
1218 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1219 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1220 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1221 addUnwrappedLine();
1222 } else {
1223 NeedsUnwrappedLine = true;
1224 }
1225 } else if (!FormatTok->is(tok::kw_catch)) {
1226 // The C++ standard requires a compound-statement after a try.
1227 // If there's none, we try to assume there's a structuralElement
1228 // and try to continue.
1229 StructuralError = true;
1230 addUnwrappedLine();
1231 ++Line->Level;
1232 parseStructuralElement();
1233 --Line->Level;
1234 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001235 while (1) {
1236 if (FormatTok->is(tok::at))
1237 nextToken();
1238 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1239 tok::kw___finally) ||
1240 ((Style.Language == FormatStyle::LK_Java ||
1241 Style.Language == FormatStyle::LK_JavaScript) &&
1242 FormatTok->is(Keywords.kw_finally)) ||
1243 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1244 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1245 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001246 nextToken();
1247 while (FormatTok->isNot(tok::l_brace)) {
1248 if (FormatTok->is(tok::l_paren)) {
1249 parseParens();
1250 continue;
1251 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001252 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001253 return;
1254 nextToken();
1255 }
1256 NeedsUnwrappedLine = false;
1257 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1258 parseBlock(/*MustBeDeclaration=*/false);
1259 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1260 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1261 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1262 addUnwrappedLine();
1263 } else {
1264 NeedsUnwrappedLine = true;
1265 }
1266 }
1267 if (NeedsUnwrappedLine) {
1268 addUnwrappedLine();
1269 }
1270}
1271
Alexander Kornienko15757312012-12-06 18:03:27 +00001272void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001273 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Stephen Hines176edba2014-12-01 14:53:08 -08001274
1275 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko15757312012-12-06 18:03:27 +00001276 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001277 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +00001278 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001279 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001280 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001281 addUnwrappedLine();
1282
Daniel Jaspereff18b92013-07-31 23:16:02 +00001283 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1284 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1285 DeclarationScopeStack.size() > 1);
1286 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001287 // Munch the semicolon after a namespace. This is more common than one would
1288 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001289 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001290 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +00001291 addUnwrappedLine();
1292 }
1293 // FIXME: Add error handling.
1294}
1295
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001296void UnwrappedLineParser::parseNew() {
1297 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1298 nextToken();
1299 if (Style.Language != FormatStyle::LK_Java)
1300 return;
1301
1302 // In Java, we can parse everything up to the parens, which aren't optional.
1303 do {
1304 // There should not be a ;, { or } before the new's open paren.
1305 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1306 return;
1307
1308 // Consume the parens.
1309 if (FormatTok->is(tok::l_paren)) {
1310 parseParens();
1311
1312 // If there is a class body of an anonymous class, consume that as child.
1313 if (FormatTok->is(tok::l_brace))
1314 parseChildBlock();
1315 return;
1316 }
1317 nextToken();
1318 } while (!eof());
1319}
1320
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001321void UnwrappedLineParser::parseForOrWhileLoop() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001322 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1323 FormatTok->IsForEachMacro) &&
1324 "'for', 'while' or foreach macro expected");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001325 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001326 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001327 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001328 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001329 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001330 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001331 addUnwrappedLine();
1332 } else {
1333 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001334 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001335 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001336 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001337 }
1338}
1339
Daniel Jasperbac016b2012-12-03 18:12:45 +00001340void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001341 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001342 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001343 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001344 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001345 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001346 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1347 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001348 } else {
1349 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001350 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001351 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001352 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001353 }
1354
Alexander Kornienko393b0082012-12-04 15:40:36 +00001355 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001356 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001357 addUnwrappedLine();
1358 return;
1359 }
1360
Daniel Jasperbac016b2012-12-03 18:12:45 +00001361 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001362 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001363}
1364
1365void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001366 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001367 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001368 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001369 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001370 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001371 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001372 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001373 if (FormatTok->Tok.is(tok::kw_break)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001374 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1375 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1376 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001377 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001378 }
Manuel Klimeke4907052013-08-02 21:31:59 +00001379 parseStructuralElement();
1380 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001381 addUnwrappedLine();
1382 } else {
1383 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001384 }
Manuel Klimek526ed112013-01-09 15:25:02 +00001385 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001386}
1387
1388void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001389 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001390 // FIXME: fix handling of complex expressions here.
1391 do {
1392 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001393 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001394 parseLabel();
1395}
1396
1397void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001398 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001399 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001400 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001401 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001402 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001403 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jaspereff18b92013-07-31 23:16:02 +00001404 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001405 addUnwrappedLine();
1406 } else {
1407 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001408 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001409 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001410 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001411 }
1412}
1413
1414void UnwrappedLineParser::parseAccessSpecifier() {
1415 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001416 // Understand Qt's slots.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001417 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Stephen Hines651f13c2014-04-23 16:59:28 -07001418 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001419 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001420 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001421 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001422 addUnwrappedLine();
1423}
1424
1425void UnwrappedLineParser::parseEnum() {
Stephen Hines176edba2014-12-01 14:53:08 -08001426 // Won't be 'enum' for NS_ENUMs.
1427 if (FormatTok->Tok.is(tok::kw_enum))
Stephen Hines651f13c2014-04-23 16:59:28 -07001428 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001429
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001430 // Eat up enum class ...
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001431 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1432 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001433 while (FormatTok->Tok.getIdentifierInfo() ||
Stephen Hines176edba2014-12-01 14:53:08 -08001434 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1435 tok::greater, tok::comma, tok::question)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001436 nextToken();
1437 // We can have macros or attributes in between 'enum' and the enum name.
Stephen Hines176edba2014-12-01 14:53:08 -08001438 if (FormatTok->is(tok::l_paren))
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001439 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -08001440 if (FormatTok->is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001441 nextToken();
1442 }
Stephen Hines176edba2014-12-01 14:53:08 -08001443
1444 // Just a declaration or something is wrong.
1445 if (FormatTok->isNot(tok::l_brace))
1446 return;
1447 FormatTok->BlockKind = BK_Block;
1448
1449 if (Style.Language == FormatStyle::LK_Java) {
1450 // Java enums are different.
1451 parseJavaEnumBody();
1452 return;
Manuel Klimek308232c2013-01-21 19:17:52 +00001453 }
Stephen Hines176edba2014-12-01 14:53:08 -08001454
1455 // Parse enum body.
1456 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1457 if (HasError) {
1458 if (FormatTok->is(tok::semi))
1459 nextToken();
1460 addUnwrappedLine();
1461 }
1462
Manuel Klimek308232c2013-01-21 19:17:52 +00001463 // We fall through to parsing a structural element afterwards, so that in
1464 // enum A {} n, m;
1465 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001466}
1467
Stephen Hines176edba2014-12-01 14:53:08 -08001468void UnwrappedLineParser::parseJavaEnumBody() {
1469 // Determine whether the enum is simple, i.e. does not have a semicolon or
1470 // constants with class bodies. Simple enums can be formatted like braced
1471 // lists, contracted to a single line, etc.
1472 unsigned StoredPosition = Tokens->getPosition();
1473 bool IsSimple = true;
1474 FormatToken *Tok = Tokens->getNextToken();
1475 while (Tok) {
1476 if (Tok->is(tok::r_brace))
1477 break;
1478 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1479 IsSimple = false;
1480 break;
1481 }
1482 // FIXME: This will also mark enums with braces in the arguments to enum
1483 // constants as "not simple". This is probably fine in practice, though.
1484 Tok = Tokens->getNextToken();
1485 }
1486 FormatTok = Tokens->setPosition(StoredPosition);
1487
1488 if (IsSimple) {
1489 parseBracedList();
1490 addUnwrappedLine();
1491 return;
1492 }
1493
1494 // Parse the body of a more complex enum.
1495 // First add a line for everything up to the "{".
Manuel Klimekde768542013-01-07 18:10:23 +00001496 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001497 addUnwrappedLine();
1498 ++Line->Level;
1499
1500 // Parse the enum constants.
1501 while (FormatTok) {
1502 if (FormatTok->is(tok::l_brace)) {
1503 // Parse the constant's class body.
1504 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1505 /*MunchSemi=*/false);
1506 } else if (FormatTok->is(tok::l_paren)) {
1507 parseParens();
1508 } else if (FormatTok->is(tok::comma)) {
1509 nextToken();
1510 addUnwrappedLine();
1511 } else if (FormatTok->is(tok::semi)) {
1512 nextToken();
1513 addUnwrappedLine();
1514 break;
1515 } else if (FormatTok->is(tok::r_brace)) {
1516 addUnwrappedLine();
1517 break;
1518 } else {
1519 nextToken();
1520 }
1521 }
1522
1523 // Parse the class body after the enum's ";" if any.
1524 parseLevel(/*HasOpeningBrace=*/true);
1525 nextToken();
1526 --Line->Level;
1527 addUnwrappedLine();
1528}
1529
1530void UnwrappedLineParser::parseRecord() {
1531 const FormatToken &InitialToken = *FormatTok;
1532 nextToken();
1533 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1534 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001535 nextToken();
1536 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001537 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001538 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001539 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001540 // The actual identifier can be a nested name specifier, and in macros
1541 // it is often token-pasted.
Stephen Hines176edba2014-12-01 14:53:08 -08001542 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1543 FormatTok->is(tok::hashhash) ||
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001544 ((Style.Language == FormatStyle::LK_Java ||
1545 Style.Language == FormatStyle::LK_JavaScript) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001546 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001547 nextToken();
1548
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001549 // Note that parsing away template declarations here leads to incorrectly
1550 // accepting function declarations as record declarations.
1551 // In general, we cannot solve this problem. Consider:
1552 // class A<int> B() {}
1553 // which can be a function definition or a class definition when B() is a
1554 // macro. If we find enough real-world cases where this is a problem, we
1555 // can parse for the 'template' keyword in the beginning of the statement,
1556 // and thus rule out the record production in case there is no template
1557 // (this would still leave us with an ambiguity between template function
1558 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001559 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1560 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1561 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001562 return;
1563 nextToken();
1564 }
1565 }
1566 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001567 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001568 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001569 addUnwrappedLine();
1570
Stephen Hines651f13c2014-04-23 16:59:28 -07001571 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekaabfb272013-10-12 22:46:56 +00001572 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001573 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001574 // We fall through to parsing a structural element afterwards, so
1575 // class A {} n, m;
1576 // will end up in one unwrapped line.
Stephen Hines176edba2014-12-01 14:53:08 -08001577 // This does not apply for Java.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001578 if (Style.Language == FormatStyle::LK_Java ||
1579 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -08001580 addUnwrappedLine();
Manuel Klimekde768542013-01-07 18:10:23 +00001581}
1582
Nico Weber1abe6ea2013-01-09 21:15:03 +00001583void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001584 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001585 do
1586 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001587 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001588 nextToken(); // Skip '>'.
1589}
1590
1591void UnwrappedLineParser::parseObjCUntilAtEnd() {
1592 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001593 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001594 nextToken();
1595 addUnwrappedLine();
1596 break;
1597 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001598 if (FormatTok->is(tok::l_brace)) {
1599 parseBlock(/*MustBeDeclaration=*/false);
1600 // In ObjC interfaces, nothing should be following the "}".
1601 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001602 } else if (FormatTok->is(tok::r_brace)) {
1603 // Ignore stray "}". parseStructuralElement doesn't consume them.
1604 nextToken();
1605 addUnwrappedLine();
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001606 } else {
1607 parseStructuralElement();
1608 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001609 } while (!eof());
1610}
1611
Nico Weber50767d82013-01-09 23:25:37 +00001612void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001613 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001614 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001615
1616 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001617 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001618 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001619 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001620 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001621 // Skip category, if present.
1622 parseParens();
1623
Manuel Klimek96e888b2013-05-28 11:55:06 +00001624 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001625 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001626
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001627 if (FormatTok->Tok.is(tok::l_brace)) {
1628 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1629 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1630 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001631 parseBlock(/*MustBeDeclaration=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001632 }
Nico Weber27d13672013-01-09 20:25:35 +00001633
1634 // With instance variables, this puts '}' on its own line. Without instance
1635 // variables, this ends the @interface line.
1636 addUnwrappedLine();
1637
Nico Weber1abe6ea2013-01-09 21:15:03 +00001638 parseObjCUntilAtEnd();
1639}
Nico Weber27d13672013-01-09 20:25:35 +00001640
Nico Weber1abe6ea2013-01-09 21:15:03 +00001641void UnwrappedLineParser::parseObjCProtocol() {
1642 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001643 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001644
Manuel Klimek96e888b2013-05-28 11:55:06 +00001645 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001646 parseObjCProtocolList();
1647
1648 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001649 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001650 nextToken();
1651 return addUnwrappedLine();
1652 }
1653
1654 addUnwrappedLine();
1655 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001656}
1657
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001658void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1659 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
1660 nextToken();
1661
1662 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1663 Keywords.kw_var))
1664 return; // Fall through to parsing the corresponding structure.
1665
1666 if (FormatTok->is(tok::kw_default)) {
1667 nextToken(); // export default ..., fall through after eating 'default'.
1668 return;
1669 }
1670
1671 if (FormatTok->is(tok::l_brace)) {
1672 FormatTok->BlockKind = BK_Block;
1673 parseBracedList();
1674 }
1675
1676 while (!eof() && FormatTok->isNot(tok::semi) &&
1677 FormatTok->isNot(tok::l_brace)) {
1678 nextToken();
1679 }
1680}
1681
Daniel Jasperd98927d2013-09-05 16:05:56 +00001682LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1683 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001684 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1685 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1686 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1687 E = Line.Tokens.end();
1688 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001689 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001690 }
1691 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1692 E = Line.Tokens.end();
1693 I != E; ++I) {
1694 const UnwrappedLineNode &Node = *I;
1695 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1696 I = Node.Children.begin(),
1697 E = Node.Children.end();
1698 I != E; ++I) {
1699 printDebugInfo(*I, "\nChild: ");
1700 }
1701 }
1702 llvm::dbgs() << "\n";
1703}
1704
Daniel Jasperbac016b2012-12-03 18:12:45 +00001705void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001706 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001707 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001708 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001709 if (CurrentLines == &Lines)
1710 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001711 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001712 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001713 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001714 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001715 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001716 I = PreprocessorDirectives.begin(),
1717 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001718 I != E; ++I) {
1719 CurrentLines->push_back(*I);
1720 }
1721 PreprocessorDirectives.clear();
1722 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001723}
1724
Manuel Klimek96e888b2013-05-28 11:55:06 +00001725bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001726
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001727bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1728 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1729 FormatTok.NewlinesBefore > 0;
1730}
1731
Manuel Klimek86721d22013-01-22 16:31:55 +00001732void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1733 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001734 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001735 I = CommentsBeforeNextToken.begin(),
1736 E = CommentsBeforeNextToken.end();
1737 I != E; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001738 if (isOnNewLine(**I) && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001739 addUnwrappedLine();
1740 }
1741 pushToken(*I);
1742 }
1743 if (NewlineBeforeNext && JustComments) {
1744 addUnwrappedLine();
1745 }
1746 CommentsBeforeNextToken.clear();
1747}
1748
Daniel Jasperbac016b2012-12-03 18:12:45 +00001749void UnwrappedLineParser::nextToken() {
1750 if (eof())
1751 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001752 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001753 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001754 readToken();
1755}
1756
1757void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001758 bool CommentsInCurrentLine = true;
1759 do {
1760 FormatTok = Tokens->getNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001761 assert(FormatTok);
Manuel Klimek96e888b2013-05-28 11:55:06 +00001762 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1763 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001764 // If there is an unfinished unwrapped line, we flush the preprocessor
1765 // directives only after that unwrapped line was finished later.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001766 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimek86721d22013-01-22 16:31:55 +00001767 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001768 // Comments stored before the preprocessor directive need to be output
1769 // before the preprocessor directive, at the same level as the
1770 // preprocessor directive, as we consider them to apply to the directive.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001771 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001772 parsePPDirective();
1773 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001774 while (FormatTok->Type == TT_ConflictStart ||
1775 FormatTok->Type == TT_ConflictEnd ||
1776 FormatTok->Type == TT_ConflictAlternative) {
1777 if (FormatTok->Type == TT_ConflictStart) {
1778 conditionalCompilationStart(/*Unreachable=*/false);
1779 } else if (FormatTok->Type == TT_ConflictAlternative) {
1780 conditionalCompilationAlternative();
1781 } else if (FormatTok->Type == TT_ConflictEnd) {
1782 conditionalCompilationEnd();
1783 }
1784 FormatTok = Tokens->getNextToken();
1785 FormatTok->MustBreakBefore = true;
1786 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001787
1788 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1789 !Line->InPPDirective) {
1790 continue;
1791 }
1792
Manuel Klimek96e888b2013-05-28 11:55:06 +00001793 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001794 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001795 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001796 CommentsInCurrentLine = false;
1797 }
1798 if (CommentsInCurrentLine) {
1799 pushToken(FormatTok);
1800 } else {
1801 CommentsBeforeNextToken.push_back(FormatTok);
1802 }
1803 } while (!eof());
1804}
1805
Manuel Klimek96e888b2013-05-28 11:55:06 +00001806void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001807 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001808 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001809 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001810 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001811 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001812}
1813
Daniel Jaspercd162382013-01-07 13:26:07 +00001814} // end namespace format
1815} // end namespace clang