blob: 10ce0e9bee273c787b937107838d374f46e457e0 [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
71 ~ScopedMacroState() {
72 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
Stephen Hines6bcf27b2014-05-29 04:14:42 -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 =
459 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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700465 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000466 }
467 nextToken();
468}
469
Daniel Jasperbac016b2012-12-03 18:12:45 +0000470void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000471 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000472 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000473 nextToken();
474
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700475 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000476 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000477 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000478 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000479
Manuel Klimek96e888b2013-05-28 11:55:06 +0000480 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000481 case tok::pp_define:
482 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000483 return;
484 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000485 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000486 break;
487 case tok::pp_ifdef:
488 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000489 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000490 break;
491 case tok::pp_else:
492 parsePPElse();
493 break;
494 case tok::pp_elif:
495 parsePPElIf();
496 break;
497 case tok::pp_endif:
498 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000499 break;
500 default:
501 parsePPUnknown();
502 break;
503 }
504}
505
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700506void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
507 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000508 PPStack.push_back(PP_Unreachable);
509 else
510 PPStack.push_back(PP_Conditional);
511}
512
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700513void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000514 ++PPBranchLevel;
515 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
516 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
517 PPLevelBranchIndex.push_back(0);
518 PPLevelBranchCount.push_back(0);
519 }
520 PPChainBranchIndex.push(0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700521 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
522 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000523}
524
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700525void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000526 if (!PPStack.empty())
527 PPStack.pop_back();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000528 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
529 if (!PPChainBranchIndex.empty())
530 ++PPChainBranchIndex.top();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700531 conditionalCompilationCondition(
532 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
533 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000534}
535
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700536void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000537 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
538 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
539 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000540 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
541 }
542 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700543 // Guard against #endif's without #if.
544 if (PPBranchLevel > 0)
545 --PPBranchLevel;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000546 if (!PPChainBranchIndex.empty())
547 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000548 if (!PPStack.empty())
549 PPStack.pop_back();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700550}
551
552void UnwrappedLineParser::parsePPIf(bool IfDef) {
553 nextToken();
554 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700555 FormatTok->Tok.getLiteralData() != nullptr &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700556 StringRef(FormatTok->Tok.getLiteralData(),
557 FormatTok->Tok.getLength()) == "0") ||
558 FormatTok->Tok.is(tok::kw_false);
559 conditionalCompilationStart(!IfDef && IsLiteralFalse);
560 parsePPUnknown();
561}
562
563void UnwrappedLineParser::parsePPElse() {
564 conditionalCompilationAlternative();
565 parsePPUnknown();
566}
567
568void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
569
570void UnwrappedLineParser::parsePPEndIf() {
571 conditionalCompilationEnd();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000572 parsePPUnknown();
573}
574
Manuel Klimekd4397b92013-01-04 23:34:14 +0000575void UnwrappedLineParser::parsePPDefine() {
576 nextToken();
577
Manuel Klimek96e888b2013-05-28 11:55:06 +0000578 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000579 parsePPUnknown();
580 return;
581 }
582 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000583 if (FormatTok->Tok.getKind() == tok::l_paren &&
584 FormatTok->WhitespaceRange.getBegin() ==
585 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000586 parseParens();
587 }
588 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000589 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000590
591 // Errors during a preprocessor directive can only affect the layout of the
592 // preprocessor directive, and thus we ignore them. An alternative approach
593 // would be to use the same approach we use on the file level (no
594 // re-indentation if there was a structural error) within the macro
595 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000596 parseFile();
597}
598
599void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000600 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000601 nextToken();
602 } while (!eof());
603 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000604}
605
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000606// Here we blacklist certain tokens that are not usually the first token in an
607// unwrapped line. This is used in attempt to distinguish macro calls without
608// trailing semicolons from other constructs split to several lines.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700609static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000610 // Semicolon can be a null-statement, l_square can be a start of a macro or
611 // a C++11 attribute, but this doesn't seem to be common.
612 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
613 Tok.isNot(tok::l_square) &&
614 // Tokens that can only be used as binary operators and a part of
615 // overloaded operator names.
616 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
617 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
618 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
619 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
620 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
621 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
622 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
623 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
624 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
625 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
626 Tok.isNot(tok::lesslessequal) &&
627 // Colon is used in labels, base class lists, initializer lists,
628 // range-based for loops, ternary operator, but should never be the
629 // first token in an unwrapped line.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700630 Tok.isNot(tok::colon) &&
631 // 'noexcept' is a trailing annotation.
632 Tok.isNot(tok::kw_noexcept);
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000633}
634
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000635void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000636 assert(!FormatTok->Tok.is(tok::l_brace));
637 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000638 case tok::at:
639 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000640 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000641 parseBracedList();
642 break;
643 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000644 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000645 case tok::objc_public:
646 case tok::objc_protected:
647 case tok::objc_package:
648 case tok::objc_private:
649 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000650 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000651 case tok::objc_implementation:
652 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000653 case tok::objc_protocol:
654 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000655 case tok::objc_end:
656 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000657 case tok::objc_optional:
658 case tok::objc_required:
659 nextToken();
660 addUnwrappedLine();
661 return;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700662 case tok::objc_try:
663 // This branch isn't strictly necessary (the kw_try case below would
664 // do this too after the tok::at is parsed above). But be explicit.
665 parseTryCatch();
666 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000667 default:
668 break;
669 }
670 break;
Stephen Hines176edba2014-12-01 14:53:08 -0800671 case tok::kw_asm:
Stephen Hines176edba2014-12-01 14:53:08 -0800672 nextToken();
673 if (FormatTok->is(tok::l_brace)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700674 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -0800675 while (FormatTok && FormatTok->isNot(tok::eof)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800676 if (FormatTok->is(tok::r_brace)) {
677 nextToken();
678 break;
679 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700680 FormatTok->Finalized = true;
Stephen Hines176edba2014-12-01 14:53:08 -0800681 nextToken();
682 }
683 }
684 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000685 case tok::kw_namespace:
686 parseNamespace();
687 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000688 case tok::kw_inline:
689 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000690 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000691 parseNamespace();
692 return;
693 }
694 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000695 case tok::kw_public:
696 case tok::kw_protected:
697 case tok::kw_private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700698 if (Style.Language == FormatStyle::LK_Java ||
699 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -0800700 nextToken();
701 else
702 parseAccessSpecifier();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000703 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000704 case tok::kw_if:
705 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000706 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000707 case tok::kw_for:
708 case tok::kw_while:
709 parseForOrWhileLoop();
710 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000711 case tok::kw_do:
712 parseDoWhile();
713 return;
714 case tok::kw_switch:
715 parseSwitch();
716 return;
717 case tok::kw_default:
718 nextToken();
719 parseLabel();
720 return;
721 case tok::kw_case:
722 parseCaseLabel();
723 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700724 case tok::kw_try:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700725 case tok::kw___try:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700726 parseTryCatch();
727 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000728 case tok::kw_extern:
729 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000730 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000731 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000732 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000733 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000734 addUnwrappedLine();
735 return;
736 }
737 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700738 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700739 case tok::kw_export:
740 if (Style.Language == FormatStyle::LK_JavaScript) {
741 parseJavaScriptEs6ImportExport();
742 return;
743 }
744 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700745 case tok::identifier:
746 if (FormatTok->IsForEachMacro) {
747 parseForOrWhileLoop();
748 return;
749 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700750 if (Style.Language == FormatStyle::LK_JavaScript &&
751 FormatTok->is(Keywords.kw_import)) {
752 parseJavaScriptEs6ImportExport();
753 return;
754 }
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000755 // In all other cases, parse the declaration.
756 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000757 default:
758 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000759 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000760 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000761 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000762 case tok::at:
763 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000764 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000765 parseBracedList();
766 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000767 case tok::kw_enum:
768 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000769 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700770 case tok::kw_typedef:
771 nextToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700772 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
773 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Stephen Hines651f13c2014-04-23 16:59:28 -0700774 parseEnum();
775 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000776 case tok::kw_struct:
777 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000778 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000779 parseRecord();
780 // A record declaration or definition is always the start of a structural
781 // element.
782 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700783 case tok::period:
784 nextToken();
785 // In Java, classes have an implicit static member "class".
786 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
787 FormatTok->is(tok::kw_class))
788 nextToken();
789 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000790 case tok::semi:
791 nextToken();
792 addUnwrappedLine();
793 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000794 case tok::r_brace:
795 addUnwrappedLine();
796 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000797 case tok::l_paren:
798 parseParens();
799 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000800 case tok::caret:
801 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700802 if (FormatTok->Tok.isAnyIdentifier() ||
803 FormatTok->isSimpleTypeSpecifier())
804 nextToken();
805 if (FormatTok->is(tok::l_paren))
806 parseParens();
807 if (FormatTok->is(tok::l_brace))
Manuel Klimek753a5112013-09-04 13:25:30 +0000808 parseChildBlock();
Manuel Klimek753a5112013-09-04 13:25:30 +0000809 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000810 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000811 if (!tryToParseBracedList()) {
812 // A block outside of parentheses must be the last part of a
813 // structural element.
814 // FIXME: Figure out cases where this is not true, and add projections
815 // for them (the one we know is missing are lambdas).
Stephen Hines651f13c2014-04-23 16:59:28 -0700816 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000817 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700818 FormatTok->Type = TT_FunctionLBrace;
Nico Weber27268772013-06-26 00:30:14 +0000819 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000820 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000821 return;
822 }
823 // Otherwise this was a braced init list, and the structural
824 // element continues.
825 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700826 case tok::kw_try:
827 // We arrive here when parsing function-try blocks.
828 parseTryCatch();
829 return;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000830 case tok::identifier: {
831 StringRef Text = FormatTok->TokenText;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700832 // Parse function literal unless 'function' is the first token in a line
833 // in which case this should be treated as a free-standing function.
834 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
835 Line->Tokens.size() > 0) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700836 tryToParseJSFunction();
837 break;
838 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000839 nextToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700840 if (Line->Tokens.size() == 1 &&
841 // JS doesn't have macros, and within classes colons indicate fields,
842 // not labels.
843 (Style.Language != FormatStyle::LK_JavaScript ||
844 !Line->MustBeDeclaration)) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000845 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000846 parseLabel();
847 return;
848 }
Stephen Hines176edba2014-12-01 14:53:08 -0800849 // Recognize function-like macro usages without trailing semicolon as
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700850 // well as free-standing macros like Q_OBJECT.
Stephen Hines176edba2014-12-01 14:53:08 -0800851 bool FunctionLike = FormatTok->is(tok::l_paren);
852 if (FunctionLike)
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000853 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -0800854 if (FormatTok->NewlinesBefore > 0 &&
855 (Text.size() >= 5 || FunctionLike) &&
856 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000857 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000858 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000859 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000860 }
861 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000862 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000863 case tok::equal:
864 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000865 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000866 parseBracedList();
867 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000868 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000869 case tok::l_square:
Stephen Hines651f13c2014-04-23 16:59:28 -0700870 parseSquare();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000871 break;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700872 case tok::kw_new:
873 parseNew();
874 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000875 default:
876 nextToken();
877 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000878 }
879 } while (!eof());
880}
881
Stephen Hines651f13c2014-04-23 16:59:28 -0700882bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000883 // FIXME: This is a dirty way to access the previous token. Find a better
884 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000885 if (!Line->Tokens.empty() &&
Stephen Hines176edba2014-12-01 14:53:08 -0800886 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
887 tok::kw_new, tok::kw_delete) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700888 Line->Tokens.back().Tok->closesScope() ||
889 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000890 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700891 return false;
Daniel Jasper2d657052013-09-05 11:49:39 +0000892 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000893 assert(FormatTok->is(tok::l_square));
894 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000895 if (!tryToParseLambdaIntroducer())
Stephen Hines651f13c2014-04-23 16:59:28 -0700896 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000897
898 while (FormatTok->isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700899 if (FormatTok->isSimpleTypeSpecifier()) {
900 nextToken();
901 continue;
902 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000903 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000904 case tok::l_brace:
905 break;
906 case tok::l_paren:
907 parseParens();
908 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700909 case tok::amp:
910 case tok::star:
911 case tok::kw_const:
912 case tok::comma:
Stephen Hines651f13c2014-04-23 16:59:28 -0700913 case tok::less:
914 case tok::greater:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000915 case tok::identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700916 case tok::coloncolon:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000917 case tok::kw_mutable:
918 nextToken();
919 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700920 case tok::arrow:
921 FormatTok->Type = TT_TrailingReturnArrow;
922 nextToken();
923 break;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000924 default:
Stephen Hines651f13c2014-04-23 16:59:28 -0700925 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000926 }
927 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000928 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000929 parseChildBlock();
Stephen Hines651f13c2014-04-23 16:59:28 -0700930 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000931}
932
933bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
934 nextToken();
935 if (FormatTok->is(tok::equal)) {
936 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000937 if (FormatTok->is(tok::r_square)) {
938 nextToken();
939 return true;
940 }
941 if (FormatTok->isNot(tok::comma))
942 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000943 nextToken();
944 } else if (FormatTok->is(tok::amp)) {
945 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000946 if (FormatTok->is(tok::r_square)) {
947 nextToken();
948 return true;
949 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000950 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
951 return false;
952 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000953 if (FormatTok->is(tok::comma))
954 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000955 } else if (FormatTok->is(tok::r_square)) {
956 nextToken();
957 return true;
958 }
959 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000960 if (FormatTok->is(tok::amp))
961 nextToken();
962 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
963 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000964 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700965 if (FormatTok->is(tok::ellipsis))
966 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000967 if (FormatTok->is(tok::comma)) {
968 nextToken();
969 } else if (FormatTok->is(tok::r_square)) {
970 nextToken();
971 return true;
972 } else {
973 return false;
974 }
975 } while (!eof());
976 return false;
977}
978
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700979void UnwrappedLineParser::tryToParseJSFunction() {
980 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700981
982 // Consume function name.
983 if (FormatTok->is(tok::identifier))
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700984 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700985
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700986 if (FormatTok->isNot(tok::l_paren))
987 return;
988 nextToken();
989 while (FormatTok->isNot(tok::l_brace)) {
990 // Err on the side of caution in order to avoid consuming the full file in
991 // case of incomplete code.
992 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
993 tok::comment))
994 return;
995 nextToken();
996 }
997 parseChildBlock();
998}
999
Manuel Klimek80829bd2013-05-23 09:41:43 +00001000bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +00001001 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +00001002 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +00001003 assert(FormatTok->BlockKind != BK_Unknown);
1004 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +00001005 return false;
1006 parseBracedList();
1007 return true;
1008}
1009
Daniel Jasper57981202013-09-13 09:20:45 +00001010bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1011 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001012 nextToken();
1013
Manuel Klimek423dd932013-04-10 09:52:05 +00001014 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1015 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001016 do {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001017 if (Style.Language == FormatStyle::LK_JavaScript &&
Stephen Hines176edba2014-12-01 14:53:08 -08001018 FormatTok->is(Keywords.kw_function)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001019 tryToParseJSFunction();
1020 continue;
1021 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001022 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +00001023 case tok::caret:
1024 nextToken();
1025 if (FormatTok->is(tok::l_brace)) {
1026 parseChildBlock();
1027 }
1028 break;
1029 case tok::l_square:
1030 tryToParseLambda();
1031 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001032 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +00001033 // Assume there are no blocks inside a braced init list apart
1034 // from the ones we explicitly parse out (like lambdas).
1035 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001036 parseBracedList();
1037 break;
1038 case tok::r_brace:
1039 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +00001040 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +00001041 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +00001042 HasError = true;
1043 if (!ContinueOnSemicolons)
1044 return !HasError;
1045 nextToken();
1046 break;
Manuel Klimek423dd932013-04-10 09:52:05 +00001047 case tok::comma:
1048 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +00001049 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001050 default:
1051 nextToken();
1052 break;
1053 }
1054 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +00001055 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001056}
1057
Daniel Jasperbac016b2012-12-03 18:12:45 +00001058void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001059 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001060 nextToken();
1061 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001062 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001063 case tok::l_paren:
1064 parseParens();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001065 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1066 parseChildBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001067 break;
1068 case tok::r_paren:
1069 nextToken();
1070 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +00001071 case tok::r_brace:
1072 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1073 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +00001074 case tok::l_square:
1075 tryToParseLambda();
1076 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001077 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +00001078 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +00001079 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +00001080 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001081 break;
Nico Weberd74fcdb2013-02-10 20:35:35 +00001082 case tok::at:
1083 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001084 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +00001085 parseBracedList();
1086 break;
Stephen Hines176edba2014-12-01 14:53:08 -08001087 case tok::identifier:
1088 if (Style.Language == FormatStyle::LK_JavaScript &&
1089 FormatTok->is(Keywords.kw_function))
1090 tryToParseJSFunction();
1091 else
1092 nextToken();
1093 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001094 default:
1095 nextToken();
1096 break;
1097 }
1098 } while (!eof());
1099}
1100
Stephen Hines651f13c2014-04-23 16:59:28 -07001101void UnwrappedLineParser::parseSquare() {
1102 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1103 if (tryToParseLambda())
1104 return;
1105 do {
1106 switch (FormatTok->Tok.getKind()) {
1107 case tok::l_paren:
1108 parseParens();
1109 break;
1110 case tok::r_square:
1111 nextToken();
1112 return;
1113 case tok::r_brace:
1114 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1115 return;
1116 case tok::l_square:
1117 parseSquare();
1118 break;
1119 case tok::l_brace: {
1120 if (!tryToParseBracedList()) {
1121 parseChildBlock();
1122 }
1123 break;
1124 }
1125 case tok::at:
1126 nextToken();
1127 if (FormatTok->Tok.is(tok::l_brace))
1128 parseBracedList();
1129 break;
1130 default:
1131 nextToken();
1132 break;
1133 }
1134 } while (!eof());
1135}
1136
Daniel Jasperbac016b2012-12-03 18:12:45 +00001137void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001138 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001139 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001140 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +00001141 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001142 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001143 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001144 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001145 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001146 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1147 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001148 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001149 } else {
Manuel Klimeke4907052013-08-02 21:31:59 +00001150 NeedsUnwrappedLine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001151 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001152 } else {
1153 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001154 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001155 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001156 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001157 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001158 if (FormatTok->Tok.is(tok::kw_else)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001159 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1160 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001161 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001162 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001163 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001164 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001165 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001166 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001167 parseIfThenElse();
1168 } else {
1169 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001170 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001171 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001172 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001173 }
1174 } else if (NeedsUnwrappedLine) {
1175 addUnwrappedLine();
1176 }
1177}
1178
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001179void UnwrappedLineParser::parseTryCatch() {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001180 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001181 nextToken();
1182 bool NeedsUnwrappedLine = false;
1183 if (FormatTok->is(tok::colon)) {
1184 // We are in a function try block, what comes is an initializer list.
1185 nextToken();
1186 while (FormatTok->is(tok::identifier)) {
1187 nextToken();
1188 if (FormatTok->is(tok::l_paren))
1189 parseParens();
1190 else
1191 StructuralError = true;
1192 if (FormatTok->is(tok::comma))
1193 nextToken();
1194 }
1195 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001196 // Parse try with resource.
1197 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1198 parseParens();
1199 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001200 if (FormatTok->is(tok::l_brace)) {
1201 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1202 parseBlock(/*MustBeDeclaration=*/false);
1203 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1204 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1205 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1206 addUnwrappedLine();
1207 } else {
1208 NeedsUnwrappedLine = true;
1209 }
1210 } else if (!FormatTok->is(tok::kw_catch)) {
1211 // The C++ standard requires a compound-statement after a try.
1212 // If there's none, we try to assume there's a structuralElement
1213 // and try to continue.
1214 StructuralError = true;
1215 addUnwrappedLine();
1216 ++Line->Level;
1217 parseStructuralElement();
1218 --Line->Level;
1219 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001220 while (1) {
1221 if (FormatTok->is(tok::at))
1222 nextToken();
1223 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1224 tok::kw___finally) ||
1225 ((Style.Language == FormatStyle::LK_Java ||
1226 Style.Language == FormatStyle::LK_JavaScript) &&
1227 FormatTok->is(Keywords.kw_finally)) ||
1228 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1229 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1230 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001231 nextToken();
1232 while (FormatTok->isNot(tok::l_brace)) {
1233 if (FormatTok->is(tok::l_paren)) {
1234 parseParens();
1235 continue;
1236 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001237 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001238 return;
1239 nextToken();
1240 }
1241 NeedsUnwrappedLine = false;
1242 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1243 parseBlock(/*MustBeDeclaration=*/false);
1244 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1245 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1246 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1247 addUnwrappedLine();
1248 } else {
1249 NeedsUnwrappedLine = true;
1250 }
1251 }
1252 if (NeedsUnwrappedLine) {
1253 addUnwrappedLine();
1254 }
1255}
1256
Alexander Kornienko15757312012-12-06 18:03:27 +00001257void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001258 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Stephen Hines176edba2014-12-01 14:53:08 -08001259
1260 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko15757312012-12-06 18:03:27 +00001261 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001262 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +00001263 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001264 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001265 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001266 addUnwrappedLine();
1267
Daniel Jaspereff18b92013-07-31 23:16:02 +00001268 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1269 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1270 DeclarationScopeStack.size() > 1);
1271 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001272 // Munch the semicolon after a namespace. This is more common than one would
1273 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001274 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001275 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +00001276 addUnwrappedLine();
1277 }
1278 // FIXME: Add error handling.
1279}
1280
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001281void UnwrappedLineParser::parseNew() {
1282 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1283 nextToken();
1284 if (Style.Language != FormatStyle::LK_Java)
1285 return;
1286
1287 // In Java, we can parse everything up to the parens, which aren't optional.
1288 do {
1289 // There should not be a ;, { or } before the new's open paren.
1290 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1291 return;
1292
1293 // Consume the parens.
1294 if (FormatTok->is(tok::l_paren)) {
1295 parseParens();
1296
1297 // If there is a class body of an anonymous class, consume that as child.
1298 if (FormatTok->is(tok::l_brace))
1299 parseChildBlock();
1300 return;
1301 }
1302 nextToken();
1303 } while (!eof());
1304}
1305
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001306void UnwrappedLineParser::parseForOrWhileLoop() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001307 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1308 FormatTok->IsForEachMacro) &&
1309 "'for', 'while' or foreach macro expected");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001310 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001311 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001312 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001313 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001314 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001315 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001316 addUnwrappedLine();
1317 } else {
1318 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001319 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001320 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001321 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001322 }
1323}
1324
Daniel Jasperbac016b2012-12-03 18:12:45 +00001325void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001326 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001327 nextToken();
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);
Stephen Hines651f13c2014-04-23 16:59:28 -07001331 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1332 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001333 } else {
1334 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001335 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001336 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001337 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001338 }
1339
Alexander Kornienko393b0082012-12-04 15:40:36 +00001340 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001341 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001342 addUnwrappedLine();
1343 return;
1344 }
1345
Daniel Jasperbac016b2012-12-03 18:12:45 +00001346 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001347 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001348}
1349
1350void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001351 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001352 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001353 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001354 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001355 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001356 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001357 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001358 if (FormatTok->Tok.is(tok::kw_break)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001359 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1360 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1361 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001362 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001363 }
Manuel Klimeke4907052013-08-02 21:31:59 +00001364 parseStructuralElement();
1365 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001366 addUnwrappedLine();
1367 } else {
1368 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001369 }
Manuel Klimek526ed112013-01-09 15:25:02 +00001370 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001371}
1372
1373void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001374 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001375 // FIXME: fix handling of complex expressions here.
1376 do {
1377 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001378 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001379 parseLabel();
1380}
1381
1382void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001383 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001384 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001385 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001386 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001387 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001388 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jaspereff18b92013-07-31 23:16:02 +00001389 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001390 addUnwrappedLine();
1391 } else {
1392 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001393 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001394 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001395 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001396 }
1397}
1398
1399void UnwrappedLineParser::parseAccessSpecifier() {
1400 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001401 // Understand Qt's slots.
1402 if (FormatTok->is(tok::identifier) &&
1403 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1404 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001405 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001406 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001407 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001408 addUnwrappedLine();
1409}
1410
1411void UnwrappedLineParser::parseEnum() {
Stephen Hines176edba2014-12-01 14:53:08 -08001412 // Won't be 'enum' for NS_ENUMs.
1413 if (FormatTok->Tok.is(tok::kw_enum))
Stephen Hines651f13c2014-04-23 16:59:28 -07001414 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001415
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001416 // Eat up enum class ...
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001417 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1418 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001419 while (FormatTok->Tok.getIdentifierInfo() ||
Stephen Hines176edba2014-12-01 14:53:08 -08001420 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1421 tok::greater, tok::comma, tok::question)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001422 nextToken();
1423 // We can have macros or attributes in between 'enum' and the enum name.
Stephen Hines176edba2014-12-01 14:53:08 -08001424 if (FormatTok->is(tok::l_paren))
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001425 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -08001426 if (FormatTok->is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001427 nextToken();
1428 }
Stephen Hines176edba2014-12-01 14:53:08 -08001429
1430 // Just a declaration or something is wrong.
1431 if (FormatTok->isNot(tok::l_brace))
1432 return;
1433 FormatTok->BlockKind = BK_Block;
1434
1435 if (Style.Language == FormatStyle::LK_Java) {
1436 // Java enums are different.
1437 parseJavaEnumBody();
1438 return;
Manuel Klimek308232c2013-01-21 19:17:52 +00001439 }
Stephen Hines176edba2014-12-01 14:53:08 -08001440
1441 // Parse enum body.
1442 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1443 if (HasError) {
1444 if (FormatTok->is(tok::semi))
1445 nextToken();
1446 addUnwrappedLine();
1447 }
1448
Manuel Klimek308232c2013-01-21 19:17:52 +00001449 // We fall through to parsing a structural element afterwards, so that in
1450 // enum A {} n, m;
1451 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001452}
1453
Stephen Hines176edba2014-12-01 14:53:08 -08001454void UnwrappedLineParser::parseJavaEnumBody() {
1455 // Determine whether the enum is simple, i.e. does not have a semicolon or
1456 // constants with class bodies. Simple enums can be formatted like braced
1457 // lists, contracted to a single line, etc.
1458 unsigned StoredPosition = Tokens->getPosition();
1459 bool IsSimple = true;
1460 FormatToken *Tok = Tokens->getNextToken();
1461 while (Tok) {
1462 if (Tok->is(tok::r_brace))
1463 break;
1464 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1465 IsSimple = false;
1466 break;
1467 }
1468 // FIXME: This will also mark enums with braces in the arguments to enum
1469 // constants as "not simple". This is probably fine in practice, though.
1470 Tok = Tokens->getNextToken();
1471 }
1472 FormatTok = Tokens->setPosition(StoredPosition);
1473
1474 if (IsSimple) {
1475 parseBracedList();
1476 addUnwrappedLine();
1477 return;
1478 }
1479
1480 // Parse the body of a more complex enum.
1481 // First add a line for everything up to the "{".
Manuel Klimekde768542013-01-07 18:10:23 +00001482 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001483 addUnwrappedLine();
1484 ++Line->Level;
1485
1486 // Parse the enum constants.
1487 while (FormatTok) {
1488 if (FormatTok->is(tok::l_brace)) {
1489 // Parse the constant's class body.
1490 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1491 /*MunchSemi=*/false);
1492 } else if (FormatTok->is(tok::l_paren)) {
1493 parseParens();
1494 } else if (FormatTok->is(tok::comma)) {
1495 nextToken();
1496 addUnwrappedLine();
1497 } else if (FormatTok->is(tok::semi)) {
1498 nextToken();
1499 addUnwrappedLine();
1500 break;
1501 } else if (FormatTok->is(tok::r_brace)) {
1502 addUnwrappedLine();
1503 break;
1504 } else {
1505 nextToken();
1506 }
1507 }
1508
1509 // Parse the class body after the enum's ";" if any.
1510 parseLevel(/*HasOpeningBrace=*/true);
1511 nextToken();
1512 --Line->Level;
1513 addUnwrappedLine();
1514}
1515
1516void UnwrappedLineParser::parseRecord() {
1517 const FormatToken &InitialToken = *FormatTok;
1518 nextToken();
1519 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1520 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001521 nextToken();
1522 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001523 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001524 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001525 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001526 // The actual identifier can be a nested name specifier, and in macros
1527 // it is often token-pasted.
Stephen Hines176edba2014-12-01 14:53:08 -08001528 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1529 FormatTok->is(tok::hashhash) ||
1530 (Style.Language == FormatStyle::LK_Java &&
1531 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001532 nextToken();
1533
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001534 // Note that parsing away template declarations here leads to incorrectly
1535 // accepting function declarations as record declarations.
1536 // In general, we cannot solve this problem. Consider:
1537 // class A<int> B() {}
1538 // which can be a function definition or a class definition when B() is a
1539 // macro. If we find enough real-world cases where this is a problem, we
1540 // can parse for the 'template' keyword in the beginning of the statement,
1541 // and thus rule out the record production in case there is no template
1542 // (this would still leave us with an ambiguity between template function
1543 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001544 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1545 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1546 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001547 return;
1548 nextToken();
1549 }
1550 }
1551 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001552 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001553 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001554 addUnwrappedLine();
1555
Stephen Hines651f13c2014-04-23 16:59:28 -07001556 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekaabfb272013-10-12 22:46:56 +00001557 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001558 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001559 // We fall through to parsing a structural element afterwards, so
1560 // class A {} n, m;
1561 // will end up in one unwrapped line.
Stephen Hines176edba2014-12-01 14:53:08 -08001562 // This does not apply for Java.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001563 if (Style.Language == FormatStyle::LK_Java ||
1564 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -08001565 addUnwrappedLine();
Manuel Klimekde768542013-01-07 18:10:23 +00001566}
1567
Nico Weber1abe6ea2013-01-09 21:15:03 +00001568void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001569 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001570 do
1571 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001572 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001573 nextToken(); // Skip '>'.
1574}
1575
1576void UnwrappedLineParser::parseObjCUntilAtEnd() {
1577 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001578 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001579 nextToken();
1580 addUnwrappedLine();
1581 break;
1582 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001583 if (FormatTok->is(tok::l_brace)) {
1584 parseBlock(/*MustBeDeclaration=*/false);
1585 // In ObjC interfaces, nothing should be following the "}".
1586 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001587 } else if (FormatTok->is(tok::r_brace)) {
1588 // Ignore stray "}". parseStructuralElement doesn't consume them.
1589 nextToken();
1590 addUnwrappedLine();
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001591 } else {
1592 parseStructuralElement();
1593 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001594 } while (!eof());
1595}
1596
Nico Weber50767d82013-01-09 23:25:37 +00001597void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001598 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001599 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001600
1601 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001602 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001603 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001604 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001605 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001606 // Skip category, if present.
1607 parseParens();
1608
Manuel Klimek96e888b2013-05-28 11:55:06 +00001609 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001610 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001611
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001612 if (FormatTok->Tok.is(tok::l_brace)) {
1613 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1614 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1615 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001616 parseBlock(/*MustBeDeclaration=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001617 }
Nico Weber27d13672013-01-09 20:25:35 +00001618
1619 // With instance variables, this puts '}' on its own line. Without instance
1620 // variables, this ends the @interface line.
1621 addUnwrappedLine();
1622
Nico Weber1abe6ea2013-01-09 21:15:03 +00001623 parseObjCUntilAtEnd();
1624}
Nico Weber27d13672013-01-09 20:25:35 +00001625
Nico Weber1abe6ea2013-01-09 21:15:03 +00001626void UnwrappedLineParser::parseObjCProtocol() {
1627 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001628 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001629
Manuel Klimek96e888b2013-05-28 11:55:06 +00001630 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001631 parseObjCProtocolList();
1632
1633 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001634 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001635 nextToken();
1636 return addUnwrappedLine();
1637 }
1638
1639 addUnwrappedLine();
1640 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001641}
1642
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001643void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1644 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
1645 nextToken();
1646
1647 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1648 Keywords.kw_var))
1649 return; // Fall through to parsing the corresponding structure.
1650
1651 if (FormatTok->is(tok::kw_default)) {
1652 nextToken(); // export default ..., fall through after eating 'default'.
1653 return;
1654 }
1655
1656 if (FormatTok->is(tok::l_brace)) {
1657 FormatTok->BlockKind = BK_Block;
1658 parseBracedList();
1659 }
1660
1661 while (!eof() && FormatTok->isNot(tok::semi) &&
1662 FormatTok->isNot(tok::l_brace)) {
1663 nextToken();
1664 }
1665}
1666
Daniel Jasperd98927d2013-09-05 16:05:56 +00001667LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1668 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001669 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1670 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1671 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1672 E = Line.Tokens.end();
1673 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001674 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001675 }
1676 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1677 E = Line.Tokens.end();
1678 I != E; ++I) {
1679 const UnwrappedLineNode &Node = *I;
1680 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1681 I = Node.Children.begin(),
1682 E = Node.Children.end();
1683 I != E; ++I) {
1684 printDebugInfo(*I, "\nChild: ");
1685 }
1686 }
1687 llvm::dbgs() << "\n";
1688}
1689
Daniel Jasperbac016b2012-12-03 18:12:45 +00001690void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001691 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001692 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001693 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001694 if (CurrentLines == &Lines)
1695 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001696 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001697 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001698 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001699 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001700 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001701 I = PreprocessorDirectives.begin(),
1702 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001703 I != E; ++I) {
1704 CurrentLines->push_back(*I);
1705 }
1706 PreprocessorDirectives.clear();
1707 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001708}
1709
Manuel Klimek96e888b2013-05-28 11:55:06 +00001710bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001711
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001712bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1713 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1714 FormatTok.NewlinesBefore > 0;
1715}
1716
Manuel Klimek86721d22013-01-22 16:31:55 +00001717void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1718 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001719 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001720 I = CommentsBeforeNextToken.begin(),
1721 E = CommentsBeforeNextToken.end();
1722 I != E; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001723 if (isOnNewLine(**I) && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001724 addUnwrappedLine();
1725 }
1726 pushToken(*I);
1727 }
1728 if (NewlineBeforeNext && JustComments) {
1729 addUnwrappedLine();
1730 }
1731 CommentsBeforeNextToken.clear();
1732}
1733
Daniel Jasperbac016b2012-12-03 18:12:45 +00001734void UnwrappedLineParser::nextToken() {
1735 if (eof())
1736 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001737 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001738 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001739 readToken();
1740}
1741
1742void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001743 bool CommentsInCurrentLine = true;
1744 do {
1745 FormatTok = Tokens->getNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001746 assert(FormatTok);
Manuel Klimek96e888b2013-05-28 11:55:06 +00001747 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1748 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001749 // If there is an unfinished unwrapped line, we flush the preprocessor
1750 // directives only after that unwrapped line was finished later.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001751 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimek86721d22013-01-22 16:31:55 +00001752 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001753 // Comments stored before the preprocessor directive need to be output
1754 // before the preprocessor directive, at the same level as the
1755 // preprocessor directive, as we consider them to apply to the directive.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001756 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001757 parsePPDirective();
1758 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001759 while (FormatTok->Type == TT_ConflictStart ||
1760 FormatTok->Type == TT_ConflictEnd ||
1761 FormatTok->Type == TT_ConflictAlternative) {
1762 if (FormatTok->Type == TT_ConflictStart) {
1763 conditionalCompilationStart(/*Unreachable=*/false);
1764 } else if (FormatTok->Type == TT_ConflictAlternative) {
1765 conditionalCompilationAlternative();
1766 } else if (FormatTok->Type == TT_ConflictEnd) {
1767 conditionalCompilationEnd();
1768 }
1769 FormatTok = Tokens->getNextToken();
1770 FormatTok->MustBreakBefore = true;
1771 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001772
1773 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1774 !Line->InPPDirective) {
1775 continue;
1776 }
1777
Manuel Klimek96e888b2013-05-28 11:55:06 +00001778 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001779 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001780 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001781 CommentsInCurrentLine = false;
1782 }
1783 if (CommentsInCurrentLine) {
1784 pushToken(FormatTok);
1785 } else {
1786 CommentsBeforeNextToken.push_back(FormatTok);
1787 }
1788 } while (!eof());
1789}
1790
Manuel Klimek96e888b2013-05-28 11:55:06 +00001791void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001792 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001793 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001794 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001795 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001796 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001797}
1798
Daniel Jaspercd162382013-01-07 13:26:07 +00001799} // end namespace format
1800} // end namespace clang