/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | /// |
9 | | /// \file |
10 | | /// This file contains the implementation of the UnwrappedLineParser, |
11 | | /// which turns a stream of tokens into UnwrappedLines. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #include "UnwrappedLineParser.h" |
16 | | #include "FormatToken.h" |
17 | | #include "TokenAnnotator.h" |
18 | | #include "clang/Basic/TokenKinds.h" |
19 | | #include "llvm/ADT/STLExtras.h" |
20 | | #include "llvm/Support/Debug.h" |
21 | | #include "llvm/Support/raw_ostream.h" |
22 | | |
23 | | #include <algorithm> |
24 | | #include <utility> |
25 | | |
26 | | #define DEBUG_TYPE "format-parser" |
27 | | |
28 | | namespace clang { |
29 | | namespace format { |
30 | | |
31 | | class FormatTokenSource { |
32 | | public: |
33 | 79.0k | virtual ~FormatTokenSource() {} |
34 | | |
35 | | // Returns the next token in the token stream. |
36 | | virtual FormatToken *getNextToken() = 0; |
37 | | |
38 | | // Returns the token preceding the token returned by the last call to |
39 | | // getNextToken() in the token stream, or nullptr if no such token exists. |
40 | | virtual FormatToken *getPreviousToken() = 0; |
41 | | |
42 | | // Returns the token that would be returned by the next call to |
43 | | // getNextToken(). |
44 | | virtual FormatToken *peekNextToken() = 0; |
45 | | |
46 | | // Returns the token that would be returned after the next N calls to |
47 | | // getNextToken(). N needs to be greater than zero, and small enough that |
48 | | // there are still tokens. Check for tok::eof with N-1 before calling it with |
49 | | // N. |
50 | | virtual FormatToken *peekNextToken(int N) = 0; |
51 | | |
52 | | // Returns whether we are at the end of the file. |
53 | | // This can be different from whether getNextToken() returned an eof token |
54 | | // when the FormatTokenSource is a view on a part of the token stream. |
55 | | virtual bool isEOF() = 0; |
56 | | |
57 | | // Gets the current position in the token stream, to be used by setPosition(). |
58 | | virtual unsigned getPosition() = 0; |
59 | | |
60 | | // Resets the token stream to the state it was in when getPosition() returned |
61 | | // Position, and return the token at that position in the stream. |
62 | | virtual FormatToken *setPosition(unsigned Position) = 0; |
63 | | }; |
64 | | |
65 | | namespace { |
66 | | |
67 | | class ScopedDeclarationState { |
68 | | public: |
69 | | ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack, |
70 | | bool MustBeDeclaration) |
71 | 111k | : Line(Line), Stack(Stack) { |
72 | 111k | Line.MustBeDeclaration = MustBeDeclaration; |
73 | 111k | Stack.push_back(MustBeDeclaration); |
74 | 111k | } |
75 | 111k | ~ScopedDeclarationState() { |
76 | 111k | Stack.pop_back(); |
77 | 111k | if (!Stack.empty()) |
78 | 42.2k | Line.MustBeDeclaration = Stack.back(); |
79 | 68.8k | else |
80 | 68.8k | Line.MustBeDeclaration = true; |
81 | 111k | } |
82 | | |
83 | | private: |
84 | | UnwrappedLine &Line; |
85 | | llvm::BitVector &Stack; |
86 | | }; |
87 | | |
88 | 62.2k | static bool isLineComment(const FormatToken &FormatTok) { |
89 | 62.2k | return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*")22.4k ; |
90 | 62.2k | } |
91 | | |
92 | | // Checks if \p FormatTok is a line comment that continues the line comment |
93 | | // \p Previous. The original column of \p MinColumnToken is used to determine |
94 | | // whether \p FormatTok is indented enough to the right to continue \p Previous. |
95 | | static bool continuesLineComment(const FormatToken &FormatTok, |
96 | | const FormatToken *Previous, |
97 | 26.8k | const FormatToken *MinColumnToken) { |
98 | 26.8k | if (!Previous || !MinColumnToken26.8k ) |
99 | 21 | return false; |
100 | 26.8k | unsigned MinContinueColumn = |
101 | 26.8k | MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 03.05k : 123.7k ); |
102 | 26.8k | return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 113.5k && |
103 | 26.8k | isLineComment(*Previous)6.41k && |
104 | 26.8k | FormatTok.OriginalColumn >= MinContinueColumn2.81k ; |
105 | 26.8k | } |
106 | | |
107 | | class ScopedMacroState : public FormatTokenSource { |
108 | | public: |
109 | | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
110 | | FormatToken *&ResetToken) |
111 | | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
112 | | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
113 | 13.7k | Token(nullptr), PreviousToken(nullptr) { |
114 | 13.7k | FakeEOF.Tok.startToken(); |
115 | 13.7k | FakeEOF.Tok.setKind(tok::eof); |
116 | 13.7k | TokenSource = this; |
117 | 13.7k | Line.Level = 0; |
118 | 13.7k | Line.InPPDirective = true; |
119 | 13.7k | } |
120 | | |
121 | 13.7k | ~ScopedMacroState() override { |
122 | 13.7k | TokenSource = PreviousTokenSource; |
123 | 13.7k | ResetToken = Token; |
124 | 13.7k | Line.InPPDirective = false; |
125 | 13.7k | Line.Level = PreviousLineLevel; |
126 | 13.7k | } |
127 | | |
128 | 60.5k | FormatToken *getNextToken() override { |
129 | | // The \c UnwrappedLineParser guards against this by never calling |
130 | | // \c getNextToken() after it has encountered the first eof token. |
131 | 60.5k | assert(!eof()); |
132 | 0 | PreviousToken = Token; |
133 | 60.5k | Token = PreviousTokenSource->getNextToken(); |
134 | 60.5k | if (eof()) |
135 | 11.0k | return &FakeEOF; |
136 | 49.5k | return Token; |
137 | 60.5k | } |
138 | | |
139 | 0 | FormatToken *getPreviousToken() override { |
140 | 0 | return PreviousTokenSource->getPreviousToken(); |
141 | 0 | } |
142 | | |
143 | 398 | FormatToken *peekNextToken() override { |
144 | 398 | if (eof()) |
145 | 0 | return &FakeEOF; |
146 | 398 | return PreviousTokenSource->peekNextToken(); |
147 | 398 | } |
148 | | |
149 | 0 | FormatToken *peekNextToken(int N) override { |
150 | 0 | assert(N > 0); |
151 | 0 | if (eof()) |
152 | 0 | return &FakeEOF; |
153 | 0 | return PreviousTokenSource->peekNextToken(N); |
154 | 0 | } |
155 | | |
156 | 51 | bool isEOF() override { return PreviousTokenSource->isEOF(); } |
157 | | |
158 | 192 | unsigned getPosition() override { return PreviousTokenSource->getPosition(); } |
159 | | |
160 | 192 | FormatToken *setPosition(unsigned Position) override { |
161 | 192 | PreviousToken = nullptr; |
162 | 192 | Token = PreviousTokenSource->setPosition(Position); |
163 | 192 | return Token; |
164 | 192 | } |
165 | | |
166 | | private: |
167 | 121k | bool eof() { |
168 | 121k | return Token && Token->HasUnescapedNewline107k && |
169 | 121k | !continuesLineComment(*Token, PreviousToken, |
170 | 11.0k | /*MinColumnToken=*/PreviousToken); |
171 | 121k | } |
172 | | |
173 | | FormatToken FakeEOF; |
174 | | UnwrappedLine &Line; |
175 | | FormatTokenSource *&TokenSource; |
176 | | FormatToken *&ResetToken; |
177 | | unsigned PreviousLineLevel; |
178 | | FormatTokenSource *PreviousTokenSource; |
179 | | |
180 | | FormatToken *Token; |
181 | | FormatToken *PreviousToken; |
182 | | }; |
183 | | |
184 | | } // end anonymous namespace |
185 | | |
186 | | class ScopedLineState { |
187 | | public: |
188 | | ScopedLineState(UnwrappedLineParser &Parser, |
189 | | bool SwitchToPreprocessorLines = false) |
190 | 18.2k | : Parser(Parser), OriginalLines(Parser.CurrentLines) { |
191 | 18.2k | if (SwitchToPreprocessorLines) |
192 | 4.84k | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
193 | 13.3k | else if (!Parser.Line->Tokens.empty()) |
194 | 4.75k | Parser.CurrentLines = &Parser.Line->Tokens.back().Children; |
195 | 18.2k | PreBlockLine = std::move(Parser.Line); |
196 | 18.2k | Parser.Line = std::make_unique<UnwrappedLine>(); |
197 | 18.2k | Parser.Line->Level = PreBlockLine->Level; |
198 | 18.2k | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
199 | 18.2k | } |
200 | | |
201 | 18.2k | ~ScopedLineState() { |
202 | 18.2k | if (!Parser.Line->Tokens.empty()) |
203 | 3 | Parser.addUnwrappedLine(); |
204 | 18.2k | assert(Parser.Line->Tokens.empty()); |
205 | 0 | Parser.Line = std::move(PreBlockLine); |
206 | 18.2k | if (Parser.CurrentLines == &Parser.PreprocessorDirectives) |
207 | 4.84k | Parser.MustBreakBeforeNextToken = true; |
208 | 18.2k | Parser.CurrentLines = OriginalLines; |
209 | 18.2k | } |
210 | | |
211 | | private: |
212 | | UnwrappedLineParser &Parser; |
213 | | |
214 | | std::unique_ptr<UnwrappedLine> PreBlockLine; |
215 | | SmallVectorImpl<UnwrappedLine> *OriginalLines; |
216 | | }; |
217 | | |
218 | | class CompoundStatementIndenter { |
219 | | public: |
220 | | CompoundStatementIndenter(UnwrappedLineParser *Parser, |
221 | | const FormatStyle &Style, unsigned &LineLevel) |
222 | | : CompoundStatementIndenter(Parser, LineLevel, |
223 | | Style.BraceWrapping.AfterControlStatement, |
224 | 8.30k | Style.BraceWrapping.IndentBraces) {} |
225 | | CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel, |
226 | | bool WrapBrace, bool IndentBrace) |
227 | 8.74k | : LineLevel(LineLevel), OldLineLevel(LineLevel) { |
228 | 8.74k | if (WrapBrace) |
229 | 1.26k | Parser->addUnwrappedLine(); |
230 | 8.74k | if (IndentBrace) |
231 | 108 | ++LineLevel; |
232 | 8.74k | } |
233 | 8.74k | ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } |
234 | | |
235 | | private: |
236 | | unsigned &LineLevel; |
237 | | unsigned OldLineLevel; |
238 | | }; |
239 | | |
240 | | namespace { |
241 | | |
242 | | class IndexedTokenSource : public FormatTokenSource { |
243 | | public: |
244 | | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
245 | 65.2k | : Tokens(Tokens), Position(-1) {} |
246 | | |
247 | 1.27M | FormatToken *getNextToken() override { |
248 | 1.27M | if (Position >= 0 && Tokens[Position]->is(tok::eof)1.21M ) { |
249 | 0 | LLVM_DEBUG({ |
250 | 0 | llvm::dbgs() << "Next "; |
251 | 0 | dbgToken(Position); |
252 | 0 | }); |
253 | 0 | return Tokens[Position]; |
254 | 0 | } |
255 | 1.27M | ++Position; |
256 | 1.27M | LLVM_DEBUG({ |
257 | 1.27M | llvm::dbgs() << "Next "; |
258 | 1.27M | dbgToken(Position); |
259 | 1.27M | }); |
260 | 1.27M | return Tokens[Position]; |
261 | 1.27M | } |
262 | | |
263 | 4.54k | FormatToken *getPreviousToken() override { |
264 | 4.54k | return Position > 0 ? Tokens[Position - 1]3.75k : nullptr792 ; |
265 | 4.54k | } |
266 | | |
267 | 70.1k | FormatToken *peekNextToken() override { |
268 | 70.1k | int Next = Position + 1; |
269 | 70.1k | LLVM_DEBUG({ |
270 | 70.1k | llvm::dbgs() << "Peeking "; |
271 | 70.1k | dbgToken(Next); |
272 | 70.1k | }); |
273 | 70.1k | return Tokens[Next]; |
274 | 70.1k | } |
275 | | |
276 | 26 | FormatToken *peekNextToken(int N) override { |
277 | 26 | assert(N > 0); |
278 | 0 | int Next = Position + N; |
279 | 26 | LLVM_DEBUG({ |
280 | 26 | llvm::dbgs() << "Peeking (+" << (N - 1) << ") "; |
281 | 26 | dbgToken(Next); |
282 | 26 | }); |
283 | 26 | return Tokens[Next]; |
284 | 26 | } |
285 | | |
286 | 51 | bool isEOF() override { return Tokens[Position]->is(tok::eof); } |
287 | | |
288 | 21.2k | unsigned getPosition() override { |
289 | 21.2k | LLVM_DEBUG(llvm::dbgs() << "Getting Position: " << Position << "\n"); |
290 | 21.2k | assert(Position >= 0); |
291 | 0 | return Position; |
292 | 21.2k | } |
293 | | |
294 | 21.2k | FormatToken *setPosition(unsigned P) override { |
295 | 21.2k | LLVM_DEBUG(llvm::dbgs() << "Setting Position: " << P << "\n"); |
296 | 21.2k | Position = P; |
297 | 21.2k | return Tokens[Position]; |
298 | 21.2k | } |
299 | | |
300 | 65.9k | void reset() { Position = -1; } |
301 | | |
302 | | private: |
303 | 0 | void dbgToken(int Position, llvm::StringRef Indent = "") { |
304 | 0 | FormatToken *Tok = Tokens[Position]; |
305 | 0 | llvm::dbgs() << Indent << "[" << Position |
306 | 0 | << "] Token: " << Tok->Tok.getName() << " / " << Tok->TokenText |
307 | 0 | << ", Macro: " << !!Tok->MacroCtx << "\n"; |
308 | 0 | } |
309 | | |
310 | | ArrayRef<FormatToken *> Tokens; |
311 | | int Position; |
312 | | }; |
313 | | |
314 | | } // end anonymous namespace |
315 | | |
316 | | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
317 | | const AdditionalKeywords &Keywords, |
318 | | unsigned FirstStartColumn, |
319 | | ArrayRef<FormatToken *> Tokens, |
320 | | UnwrappedLineConsumer &Callback) |
321 | | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
322 | | CurrentLines(&Lines), Style(Style), Keywords(Keywords), |
323 | | CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), |
324 | | Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), |
325 | | IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None |
326 | | ? IG_Rejected |
327 | | : IG_Inited), |
328 | 65.2k | IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {} |
329 | | |
330 | 65.9k | void UnwrappedLineParser::reset() { |
331 | 65.9k | PPBranchLevel = -1; |
332 | 65.9k | IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None |
333 | 65.9k | ? IG_Rejected65.7k |
334 | 65.9k | : IG_Inited273 ; |
335 | 65.9k | IncludeGuardToken = nullptr; |
336 | 65.9k | Line.reset(new UnwrappedLine); |
337 | 65.9k | CommentsBeforeNextToken.clear(); |
338 | 65.9k | FormatTok = nullptr; |
339 | 65.9k | MustBreakBeforeNextToken = false; |
340 | 65.9k | PreprocessorDirectives.clear(); |
341 | 65.9k | CurrentLines = &Lines; |
342 | 65.9k | DeclarationScopeStack.clear(); |
343 | 65.9k | NestedTooDeep.clear(); |
344 | 65.9k | PPStack.clear(); |
345 | 65.9k | Line->FirstStartColumn = FirstStartColumn; |
346 | 65.9k | } |
347 | | |
348 | 65.2k | void UnwrappedLineParser::parse() { |
349 | 65.2k | IndexedTokenSource TokenSource(AllTokens); |
350 | 65.2k | Line->FirstStartColumn = FirstStartColumn; |
351 | 65.9k | do { |
352 | 65.9k | LLVM_DEBUG(llvm::dbgs() << "----\n"); |
353 | 65.9k | reset(); |
354 | 65.9k | Tokens = &TokenSource; |
355 | 65.9k | TokenSource.reset(); |
356 | | |
357 | 65.9k | readToken(); |
358 | 65.9k | parseFile(); |
359 | | |
360 | | // If we found an include guard then all preprocessor directives (other than |
361 | | // the guard) are over-indented by one. |
362 | 65.9k | if (IncludeGuard == IG_Found) { |
363 | 42 | for (auto &Line : Lines) |
364 | 219 | if (Line.InPPDirective && Line.Level > 0162 ) |
365 | 78 | --Line.Level; |
366 | 42 | } |
367 | | |
368 | | // Create line with eof token. |
369 | 65.9k | pushToken(FormatTok); |
370 | 65.9k | addUnwrappedLine(); |
371 | | |
372 | 65.9k | for (const UnwrappedLine &Line : Lines) |
373 | 247k | Callback.consumeUnwrappedLine(Line); |
374 | | |
375 | 65.9k | Callback.finishRun(); |
376 | 65.9k | Lines.clear(); |
377 | 67.3k | while (!PPLevelBranchIndex.empty() && |
378 | 67.3k | PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()2.11k ) { |
379 | 1.39k | PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); |
380 | 1.39k | PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); |
381 | 1.39k | } |
382 | 65.9k | if (!PPLevelBranchIndex.empty()) { |
383 | 719 | ++PPLevelBranchIndex.back(); |
384 | 719 | assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); |
385 | 0 | assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); |
386 | 719 | } |
387 | 65.9k | } while (!PPLevelBranchIndex.empty()); |
388 | 65.2k | } |
389 | | |
390 | 69.2k | void UnwrappedLineParser::parseFile() { |
391 | | // The top-level context in a file always has declarations, except for pre- |
392 | | // processor directives and JavaScript files. |
393 | 69.2k | bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript()65.9k ; |
394 | 69.2k | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
395 | 69.2k | MustBeDeclaration); |
396 | 69.2k | if (Style.Language == FormatStyle::LK_TextProto) |
397 | 607 | parseBracedList(); |
398 | 68.6k | else |
399 | 68.6k | parseLevel(); |
400 | | // Make sure to format the remaining tokens. |
401 | | // |
402 | | // LK_TextProto is special since its top-level is parsed as the body of a |
403 | | // braced list, which does not necessarily have natural line separators such |
404 | | // as a semicolon. Comments after the last entry that have been determined to |
405 | | // not belong to that line, as in: |
406 | | // key: value |
407 | | // // endfile comment |
408 | | // do not have a chance to be put on a line of their own until this point. |
409 | | // Here we add this newline before end-of-file comments. |
410 | 69.2k | if (Style.Language == FormatStyle::LK_TextProto && |
411 | 69.2k | !CommentsBeforeNextToken.empty()607 ) { |
412 | 15 | addUnwrappedLine(); |
413 | 15 | } |
414 | 69.2k | flushComments(true); |
415 | 69.2k | addUnwrappedLine(); |
416 | 69.2k | } |
417 | | |
418 | 14 | void UnwrappedLineParser::parseCSharpGenericTypeConstraint() { |
419 | 140 | do { |
420 | 140 | switch (FormatTok->Tok.getKind()) { |
421 | 14 | case tok::l_brace: |
422 | 14 | return; |
423 | 126 | default: |
424 | 126 | if (FormatTok->is(Keywords.kw_where)) { |
425 | 4 | addUnwrappedLine(); |
426 | 4 | nextToken(); |
427 | 4 | parseCSharpGenericTypeConstraint(); |
428 | 4 | break; |
429 | 4 | } |
430 | 122 | nextToken(); |
431 | 122 | break; |
432 | 140 | } |
433 | 140 | } while (!eof()126 ); |
434 | 14 | } |
435 | | |
436 | 76 | void UnwrappedLineParser::parseCSharpAttribute() { |
437 | 76 | int UnpairedSquareBrackets = 1; |
438 | 322 | do { |
439 | 322 | switch (FormatTok->Tok.getKind()) { |
440 | 78 | case tok::r_square: |
441 | 78 | nextToken(); |
442 | 78 | --UnpairedSquareBrackets; |
443 | 78 | if (UnpairedSquareBrackets == 0) { |
444 | 76 | addUnwrappedLine(); |
445 | 76 | return; |
446 | 76 | } |
447 | 2 | break; |
448 | 2 | case tok::l_square: |
449 | 2 | ++UnpairedSquareBrackets; |
450 | 2 | nextToken(); |
451 | 2 | break; |
452 | 242 | default: |
453 | 242 | nextToken(); |
454 | 242 | break; |
455 | 322 | } |
456 | 322 | } while (!eof()246 ); |
457 | 76 | } |
458 | | |
459 | 2.81k | bool UnwrappedLineParser::precededByCommentOrPPDirective() const { |
460 | 2.81k | if (!Lines.empty() && Lines.back().InPPDirective1.92k ) |
461 | 12 | return true; |
462 | | |
463 | 2.80k | const FormatToken *Previous = Tokens->getPreviousToken(); |
464 | 2.80k | return Previous && Previous->is(tok::comment)2.00k && |
465 | 2.80k | (164 Previous->IsMultiline164 || Previous->NewlinesBefore > 0164 ); |
466 | 2.81k | } |
467 | | |
468 | | /// \brief Parses a level, that is ???. |
469 | | /// \param OpeningBrace Opening brace (\p nullptr if absent) of that level |
470 | | /// \param CanContainBracedList If the content can contain (at any level) a |
471 | | /// braced list. |
472 | | /// \param NextLBracesType The type for left brace found in this level. |
473 | | /// \param IfKind The \p if statement kind in the level. |
474 | | /// \param IfLeftBrace The left brace of the \p if block in the level. |
475 | | /// \returns true if a simple block of if/else/for/while, or false otherwise. |
476 | | /// (A simple block has a single statement.) |
477 | | bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace, |
478 | | bool CanContainBracedList, |
479 | | TokenType NextLBracesType, |
480 | | IfStmtKind *IfKind, |
481 | 110k | FormatToken **IfLeftBrace) { |
482 | 110k | auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace |
483 | 110k | ? TT_BracedListLBrace945 |
484 | 110k | : TT_Unknown109k ; |
485 | 110k | const bool IsPrecededByCommentOrPPDirective = |
486 | 110k | !Style.RemoveBracesLLVM || precededByCommentOrPPDirective()2.00k ; |
487 | 110k | FormatToken *IfLBrace = nullptr; |
488 | 110k | bool HasDoWhile = false; |
489 | 110k | bool HasLabel = false; |
490 | 110k | unsigned StatementCount = 0; |
491 | 110k | bool SwitchLabelEncountered = false; |
492 | | |
493 | 166k | do { |
494 | 166k | if (FormatTok->getType() == TT_AttributeMacro) { |
495 | 36 | nextToken(); |
496 | 36 | continue; |
497 | 36 | } |
498 | 166k | tok::TokenKind kind = FormatTok->Tok.getKind(); |
499 | 166k | if (FormatTok->getType() == TT_MacroBlockBegin) |
500 | 36 | kind = tok::l_brace; |
501 | 166k | else if (FormatTok->getType() == TT_MacroBlockEnd) |
502 | 36 | kind = tok::r_brace; |
503 | | |
504 | 166k | auto ParseDefault = [this, OpeningBrace, NextLevelLBracesType, IfKind, |
505 | 166k | &IfLBrace, &HasDoWhile, &HasLabel, &StatementCount] { |
506 | 122k | parseStructuralElement(!OpeningBrace, NextLevelLBracesType, IfKind, |
507 | 122k | &IfLBrace, HasDoWhile ? nullptr18 : &HasDoWhile122k , |
508 | 122k | HasLabel ? nullptr0 : &HasLabel); |
509 | 122k | ++StatementCount; |
510 | 122k | assert(StatementCount > 0 && "StatementCount overflow!"); |
511 | 122k | }; |
512 | | |
513 | 166k | switch (kind) { |
514 | 0 | case tok::comment: |
515 | 0 | nextToken(); |
516 | 0 | addUnwrappedLine(); |
517 | 0 | break; |
518 | 1.40k | case tok::l_brace: |
519 | 1.40k | if (NextLBracesType != TT_Unknown) { |
520 | 344 | FormatTok->setFinalizedType(NextLBracesType); |
521 | 1.06k | } else if (FormatTok->Previous && |
522 | 1.06k | FormatTok->Previous->ClosesRequiresClause258 ) { |
523 | | // We need the 'default' case here to correctly parse a function |
524 | | // l_brace. |
525 | 210 | ParseDefault(); |
526 | 210 | continue; |
527 | 210 | } |
528 | 1.19k | if (CanContainBracedList && !FormatTok->is(TT_MacroBlockBegin)851 && |
529 | 1.19k | tryToParseBracedList()815 ) { |
530 | 9 | continue; |
531 | 9 | } |
532 | 1.18k | parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, |
533 | 1.18k | /*MunchSemi=*/true, /*KeepBraces=*/true, /*IfKind=*/nullptr, |
534 | 1.18k | /*UnindentWhitesmithsBraces=*/false, CanContainBracedList, |
535 | 1.18k | NextLBracesType); |
536 | 1.18k | ++StatementCount; |
537 | 1.18k | assert(StatementCount > 0 && "StatementCount overflow!"); |
538 | 0 | addUnwrappedLine(); |
539 | 1.18k | break; |
540 | 41.7k | case tok::r_brace: |
541 | 41.7k | if (OpeningBrace) { |
542 | 41.6k | if (!Style.RemoveBracesLLVM || |
543 | 41.6k | !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)1.19k ) { |
544 | 40.5k | return false; |
545 | 40.5k | } |
546 | 1.07k | if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel908 || |
547 | 1.07k | HasDoWhile884 || IsPrecededByCommentOrPPDirective872 || |
548 | 1.07k | precededByCommentOrPPDirective()812 ) { |
549 | 276 | return false; |
550 | 276 | } |
551 | 800 | const FormatToken *Next = Tokens->peekNextToken(); |
552 | 800 | if (Next->is(tok::comment) && Next->NewlinesBefore == 028 ) |
553 | 12 | return false; |
554 | 788 | if (IfLeftBrace) |
555 | 788 | *IfLeftBrace = IfLBrace; |
556 | 788 | return true; |
557 | 800 | } |
558 | 91 | nextToken(); |
559 | 91 | addUnwrappedLine(); |
560 | 91 | break; |
561 | 189 | case tok::kw_default: { |
562 | 189 | unsigned StoredPosition = Tokens->getPosition(); |
563 | 189 | FormatToken *Next; |
564 | 192 | do { |
565 | 192 | Next = Tokens->getNextToken(); |
566 | 192 | assert(Next); |
567 | 192 | } while (Next->is(tok::comment)); |
568 | 0 | FormatTok = Tokens->setPosition(StoredPosition); |
569 | 189 | if (Next->isNot(tok::colon)) { |
570 | | // default not followed by ':' is not a case label; treat it like |
571 | | // an identifier. |
572 | 2 | parseStructuralElement(); |
573 | 2 | break; |
574 | 2 | } |
575 | | // Else, if it is 'default:', fall through to the case handling. |
576 | 189 | LLVM_FALLTHROUGH187 ; |
577 | 187 | } |
578 | 1.00k | case tok::kw_case: |
579 | 1.00k | if (Style.isJavaScript() && Line->MustBeDeclaration12 ) { |
580 | | // A 'case: string' style field declaration. |
581 | 8 | parseStructuralElement(); |
582 | 8 | break; |
583 | 8 | } |
584 | 1.00k | if (!SwitchLabelEncountered && |
585 | 1.00k | (688 Style.IndentCaseLabels688 || |
586 | 688 | (579 Line->InPPDirective579 && Line->Level == 127 ))) { |
587 | 127 | ++Line->Level; |
588 | 127 | } |
589 | 1.00k | SwitchLabelEncountered = true; |
590 | 1.00k | parseStructuralElement(); |
591 | 1.00k | break; |
592 | 1.74k | case tok::l_square: |
593 | 1.74k | if (Style.isCSharp()) { |
594 | 76 | nextToken(); |
595 | 76 | parseCSharpAttribute(); |
596 | 76 | break; |
597 | 76 | } |
598 | 1.67k | if (handleCppAttributes()) |
599 | 133 | break; |
600 | 1.67k | LLVM_FALLTHROUGH1.53k ;1.53k |
601 | 122k | default: |
602 | 122k | ParseDefault(); |
603 | 122k | break; |
604 | 166k | } |
605 | 166k | } while (!eof()125k ); |
606 | | |
607 | 68.8k | return false; |
608 | 110k | } |
609 | | |
610 | 19.0k | void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { |
611 | | // We'll parse forward through the tokens until we hit |
612 | | // a closing brace or eof - note that getNextToken() will |
613 | | // parse macros, so this will magically work inside macro |
614 | | // definitions, too. |
615 | 19.0k | unsigned StoredPosition = Tokens->getPosition(); |
616 | 19.0k | FormatToken *Tok = FormatTok; |
617 | 19.0k | const FormatToken *PrevTok = Tok->Previous; |
618 | | // Keep a stack of positions of lbrace tokens. We will |
619 | | // update information about whether an lbrace starts a |
620 | | // braced init list or a different block during the loop. |
621 | 19.0k | SmallVector<FormatToken *, 8> LBraceStack; |
622 | 19.0k | assert(Tok->is(tok::l_brace)); |
623 | 151k | do { |
624 | | // Get next non-comment token. |
625 | 151k | FormatToken *NextTok; |
626 | 153k | do { |
627 | 153k | NextTok = Tokens->getNextToken(); |
628 | 153k | } while (NextTok->is(tok::comment)); |
629 | | |
630 | 151k | switch (Tok->Tok.getKind()) { |
631 | 22.5k | case tok::l_brace: |
632 | 22.5k | if (Style.isJavaScript() && PrevTok802 ) { |
633 | 792 | if (PrevTok->isOneOf(tok::colon, tok::less)) { |
634 | | // A ':' indicates this code is in a type, or a braced list |
635 | | // following a label in an object literal ({a: {b: 1}}). |
636 | | // A '<' could be an object used in a comparison, but that is nonsense |
637 | | // code (can never return true), so more likely it is a generic type |
638 | | // argument (`X<{a: string; b: number}>`). |
639 | | // The code below could be confused by semicolons between the |
640 | | // individual members in a type member list, which would normally |
641 | | // trigger BK_Block. In both cases, this must be parsed as an inline |
642 | | // braced init. |
643 | 94 | Tok->setBlockKind(BK_BracedInit); |
644 | 698 | } else if (PrevTok->is(tok::r_paren)) { |
645 | | // `) { }` can only occur in function or method declarations in JS. |
646 | 374 | Tok->setBlockKind(BK_Block); |
647 | 374 | } |
648 | 21.8k | } else { |
649 | 21.8k | Tok->setBlockKind(BK_Unknown); |
650 | 21.8k | } |
651 | 22.5k | LBraceStack.push_back(Tok); |
652 | 22.5k | break; |
653 | 22.3k | case tok::r_brace: |
654 | 22.3k | if (LBraceStack.empty()) |
655 | 0 | break; |
656 | 22.3k | if (LBraceStack.back()->is(BK_Unknown)) { |
657 | 11.4k | bool ProbablyBracedList = false; |
658 | 11.4k | if (Style.Language == FormatStyle::LK_Proto) { |
659 | 36 | ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); |
660 | 11.4k | } else { |
661 | | // Skip NextTok over preprocessor lines, otherwise we may not |
662 | | // properly diagnose the block as a braced intializer |
663 | | // if the comma separator appears after the pp directive. |
664 | 11.7k | while (NextTok->is(tok::hash)) { |
665 | 310 | ScopedMacroState MacroState(*Line, Tokens, NextTok); |
666 | 694 | do { |
667 | 694 | NextTok = Tokens->getNextToken(); |
668 | 694 | } while (NextTok->isNot(tok::eof)); |
669 | 310 | } |
670 | | |
671 | | // Using OriginalColumn to distinguish between ObjC methods and |
672 | | // binary operators is a bit hacky. |
673 | 11.4k | bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && |
674 | 11.4k | NextTok->OriginalColumn == 018 ; |
675 | | |
676 | | // Try to detect a braced list. Note that regardless how we mark inner |
677 | | // braces here, we will overwrite the BlockKind later if we parse a |
678 | | // braced list (where all blocks inside are by default braced lists), |
679 | | // or when we explicitly detect blocks (for example while parsing |
680 | | // lambdas). |
681 | | |
682 | | // If we already marked the opening brace as braced list, the closing |
683 | | // must also be part of it. |
684 | 11.4k | ProbablyBracedList = LBraceStack.back()->is(TT_BracedListLBrace); |
685 | | |
686 | 11.4k | ProbablyBracedList = ProbablyBracedList || |
687 | 11.4k | (11.4k Style.isJavaScript()11.4k && |
688 | 11.4k | NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, |
689 | 258 | Keywords.kw_as)); |
690 | 11.4k | ProbablyBracedList = ProbablyBracedList || |
691 | 11.4k | (11.3k Style.isCpp()11.3k && NextTok->is(tok::l_paren)10.8k ); |
692 | | |
693 | | // If there is a comma, semicolon or right paren after the closing |
694 | | // brace, we assume this is a braced initializer list. |
695 | | // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a |
696 | | // braced list in JS. |
697 | 11.4k | ProbablyBracedList = |
698 | 11.4k | ProbablyBracedList || |
699 | 11.4k | NextTok->isOneOf(tok::comma, tok::period, tok::colon, |
700 | 11.3k | tok::r_paren, tok::r_square, tok::l_brace, |
701 | 11.3k | tok::ellipsis); |
702 | | |
703 | 11.4k | ProbablyBracedList = |
704 | 11.4k | ProbablyBracedList || |
705 | 11.4k | (9.47k NextTok->is(tok::identifier)9.47k && |
706 | 9.47k | !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)242 ); |
707 | | |
708 | 11.4k | ProbablyBracedList = ProbablyBracedList || |
709 | 11.4k | (9.46k NextTok->is(tok::semi)9.46k && |
710 | 9.46k | (2.42k !ExpectClassBody2.42k || LBraceStack.size() != 1735 )); |
711 | | |
712 | 11.4k | ProbablyBracedList = |
713 | 11.4k | ProbablyBracedList || |
714 | 11.4k | (7.74k NextTok->isBinaryOperator()7.74k && !NextIsObjCMethod111 ); |
715 | | |
716 | 11.4k | if (!Style.isCSharp() && NextTok->is(tok::l_square)11.2k ) { |
717 | | // We can have an array subscript after a braced init |
718 | | // list, but C++11 attributes are expected after blocks. |
719 | 18 | NextTok = Tokens->getNextToken(); |
720 | 18 | ProbablyBracedList = NextTok->isNot(tok::l_square); |
721 | 18 | } |
722 | 11.4k | } |
723 | 11.4k | if (ProbablyBracedList) { |
724 | 3.82k | Tok->setBlockKind(BK_BracedInit); |
725 | 3.82k | LBraceStack.back()->setBlockKind(BK_BracedInit); |
726 | 7.63k | } else { |
727 | 7.63k | Tok->setBlockKind(BK_Block); |
728 | 7.63k | LBraceStack.back()->setBlockKind(BK_Block); |
729 | 7.63k | } |
730 | 11.4k | } |
731 | 22.3k | LBraceStack.pop_back(); |
732 | 22.3k | break; |
733 | 30.4k | case tok::identifier: |
734 | 30.4k | if (!Tok->is(TT_StatementMacro)) |
735 | 30.4k | break; |
736 | 30.4k | LLVM_FALLTHROUGH18 ;18 |
737 | 205 | case tok::at: |
738 | 13.6k | case tok::semi: |
739 | 14.5k | case tok::kw_if: |
740 | 14.7k | case tok::kw_while: |
741 | 14.9k | case tok::kw_for: |
742 | 15.0k | case tok::kw_switch: |
743 | 15.0k | case tok::kw_try: |
744 | 15.0k | case tok::kw___try: |
745 | 15.0k | if (!LBraceStack.empty() && LBraceStack.back()->is(BK_Unknown)) |
746 | 10.4k | LBraceStack.back()->setBlockKind(BK_Block); |
747 | 15.0k | break; |
748 | 60.7k | default: |
749 | 60.7k | break; |
750 | 151k | } |
751 | 151k | PrevTok = Tok; |
752 | 151k | Tok = NextTok; |
753 | 151k | } while (Tok->isNot(tok::eof) && !LBraceStack.empty()141k ); |
754 | | |
755 | | // Assume other blocks for all unclosed opening braces. |
756 | 19.0k | for (FormatToken *LBrace : LBraceStack) |
757 | 247 | if (LBrace->is(BK_Unknown)) |
758 | 202 | LBrace->setBlockKind(BK_Block); |
759 | | |
760 | 19.0k | FormatTok = Tokens->setPosition(StoredPosition); |
761 | 19.0k | } |
762 | | |
763 | | template <class T> |
764 | 1.39k | static inline void hash_combine(std::size_t &seed, const T &v) { |
765 | 1.39k | std::hash<T> hasher; |
766 | 1.39k | seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
767 | 1.39k | } |
768 | | |
769 | 74.0k | size_t UnwrappedLineParser::computePPHash() const { |
770 | 74.0k | size_t h = 0; |
771 | 74.0k | for (const auto &i : PPStack) { |
772 | 698 | hash_combine(h, size_t(i.Kind)); |
773 | 698 | hash_combine(h, i.Line); |
774 | 698 | } |
775 | 74.0k | return h; |
776 | 74.0k | } |
777 | | |
778 | | // Checks whether \p ParsedLine might fit on a single line. If \p OpeningBrace |
779 | | // is not null, subtracts its length (plus the preceding space) when computing |
780 | | // the length of \p ParsedLine. We must clone the tokens of \p ParsedLine before |
781 | | // running the token annotator on it so that we can restore them afterward. |
782 | | bool UnwrappedLineParser::mightFitOnOneLine( |
783 | 766 | UnwrappedLine &ParsedLine, const FormatToken *OpeningBrace) const { |
784 | 766 | const auto ColumnLimit = Style.ColumnLimit; |
785 | 766 | if (ColumnLimit == 0) |
786 | 4 | return true; |
787 | | |
788 | 762 | auto &Tokens = ParsedLine.Tokens; |
789 | 762 | assert(!Tokens.empty()); |
790 | | |
791 | 0 | const auto *LastToken = Tokens.back().Tok; |
792 | 762 | assert(LastToken); |
793 | | |
794 | 0 | SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size()); |
795 | | |
796 | 762 | int Index = 0; |
797 | 4.40k | for (const auto &Token : Tokens) { |
798 | 4.40k | assert(Token.Tok); |
799 | 0 | auto &SavedToken = SavedTokens[Index++]; |
800 | 4.40k | SavedToken.Tok = new FormatToken; |
801 | 4.40k | SavedToken.Tok->copyFrom(*Token.Tok); |
802 | 4.40k | SavedToken.Children = std::move(Token.Children); |
803 | 4.40k | } |
804 | | |
805 | 762 | AnnotatedLine Line(ParsedLine); |
806 | 762 | assert(Line.Last == LastToken); |
807 | | |
808 | 0 | TokenAnnotator Annotator(Style, Keywords); |
809 | 762 | Annotator.annotate(Line); |
810 | 762 | Annotator.calculateFormattingInformation(Line); |
811 | | |
812 | 762 | auto Length = LastToken->TotalLength; |
813 | 762 | if (OpeningBrace) { |
814 | 328 | assert(OpeningBrace != Tokens.front().Tok); |
815 | 0 | Length -= OpeningBrace->TokenText.size() + 1; |
816 | 328 | } |
817 | | |
818 | 0 | Index = 0; |
819 | 4.40k | for (auto &Token : Tokens) { |
820 | 4.40k | const auto &SavedToken = SavedTokens[Index++]; |
821 | 4.40k | Token.Tok->copyFrom(*SavedToken.Tok); |
822 | 4.40k | Token.Children = std::move(SavedToken.Children); |
823 | 4.40k | delete SavedToken.Tok; |
824 | 4.40k | } |
825 | | |
826 | 762 | return Line.Level * Style.IndentWidth + Length <= ColumnLimit; |
827 | 766 | } |
828 | | |
829 | | FormatToken *UnwrappedLineParser::parseBlock( |
830 | | bool MustBeDeclaration, unsigned AddLevels, bool MunchSemi, bool KeepBraces, |
831 | | IfStmtKind *IfKind, bool UnindentWhitesmithsBraces, |
832 | 37.1k | bool CanContainBracedList, TokenType NextLBracesType) { |
833 | 74.0k | auto HandleVerilogBlockLabel = [this]() { |
834 | | // ":" name |
835 | 74.0k | if (Style.isVerilog() && FormatTok->is(tok::colon)56 ) { |
836 | 20 | nextToken(); |
837 | 20 | if (Keywords.isVerilogIdentifier(*FormatTok)) |
838 | 20 | nextToken(); |
839 | 20 | } |
840 | 74.0k | }; |
841 | | |
842 | 37.1k | assert((FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) || |
843 | 37.1k | (Style.isVerilog() && Keywords.isVerilogBegin(*FormatTok))) && |
844 | 37.1k | "'{' or macro block token expected"); |
845 | 0 | FormatToken *Tok = FormatTok; |
846 | 37.1k | const bool FollowedByComment = Tokens->peekNextToken()->is(tok::comment); |
847 | 37.1k | auto Index = CurrentLines->size(); |
848 | 37.1k | const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); |
849 | 37.1k | FormatTok->setBlockKind(BK_Block); |
850 | | |
851 | | // For Whitesmiths mode, jump to the next level prior to skipping over the |
852 | | // braces. |
853 | 37.1k | if (AddLevels > 0 && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths33.1k ) |
854 | 666 | ++Line->Level; |
855 | | |
856 | 37.1k | size_t PPStartHash = computePPHash(); |
857 | | |
858 | 37.1k | const unsigned InitialLevel = Line->Level; |
859 | 37.1k | nextToken(/*LevelDifference=*/AddLevels); |
860 | 37.1k | HandleVerilogBlockLabel(); |
861 | | |
862 | | // Bail out if there are too many levels. Otherwise, the stack might overflow. |
863 | 37.1k | if (Line->Level > 300) |
864 | 0 | return nullptr; |
865 | | |
866 | 37.1k | if (MacroBlock && FormatTok->is(tok::l_paren)36 ) |
867 | 9 | parseParens(); |
868 | | |
869 | 37.1k | size_t NbPreprocessorDirectives = |
870 | 37.1k | CurrentLines == &Lines ? PreprocessorDirectives.size()36.5k : 0651 ; |
871 | 37.1k | addUnwrappedLine(); |
872 | 37.1k | size_t OpeningLineIndex = |
873 | 37.1k | CurrentLines->empty() |
874 | 37.1k | ? (UnwrappedLine::kInvalidIndex)0 |
875 | 37.1k | : (CurrentLines->size() - 1 - NbPreprocessorDirectives); |
876 | | |
877 | | // Whitesmiths is weird here. The brace needs to be indented for the namespace |
878 | | // block, but the block itself may not be indented depending on the style |
879 | | // settings. This allows the format to back up one level in those cases. |
880 | 37.1k | if (UnindentWhitesmithsBraces) |
881 | 36 | --Line->Level; |
882 | | |
883 | 37.1k | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
884 | 37.1k | MustBeDeclaration); |
885 | 37.1k | if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths33.1k ) |
886 | 32.4k | Line->Level += AddLevels; |
887 | | |
888 | 37.1k | FormatToken *IfLBrace = nullptr; |
889 | 37.1k | const bool SimpleBlock = |
890 | 37.1k | parseLevel(Tok, CanContainBracedList, NextLBracesType, IfKind, &IfLBrace); |
891 | | |
892 | 37.1k | if (eof()) |
893 | 273 | return IfLBrace; |
894 | | |
895 | 36.8k | if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)36 |
896 | 36.8k | : !FormatTok->is(tok::r_brace)36.8k ) { |
897 | 0 | Line->Level = InitialLevel; |
898 | 0 | FormatTok->setBlockKind(BK_Block); |
899 | 0 | return IfLBrace; |
900 | 0 | } |
901 | | |
902 | 36.8k | auto RemoveBraces = [=]() mutable { |
903 | 36.8k | if (!SimpleBlock) |
904 | 36.1k | return false; |
905 | 788 | assert(Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)); |
906 | 0 | assert(FormatTok->is(tok::r_brace)); |
907 | 0 | const bool WrappedOpeningBrace = !Tok->Previous; |
908 | 788 | if (WrappedOpeningBrace && FollowedByComment104 ) |
909 | 24 | return false; |
910 | 764 | const bool HasRequiredIfBraces = IfLBrace && !IfLBrace->Optional64 ; |
911 | 764 | if (KeepBraces && !HasRequiredIfBraces206 ) |
912 | 192 | return false; |
913 | 572 | if (Tok->isNot(TT_ElseLBrace) || !HasRequiredIfBraces94 ) { |
914 | 536 | const FormatToken *Previous = Tokens->getPreviousToken(); |
915 | 536 | assert(Previous); |
916 | 536 | if (Previous->is(tok::r_brace) && !Previous->Optional76 ) |
917 | 46 | return false; |
918 | 536 | } |
919 | 526 | assert(!CurrentLines->empty()); |
920 | 0 | auto &LastLine = CurrentLines->back(); |
921 | 526 | if (LastLine.Level == InitialLevel + 1 && !mightFitOnOneLine(LastLine)418 ) |
922 | 96 | return false; |
923 | 430 | if (Tok->is(TT_ElseLBrace)) |
924 | 82 | return true; |
925 | 348 | if (WrappedOpeningBrace) { |
926 | 18 | assert(Index > 0); |
927 | 0 | --Index; // The line above the wrapped l_brace. |
928 | 18 | Tok = nullptr; |
929 | 18 | } |
930 | 0 | return mightFitOnOneLine((*CurrentLines)[Index], Tok); |
931 | 430 | }; |
932 | 36.8k | if (RemoveBraces()) { |
933 | 394 | Tok->MatchingParen = FormatTok; |
934 | 394 | FormatTok->MatchingParen = Tok; |
935 | 394 | } |
936 | | |
937 | 36.8k | size_t PPEndHash = computePPHash(); |
938 | | |
939 | | // Munch the closing brace. |
940 | 36.8k | nextToken(/*LevelDifference=*/-AddLevels); |
941 | 36.8k | HandleVerilogBlockLabel(); |
942 | | |
943 | 36.8k | if (MacroBlock && FormatTok->is(tok::l_paren)36 ) |
944 | 9 | parseParens(); |
945 | | |
946 | 36.8k | if (FormatTok->is(tok::kw_noexcept)) { |
947 | | // A noexcept in a requires expression. |
948 | 54 | nextToken(); |
949 | 54 | } |
950 | | |
951 | 36.8k | if (FormatTok->is(tok::arrow)) { |
952 | | // Following the } or noexcept we can find a trailing return type arrow |
953 | | // as part of an implicit conversion constraint. |
954 | 118 | nextToken(); |
955 | 118 | parseStructuralElement(); |
956 | 118 | } |
957 | | |
958 | 36.8k | if (MunchSemi && FormatTok->is(tok::semi)28.8k ) |
959 | 676 | nextToken(); |
960 | | |
961 | 36.8k | Line->Level = InitialLevel; |
962 | | |
963 | 36.8k | if (PPStartHash == PPEndHash) { |
964 | 36.8k | Line->MatchingOpeningBlockLineIndex = OpeningLineIndex; |
965 | 36.8k | if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) { |
966 | | // Update the opening line to add the forward reference as well |
967 | 36.8k | (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex = |
968 | 36.8k | CurrentLines->size() - 1; |
969 | 36.8k | } |
970 | 36.8k | } |
971 | | |
972 | 36.8k | return IfLBrace; |
973 | 36.8k | } |
974 | | |
975 | 396 | static bool isGoogScope(const UnwrappedLine &Line) { |
976 | | // FIXME: Closure-library specific stuff should not be hard-coded but be |
977 | | // configurable. |
978 | 396 | if (Line.Tokens.size() < 4) |
979 | 0 | return false; |
980 | 396 | auto I = Line.Tokens.begin(); |
981 | 396 | if (I->Tok->TokenText != "goog") |
982 | 388 | return false; |
983 | 8 | ++I; |
984 | 8 | if (I->Tok->isNot(tok::period)) |
985 | 0 | return false; |
986 | 8 | ++I; |
987 | 8 | if (I->Tok->TokenText != "scope") |
988 | 0 | return false; |
989 | 8 | ++I; |
990 | 8 | return I->Tok->is(tok::l_paren); |
991 | 8 | } |
992 | | |
993 | | static bool isIIFE(const UnwrappedLine &Line, |
994 | 388 | const AdditionalKeywords &Keywords) { |
995 | | // Look for the start of an immediately invoked anonymous function. |
996 | | // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression |
997 | | // This is commonly done in JavaScript to create a new, anonymous scope. |
998 | | // Example: (function() { ... })() |
999 | 388 | if (Line.Tokens.size() < 3) |
1000 | 0 | return false; |
1001 | 388 | auto I = Line.Tokens.begin(); |
1002 | 388 | if (I->Tok->isNot(tok::l_paren)) |
1003 | 368 | return false; |
1004 | 20 | ++I; |
1005 | 20 | if (I->Tok->isNot(Keywords.kw_function)) |
1006 | 0 | return false; |
1007 | 20 | ++I; |
1008 | 20 | return I->Tok->is(tok::l_paren); |
1009 | 20 | } |
1010 | | |
1011 | | static bool ShouldBreakBeforeBrace(const FormatStyle &Style, |
1012 | 12.3k | const FormatToken &InitialToken) { |
1013 | 12.3k | tok::TokenKind Kind = InitialToken.Tok.getKind(); |
1014 | 12.3k | if (InitialToken.is(TT_NamespaceMacro)) |
1015 | 160 | Kind = tok::kw_namespace; |
1016 | | |
1017 | 12.3k | switch (Kind) { |
1018 | 4.23k | case tok::kw_namespace: |
1019 | 4.23k | return Style.BraceWrapping.AfterNamespace; |
1020 | 3.90k | case tok::kw_class: |
1021 | 3.90k | return Style.BraceWrapping.AfterClass; |
1022 | 269 | case tok::kw_union: |
1023 | 269 | return Style.BraceWrapping.AfterUnion; |
1024 | 3.76k | case tok::kw_struct: |
1025 | 3.76k | return Style.BraceWrapping.AfterStruct; |
1026 | 107 | case tok::kw_enum: |
1027 | 107 | return Style.BraceWrapping.AfterEnum; |
1028 | 80 | default: |
1029 | 80 | return false; |
1030 | 12.3k | } |
1031 | 12.3k | } |
1032 | | |
1033 | | void UnwrappedLineParser::parseChildBlock( |
1034 | 4.75k | bool CanContainBracedList, clang::format::TokenType NextLBracesType) { |
1035 | 4.75k | assert(FormatTok->is(tok::l_brace)); |
1036 | 0 | FormatTok->setBlockKind(BK_Block); |
1037 | 4.75k | const FormatToken *OpeningBrace = FormatTok; |
1038 | 4.75k | nextToken(); |
1039 | 4.75k | { |
1040 | 4.75k | bool SkipIndent = (Style.isJavaScript() && |
1041 | 4.75k | (396 isGoogScope(*Line)396 || isIIFE(*Line, Keywords)388 )); |
1042 | 4.75k | ScopedLineState LineState(*this); |
1043 | 4.75k | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
1044 | 4.75k | /*MustBeDeclaration=*/false); |
1045 | 4.75k | Line->Level += SkipIndent ? 024 : 14.73k ; |
1046 | 4.75k | parseLevel(OpeningBrace, CanContainBracedList, NextLBracesType); |
1047 | 4.75k | flushComments(isOnNewLine(*FormatTok)); |
1048 | 4.75k | Line->Level -= SkipIndent ? 024 : 14.73k ; |
1049 | 4.75k | } |
1050 | 4.75k | nextToken(); |
1051 | 4.75k | } |
1052 | | |
1053 | 13.4k | void UnwrappedLineParser::parsePPDirective() { |
1054 | 13.4k | assert(FormatTok->is(tok::hash) && "'#' expected"); |
1055 | 0 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
1056 | | |
1057 | 13.4k | nextToken(); |
1058 | | |
1059 | 13.4k | if (!FormatTok->Tok.getIdentifierInfo()) { |
1060 | 52 | parsePPUnknown(); |
1061 | 52 | return; |
1062 | 52 | } |
1063 | | |
1064 | 13.4k | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
1065 | 3.23k | case tok::pp_define: |
1066 | 3.23k | parsePPDefine(); |
1067 | 3.23k | return; |
1068 | 1.25k | case tok::pp_if: |
1069 | 1.25k | parsePPIf(/*IfDef=*/false); |
1070 | 1.25k | break; |
1071 | 931 | case tok::pp_ifdef: |
1072 | 1.06k | case tok::pp_ifndef: |
1073 | 1.06k | parsePPIf(/*IfDef=*/true); |
1074 | 1.06k | break; |
1075 | 1.35k | case tok::pp_else: |
1076 | 1.35k | parsePPElse(); |
1077 | 1.35k | break; |
1078 | 0 | case tok::pp_elifdef: |
1079 | 96 | case tok::pp_elifndef: |
1080 | 210 | case tok::pp_elif: |
1081 | 210 | parsePPElIf(); |
1082 | 210 | break; |
1083 | 2.25k | case tok::pp_endif: |
1084 | 2.25k | parsePPEndIf(); |
1085 | 2.25k | break; |
1086 | 4.03k | default: |
1087 | 4.03k | parsePPUnknown(); |
1088 | 4.03k | break; |
1089 | 13.4k | } |
1090 | 13.4k | } |
1091 | | |
1092 | 3.98k | void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { |
1093 | 3.98k | size_t Line = CurrentLines->size(); |
1094 | 3.98k | if (CurrentLines == &PreprocessorDirectives) |
1095 | 2.57k | Line += Lines.size(); |
1096 | | |
1097 | 3.98k | if (Unreachable || |
1098 | 3.98k | (2.07k !PPStack.empty()2.07k && PPStack.back().Kind == PP_Unreachable342 )) { |
1099 | 2.07k | PPStack.push_back({PP_Unreachable, Line}); |
1100 | 2.07k | } else { |
1101 | 1.91k | PPStack.push_back({PP_Conditional, Line}); |
1102 | 1.91k | } |
1103 | 3.98k | } |
1104 | | |
1105 | 2.34k | void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { |
1106 | 2.34k | ++PPBranchLevel; |
1107 | 2.34k | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
1108 | 2.34k | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
1109 | 1.39k | PPLevelBranchIndex.push_back(0); |
1110 | 1.39k | PPLevelBranchCount.push_back(0); |
1111 | 1.39k | } |
1112 | 2.34k | PPChainBranchIndex.push(0); |
1113 | 2.34k | bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; |
1114 | 2.34k | conditionalCompilationCondition(Unreachable || Skip1.79k ); |
1115 | 2.34k | } |
1116 | | |
1117 | 1.64k | void UnwrappedLineParser::conditionalCompilationAlternative() { |
1118 | 1.64k | if (!PPStack.empty()) |
1119 | 1.61k | PPStack.pop_back(); |
1120 | 1.64k | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
1121 | 1.64k | if (!PPChainBranchIndex.empty()) |
1122 | 1.61k | ++PPChainBranchIndex.top(); |
1123 | 1.64k | conditionalCompilationCondition( |
1124 | 1.64k | PPBranchLevel >= 0 && !PPChainBranchIndex.empty()1.61k && |
1125 | 1.64k | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()1.61k ); |
1126 | 1.64k | } |
1127 | | |
1128 | 2.27k | void UnwrappedLineParser::conditionalCompilationEnd() { |
1129 | 2.27k | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
1130 | 2.27k | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()2.26k ) { |
1131 | 2.26k | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) |
1132 | 1.32k | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
1133 | 2.26k | } |
1134 | | // Guard against #endif's without #if. |
1135 | 2.27k | if (PPBranchLevel > -1) |
1136 | 2.26k | --PPBranchLevel; |
1137 | 2.27k | if (!PPChainBranchIndex.empty()) |
1138 | 2.26k | PPChainBranchIndex.pop(); |
1139 | 2.27k | if (!PPStack.empty()) |
1140 | 2.26k | PPStack.pop_back(); |
1141 | 2.27k | } |
1142 | | |
1143 | 2.32k | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
1144 | 2.32k | bool IfNDef = FormatTok->is(tok::pp_ifndef); |
1145 | 2.32k | nextToken(); |
1146 | 2.32k | bool Unreachable = false; |
1147 | 2.32k | if (!IfDef && (1.25k FormatTok->is(tok::kw_false)1.25k || FormatTok->TokenText == "0"1.25k )) |
1148 | 556 | Unreachable = true; |
1149 | 2.32k | if (IfDef && !IfNDef1.06k && FormatTok->TokenText == "SWIG"931 ) |
1150 | 3 | Unreachable = true; |
1151 | 2.32k | conditionalCompilationStart(Unreachable); |
1152 | 2.32k | FormatToken *IfCondition = FormatTok; |
1153 | | // If there's a #ifndef on the first line, and the only lines before it are |
1154 | | // comments, it could be an include guard. |
1155 | 2.32k | bool MaybeIncludeGuard = IfNDef; |
1156 | 2.32k | if (IncludeGuard == IG_Inited && MaybeIncludeGuard336 ) { |
1157 | 117 | for (auto &Line : Lines) { |
1158 | 21 | if (!Line.Tokens.front().Tok->is(tok::comment)) { |
1159 | 12 | MaybeIncludeGuard = false; |
1160 | 12 | IncludeGuard = IG_Rejected; |
1161 | 12 | break; |
1162 | 12 | } |
1163 | 21 | } |
1164 | 117 | } |
1165 | 2.32k | --PPBranchLevel; |
1166 | 2.32k | parsePPUnknown(); |
1167 | 2.32k | ++PPBranchLevel; |
1168 | 2.32k | if (IncludeGuard == IG_Inited && MaybeIncludeGuard324 ) { |
1169 | 105 | IncludeGuard = IG_IfNdefed; |
1170 | 105 | IncludeGuardToken = IfCondition; |
1171 | 105 | } |
1172 | 2.32k | } |
1173 | | |
1174 | 1.56k | void UnwrappedLineParser::parsePPElse() { |
1175 | | // If a potential include guard has an #else, it's not an include guard. |
1176 | 1.56k | if (IncludeGuard == IG_Defined && PPBranchLevel == 036 ) |
1177 | 36 | IncludeGuard = IG_Rejected; |
1178 | 1.56k | conditionalCompilationAlternative(); |
1179 | 1.56k | if (PPBranchLevel > -1) |
1180 | 1.55k | --PPBranchLevel; |
1181 | 1.56k | parsePPUnknown(); |
1182 | 1.56k | ++PPBranchLevel; |
1183 | 1.56k | } |
1184 | | |
1185 | 210 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
1186 | | |
1187 | 2.25k | void UnwrappedLineParser::parsePPEndIf() { |
1188 | 2.25k | conditionalCompilationEnd(); |
1189 | 2.25k | parsePPUnknown(); |
1190 | | // If the #endif of a potential include guard is the last thing in the file, |
1191 | | // then we found an include guard. |
1192 | 2.25k | if (IncludeGuard == IG_Defined && PPBranchLevel == -160 && Tokens->isEOF()51 && |
1193 | 2.25k | Style.IndentPPDirectives != FormatStyle::PPDIS_None42 ) { |
1194 | 42 | IncludeGuard = IG_Found; |
1195 | 42 | } |
1196 | 2.25k | } |
1197 | | |
1198 | 3.23k | void UnwrappedLineParser::parsePPDefine() { |
1199 | 3.23k | nextToken(); |
1200 | | |
1201 | 3.23k | if (!FormatTok->Tok.getIdentifierInfo()) { |
1202 | 10 | IncludeGuard = IG_Rejected; |
1203 | 10 | IncludeGuardToken = nullptr; |
1204 | 10 | parsePPUnknown(); |
1205 | 10 | return; |
1206 | 10 | } |
1207 | | |
1208 | 3.22k | if (IncludeGuard == IG_IfNdefed && |
1209 | 3.22k | IncludeGuardToken->TokenText == FormatTok->TokenText96 ) { |
1210 | 87 | IncludeGuard = IG_Defined; |
1211 | 87 | IncludeGuardToken = nullptr; |
1212 | 93 | for (auto &Line : Lines) { |
1213 | 93 | if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { |
1214 | 0 | IncludeGuard = IG_Rejected; |
1215 | 0 | break; |
1216 | 0 | } |
1217 | 93 | } |
1218 | 87 | } |
1219 | | |
1220 | | // In the context of a define, even keywords should be treated as normal |
1221 | | // identifiers. Setting the kind to identifier is not enough, because we need |
1222 | | // to treat additional keywords like __except as well, which are already |
1223 | | // identifiers. Setting the identifier info to null interferes with include |
1224 | | // guard processing above, and changes preprocessing nesting. |
1225 | 3.22k | FormatTok->Tok.setKind(tok::identifier); |
1226 | 3.22k | FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define); |
1227 | 3.22k | nextToken(); |
1228 | 3.22k | if (FormatTok->Tok.getKind() == tok::l_paren && |
1229 | 3.22k | !FormatTok->hasWhitespaceBefore()1.58k ) { |
1230 | 1.29k | parseParens(); |
1231 | 1.29k | } |
1232 | 3.22k | if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) |
1233 | 435 | Line->Level += PPBranchLevel + 1; |
1234 | 3.22k | addUnwrappedLine(); |
1235 | 3.22k | ++Line->Level; |
1236 | | |
1237 | | // Errors during a preprocessor directive can only affect the layout of the |
1238 | | // preprocessor directive, and thus we ignore them. An alternative approach |
1239 | | // would be to use the same approach we use on the file level (no |
1240 | | // re-indentation if there was a structural error) within the macro |
1241 | | // definition. |
1242 | 3.22k | parseFile(); |
1243 | 3.22k | } |
1244 | | |
1245 | 10.2k | void UnwrappedLineParser::parsePPUnknown() { |
1246 | 18.2k | do { |
1247 | 18.2k | nextToken(); |
1248 | 18.2k | } while (!eof()); |
1249 | 10.2k | if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) |
1250 | 900 | Line->Level += PPBranchLevel + 1; |
1251 | 10.2k | addUnwrappedLine(); |
1252 | 10.2k | } |
1253 | | |
1254 | | // Here we exclude certain tokens that are not usually the first token in an |
1255 | | // unwrapped line. This is used in attempt to distinguish macro calls without |
1256 | | // trailing semicolons from other constructs split to several lines. |
1257 | 533 | static bool tokenCanStartNewLine(const FormatToken &Tok) { |
1258 | | // Semicolon can be a null-statement, l_square can be a start of a macro or |
1259 | | // a C++11 attribute, but this doesn't seem to be common. |
1260 | 533 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace)528 && |
1261 | 533 | Tok.isNot(TT_AttributeSquare)517 && |
1262 | | // Tokens that can only be used as binary operators and a part of |
1263 | | // overloaded operator names. |
1264 | 533 | Tok.isNot(tok::period)517 && Tok.isNot(tok::periodstar)472 && |
1265 | 533 | Tok.isNot(tok::arrow)472 && Tok.isNot(tok::arrowstar)466 && |
1266 | 533 | Tok.isNot(tok::less)463 && Tok.isNot(tok::greater)463 && |
1267 | 533 | Tok.isNot(tok::slash)460 && Tok.isNot(tok::percent)460 && |
1268 | 533 | Tok.isNot(tok::lessless)460 && Tok.isNot(tok::greatergreater)430 && |
1269 | 533 | Tok.isNot(tok::equal)430 && Tok.isNot(tok::plusequal)415 && |
1270 | 533 | Tok.isNot(tok::minusequal)412 && Tok.isNot(tok::starequal)409 && |
1271 | 533 | Tok.isNot(tok::slashequal)406 && Tok.isNot(tok::percentequal)403 && |
1272 | 533 | Tok.isNot(tok::ampequal)400 && Tok.isNot(tok::pipeequal)397 && |
1273 | 533 | Tok.isNot(tok::caretequal)394 && Tok.isNot(tok::greatergreaterequal)391 && |
1274 | 533 | Tok.isNot(tok::lesslessequal)388 && |
1275 | | // Colon is used in labels, base class lists, initializer lists, |
1276 | | // range-based for loops, ternary operator, but should never be the |
1277 | | // first token in an unwrapped line. |
1278 | 533 | Tok.isNot(tok::colon)385 && |
1279 | | // 'noexcept' is a trailing annotation. |
1280 | 533 | Tok.isNot(tok::kw_noexcept)258 ; |
1281 | 533 | } |
1282 | | |
1283 | | static bool mustBeJSIdent(const AdditionalKeywords &Keywords, |
1284 | 5.70k | const FormatToken *FormatTok) { |
1285 | | // FIXME: This returns true for C/C++ keywords like 'struct'. |
1286 | 5.70k | return FormatTok->is(tok::identifier) && |
1287 | 5.70k | (1.35k FormatTok->Tok.getIdentifierInfo() == nullptr1.35k || |
1288 | 1.35k | !FormatTok->isOneOf( |
1289 | 1.34k | Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, |
1290 | 1.34k | Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, |
1291 | 1.34k | Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, |
1292 | 1.34k | Keywords.kw_let, Keywords.kw_var, tok::kw_const, |
1293 | 1.34k | Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, |
1294 | 1.34k | Keywords.kw_instanceof, Keywords.kw_interface, |
1295 | 1.34k | Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from)); |
1296 | 5.70k | } |
1297 | | |
1298 | | static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, |
1299 | 5.85k | const FormatToken *FormatTok) { |
1300 | 5.85k | return FormatTok->Tok.isLiteral() || |
1301 | 5.85k | FormatTok->isOneOf(tok::kw_true, tok::kw_false)5.65k || |
1302 | 5.85k | mustBeJSIdent(Keywords, FormatTok)5.65k ; |
1303 | 5.85k | } |
1304 | | |
1305 | | // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement |
1306 | | // when encountered after a value (see mustBeJSIdentOrValue). |
1307 | | static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, |
1308 | 188 | const FormatToken *FormatTok) { |
1309 | 188 | return FormatTok->isOneOf( |
1310 | 188 | tok::kw_return, Keywords.kw_yield, |
1311 | | // conditionals |
1312 | 188 | tok::kw_if, tok::kw_else, |
1313 | | // loops |
1314 | 188 | tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, |
1315 | | // switch/case |
1316 | 188 | tok::kw_switch, tok::kw_case, |
1317 | | // exceptions |
1318 | 188 | tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, |
1319 | | // declaration |
1320 | 188 | tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, |
1321 | 188 | Keywords.kw_async, Keywords.kw_function, |
1322 | | // import/export |
1323 | 188 | Keywords.kw_import, tok::kw_export); |
1324 | 188 | } |
1325 | | |
1326 | | // Checks whether a token is a type in K&R C (aka C78). |
1327 | 21.6k | static bool isC78Type(const FormatToken &Tok) { |
1328 | 21.6k | return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long, |
1329 | 21.6k | tok::kw_unsigned, tok::kw_float, tok::kw_double, |
1330 | 21.6k | tok::identifier); |
1331 | 21.6k | } |
1332 | | |
1333 | | // This function checks whether a token starts the first parameter declaration |
1334 | | // in a K&R C (aka C78) function definition, e.g.: |
1335 | | // int f(a, b) |
1336 | | // short a, b; |
1337 | | // { |
1338 | | // return a + b; |
1339 | | // } |
1340 | | static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next, |
1341 | 28.5k | const FormatToken *FuncName) { |
1342 | 28.5k | assert(Tok); |
1343 | 0 | assert(Next); |
1344 | 0 | assert(FuncName); |
1345 | | |
1346 | 28.5k | if (FuncName->isNot(tok::identifier)) |
1347 | 9.94k | return false; |
1348 | | |
1349 | 18.6k | const FormatToken *Prev = FuncName->Previous; |
1350 | 18.6k | if (!Prev || (18.6k Prev->isNot(tok::star)18.6k && !isC78Type(*Prev)18.3k )) |
1351 | 15.3k | return false; |
1352 | | |
1353 | 3.26k | if (!isC78Type(*Tok) && |
1354 | 3.26k | !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)3.18k ) { |
1355 | 3.18k | return false; |
1356 | 3.18k | } |
1357 | | |
1358 | 81 | if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo()) |
1359 | 54 | return false; |
1360 | | |
1361 | 27 | Tok = Tok->Previous; |
1362 | 27 | if (!Tok || Tok->isNot(tok::r_paren)) |
1363 | 0 | return false; |
1364 | | |
1365 | 27 | Tok = Tok->Previous; |
1366 | 27 | if (!Tok || Tok->isNot(tok::identifier)) |
1367 | 0 | return false; |
1368 | | |
1369 | 27 | return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma); |
1370 | 27 | } |
1371 | | |
1372 | 117 | void UnwrappedLineParser::parseModuleImport() { |
1373 | 117 | nextToken(); |
1374 | 369 | while (!eof()) { |
1375 | 351 | if (FormatTok->is(tok::colon)) { |
1376 | 18 | FormatTok->setFinalizedType(TT_ModulePartitionColon); |
1377 | 18 | } |
1378 | | // Handle import <foo/bar.h> as we would an include statement. |
1379 | 333 | else if (FormatTok->is(tok::less)) { |
1380 | 18 | nextToken(); |
1381 | 72 | while (!FormatTok->isOneOf(tok::semi, tok::greater, tok::eof)) { |
1382 | | // Mark tokens up to the trailing line comments as implicit string |
1383 | | // literals. |
1384 | 54 | if (FormatTok->isNot(tok::comment) && |
1385 | 54 | !FormatTok->TokenText.startswith("//")) { |
1386 | 54 | FormatTok->setFinalizedType(TT_ImplicitStringLiteral); |
1387 | 54 | } |
1388 | 54 | nextToken(); |
1389 | 54 | } |
1390 | 18 | } |
1391 | 351 | if (FormatTok->is(tok::semi)) { |
1392 | 99 | nextToken(); |
1393 | 99 | break; |
1394 | 99 | } |
1395 | 252 | nextToken(); |
1396 | 252 | } |
1397 | | |
1398 | 117 | addUnwrappedLine(); |
1399 | 117 | } |
1400 | | |
1401 | | // readTokenWithJavaScriptASI reads the next token and terminates the current |
1402 | | // line if JavaScript Automatic Semicolon Insertion must |
1403 | | // happen between the current token and the next token. |
1404 | | // |
1405 | | // This method is conservative - it cannot cover all edge cases of JavaScript, |
1406 | | // but only aims to correctly handle certain well known cases. It *must not* |
1407 | | // return true in speculative cases. |
1408 | 34.1k | void UnwrappedLineParser::readTokenWithJavaScriptASI() { |
1409 | 34.1k | FormatToken *Previous = FormatTok; |
1410 | 34.1k | readToken(); |
1411 | 34.1k | FormatToken *Next = FormatTok; |
1412 | | |
1413 | 34.1k | bool IsOnSameLine = |
1414 | 34.1k | CommentsBeforeNextToken.empty() |
1415 | 34.1k | ? Next->NewlinesBefore == 034.0k |
1416 | 34.1k | : CommentsBeforeNextToken.front()->NewlinesBefore == 093 ; |
1417 | 34.1k | if (IsOnSameLine) |
1418 | 31.2k | return; |
1419 | | |
1420 | 2.93k | bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); |
1421 | 2.93k | bool PreviousStartsTemplateExpr = |
1422 | 2.93k | Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${")14 ; |
1423 | 2.93k | if (PreviousMustBeValue || Previous->is(tok::r_paren)2.76k ) { |
1424 | | // If the line contains an '@' sign, the previous token might be an |
1425 | | // annotation, which can precede another identifier/value. |
1426 | 1.41k | bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) { |
1427 | 1.41k | return LineNode.Tok->is(tok::at); |
1428 | 1.41k | }); |
1429 | 240 | if (HasAt) |
1430 | 22 | return; |
1431 | 240 | } |
1432 | 2.91k | if (Next->is(tok::exclaim) && PreviousMustBeValue4 ) |
1433 | 4 | return addUnwrappedLine(); |
1434 | 2.91k | bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); |
1435 | 2.91k | bool NextEndsTemplateExpr = |
1436 | 2.91k | Next->is(TT_TemplateString) && Next->TokenText.startswith("}")26 ; |
1437 | 2.91k | if (NextMustBeValue && !NextEndsTemplateExpr912 && !PreviousStartsTemplateExpr910 && |
1438 | 2.91k | (898 PreviousMustBeValue898 || |
1439 | 898 | Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, |
1440 | 882 | tok::minusminus))) { |
1441 | 34 | return addUnwrappedLine(); |
1442 | 34 | } |
1443 | 2.87k | if ((PreviousMustBeValue || Previous->is(tok::r_paren)2.73k ) && |
1444 | 2.87k | isJSDeclOrStmt(Keywords, Next)188 ) { |
1445 | 28 | return addUnwrappedLine(); |
1446 | 28 | } |
1447 | 2.87k | } |
1448 | | |
1449 | | void UnwrappedLineParser::parseStructuralElement( |
1450 | | bool IsTopLevel, TokenType NextLBracesType, IfStmtKind *IfKind, |
1451 | 132k | FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) { |
1452 | 132k | if (Style.Language == FormatStyle::LK_TableGen && |
1453 | 132k | FormatTok->is(tok::pp_include)10 ) { |
1454 | 6 | nextToken(); |
1455 | 6 | if (FormatTok->is(tok::string_literal)) |
1456 | 6 | nextToken(); |
1457 | 6 | addUnwrappedLine(); |
1458 | 6 | return; |
1459 | 6 | } |
1460 | 132k | switch (FormatTok->Tok.getKind()) { |
1461 | 92 | case tok::kw_asm: |
1462 | 92 | nextToken(); |
1463 | 92 | if (FormatTok->is(tok::l_brace)) { |
1464 | 10 | FormatTok->setFinalizedType(TT_InlineASMBrace); |
1465 | 10 | nextToken(); |
1466 | 90 | while (FormatTok && FormatTok->isNot(tok::eof)) { |
1467 | 90 | if (FormatTok->is(tok::r_brace)) { |
1468 | 10 | FormatTok->setFinalizedType(TT_InlineASMBrace); |
1469 | 10 | nextToken(); |
1470 | 10 | addUnwrappedLine(); |
1471 | 10 | break; |
1472 | 10 | } |
1473 | 80 | FormatTok->Finalized = true; |
1474 | 80 | nextToken(); |
1475 | 80 | } |
1476 | 10 | } |
1477 | 92 | break; |
1478 | 4.00k | case tok::kw_namespace: |
1479 | 4.00k | parseNamespace(); |
1480 | 4.00k | return; |
1481 | 1.29k | case tok::kw_public: |
1482 | 1.64k | case tok::kw_protected: |
1483 | 2.60k | case tok::kw_private: |
1484 | 2.60k | if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript()2.54k || |
1485 | 2.60k | Style.isCSharp()2.49k ) { |
1486 | 359 | nextToken(); |
1487 | 2.25k | } else { |
1488 | 2.25k | parseAccessSpecifier(); |
1489 | 2.25k | } |
1490 | 2.60k | return; |
1491 | 6.23k | case tok::kw_if: { |
1492 | 6.23k | if (Style.isJavaScript() && Line->MustBeDeclaration40 ) { |
1493 | | // field/method declaration. |
1494 | 4 | break; |
1495 | 4 | } |
1496 | 6.23k | FormatToken *Tok = parseIfThenElse(IfKind); |
1497 | 6.23k | if (IfLeftBrace) |
1498 | 5.95k | *IfLeftBrace = Tok; |
1499 | 6.23k | return; |
1500 | 6.23k | } |
1501 | 1.89k | case tok::kw_for: |
1502 | 2.45k | case tok::kw_while: |
1503 | 2.45k | if (Style.isJavaScript() && Line->MustBeDeclaration56 ) { |
1504 | | // field/method declaration. |
1505 | 8 | break; |
1506 | 8 | } |
1507 | 2.44k | parseForOrWhileLoop(); |
1508 | 2.44k | return; |
1509 | 302 | case tok::kw_do: |
1510 | 302 | if (Style.isJavaScript() && Line->MustBeDeclaration4 ) { |
1511 | | // field/method declaration. |
1512 | 4 | break; |
1513 | 4 | } |
1514 | 298 | parseDoWhile(); |
1515 | 298 | if (HasDoWhile) |
1516 | 297 | *HasDoWhile = true; |
1517 | 298 | return; |
1518 | 690 | case tok::kw_switch: |
1519 | 690 | if (Style.isJavaScript() && Line->MustBeDeclaration8 ) { |
1520 | | // 'switch: string' field declaration. |
1521 | 4 | break; |
1522 | 4 | } |
1523 | 686 | parseSwitch(); |
1524 | 686 | return; |
1525 | 390 | case tok::kw_default: |
1526 | 390 | if (Style.isJavaScript() && Line->MustBeDeclaration4 ) { |
1527 | | // 'default: string' field declaration. |
1528 | 4 | break; |
1529 | 4 | } |
1530 | 386 | nextToken(); |
1531 | 386 | if (FormatTok->is(tok::colon)) { |
1532 | 384 | parseLabel(); |
1533 | 384 | return; |
1534 | 384 | } |
1535 | | // e.g. "default void f() {}" in a Java interface. |
1536 | 2 | break; |
1537 | 1.07k | case tok::kw_case: |
1538 | 1.07k | if (Style.isJavaScript() && Line->MustBeDeclaration8 ) { |
1539 | | // 'case: string' field declaration. |
1540 | 4 | nextToken(); |
1541 | 4 | break; |
1542 | 4 | } |
1543 | 1.07k | parseCaseLabel(); |
1544 | 1.07k | return; |
1545 | 235 | case tok::kw_try: |
1546 | 271 | case tok::kw___try: |
1547 | 271 | if (Style.isJavaScript() && Line->MustBeDeclaration10 ) { |
1548 | | // field/method declaration. |
1549 | 4 | break; |
1550 | 4 | } |
1551 | 267 | parseTryCatch(); |
1552 | 267 | return; |
1553 | 371 | case tok::kw_extern: |
1554 | 371 | nextToken(); |
1555 | 371 | if (FormatTok->is(tok::string_literal)) { |
1556 | 294 | nextToken(); |
1557 | 294 | if (FormatTok->is(tok::l_brace)) { |
1558 | 228 | if (Style.BraceWrapping.AfterExternBlock) |
1559 | 93 | addUnwrappedLine(); |
1560 | | // Either we indent or for backwards compatibility we follow the |
1561 | | // AfterExternBlock style. |
1562 | 228 | unsigned AddLevels = |
1563 | 228 | (Style.IndentExternBlock == FormatStyle::IEBS_Indent) || |
1564 | 228 | (165 Style.BraceWrapping.AfterExternBlock165 && |
1565 | 165 | Style.IndentExternBlock == |
1566 | 57 | FormatStyle::IEBS_AfterExternBlock) |
1567 | 228 | ? 1u102 |
1568 | 228 | : 0u126 ; |
1569 | 228 | parseBlock(/*MustBeDeclaration=*/true, AddLevels); |
1570 | 228 | addUnwrappedLine(); |
1571 | 228 | return; |
1572 | 228 | } |
1573 | 294 | } |
1574 | 143 | break; |
1575 | 451 | case tok::kw_export: |
1576 | 451 | if (Style.isJavaScript()) { |
1577 | 232 | parseJavaScriptEs6ImportExport(); |
1578 | 232 | return; |
1579 | 232 | } |
1580 | 219 | if (!Style.isCpp()) |
1581 | 0 | break; |
1582 | | // Handle C++ "(inline|export) namespace". |
1583 | 219 | LLVM_FALLTHROUGH; |
1584 | 436 | case tok::kw_inline: |
1585 | 436 | nextToken(); |
1586 | 436 | if (FormatTok->is(tok::kw_namespace)) { |
1587 | 78 | parseNamespace(); |
1588 | 78 | return; |
1589 | 78 | } |
1590 | 358 | break; |
1591 | 36.3k | case tok::identifier: |
1592 | 36.3k | if (FormatTok->is(TT_ForEachMacro)) { |
1593 | 240 | parseForOrWhileLoop(); |
1594 | 240 | return; |
1595 | 240 | } |
1596 | 36.1k | if (FormatTok->is(TT_MacroBlockBegin)) { |
1597 | 0 | parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, |
1598 | 0 | /*MunchSemi=*/false); |
1599 | 0 | return; |
1600 | 0 | } |
1601 | 36.1k | if (FormatTok->is(Keywords.kw_import)) { |
1602 | 528 | if (Style.isJavaScript()) { |
1603 | 322 | parseJavaScriptEs6ImportExport(); |
1604 | 322 | return; |
1605 | 322 | } |
1606 | 206 | if (Style.Language == FormatStyle::LK_Proto) { |
1607 | 12 | nextToken(); |
1608 | 12 | if (FormatTok->is(tok::kw_public)) |
1609 | 2 | nextToken(); |
1610 | 12 | if (!FormatTok->is(tok::string_literal)) |
1611 | 0 | return; |
1612 | 12 | nextToken(); |
1613 | 12 | if (FormatTok->is(tok::semi)) |
1614 | 8 | nextToken(); |
1615 | 12 | addUnwrappedLine(); |
1616 | 12 | return; |
1617 | 12 | } |
1618 | 194 | if (Style.isCpp()) { |
1619 | 117 | parseModuleImport(); |
1620 | 117 | return; |
1621 | 117 | } |
1622 | 194 | } |
1623 | 35.6k | if (Style.isCpp() && |
1624 | 35.6k | FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, |
1625 | 31.9k | Keywords.kw_slots, Keywords.kw_qslots)) { |
1626 | 45 | nextToken(); |
1627 | 45 | if (FormatTok->is(tok::colon)) { |
1628 | 18 | nextToken(); |
1629 | 18 | addUnwrappedLine(); |
1630 | 18 | return; |
1631 | 18 | } |
1632 | 45 | } |
1633 | 35.6k | if (Style.isCpp() && FormatTok->is(TT_StatementMacro)31.9k ) { |
1634 | 90 | parseStatementMacro(); |
1635 | 90 | return; |
1636 | 90 | } |
1637 | 35.5k | if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)31.8k ) { |
1638 | 160 | parseNamespace(); |
1639 | 160 | return; |
1640 | 160 | } |
1641 | | // In all other cases, parse the declaration. |
1642 | 35.3k | break; |
1643 | 76.5k | default: |
1644 | 76.5k | break; |
1645 | 132k | } |
1646 | 487k | do 112k { |
1647 | 487k | const FormatToken *Previous = FormatTok->Previous; |
1648 | 487k | switch (FormatTok->Tok.getKind()) { |
1649 | 1.95k | case tok::at: |
1650 | 1.95k | nextToken(); |
1651 | 1.95k | if (FormatTok->is(tok::l_brace)) { |
1652 | 139 | nextToken(); |
1653 | 139 | parseBracedList(); |
1654 | 139 | break; |
1655 | 1.81k | } else if (Style.Language == FormatStyle::LK_Java && |
1656 | 1.81k | FormatTok->is(Keywords.kw_interface)82 ) { |
1657 | 8 | nextToken(); |
1658 | 8 | break; |
1659 | 8 | } |
1660 | 1.81k | switch (FormatTok->Tok.getObjCKeywordID()) { |
1661 | 30 | case tok::objc_public: |
1662 | 60 | case tok::objc_protected: |
1663 | 90 | case tok::objc_package: |
1664 | 147 | case tok::objc_private: |
1665 | 147 | return parseAccessSpecifier(); |
1666 | 314 | case tok::objc_interface: |
1667 | 437 | case tok::objc_implementation: |
1668 | 437 | return parseObjCInterfaceOrImplementation(); |
1669 | 75 | case tok::objc_protocol: |
1670 | 75 | if (parseObjCProtocol()) |
1671 | 69 | return; |
1672 | 6 | break; |
1673 | 433 | case tok::objc_end: |
1674 | 433 | return; // Handled by the caller. |
1675 | 20 | case tok::objc_optional: |
1676 | 32 | case tok::objc_required: |
1677 | 32 | nextToken(); |
1678 | 32 | addUnwrappedLine(); |
1679 | 32 | return; |
1680 | 54 | case tok::objc_autoreleasepool: |
1681 | 54 | nextToken(); |
1682 | 54 | if (FormatTok->is(tok::l_brace)) { |
1683 | 48 | if (Style.BraceWrapping.AfterControlStatement == |
1684 | 48 | FormatStyle::BWACS_Always) { |
1685 | 12 | addUnwrappedLine(); |
1686 | 12 | } |
1687 | 48 | parseBlock(); |
1688 | 48 | } |
1689 | 54 | addUnwrappedLine(); |
1690 | 54 | return; |
1691 | 36 | case tok::objc_synchronized: |
1692 | 36 | nextToken(); |
1693 | 36 | if (FormatTok->is(tok::l_paren)) { |
1694 | | // Skip synchronization object |
1695 | 30 | parseParens(); |
1696 | 30 | } |
1697 | 36 | if (FormatTok->is(tok::l_brace)) { |
1698 | 30 | if (Style.BraceWrapping.AfterControlStatement == |
1699 | 30 | FormatStyle::BWACS_Always) { |
1700 | 12 | addUnwrappedLine(); |
1701 | 12 | } |
1702 | 30 | parseBlock(); |
1703 | 30 | } |
1704 | 36 | addUnwrappedLine(); |
1705 | 36 | return; |
1706 | 28 | case tok::objc_try: |
1707 | | // This branch isn't strictly necessary (the kw_try case below would |
1708 | | // do this too after the tok::at is parsed above). But be explicit. |
1709 | 28 | parseTryCatch(); |
1710 | 28 | return; |
1711 | 569 | default: |
1712 | 569 | break; |
1713 | 1.81k | } |
1714 | 575 | break; |
1715 | 621 | case tok::kw_concept: |
1716 | 621 | parseConcept(); |
1717 | 621 | return; |
1718 | 1.01k | case tok::kw_requires: { |
1719 | 1.01k | if (Style.isCpp()) { |
1720 | 1.01k | bool ParsedClause = parseRequires(); |
1721 | 1.01k | if (ParsedClause) |
1722 | 975 | return; |
1723 | 1.01k | } else { |
1724 | 4 | nextToken(); |
1725 | 4 | } |
1726 | 42 | break; |
1727 | 1.01k | } |
1728 | 2.56k | case tok::kw_enum: |
1729 | | // Ignore if this is part of "template <enum ...". |
1730 | 2.56k | if (Previous && Previous->is(tok::less)186 ) { |
1731 | 18 | nextToken(); |
1732 | 18 | break; |
1733 | 18 | } |
1734 | | |
1735 | | // parseEnum falls through and does not yet add an unwrapped line as an |
1736 | | // enum definition can start a structural element. |
1737 | 2.54k | if (!parseEnum()) |
1738 | 46 | break; |
1739 | | // This only applies for C++. |
1740 | 2.50k | if (!Style.isCpp()) { |
1741 | 102 | addUnwrappedLine(); |
1742 | 102 | return; |
1743 | 102 | } |
1744 | 2.40k | break; |
1745 | 2.40k | case tok::kw_typedef: |
1746 | 958 | nextToken(); |
1747 | 958 | if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, |
1748 | 958 | Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS, |
1749 | 958 | Keywords.kw_CF_CLOSED_ENUM, |
1750 | 958 | Keywords.kw_NS_CLOSED_ENUM)) { |
1751 | 74 | parseEnum(); |
1752 | 74 | } |
1753 | 958 | break; |
1754 | 4.20k | case tok::kw_struct: |
1755 | 4.50k | case tok::kw_union: |
1756 | 9.80k | case tok::kw_class: |
1757 | 9.80k | if (parseStructLike()) |
1758 | 411 | return; |
1759 | 9.39k | break; |
1760 | 9.39k | case tok::period: |
1761 | 3.18k | nextToken(); |
1762 | | // In Java, classes have an implicit static member "class". |
1763 | 3.18k | if (Style.Language == FormatStyle::LK_Java && FormatTok184 && |
1764 | 3.18k | FormatTok->is(tok::kw_class)184 ) { |
1765 | 4 | nextToken(); |
1766 | 4 | } |
1767 | 3.18k | if (Style.isJavaScript() && FormatTok473 && |
1768 | 3.18k | FormatTok->Tok.getIdentifierInfo()473 ) { |
1769 | | // JavaScript only has pseudo keywords, all keywords are allowed to |
1770 | | // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 |
1771 | 453 | nextToken(); |
1772 | 453 | } |
1773 | 3.18k | break; |
1774 | 82.1k | case tok::semi: |
1775 | 82.1k | nextToken(); |
1776 | 82.1k | addUnwrappedLine(); |
1777 | 82.1k | return; |
1778 | 954 | case tok::r_brace: |
1779 | 954 | addUnwrappedLine(); |
1780 | 954 | return; |
1781 | 42.2k | case tok::l_paren: { |
1782 | 42.2k | parseParens(); |
1783 | | // Break the unwrapped line if a K&R C function definition has a parameter |
1784 | | // declaration. |
1785 | 42.2k | if (!IsTopLevel || !Style.isCpp()32.0k || !Previous30.7k || FormatTok->is(tok::eof)30.0k ) |
1786 | 13.6k | break; |
1787 | 28.5k | if (isC78ParameterDecl(FormatTok, Tokens->peekNextToken(), Previous)) { |
1788 | 27 | addUnwrappedLine(); |
1789 | 27 | return; |
1790 | 27 | } |
1791 | 28.5k | break; |
1792 | 28.5k | } |
1793 | 28.5k | case tok::kw_operator: |
1794 | 3.24k | nextToken(); |
1795 | 3.24k | if (FormatTok->isBinaryOperator()) |
1796 | 1.42k | nextToken(); |
1797 | 3.24k | break; |
1798 | 195 | case tok::caret: |
1799 | 195 | nextToken(); |
1800 | 195 | if (FormatTok->Tok.isAnyIdentifier() || |
1801 | 195 | FormatTok->isSimpleTypeSpecifier()132 ) { |
1802 | 82 | nextToken(); |
1803 | 82 | } |
1804 | 195 | if (FormatTok->is(tok::l_paren)) |
1805 | 59 | parseParens(); |
1806 | 195 | if (FormatTok->is(tok::l_brace)) |
1807 | 97 | parseChildBlock(); |
1808 | 195 | break; |
1809 | 16.5k | case tok::l_brace: |
1810 | 16.5k | if (NextLBracesType != TT_Unknown) |
1811 | 18 | FormatTok->setFinalizedType(NextLBracesType); |
1812 | 16.5k | if (!tryToParsePropertyAccessor() && !tryToParseBracedList()16.4k ) { |
1813 | | // A block outside of parentheses must be the last part of a |
1814 | | // structural element. |
1815 | | // FIXME: Figure out cases where this is not true, and add projections |
1816 | | // for them (the one we know is missing are lambdas). |
1817 | 14.3k | if (Style.Language == FormatStyle::LK_Java && |
1818 | 14.3k | Line->Tokens.front().Tok->is(Keywords.kw_synchronized)113 ) { |
1819 | | // If necessary, we could set the type to something different than |
1820 | | // TT_FunctionLBrace. |
1821 | 6 | if (Style.BraceWrapping.AfterControlStatement == |
1822 | 6 | FormatStyle::BWACS_Always) { |
1823 | 2 | addUnwrappedLine(); |
1824 | 2 | } |
1825 | 14.3k | } else if (Style.BraceWrapping.AfterFunction) { |
1826 | 1.52k | addUnwrappedLine(); |
1827 | 1.52k | } |
1828 | 14.3k | if (!Line->InPPDirective) |
1829 | 14.2k | FormatTok->setFinalizedType(TT_FunctionLBrace); |
1830 | 14.3k | parseBlock(); |
1831 | 14.3k | addUnwrappedLine(); |
1832 | 14.3k | return; |
1833 | 14.3k | } |
1834 | | // Otherwise this was a braced init list, and the structural |
1835 | | // element continues. |
1836 | 2.20k | break; |
1837 | 2.20k | case tok::kw_try: |
1838 | 64 | if (Style.isJavaScript() && Line->MustBeDeclaration4 ) { |
1839 | | // field/method declaration. |
1840 | 4 | nextToken(); |
1841 | 4 | break; |
1842 | 4 | } |
1843 | | // We arrive here when parsing function-try blocks. |
1844 | 60 | if (Style.BraceWrapping.AfterFunction) |
1845 | 11 | addUnwrappedLine(); |
1846 | 60 | parseTryCatch(); |
1847 | 60 | return; |
1848 | 133k | case tok::identifier: { |
1849 | 133k | if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)1.09k && |
1850 | 133k | Line->MustBeDeclaration2 ) { |
1851 | 2 | addUnwrappedLine(); |
1852 | 2 | parseCSharpGenericTypeConstraint(); |
1853 | 2 | break; |
1854 | 2 | } |
1855 | 133k | if (FormatTok->is(TT_MacroBlockEnd)) { |
1856 | 12 | addUnwrappedLine(); |
1857 | 12 | return; |
1858 | 12 | } |
1859 | | |
1860 | | // Function declarations (as opposed to function expressions) are parsed |
1861 | | // on their own unwrapped line by continuing this loop. Function |
1862 | | // expressions (functions that are not on their own line) must not create |
1863 | | // a new unwrapped line, so they are special cased below. |
1864 | 133k | size_t TokenCount = Line->Tokens.size(); |
1865 | 133k | if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function)6.01k && |
1866 | 133k | (290 TokenCount > 1290 || (212 TokenCount == 1212 && !Line->Tokens.front().Tok->is( |
1867 | 98 | Keywords.kw_async)))) { |
1868 | 98 | tryToParseJSFunction(); |
1869 | 98 | break; |
1870 | 98 | } |
1871 | 132k | if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java126k ) && |
1872 | 132k | FormatTok->is(Keywords.kw_interface)6.88k ) { |
1873 | 56 | if (Style.isJavaScript()) { |
1874 | | // In JavaScript/TypeScript, "interface" can be used as a standalone |
1875 | | // identifier, e.g. in `var interface = 1;`. If "interface" is |
1876 | | // followed by another identifier, it is very like to be an actual |
1877 | | // interface declaration. |
1878 | 52 | unsigned StoredPosition = Tokens->getPosition(); |
1879 | 52 | FormatToken *Next = Tokens->getNextToken(); |
1880 | 52 | FormatTok = Tokens->setPosition(StoredPosition); |
1881 | 52 | if (!mustBeJSIdent(Keywords, Next)) { |
1882 | 16 | nextToken(); |
1883 | 16 | break; |
1884 | 16 | } |
1885 | 52 | } |
1886 | 40 | parseRecord(); |
1887 | 40 | addUnwrappedLine(); |
1888 | 40 | return; |
1889 | 56 | } |
1890 | | |
1891 | 132k | if (FormatTok->is(Keywords.kw_interface)) { |
1892 | 41 | if (parseStructLike()) |
1893 | 40 | return; |
1894 | 1 | break; |
1895 | 41 | } |
1896 | | |
1897 | 132k | if (Style.isCpp() && FormatTok->is(TT_StatementMacro)124k ) { |
1898 | 9 | parseStatementMacro(); |
1899 | 9 | return; |
1900 | 9 | } |
1901 | | |
1902 | | // See if the following token should start a new unwrapped line. |
1903 | 132k | StringRef Text = FormatTok->TokenText; |
1904 | | |
1905 | 132k | FormatToken *PreviousToken = FormatTok; |
1906 | 132k | nextToken(); |
1907 | | |
1908 | | // JS doesn't have macros, and within classes colons indicate fields, not |
1909 | | // labels. |
1910 | 132k | if (Style.isJavaScript()) |
1911 | 5.86k | break; |
1912 | | |
1913 | 126k | auto OneTokenSoFar = [&]() { |
1914 | 126k | auto I = Line->Tokens.begin(), E = Line->Tokens.end(); |
1915 | 127k | while (I != E && I->Tok->is(tok::comment)) |
1916 | 72 | ++I; |
1917 | 126k | while (I != E && Style.isVerilog() && I->Tok->is(tok::hash)197 ) |
1918 | 21 | ++I; |
1919 | 126k | return I != E && (++I == E); |
1920 | 126k | }; |
1921 | 126k | if (OneTokenSoFar()) { |
1922 | 32.2k | if (FormatTok->is(tok::colon) && !Line->MustBeDeclaration282 ) { |
1923 | 252 | Line->Tokens.begin()->Tok->MustBreakBefore = true; |
1924 | 252 | parseLabel(!Style.IndentGotoLabels); |
1925 | 252 | if (HasLabel) |
1926 | 171 | *HasLabel = true; |
1927 | 252 | return; |
1928 | 252 | } |
1929 | | // Recognize function-like macro usages without trailing semicolon as |
1930 | | // well as free-standing macros like Q_OBJECT. |
1931 | 31.9k | bool FunctionLike = FormatTok->is(tok::l_paren); |
1932 | 31.9k | if (FunctionLike) |
1933 | 14.1k | parseParens(); |
1934 | | |
1935 | 31.9k | bool FollowedByNewline = |
1936 | 31.9k | CommentsBeforeNextToken.empty() |
1937 | 31.9k | ? FormatTok->NewlinesBefore > 031.9k |
1938 | 31.9k | : CommentsBeforeNextToken.front()->NewlinesBefore > 012 ; |
1939 | | |
1940 | 31.9k | if (FollowedByNewline && (572 Text.size() >= 5572 || FunctionLike193 ) && |
1941 | 31.9k | tokenCanStartNewLine(*FormatTok)533 && Text == Text.upper()255 ) { |
1942 | 115 | PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro); |
1943 | 115 | addUnwrappedLine(); |
1944 | 115 | return; |
1945 | 115 | } |
1946 | 31.9k | } |
1947 | 126k | break; |
1948 | 126k | } |
1949 | 126k | case tok::equal: |
1950 | 18.1k | if ((Style.isJavaScript() || Style.isCSharp()16.4k ) && |
1951 | 18.1k | FormatTok->is(TT_FatArrow)1.83k ) { |
1952 | 102 | tryToParseChildBlock(); |
1953 | 102 | break; |
1954 | 102 | } |
1955 | | |
1956 | 18.0k | nextToken(); |
1957 | 18.0k | if (FormatTok->is(tok::l_brace)) { |
1958 | | // Block kind should probably be set to BK_BracedInit for any language. |
1959 | | // C# needs this change to ensure that array initialisers and object |
1960 | | // initialisers are indented the same way. |
1961 | 1.77k | if (Style.isCSharp()) |
1962 | 4 | FormatTok->setBlockKind(BK_BracedInit); |
1963 | 1.77k | nextToken(); |
1964 | 1.77k | parseBracedList(); |
1965 | 16.3k | } else if (Style.Language == FormatStyle::LK_Proto && |
1966 | 16.3k | FormatTok->is(tok::less)95 ) { |
1967 | 22 | nextToken(); |
1968 | 22 | parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, |
1969 | 22 | /*ClosingBraceKind=*/tok::greater); |
1970 | 22 | } |
1971 | 18.0k | break; |
1972 | 5.25k | case tok::l_square: |
1973 | 5.25k | parseSquare(); |
1974 | 5.25k | break; |
1975 | 509 | case tok::kw_new: |
1976 | 509 | parseNew(); |
1977 | 509 | break; |
1978 | 31 | case tok::kw_case: |
1979 | 31 | if (Style.isJavaScript() && Line->MustBeDeclaration4 ) { |
1980 | | // 'case: string' field declaration. |
1981 | 4 | nextToken(); |
1982 | 4 | break; |
1983 | 4 | } |
1984 | 27 | parseCaseLabel(); |
1985 | 27 | break; |
1986 | 164k | default: |
1987 | 164k | nextToken(); |
1988 | 164k | break; |
1989 | 487k | } |
1990 | 487k | } while (!eof()385k ); |
1991 | 112k | } |
1992 | | |
1993 | 16.5k | bool UnwrappedLineParser::tryToParsePropertyAccessor() { |
1994 | 16.5k | assert(FormatTok->is(tok::l_brace)); |
1995 | 16.5k | if (!Style.isCSharp()) |
1996 | 16.2k | return false; |
1997 | | // See if it's a property accessor. |
1998 | 296 | if (FormatTok->Previous->isNot(tok::identifier)) |
1999 | 204 | return false; |
2000 | | |
2001 | | // See if we are inside a property accessor. |
2002 | | // |
2003 | | // Record the current tokenPosition so that we can advance and |
2004 | | // reset the current token. `Next` is not set yet so we need |
2005 | | // another way to advance along the token stream. |
2006 | 92 | unsigned int StoredPosition = Tokens->getPosition(); |
2007 | 92 | FormatToken *Tok = Tokens->getNextToken(); |
2008 | | |
2009 | | // A trivial property accessor is of the form: |
2010 | | // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] } |
2011 | | // Track these as they do not require line breaks to be introduced. |
2012 | 92 | bool HasSpecialAccessor = false; |
2013 | 92 | bool IsTrivialPropertyAccessor = true; |
2014 | 334 | while (!eof()) { |
2015 | 334 | if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private, |
2016 | 334 | tok::kw_protected, Keywords.kw_internal, Keywords.kw_get, |
2017 | 334 | Keywords.kw_init, Keywords.kw_set)) { |
2018 | 242 | if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set)) |
2019 | 128 | HasSpecialAccessor = true; |
2020 | 242 | Tok = Tokens->getNextToken(); |
2021 | 242 | continue; |
2022 | 242 | } |
2023 | 92 | if (Tok->isNot(tok::r_brace)) |
2024 | 20 | IsTrivialPropertyAccessor = false; |
2025 | 92 | break; |
2026 | 334 | } |
2027 | | |
2028 | 92 | if (!HasSpecialAccessor) { |
2029 | 10 | Tokens->setPosition(StoredPosition); |
2030 | 10 | return false; |
2031 | 10 | } |
2032 | | |
2033 | | // Try to parse the property accessor: |
2034 | | // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties |
2035 | 82 | Tokens->setPosition(StoredPosition); |
2036 | 82 | if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction14 ) |
2037 | 2 | addUnwrappedLine(); |
2038 | 82 | nextToken(); |
2039 | 362 | do { |
2040 | 362 | switch (FormatTok->Tok.getKind()) { |
2041 | 82 | case tok::r_brace: |
2042 | 82 | nextToken(); |
2043 | 82 | if (FormatTok->is(tok::equal)) { |
2044 | 96 | while (!eof() && FormatTok->isNot(tok::semi)90 ) |
2045 | 78 | nextToken(); |
2046 | 18 | nextToken(); |
2047 | 18 | } |
2048 | 82 | addUnwrappedLine(); |
2049 | 82 | return true; |
2050 | 8 | case tok::l_brace: |
2051 | 8 | ++Line->Level; |
2052 | 8 | parseBlock(/*MustBeDeclaration=*/true); |
2053 | 8 | addUnwrappedLine(); |
2054 | 8 | --Line->Level; |
2055 | 8 | break; |
2056 | 20 | case tok::equal: |
2057 | 20 | if (FormatTok->is(TT_FatArrow)) { |
2058 | 20 | ++Line->Level; |
2059 | 60 | do { |
2060 | 60 | nextToken(); |
2061 | 60 | } while (!eof() && FormatTok->isNot(tok::semi)); |
2062 | 20 | nextToken(); |
2063 | 20 | addUnwrappedLine(); |
2064 | 20 | --Line->Level; |
2065 | 20 | break; |
2066 | 20 | } |
2067 | 0 | nextToken(); |
2068 | 0 | break; |
2069 | 252 | default: |
2070 | 252 | if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init, |
2071 | 252 | Keywords.kw_set) && |
2072 | 252 | !IsTrivialPropertyAccessor142 ) { |
2073 | | // Non-trivial get/set needs to be on its own line. |
2074 | 28 | addUnwrappedLine(); |
2075 | 28 | } |
2076 | 252 | nextToken(); |
2077 | 362 | } |
2078 | 362 | } while (!eof()280 ); |
2079 | | |
2080 | | // Unreachable for well-formed code (paired '{' and '}'). |
2081 | 0 | return true; |
2082 | 82 | } |
2083 | | |
2084 | 9.40k | bool UnwrappedLineParser::tryToParseLambda() { |
2085 | 9.40k | assert(FormatTok->is(tok::l_square)); |
2086 | 9.40k | if (!Style.isCpp()) { |
2087 | 658 | nextToken(); |
2088 | 658 | return false; |
2089 | 658 | } |
2090 | 8.75k | FormatToken &LSquare = *FormatTok; |
2091 | 8.75k | if (!tryToParseLambdaIntroducer()) |
2092 | 3.93k | return false; |
2093 | | |
2094 | 4.81k | bool SeenArrow = false; |
2095 | 4.81k | bool InTemplateParameterList = false; |
2096 | | |
2097 | 12.0k | while (FormatTok->isNot(tok::l_brace)) { |
2098 | 9.19k | if (FormatTok->isSimpleTypeSpecifier()) { |
2099 | 171 | nextToken(); |
2100 | 171 | continue; |
2101 | 171 | } |
2102 | 9.02k | switch (FormatTok->Tok.getKind()) { |
2103 | 0 | case tok::l_brace: |
2104 | 0 | break; |
2105 | 2.50k | case tok::l_paren: |
2106 | 2.50k | parseParens(); |
2107 | 2.50k | break; |
2108 | 72 | case tok::l_square: |
2109 | 72 | parseSquare(); |
2110 | 72 | break; |
2111 | 54 | case tok::kw_class: |
2112 | 72 | case tok::kw_template: |
2113 | 99 | case tok::kw_typename: |
2114 | 99 | assert(FormatTok->Previous); |
2115 | 99 | if (FormatTok->Previous->is(tok::less)) |
2116 | 72 | InTemplateParameterList = true; |
2117 | 99 | nextToken(); |
2118 | 99 | break; |
2119 | 42 | case tok::amp: |
2120 | 129 | case tok::star: |
2121 | 156 | case tok::kw_const: |
2122 | 198 | case tok::comma: |
2123 | 816 | case tok::less: |
2124 | 1.43k | case tok::greater: |
2125 | 2.33k | case tok::identifier: |
2126 | 3.00k | case tok::numeric_constant: |
2127 | 3.09k | case tok::coloncolon: |
2128 | 3.12k | case tok::kw_mutable: |
2129 | 3.14k | case tok::kw_noexcept: |
2130 | 3.14k | nextToken(); |
2131 | 3.14k | break; |
2132 | | // Specialization of a template with an integer parameter can contain |
2133 | | // arithmetic, logical, comparison and ternary operators. |
2134 | | // |
2135 | | // FIXME: This also accepts sequences of operators that are not in the scope |
2136 | | // of a template argument list. |
2137 | | // |
2138 | | // In a C++ lambda a template type can only occur after an arrow. We use |
2139 | | // this as an heuristic to distinguish between Objective-C expressions |
2140 | | // followed by an `a->b` expression, such as: |
2141 | | // ([obj func:arg] + a->b) |
2142 | | // Otherwise the code below would parse as a lambda. |
2143 | | // |
2144 | | // FIXME: This heuristic is incorrect for C++20 generic lambdas with |
2145 | | // explicit template lists: []<bool b = true && false>(U &&u){} |
2146 | 42 | case tok::plus: |
2147 | 72 | case tok::minus: |
2148 | 90 | case tok::exclaim: |
2149 | 108 | case tok::tilde: |
2150 | 138 | case tok::slash: |
2151 | 168 | case tok::percent: |
2152 | 198 | case tok::lessless: |
2153 | 228 | case tok::pipe: |
2154 | 258 | case tok::pipepipe: |
2155 | 312 | case tok::ampamp: |
2156 | 318 | case tok::caret: |
2157 | 372 | case tok::equalequal: |
2158 | 402 | case tok::exclaimequal: |
2159 | 432 | case tok::greaterequal: |
2160 | 462 | case tok::lessequal: |
2161 | 495 | case tok::question: |
2162 | 557 | case tok::colon: |
2163 | 611 | case tok::ellipsis: |
2164 | 647 | case tok::kw_true: |
2165 | 701 | case tok::kw_false: |
2166 | 701 | if (SeenArrow || InTemplateParameterList305 ) { |
2167 | 432 | nextToken(); |
2168 | 432 | break; |
2169 | 432 | } |
2170 | 269 | return true; |
2171 | 861 | case tok::arrow: |
2172 | | // This might or might not actually be a lambda arrow (this could be an |
2173 | | // ObjC method invocation followed by a dereferencing arrow). We might |
2174 | | // reset this back to TT_Unknown in TokenAnnotator. |
2175 | 861 | FormatTok->setFinalizedType(TT_LambdaArrow); |
2176 | 861 | SeenArrow = true; |
2177 | 861 | nextToken(); |
2178 | 861 | break; |
2179 | 1.64k | default: |
2180 | 1.64k | return true; |
2181 | 9.02k | } |
2182 | 9.02k | } |
2183 | 2.90k | FormatTok->setFinalizedType(TT_LambdaLBrace); |
2184 | 2.90k | LSquare.setFinalizedType(TT_LambdaLSquare); |
2185 | 2.90k | parseChildBlock(); |
2186 | 2.90k | return true; |
2187 | 4.81k | } |
2188 | | |
2189 | 8.75k | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
2190 | 8.75k | const FormatToken *Previous = FormatTok->Previous; |
2191 | 8.75k | const FormatToken *LeftSquare = FormatTok; |
2192 | 8.75k | nextToken(); |
2193 | 8.75k | if (Previous && |
2194 | 8.75k | (7.15k Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, |
2195 | 7.15k | tok::kw_delete, tok::l_square) || |
2196 | 7.15k | LeftSquare->isCppStructuredBinding(Style)4.77k || Previous->closesScope()4.51k || |
2197 | 7.15k | Previous->isSimpleTypeSpecifier()3.87k )) { |
2198 | 3.34k | return false; |
2199 | 3.34k | } |
2200 | 5.41k | if (FormatTok->is(tok::l_square)) |
2201 | 559 | return false; |
2202 | 4.85k | if (FormatTok->is(tok::r_square)) { |
2203 | 2.34k | const FormatToken *Next = Tokens->peekNextToken(); |
2204 | 2.34k | if (Next->is(tok::greater)) |
2205 | 36 | return false; |
2206 | 2.34k | } |
2207 | 4.81k | parseSquare(/*LambdaIntroducer=*/true); |
2208 | 4.81k | return true; |
2209 | 4.85k | } |
2210 | | |
2211 | 298 | void UnwrappedLineParser::tryToParseJSFunction() { |
2212 | 298 | assert(FormatTok->is(Keywords.kw_function) || |
2213 | 298 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); |
2214 | 298 | if (FormatTok->is(Keywords.kw_async)) |
2215 | 0 | nextToken(); |
2216 | | // Consume "function". |
2217 | 298 | nextToken(); |
2218 | | |
2219 | | // Consume * (generator function). Treat it like C++'s overloaded operators. |
2220 | 298 | if (FormatTok->is(tok::star)) { |
2221 | 4 | FormatTok->setFinalizedType(TT_OverloadedOperator); |
2222 | 4 | nextToken(); |
2223 | 4 | } |
2224 | | |
2225 | | // Consume function name. |
2226 | 298 | if (FormatTok->is(tok::identifier)) |
2227 | 52 | nextToken(); |
2228 | | |
2229 | 298 | if (FormatTok->isNot(tok::l_paren)) |
2230 | 0 | return; |
2231 | | |
2232 | | // Parse formal parameter list. |
2233 | 298 | parseParens(); |
2234 | | |
2235 | 298 | if (FormatTok->is(tok::colon)) { |
2236 | | // Parse a type definition. |
2237 | 16 | nextToken(); |
2238 | | |
2239 | | // Eat the type declaration. For braced inline object types, balance braces, |
2240 | | // otherwise just parse until finding an l_brace for the function body. |
2241 | 16 | if (FormatTok->is(tok::l_brace)) |
2242 | 8 | tryToParseBracedList(); |
2243 | 8 | else |
2244 | 16 | while (8 !FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()8 ) |
2245 | 8 | nextToken(); |
2246 | 16 | } |
2247 | | |
2248 | 298 | if (FormatTok->is(tok::semi)) |
2249 | 16 | return; |
2250 | | |
2251 | 282 | parseChildBlock(); |
2252 | 282 | } |
2253 | | |
2254 | 20.1k | bool UnwrappedLineParser::tryToParseBracedList() { |
2255 | 20.1k | if (FormatTok->is(BK_Unknown)) |
2256 | 18.0k | calculateBraceTypes(); |
2257 | 20.1k | assert(FormatTok->isNot(BK_Unknown)); |
2258 | 20.1k | if (FormatTok->is(BK_Block)) |
2259 | 16.8k | return false; |
2260 | 3.30k | nextToken(); |
2261 | 3.30k | parseBracedList(); |
2262 | 3.30k | return true; |
2263 | 20.1k | } |
2264 | | |
2265 | 164 | bool UnwrappedLineParser::tryToParseChildBlock() { |
2266 | 164 | assert(Style.isJavaScript() || Style.isCSharp()); |
2267 | 0 | assert(FormatTok->is(TT_FatArrow)); |
2268 | | // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow. |
2269 | | // They always start an expression or a child block if followed by a curly |
2270 | | // brace. |
2271 | 0 | nextToken(); |
2272 | 164 | if (FormatTok->isNot(tok::l_brace)) |
2273 | 66 | return false; |
2274 | 98 | parseChildBlock(); |
2275 | 98 | return true; |
2276 | 164 | } |
2277 | | |
2278 | | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, |
2279 | | bool IsEnum, |
2280 | 13.1k | tok::TokenKind ClosingBraceKind) { |
2281 | 13.1k | bool HasError = false; |
2282 | | |
2283 | | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
2284 | | // replace this by using parseAssignmentExpression() inside. |
2285 | 65.5k | do { |
2286 | 65.5k | if (Style.isCSharp() && FormatTok->is(TT_FatArrow)482 && |
2287 | 65.5k | tryToParseChildBlock()10 ) { |
2288 | 4 | continue; |
2289 | 4 | } |
2290 | 65.5k | if (Style.isJavaScript()) { |
2291 | 4.00k | if (FormatTok->is(Keywords.kw_function) || |
2292 | 4.00k | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)3.94k ) { |
2293 | 60 | tryToParseJSFunction(); |
2294 | 60 | continue; |
2295 | 60 | } |
2296 | 3.94k | if (FormatTok->is(tok::l_brace)) { |
2297 | | // Could be a method inside of a braced list `{a() { return 1; }}`. |
2298 | 36 | if (tryToParseBracedList()) |
2299 | 28 | continue; |
2300 | 8 | parseChildBlock(); |
2301 | 8 | } |
2302 | 3.94k | } |
2303 | 65.4k | if (FormatTok->Tok.getKind() == ClosingBraceKind) { |
2304 | 12.4k | if (IsEnum && !Style.AllowShortEnumsOnASingleLine2.50k ) |
2305 | 107 | addUnwrappedLine(); |
2306 | 12.4k | nextToken(); |
2307 | 12.4k | return !HasError; |
2308 | 12.4k | } |
2309 | 52.9k | switch (FormatTok->Tok.getKind()) { |
2310 | 680 | case tok::l_square: |
2311 | 680 | if (Style.isCSharp()) |
2312 | 8 | parseSquare(); |
2313 | 672 | else |
2314 | 672 | tryToParseLambda(); |
2315 | 680 | break; |
2316 | 733 | case tok::l_paren: |
2317 | 733 | parseParens(); |
2318 | | // JavaScript can just have free standing methods and getters/setters in |
2319 | | // object literals. Detect them by a "{" following ")". |
2320 | 733 | if (Style.isJavaScript()) { |
2321 | 40 | if (FormatTok->is(tok::l_brace)) |
2322 | 24 | parseChildBlock(); |
2323 | 40 | break; |
2324 | 40 | } |
2325 | 693 | break; |
2326 | 2.93k | case tok::l_brace: |
2327 | | // Assume there are no blocks inside a braced init list apart |
2328 | | // from the ones we explicitly parse out (like lambdas). |
2329 | 2.93k | FormatTok->setBlockKind(BK_BracedInit); |
2330 | 2.93k | nextToken(); |
2331 | 2.93k | parseBracedList(); |
2332 | 2.93k | break; |
2333 | 492 | case tok::less: |
2334 | 492 | if (Style.Language == FormatStyle::LK_Proto || |
2335 | 492 | ClosingBraceKind == tok::greater436 ) { |
2336 | 220 | nextToken(); |
2337 | 220 | parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, |
2338 | 220 | /*ClosingBraceKind=*/tok::greater); |
2339 | 272 | } else { |
2340 | 272 | nextToken(); |
2341 | 272 | } |
2342 | 492 | break; |
2343 | 96 | case tok::semi: |
2344 | | // JavaScript (or more precisely TypeScript) can have semicolons in braced |
2345 | | // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be |
2346 | | // used for error recovery if we have otherwise determined that this is |
2347 | | // a braced list. |
2348 | 96 | if (Style.isJavaScript()) { |
2349 | 36 | nextToken(); |
2350 | 36 | break; |
2351 | 36 | } |
2352 | 60 | HasError = true; |
2353 | 60 | if (!ContinueOnSemicolons) |
2354 | 33 | return !HasError; |
2355 | 27 | nextToken(); |
2356 | 27 | break; |
2357 | 14.3k | case tok::comma: |
2358 | 14.3k | nextToken(); |
2359 | 14.3k | if (IsEnum && !Style.AllowShortEnumsOnASingleLine2.12k ) |
2360 | 183 | addUnwrappedLine(); |
2361 | 14.3k | break; |
2362 | 33.7k | default: |
2363 | 33.7k | nextToken(); |
2364 | 33.7k | break; |
2365 | 52.9k | } |
2366 | 53.0k | } while (!eof()); |
2367 | 628 | return false; |
2368 | 13.1k | } |
2369 | | |
2370 | | /// \brief Parses a pair of parentheses (and everything between them). |
2371 | | /// \param AmpAmpTokenType If different than TT_Unknown sets this type for all |
2372 | | /// double ampersands. This only counts for the current parens scope. |
2373 | 82.1k | void UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { |
2374 | 82.1k | assert(FormatTok->is(tok::l_paren) && "'(' expected."); |
2375 | 0 | nextToken(); |
2376 | 233k | do { |
2377 | 233k | switch (FormatTok->Tok.getKind()) { |
2378 | 7.59k | case tok::l_paren: |
2379 | 7.59k | parseParens(); |
2380 | 7.59k | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)22 ) |
2381 | 10 | parseChildBlock(); |
2382 | 7.59k | break; |
2383 | 82.1k | case tok::r_paren: |
2384 | 82.1k | nextToken(); |
2385 | 82.1k | return; |
2386 | 3 | case tok::r_brace: |
2387 | | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
2388 | 3 | return; |
2389 | 2.24k | case tok::l_square: |
2390 | 2.24k | tryToParseLambda(); |
2391 | 2.24k | break; |
2392 | 1.43k | case tok::l_brace: |
2393 | 1.43k | if (!tryToParseBracedList()) |
2394 | 393 | parseChildBlock(); |
2395 | 1.43k | break; |
2396 | 42 | case tok::at: |
2397 | 42 | nextToken(); |
2398 | 42 | if (FormatTok->is(tok::l_brace)) { |
2399 | 6 | nextToken(); |
2400 | 6 | parseBracedList(); |
2401 | 6 | } |
2402 | 42 | break; |
2403 | 2.16k | case tok::equal: |
2404 | 2.16k | if (Style.isCSharp() && FormatTok->is(TT_FatArrow)84 ) |
2405 | 52 | tryToParseChildBlock(); |
2406 | 2.11k | else |
2407 | 2.11k | nextToken(); |
2408 | 2.16k | break; |
2409 | 112 | case tok::kw_class: |
2410 | 112 | if (Style.isJavaScript()) |
2411 | 4 | parseRecord(/*ParseAsExpr=*/true); |
2412 | 108 | else |
2413 | 108 | nextToken(); |
2414 | 112 | break; |
2415 | 69.6k | case tok::identifier: |
2416 | 69.6k | if (Style.isJavaScript() && |
2417 | 69.6k | (1.70k FormatTok->is(Keywords.kw_function)1.70k || |
2418 | 1.70k | FormatTok->startsSequence(Keywords.kw_async, |
2419 | 1.56k | Keywords.kw_function))) { |
2420 | 140 | tryToParseJSFunction(); |
2421 | 69.5k | } else { |
2422 | 69.5k | nextToken(); |
2423 | 69.5k | } |
2424 | 69.6k | break; |
2425 | 98 | case tok::kw_requires: { |
2426 | 98 | auto RequiresToken = FormatTok; |
2427 | 98 | nextToken(); |
2428 | 98 | parseRequiresExpression(RequiresToken); |
2429 | 98 | break; |
2430 | 0 | } |
2431 | 1.26k | case tok::ampamp: |
2432 | 1.26k | if (AmpAmpTokenType != TT_Unknown) |
2433 | 59 | FormatTok->setFinalizedType(AmpAmpTokenType); |
2434 | 1.26k | LLVM_FALLTHROUGH; |
2435 | 68.3k | default: |
2436 | 68.3k | nextToken(); |
2437 | 68.3k | break; |
2438 | 233k | } |
2439 | 233k | } while (!eof()151k ); |
2440 | 82.1k | } |
2441 | | |
2442 | 11.2k | void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { |
2443 | 11.2k | if (!LambdaIntroducer) { |
2444 | 6.42k | assert(FormatTok->is(tok::l_square) && "'[' expected."); |
2445 | 6.42k | if (tryToParseLambda()) |
2446 | 2.93k | return; |
2447 | 6.42k | } |
2448 | 25.1k | do 8.30k { |
2449 | 25.1k | switch (FormatTok->Tok.getKind()) { |
2450 | 592 | case tok::l_paren: |
2451 | 592 | parseParens(); |
2452 | 592 | break; |
2453 | 8.29k | case tok::r_square: |
2454 | 8.29k | nextToken(); |
2455 | 8.29k | return; |
2456 | 0 | case tok::r_brace: |
2457 | | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
2458 | 0 | return; |
2459 | 766 | case tok::l_square: |
2460 | 766 | parseSquare(); |
2461 | 766 | break; |
2462 | 384 | case tok::l_brace: { |
2463 | 384 | if (!tryToParseBracedList()) |
2464 | 330 | parseChildBlock(); |
2465 | 384 | break; |
2466 | 0 | } |
2467 | 143 | case tok::at: |
2468 | 143 | nextToken(); |
2469 | 143 | if (FormatTok->is(tok::l_brace)) { |
2470 | 39 | nextToken(); |
2471 | 39 | parseBracedList(); |
2472 | 39 | } |
2473 | 143 | break; |
2474 | 14.9k | default: |
2475 | 14.9k | nextToken(); |
2476 | 14.9k | break; |
2477 | 25.1k | } |
2478 | 25.1k | } while (!eof()16.8k ); |
2479 | 8.30k | } |
2480 | | |
2481 | 11.4k | void UnwrappedLineParser::keepAncestorBraces() { |
2482 | 11.4k | if (!Style.RemoveBracesLLVM) |
2483 | 10.0k | return; |
2484 | | |
2485 | 1.36k | const int MaxNestingLevels = 2; |
2486 | 1.36k | const int Size = NestedTooDeep.size(); |
2487 | 1.36k | if (Size >= MaxNestingLevels) |
2488 | 76 | NestedTooDeep[Size - MaxNestingLevels] = true; |
2489 | 1.36k | NestedTooDeep.push_back(false); |
2490 | 1.36k | } |
2491 | | |
2492 | 130 | static FormatToken *getLastNonComment(const UnwrappedLine &Line) { |
2493 | 130 | for (const auto &Token : llvm::reverse(Line.Tokens)) |
2494 | 134 | if (Token.Tok->isNot(tok::comment)) |
2495 | 130 | return Token.Tok; |
2496 | | |
2497 | 0 | return nullptr; |
2498 | 130 | } |
2499 | | |
2500 | 5.54k | void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) { |
2501 | 5.54k | FormatToken *Tok = nullptr; |
2502 | | |
2503 | 5.54k | if (Style.InsertBraces && !Line->InPPDirective102 && !Line->Tokens.empty()90 && |
2504 | 5.54k | PreprocessorDirectives.empty()90 ) { |
2505 | 66 | Tok = getLastNonComment(*Line); |
2506 | 66 | assert(Tok); |
2507 | 66 | if (Tok->BraceCount < 0) { |
2508 | 2 | assert(Tok->BraceCount == -1); |
2509 | 0 | Tok = nullptr; |
2510 | 64 | } else { |
2511 | 64 | Tok->BraceCount = -1; |
2512 | 64 | } |
2513 | 66 | } |
2514 | | |
2515 | 0 | addUnwrappedLine(); |
2516 | 5.54k | ++Line->Level; |
2517 | 5.54k | parseStructuralElement(); |
2518 | | |
2519 | 5.54k | if (Tok) { |
2520 | 64 | assert(!Line->InPPDirective); |
2521 | 0 | Tok = nullptr; |
2522 | 68 | for (const auto &L : llvm::reverse(*CurrentLines)) { |
2523 | 68 | if (!L.InPPDirective && getLastNonComment(L)64 ) { |
2524 | 64 | Tok = L.Tokens.back().Tok; |
2525 | 64 | break; |
2526 | 64 | } |
2527 | 68 | } |
2528 | 64 | assert(Tok); |
2529 | 0 | ++Tok->BraceCount; |
2530 | 64 | } |
2531 | | |
2532 | 5.54k | if (CheckEOF && FormatTok->is(tok::eof)858 ) |
2533 | 773 | addUnwrappedLine(); |
2534 | | |
2535 | 5.54k | --Line->Level; |
2536 | 5.54k | } |
2537 | | |
2538 | 844 | static void markOptionalBraces(FormatToken *LeftBrace) { |
2539 | 844 | if (!LeftBrace) |
2540 | 684 | return; |
2541 | | |
2542 | 160 | assert(LeftBrace->is(tok::l_brace)); |
2543 | | |
2544 | 0 | FormatToken *RightBrace = LeftBrace->MatchingParen; |
2545 | 160 | if (!RightBrace) { |
2546 | 36 | assert(!LeftBrace->Optional); |
2547 | 0 | return; |
2548 | 36 | } |
2549 | | |
2550 | 124 | assert(RightBrace->is(tok::r_brace)); |
2551 | 0 | assert(RightBrace->MatchingParen == LeftBrace); |
2552 | 0 | assert(LeftBrace->Optional == RightBrace->Optional); |
2553 | | |
2554 | 0 | LeftBrace->Optional = true; |
2555 | 124 | RightBrace->Optional = true; |
2556 | 124 | } |
2557 | | |
2558 | 13.3k | void UnwrappedLineParser::handleAttributes() { |
2559 | | // Handle AttributeMacro, e.g. `if (x) UNLIKELY`. |
2560 | 13.3k | if (FormatTok->is(TT_AttributeMacro)) |
2561 | 90 | nextToken(); |
2562 | 13.3k | handleCppAttributes(); |
2563 | 13.3k | } |
2564 | | |
2565 | 15.0k | bool UnwrappedLineParser::handleCppAttributes() { |
2566 | | // Handle [[likely]] / [[unlikely]] attributes. |
2567 | 15.0k | if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()1.85k ) { |
2568 | 280 | parseSquare(); |
2569 | 280 | return true; |
2570 | 280 | } |
2571 | 14.7k | return false; |
2572 | 15.0k | } |
2573 | | |
2574 | | FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind, |
2575 | 7.43k | bool KeepBraces) { |
2576 | 7.43k | assert(FormatTok->is(tok::kw_if) && "'if' expected"); |
2577 | 0 | nextToken(); |
2578 | 7.43k | if (FormatTok->is(tok::exclaim)) |
2579 | 78 | nextToken(); |
2580 | | |
2581 | 7.43k | bool KeepIfBraces = true; |
2582 | 7.43k | if (FormatTok->is(tok::kw_consteval)) { |
2583 | 144 | nextToken(); |
2584 | 7.29k | } else { |
2585 | 7.29k | KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces1.16k ; |
2586 | 7.29k | if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier)) |
2587 | 666 | nextToken(); |
2588 | 7.29k | if (FormatTok->is(tok::l_paren)) |
2589 | 7.23k | parseParens(); |
2590 | 7.29k | } |
2591 | 7.43k | handleAttributes(); |
2592 | | |
2593 | 7.43k | bool NeedsUnwrappedLine = false; |
2594 | 7.43k | keepAncestorBraces(); |
2595 | | |
2596 | 7.43k | FormatToken *IfLeftBrace = nullptr; |
2597 | 7.43k | IfStmtKind IfBlockKind = IfStmtKind::NotIf; |
2598 | | |
2599 | 7.43k | if (Keywords.isBlockBegin(*FormatTok, Style)) { |
2600 | 4.04k | FormatTok->setFinalizedType(TT_ControlStatementLBrace); |
2601 | 4.04k | IfLeftBrace = FormatTok; |
2602 | 4.04k | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
2603 | 4.04k | parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, |
2604 | 4.04k | /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind); |
2605 | 4.04k | if (Style.BraceWrapping.BeforeElse) |
2606 | 293 | addUnwrappedLine(); |
2607 | 3.75k | else |
2608 | 3.75k | NeedsUnwrappedLine = true; |
2609 | 4.04k | } else { |
2610 | 3.39k | parseUnbracedBody(); |
2611 | 3.39k | } |
2612 | | |
2613 | 7.43k | if (Style.RemoveBracesLLVM) { |
2614 | 1.20k | assert(!NestedTooDeep.empty()); |
2615 | 1.20k | KeepIfBraces = KeepIfBraces || |
2616 | 1.20k | (1.09k IfLeftBrace1.09k && !IfLeftBrace->MatchingParen668 ) || |
2617 | 1.20k | NestedTooDeep.back()710 || IfBlockKind == IfStmtKind::IfOnly646 || |
2618 | 1.20k | IfBlockKind == IfStmtKind::IfElseIf598 ; |
2619 | 1.20k | } |
2620 | | |
2621 | 0 | bool KeepElseBraces = KeepIfBraces; |
2622 | 7.43k | FormatToken *ElseLeftBrace = nullptr; |
2623 | 7.43k | IfStmtKind Kind = IfStmtKind::IfOnly; |
2624 | | |
2625 | 7.43k | if (FormatTok->is(tok::kw_else)) { |
2626 | 3.26k | if (Style.RemoveBracesLLVM) { |
2627 | 612 | NestedTooDeep.back() = false; |
2628 | 612 | Kind = IfStmtKind::IfElse; |
2629 | 612 | } |
2630 | 3.26k | nextToken(); |
2631 | 3.26k | handleAttributes(); |
2632 | 3.26k | if (Keywords.isBlockBegin(*FormatTok, Style)) { |
2633 | 1.19k | const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if); |
2634 | 1.19k | FormatTok->setFinalizedType(TT_ElseLBrace); |
2635 | 1.19k | ElseLeftBrace = FormatTok; |
2636 | 1.19k | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
2637 | 1.19k | IfStmtKind ElseBlockKind = IfStmtKind::NotIf; |
2638 | 1.19k | FormatToken *IfLBrace = |
2639 | 1.19k | parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, |
2640 | 1.19k | /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind); |
2641 | 1.19k | if (FormatTok->is(tok::kw_else)) { |
2642 | 63 | KeepElseBraces = KeepElseBraces || |
2643 | 63 | ElseBlockKind == IfStmtKind::IfOnly36 || |
2644 | 63 | ElseBlockKind == IfStmtKind::IfElseIf12 ; |
2645 | 1.13k | } else if (FollowedByIf && IfLBrace52 && !IfLBrace->Optional14 ) { |
2646 | 12 | KeepElseBraces = true; |
2647 | 12 | assert(ElseLeftBrace->MatchingParen); |
2648 | 0 | markOptionalBraces(ElseLeftBrace); |
2649 | 12 | } |
2650 | 0 | addUnwrappedLine(); |
2651 | 2.06k | } else if (FormatTok->is(tok::kw_if)) { |
2652 | 1.20k | const FormatToken *Previous = Tokens->getPreviousToken(); |
2653 | 1.20k | assert(Previous); |
2654 | 0 | const bool IsPrecededByComment = Previous->is(tok::comment); |
2655 | 1.20k | if (IsPrecededByComment) { |
2656 | 9 | addUnwrappedLine(); |
2657 | 9 | ++Line->Level; |
2658 | 9 | } |
2659 | 1.20k | bool TooDeep = true; |
2660 | 1.20k | if (Style.RemoveBracesLLVM) { |
2661 | 236 | Kind = IfStmtKind::IfElseIf; |
2662 | 236 | TooDeep = NestedTooDeep.pop_back_val(); |
2663 | 236 | } |
2664 | 1.20k | ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces); |
2665 | 1.20k | if (Style.RemoveBracesLLVM) |
2666 | 236 | NestedTooDeep.push_back(TooDeep); |
2667 | 1.20k | if (IsPrecededByComment) |
2668 | 9 | --Line->Level; |
2669 | 1.20k | } else { |
2670 | 858 | parseUnbracedBody(/*CheckEOF=*/true); |
2671 | 858 | } |
2672 | 4.17k | } else { |
2673 | 4.17k | KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse204 ; |
2674 | 4.17k | if (NeedsUnwrappedLine) |
2675 | 2.24k | addUnwrappedLine(); |
2676 | 4.17k | } |
2677 | | |
2678 | 7.43k | if (!Style.RemoveBracesLLVM) |
2679 | 6.23k | return nullptr; |
2680 | | |
2681 | 1.20k | assert(!NestedTooDeep.empty()); |
2682 | 1.20k | KeepElseBraces = KeepElseBraces || |
2683 | 1.20k | (542 ElseLeftBrace542 && !ElseLeftBrace->MatchingParen176 ) || |
2684 | 1.20k | NestedTooDeep.back()396 ; |
2685 | | |
2686 | 1.20k | NestedTooDeep.pop_back(); |
2687 | | |
2688 | 1.20k | if (!KeepIfBraces && !KeepElseBraces576 ) { |
2689 | 384 | markOptionalBraces(IfLeftBrace); |
2690 | 384 | markOptionalBraces(ElseLeftBrace); |
2691 | 816 | } else if (IfLeftBrace) { |
2692 | 720 | FormatToken *IfRightBrace = IfLeftBrace->MatchingParen; |
2693 | 720 | if (IfRightBrace) { |
2694 | 230 | assert(IfRightBrace->MatchingParen == IfLeftBrace); |
2695 | 0 | assert(!IfLeftBrace->Optional); |
2696 | 0 | assert(!IfRightBrace->Optional); |
2697 | 0 | IfLeftBrace->MatchingParen = nullptr; |
2698 | 230 | IfRightBrace->MatchingParen = nullptr; |
2699 | 230 | } |
2700 | 720 | } |
2701 | | |
2702 | 1.20k | if (IfKind) |
2703 | 174 | *IfKind = Kind; |
2704 | | |
2705 | 1.20k | return IfLeftBrace; |
2706 | 7.43k | } |
2707 | | |
2708 | 355 | void UnwrappedLineParser::parseTryCatch() { |
2709 | 355 | assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); |
2710 | 0 | nextToken(); |
2711 | 355 | bool NeedsUnwrappedLine = false; |
2712 | 355 | if (FormatTok->is(tok::colon)) { |
2713 | | // We are in a function try block, what comes is an initializer list. |
2714 | 44 | nextToken(); |
2715 | | |
2716 | | // In case identifiers were removed by clang-tidy, what might follow is |
2717 | | // multiple commas in sequence - before the first identifier. |
2718 | 50 | while (FormatTok->is(tok::comma)) |
2719 | 6 | nextToken(); |
2720 | | |
2721 | 130 | while (FormatTok->is(tok::identifier)) { |
2722 | 86 | nextToken(); |
2723 | 86 | if (FormatTok->is(tok::l_paren)) |
2724 | 41 | parseParens(); |
2725 | 86 | if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) && |
2726 | 86 | FormatTok->is(tok::l_brace)45 ) { |
2727 | 90 | do { |
2728 | 90 | nextToken(); |
2729 | 90 | } while (!FormatTok->is(tok::r_brace)); |
2730 | 45 | nextToken(); |
2731 | 45 | } |
2732 | | |
2733 | | // In case identifiers were removed by clang-tidy, what might follow is |
2734 | | // multiple commas in sequence - after the first identifier. |
2735 | 135 | while (FormatTok->is(tok::comma)) |
2736 | 49 | nextToken(); |
2737 | 86 | } |
2738 | 44 | } |
2739 | | // Parse try with resource. |
2740 | 355 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)12 ) |
2741 | 4 | parseParens(); |
2742 | | |
2743 | 355 | keepAncestorBraces(); |
2744 | | |
2745 | 355 | if (FormatTok->is(tok::l_brace)) { |
2746 | 349 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
2747 | 349 | parseBlock(); |
2748 | 349 | if (Style.BraceWrapping.BeforeCatch) |
2749 | 118 | addUnwrappedLine(); |
2750 | 231 | else |
2751 | 231 | NeedsUnwrappedLine = true; |
2752 | 349 | } else if (6 !FormatTok->is(tok::kw_catch)6 ) { |
2753 | | // The C++ standard requires a compound-statement after a try. |
2754 | | // If there's none, we try to assume there's a structuralElement |
2755 | | // and try to continue. |
2756 | 6 | addUnwrappedLine(); |
2757 | 6 | ++Line->Level; |
2758 | 6 | parseStructuralElement(); |
2759 | 6 | --Line->Level; |
2760 | 6 | } |
2761 | 719 | while (true) { |
2762 | 719 | if (FormatTok->is(tok::at)) |
2763 | 28 | nextToken(); |
2764 | 719 | if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, |
2765 | 719 | tok::kw___finally) || |
2766 | 719 | (382 (382 Style.Language == FormatStyle::LK_Java382 || Style.isJavaScript()366 ) && |
2767 | 382 | FormatTok->is(Keywords.kw_finally)26 ) || |
2768 | 719 | (374 FormatTok->isObjCAtKeyword(tok::objc_catch)374 || |
2769 | 374 | FormatTok->isObjCAtKeyword(tok::objc_finally)))) { |
2770 | 352 | break; |
2771 | 352 | } |
2772 | 367 | nextToken(); |
2773 | 704 | while (FormatTok->isNot(tok::l_brace)) { |
2774 | 340 | if (FormatTok->is(tok::l_paren)) { |
2775 | 319 | parseParens(); |
2776 | 319 | continue; |
2777 | 319 | } |
2778 | 21 | if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) { |
2779 | 3 | if (Style.RemoveBracesLLVM) |
2780 | 0 | NestedTooDeep.pop_back(); |
2781 | 3 | return; |
2782 | 3 | } |
2783 | 18 | nextToken(); |
2784 | 18 | } |
2785 | 364 | NeedsUnwrappedLine = false; |
2786 | 364 | Line->MustBeDeclaration = false; |
2787 | 364 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
2788 | 364 | parseBlock(); |
2789 | 364 | if (Style.BraceWrapping.BeforeCatch) |
2790 | 118 | addUnwrappedLine(); |
2791 | 246 | else |
2792 | 246 | NeedsUnwrappedLine = true; |
2793 | 364 | } |
2794 | | |
2795 | 352 | if (Style.RemoveBracesLLVM) |
2796 | 0 | NestedTooDeep.pop_back(); |
2797 | | |
2798 | 352 | if (NeedsUnwrappedLine) |
2799 | 228 | addUnwrappedLine(); |
2800 | 352 | } |
2801 | | |
2802 | 4.24k | void UnwrappedLineParser::parseNamespace() { |
2803 | 4.24k | assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) && |
2804 | 4.24k | "'namespace' expected"); |
2805 | | |
2806 | 0 | const FormatToken &InitialToken = *FormatTok; |
2807 | 4.24k | nextToken(); |
2808 | 4.24k | if (InitialToken.is(TT_NamespaceMacro)) { |
2809 | 160 | parseParens(); |
2810 | 4.08k | } else { |
2811 | 8.20k | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline, |
2812 | 8.20k | tok::l_square, tok::period, tok::l_paren) || |
2813 | 8.20k | (4.08k Style.isCSharp()4.08k && FormatTok->is(tok::kw_union)28 )) { |
2814 | 4.12k | if (FormatTok->is(tok::l_square)) |
2815 | 16 | parseSquare(); |
2816 | 4.10k | else if (FormatTok->is(tok::l_paren)) |
2817 | 41 | parseParens(); |
2818 | 4.06k | else |
2819 | 4.06k | nextToken(); |
2820 | 4.12k | } |
2821 | 4.08k | } |
2822 | 4.24k | if (FormatTok->is(tok::l_brace)) { |
2823 | 4.23k | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
2824 | 220 | addUnwrappedLine(); |
2825 | | |
2826 | 4.23k | unsigned AddLevels = |
2827 | 4.23k | Style.NamespaceIndentation == FormatStyle::NI_All || |
2828 | 4.23k | (3.99k Style.NamespaceIndentation == FormatStyle::NI_Inner3.99k && |
2829 | 3.99k | DeclarationScopeStack.size() > 1139 ) |
2830 | 4.23k | ? 1u302 |
2831 | 4.23k | : 0u3.92k ; |
2832 | 4.23k | bool ManageWhitesmithsBraces = |
2833 | 4.23k | AddLevels == 0u && |
2834 | 4.23k | Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths3.92k ; |
2835 | | |
2836 | | // If we're in Whitesmiths mode, indent the brace if we're not indenting |
2837 | | // the whole block. |
2838 | 4.23k | if (ManageWhitesmithsBraces) |
2839 | 36 | ++Line->Level; |
2840 | | |
2841 | 4.23k | parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true, |
2842 | 4.23k | /*KeepBraces=*/true, /*IfKind=*/nullptr, |
2843 | 4.23k | ManageWhitesmithsBraces); |
2844 | | |
2845 | | // Munch the semicolon after a namespace. This is more common than one would |
2846 | | // think. Putting the semicolon into its own line is very ugly. |
2847 | 4.23k | if (FormatTok->is(tok::semi)) |
2848 | 1 | nextToken(); |
2849 | | |
2850 | 4.23k | addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove302 : LineLevel::Keep3.92k ); |
2851 | | |
2852 | 4.23k | if (ManageWhitesmithsBraces) |
2853 | 36 | --Line->Level; |
2854 | 4.23k | } |
2855 | | // FIXME: Add error handling. |
2856 | 4.24k | } |
2857 | | |
2858 | 509 | void UnwrappedLineParser::parseNew() { |
2859 | 509 | assert(FormatTok->is(tok::kw_new) && "'new' expected"); |
2860 | 0 | nextToken(); |
2861 | | |
2862 | 509 | if (Style.isCSharp()) { |
2863 | 128 | do { |
2864 | 128 | if (FormatTok->is(tok::l_brace)) |
2865 | 18 | parseBracedList(); |
2866 | | |
2867 | 128 | if (FormatTok->isOneOf(tok::semi, tok::comma)) |
2868 | 20 | return; |
2869 | | |
2870 | 108 | nextToken(); |
2871 | 108 | } while (!eof()); |
2872 | 32 | } |
2873 | | |
2874 | 489 | if (Style.Language != FormatStyle::LK_Java) |
2875 | 479 | return; |
2876 | | |
2877 | | // In Java, we can parse everything up to the parens, which aren't optional. |
2878 | 32 | do 10 { |
2879 | | // There should not be a ;, { or } before the new's open paren. |
2880 | 32 | if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) |
2881 | 6 | return; |
2882 | | |
2883 | | // Consume the parens. |
2884 | 26 | if (FormatTok->is(tok::l_paren)) { |
2885 | 4 | parseParens(); |
2886 | | |
2887 | | // If there is a class body of an anonymous class, consume that as child. |
2888 | 4 | if (FormatTok->is(tok::l_brace)) |
2889 | 4 | parseChildBlock(); |
2890 | 4 | return; |
2891 | 4 | } |
2892 | 22 | nextToken(); |
2893 | 22 | } while (!eof()); |
2894 | 10 | } |
2895 | | |
2896 | 2.98k | void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) { |
2897 | 2.98k | keepAncestorBraces(); |
2898 | | |
2899 | 2.98k | if (Keywords.isBlockBegin(*FormatTok, Style)) { |
2900 | 1.69k | if (!KeepBraces) |
2901 | 64 | FormatTok->setFinalizedType(TT_ControlStatementLBrace); |
2902 | 1.69k | FormatToken *LeftBrace = FormatTok; |
2903 | 1.69k | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
2904 | 1.69k | parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, |
2905 | 1.69k | /*MunchSemi=*/true, KeepBraces); |
2906 | 1.69k | if (!KeepBraces) { |
2907 | 64 | assert(!NestedTooDeep.empty()); |
2908 | 64 | if (!NestedTooDeep.back()) |
2909 | 64 | markOptionalBraces(LeftBrace); |
2910 | 64 | } |
2911 | 1.69k | if (WrapRightBrace) |
2912 | 1.46k | addUnwrappedLine(); |
2913 | 1.69k | } else { |
2914 | 1.29k | parseUnbracedBody(); |
2915 | 1.29k | } |
2916 | | |
2917 | 2.98k | if (!KeepBraces) |
2918 | 144 | NestedTooDeep.pop_back(); |
2919 | 2.98k | } |
2920 | | |
2921 | 2.68k | void UnwrappedLineParser::parseForOrWhileLoop() { |
2922 | 2.68k | assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && |
2923 | 2.68k | "'for', 'while' or foreach macro expected"); |
2924 | 2.68k | const bool KeepBraces = !Style.RemoveBracesLLVM || |
2925 | 2.68k | !FormatTok->isOneOf(tok::kw_for, tok::kw_while)144 ; |
2926 | | |
2927 | 2.68k | nextToken(); |
2928 | | // JS' for await ( ... |
2929 | 2.68k | if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await)48 ) |
2930 | 8 | nextToken(); |
2931 | 2.68k | if (Style.isCpp() && FormatTok->is(tok::kw_co_await)2.61k ) |
2932 | 27 | nextToken(); |
2933 | 2.68k | if (FormatTok->is(tok::l_paren)) |
2934 | 2.66k | parseParens(); |
2935 | | |
2936 | 2.68k | handleAttributes(); |
2937 | 2.68k | parseLoopBody(KeepBraces, /*WrapRightBrace=*/true); |
2938 | 2.68k | } |
2939 | | |
2940 | 298 | void UnwrappedLineParser::parseDoWhile() { |
2941 | 298 | assert(FormatTok->is(tok::kw_do) && "'do' expected"); |
2942 | 0 | nextToken(); |
2943 | | |
2944 | 298 | parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile); |
2945 | | |
2946 | | // FIXME: Add error handling. |
2947 | 298 | if (!FormatTok->is(tok::kw_while)) { |
2948 | 37 | addUnwrappedLine(); |
2949 | 37 | return; |
2950 | 37 | } |
2951 | | |
2952 | | // If in Whitesmiths mode, the line with the while() needs to be indented |
2953 | | // to the same level as the block. |
2954 | 261 | if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) |
2955 | 9 | ++Line->Level; |
2956 | | |
2957 | 261 | nextToken(); |
2958 | 261 | parseStructuralElement(); |
2959 | 261 | } |
2960 | | |
2961 | 1.73k | void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) { |
2962 | 1.73k | nextToken(); |
2963 | 1.73k | unsigned OldLineLevel = Line->Level; |
2964 | 1.73k | if (Line->Level > 1 || (1.21k !Line->InPPDirective1.21k && Line->Level > 01.21k )) |
2965 | 1.70k | --Line->Level; |
2966 | 1.73k | if (LeftAlignLabel) |
2967 | 27 | Line->Level = 0; |
2968 | | |
2969 | 1.73k | if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty()1.72k && |
2970 | 1.73k | FormatTok->is(tok::l_brace)1.68k ) { |
2971 | | |
2972 | 438 | CompoundStatementIndenter Indenter(this, Line->Level, |
2973 | 438 | Style.BraceWrapping.AfterCaseLabel, |
2974 | 438 | Style.BraceWrapping.IndentBraces); |
2975 | 438 | parseBlock(); |
2976 | 438 | if (FormatTok->is(tok::kw_break)) { |
2977 | 99 | if (Style.BraceWrapping.AfterControlStatement == |
2978 | 99 | FormatStyle::BWACS_Always) { |
2979 | 90 | addUnwrappedLine(); |
2980 | 90 | if (!Style.IndentCaseBlocks && |
2981 | 90 | Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { |
2982 | 72 | ++Line->Level; |
2983 | 72 | } |
2984 | 90 | } |
2985 | 99 | parseStructuralElement(); |
2986 | 99 | } |
2987 | 438 | addUnwrappedLine(); |
2988 | 1.30k | } else { |
2989 | 1.30k | if (FormatTok->is(tok::semi)) |
2990 | 36 | nextToken(); |
2991 | 1.30k | addUnwrappedLine(); |
2992 | 1.30k | } |
2993 | 1.73k | Line->Level = OldLineLevel; |
2994 | 1.73k | if (FormatTok->isNot(tok::l_brace)) { |
2995 | 1.71k | parseStructuralElement(); |
2996 | 1.71k | addUnwrappedLine(); |
2997 | 1.71k | } |
2998 | 1.73k | } |
2999 | | |
3000 | 1.10k | void UnwrappedLineParser::parseCaseLabel() { |
3001 | 1.10k | assert(FormatTok->is(tok::kw_case) && "'case' expected"); |
3002 | | |
3003 | | // FIXME: fix handling of complex expressions here. |
3004 | 2.38k | do { |
3005 | 2.38k | nextToken(); |
3006 | 2.38k | } while (!eof() && !FormatTok->is(tok::colon)2.37k ); |
3007 | 1.10k | parseLabel(); |
3008 | 1.10k | } |
3009 | | |
3010 | 686 | void UnwrappedLineParser::parseSwitch() { |
3011 | 686 | assert(FormatTok->is(tok::kw_switch) && "'switch' expected"); |
3012 | 0 | nextToken(); |
3013 | 686 | if (FormatTok->is(tok::l_paren)) |
3014 | 677 | parseParens(); |
3015 | | |
3016 | 686 | keepAncestorBraces(); |
3017 | | |
3018 | 686 | if (FormatTok->is(tok::l_brace)) { |
3019 | 659 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
3020 | 659 | parseBlock(); |
3021 | 659 | addUnwrappedLine(); |
3022 | 659 | } else { |
3023 | 27 | addUnwrappedLine(); |
3024 | 27 | ++Line->Level; |
3025 | 27 | parseStructuralElement(); |
3026 | 27 | --Line->Level; |
3027 | 27 | } |
3028 | | |
3029 | 686 | if (Style.RemoveBracesLLVM) |
3030 | 0 | NestedTooDeep.pop_back(); |
3031 | 686 | } |
3032 | | |
3033 | | // Operators that can follow a C variable. |
3034 | 399 | static bool isCOperatorFollowingVar(tok::TokenKind kind) { |
3035 | 399 | switch (kind) { |
3036 | 0 | case tok::ampamp: |
3037 | 9 | case tok::ampequal: |
3038 | 18 | case tok::arrow: |
3039 | 27 | case tok::caret: |
3040 | 36 | case tok::caretequal: |
3041 | 36 | case tok::comma: |
3042 | 36 | case tok::ellipsis: |
3043 | 54 | case tok::equal: |
3044 | 54 | case tok::equalequal: |
3045 | 54 | case tok::exclaim: |
3046 | 63 | case tok::exclaimequal: |
3047 | 72 | case tok::greater: |
3048 | 72 | case tok::greaterequal: |
3049 | 72 | case tok::greatergreater: |
3050 | 81 | case tok::greatergreaterequal: |
3051 | 90 | case tok::l_paren: |
3052 | 99 | case tok::l_square: |
3053 | 99 | case tok::less: |
3054 | 99 | case tok::lessequal: |
3055 | 108 | case tok::lessless: |
3056 | 117 | case tok::lesslessequal: |
3057 | 117 | case tok::minus: |
3058 | 126 | case tok::minusequal: |
3059 | 135 | case tok::minusminus: |
3060 | 135 | case tok::percent: |
3061 | 144 | case tok::percentequal: |
3062 | 153 | case tok::period: |
3063 | 162 | case tok::pipe: |
3064 | 171 | case tok::pipeequal: |
3065 | 171 | case tok::pipepipe: |
3066 | 171 | case tok::plus: |
3067 | 180 | case tok::plusequal: |
3068 | 189 | case tok::plusplus: |
3069 | 189 | case tok::question: |
3070 | 189 | case tok::r_brace: |
3071 | 189 | case tok::r_paren: |
3072 | 189 | case tok::r_square: |
3073 | 189 | case tok::semi: |
3074 | 189 | case tok::slash: |
3075 | 198 | case tok::slashequal: |
3076 | 198 | case tok::star: |
3077 | 207 | case tok::starequal: |
3078 | 207 | return true; |
3079 | 192 | default: |
3080 | 192 | return false; |
3081 | 399 | } |
3082 | 399 | } |
3083 | | |
3084 | 2.39k | void UnwrappedLineParser::parseAccessSpecifier() { |
3085 | 2.39k | FormatToken *AccessSpecifierCandidate = FormatTok; |
3086 | 2.39k | nextToken(); |
3087 | | // Understand Qt's slots. |
3088 | 2.39k | if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) |
3089 | 54 | nextToken(); |
3090 | | // Otherwise, we don't know what it is, and we'd better keep the next token. |
3091 | 2.39k | if (FormatTok->is(tok::colon)) { |
3092 | 1.98k | nextToken(); |
3093 | 1.98k | addUnwrappedLine(); |
3094 | 1.98k | } else if (408 !FormatTok->is(tok::coloncolon)408 && |
3095 | 408 | !isCOperatorFollowingVar(FormatTok->Tok.getKind())399 ) { |
3096 | | // Not a variable name nor namespace name. |
3097 | 192 | addUnwrappedLine(); |
3098 | 216 | } else if (AccessSpecifierCandidate) { |
3099 | | // Consider the access specifier to be a C identifier. |
3100 | 216 | AccessSpecifierCandidate->Tok.setKind(tok::identifier); |
3101 | 216 | } |
3102 | 2.39k | } |
3103 | | |
3104 | | /// \brief Parses a concept definition. |
3105 | | /// \pre The current token has to be the concept keyword. |
3106 | | /// |
3107 | | /// Returns if either the concept has been completely parsed, or if it detects |
3108 | | /// that the concept definition is incorrect. |
3109 | 621 | void UnwrappedLineParser::parseConcept() { |
3110 | 621 | assert(FormatTok->is(tok::kw_concept) && "'concept' expected"); |
3111 | 0 | nextToken(); |
3112 | 621 | if (!FormatTok->is(tok::identifier)) |
3113 | 4 | return; |
3114 | 617 | nextToken(); |
3115 | 617 | if (!FormatTok->is(tok::equal)) |
3116 | 0 | return; |
3117 | 617 | nextToken(); |
3118 | 617 | parseConstraintExpression(); |
3119 | 617 | if (FormatTok->is(tok::semi)) |
3120 | 599 | nextToken(); |
3121 | 617 | addUnwrappedLine(); |
3122 | 617 | } |
3123 | | |
3124 | | /// \brief Parses a requires, decides if it is a clause or an expression. |
3125 | | /// \pre The current token has to be the requires keyword. |
3126 | | /// \returns true if it parsed a clause. |
3127 | 1.01k | bool clang::format::UnwrappedLineParser::parseRequires() { |
3128 | 1.01k | assert(FormatTok->is(tok::kw_requires) && "'requires' expected"); |
3129 | 0 | auto RequiresToken = FormatTok; |
3130 | | |
3131 | | // We try to guess if it is a requires clause, or a requires expression. For |
3132 | | // that we first consume the keyword and check the next token. |
3133 | 1.01k | nextToken(); |
3134 | | |
3135 | 1.01k | switch (FormatTok->Tok.getKind()) { |
3136 | 0 | case tok::l_brace: |
3137 | | // This can only be an expression, never a clause. |
3138 | 0 | parseRequiresExpression(RequiresToken); |
3139 | 0 | return false; |
3140 | 193 | case tok::l_paren: |
3141 | | // Clauses and expression can start with a paren, it's unclear what we have. |
3142 | 193 | break; |
3143 | 820 | default: |
3144 | | // All other tokens can only be a clause. |
3145 | 820 | parseRequiresClause(RequiresToken); |
3146 | 820 | return true; |
3147 | 1.01k | } |
3148 | | |
3149 | | // Looking forward we would have to decide if there are function declaration |
3150 | | // like arguments to the requires expression: |
3151 | | // requires (T t) { |
3152 | | // Or there is a constraint expression for the requires clause: |
3153 | | // requires (C<T> && ... |
3154 | | |
3155 | | // But first let's look behind. |
3156 | 193 | auto *PreviousNonComment = RequiresToken->getPreviousNonComment(); |
3157 | | |
3158 | 193 | if (!PreviousNonComment || |
3159 | 193 | PreviousNonComment->is(TT_RequiresExpressionLBrace)157 ) { |
3160 | | // If there is no token, or an expression left brace, we are a requires |
3161 | | // clause within a requires expression. |
3162 | 36 | parseRequiresClause(RequiresToken); |
3163 | 36 | return true; |
3164 | 36 | } |
3165 | | |
3166 | 157 | switch (PreviousNonComment->Tok.getKind()) { |
3167 | 65 | case tok::greater: |
3168 | 110 | case tok::r_paren: |
3169 | 110 | case tok::kw_noexcept: |
3170 | 110 | case tok::kw_const: |
3171 | | // This is a requires clause. |
3172 | 110 | parseRequiresClause(RequiresToken); |
3173 | 110 | return true; |
3174 | 0 | case tok::amp: |
3175 | 4 | case tok::ampamp: { |
3176 | | // This can be either: |
3177 | | // if (... && requires (T t) ...) |
3178 | | // Or |
3179 | | // void member(...) && requires (C<T> ... |
3180 | | // We check the one token before that for a const: |
3181 | | // void member(...) const && requires (C<T> ... |
3182 | 4 | auto PrevPrev = PreviousNonComment->getPreviousNonComment(); |
3183 | 4 | if (PrevPrev && PrevPrev->is(tok::kw_const)) { |
3184 | 0 | parseRequiresClause(RequiresToken); |
3185 | 0 | return true; |
3186 | 0 | } |
3187 | 4 | break; |
3188 | 4 | } |
3189 | 43 | default: |
3190 | 43 | if (PreviousNonComment->isTypeOrIdentifier()) { |
3191 | | // This is a requires clause. |
3192 | 5 | parseRequiresClause(RequiresToken); |
3193 | 5 | return true; |
3194 | 5 | } |
3195 | | // It's an expression. |
3196 | 38 | parseRequiresExpression(RequiresToken); |
3197 | 38 | return false; |
3198 | 157 | } |
3199 | | |
3200 | | // Now we look forward and try to check if the paren content is a parameter |
3201 | | // list. The parameters can be cv-qualified and contain references or |
3202 | | // pointers. |
3203 | | // So we want basically to check for TYPE NAME, but TYPE can contain all kinds |
3204 | | // of stuff: typename, const, *, &, &&, ::, identifiers. |
3205 | | |
3206 | 4 | int NextTokenOffset = 1; |
3207 | 4 | auto NextToken = Tokens->peekNextToken(NextTokenOffset); |
3208 | 22 | auto PeekNext = [&NextTokenOffset, &NextToken, this] { |
3209 | 22 | ++NextTokenOffset; |
3210 | 22 | NextToken = Tokens->peekNextToken(NextTokenOffset); |
3211 | 22 | }; |
3212 | | |
3213 | 4 | bool FoundType = false; |
3214 | 4 | bool LastWasColonColon = false; |
3215 | 4 | int OpenAngles = 0; |
3216 | | |
3217 | 26 | for (; NextTokenOffset < 50; PeekNext()22 ) { |
3218 | 26 | switch (NextToken->Tok.getKind()) { |
3219 | 0 | case tok::kw_volatile: |
3220 | 0 | case tok::kw_const: |
3221 | 0 | case tok::comma: |
3222 | 0 | parseRequiresExpression(RequiresToken); |
3223 | 0 | return false; |
3224 | 4 | case tok::r_paren: |
3225 | 4 | case tok::pipepipe: |
3226 | 4 | parseRequiresClause(RequiresToken); |
3227 | 4 | return true; |
3228 | 0 | case tok::eof: |
3229 | | // Break out of the loop. |
3230 | 0 | NextTokenOffset = 50; |
3231 | 0 | break; |
3232 | 4 | case tok::coloncolon: |
3233 | 4 | LastWasColonColon = true; |
3234 | 4 | break; |
3235 | 11 | case tok::identifier: |
3236 | 11 | if (FoundType && !LastWasColonColon7 && OpenAngles == 03 ) { |
3237 | 0 | parseRequiresExpression(RequiresToken); |
3238 | 0 | return false; |
3239 | 0 | } |
3240 | 11 | FoundType = true; |
3241 | 11 | LastWasColonColon = false; |
3242 | 11 | break; |
3243 | 3 | case tok::less: |
3244 | 3 | ++OpenAngles; |
3245 | 3 | break; |
3246 | 3 | case tok::greater: |
3247 | 3 | --OpenAngles; |
3248 | 3 | break; |
3249 | 1 | default: |
3250 | 1 | if (NextToken->isSimpleTypeSpecifier()) { |
3251 | 0 | parseRequiresExpression(RequiresToken); |
3252 | 0 | return false; |
3253 | 0 | } |
3254 | 1 | break; |
3255 | 26 | } |
3256 | 26 | } |
3257 | | |
3258 | | // This seems to be a complicated expression, just assume it's a clause. |
3259 | 0 | parseRequiresClause(RequiresToken); |
3260 | 0 | return true; |
3261 | 4 | } |
3262 | | |
3263 | | /// \brief Parses a requires clause. |
3264 | | /// \param RequiresToken The requires keyword token, which starts this clause. |
3265 | | /// \pre We need to be on the next token after the requires keyword. |
3266 | | /// \sa parseRequiresExpression |
3267 | | /// |
3268 | | /// Returns if it either has finished parsing the clause, or it detects, that |
3269 | | /// the clause is incorrect. |
3270 | 975 | void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) { |
3271 | 975 | assert(FormatTok->getPreviousNonComment() == RequiresToken); |
3272 | 0 | assert(RequiresToken->is(tok::kw_requires) && "'requires' expected"); |
3273 | | |
3274 | | // If there is no previous token, we are within a requires expression, |
3275 | | // otherwise we will always have the template or function declaration in front |
3276 | | // of it. |
3277 | 0 | bool InRequiresExpression = |
3278 | 975 | !RequiresToken->Previous || |
3279 | 975 | RequiresToken->Previous->is(TT_RequiresExpressionLBrace)830 ; |
3280 | | |
3281 | 975 | RequiresToken->setFinalizedType(InRequiresExpression |
3282 | 975 | ? TT_RequiresClauseInARequiresExpression219 |
3283 | 975 | : TT_RequiresClause756 ); |
3284 | | |
3285 | 975 | parseConstraintExpression(); |
3286 | | |
3287 | 975 | if (!InRequiresExpression) |
3288 | 756 | FormatTok->Previous->ClosesRequiresClause = true; |
3289 | 975 | } |
3290 | | |
3291 | | /// \brief Parses a requires expression. |
3292 | | /// \param RequiresToken The requires keyword token, which starts this clause. |
3293 | | /// \pre We need to be on the next token after the requires keyword. |
3294 | | /// \sa parseRequiresClause |
3295 | | /// |
3296 | | /// Returns if it either has finished parsing the expression, or it detects, |
3297 | | /// that the expression is incorrect. |
3298 | 628 | void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) { |
3299 | 628 | assert(FormatTok->getPreviousNonComment() == RequiresToken); |
3300 | 0 | assert(RequiresToken->is(tok::kw_requires) && "'requires' expected"); |
3301 | | |
3302 | 0 | RequiresToken->setFinalizedType(TT_RequiresExpression); |
3303 | | |
3304 | 628 | if (FormatTok->is(tok::l_paren)) { |
3305 | 581 | FormatTok->setFinalizedType(TT_RequiresExpressionLParen); |
3306 | 581 | parseParens(); |
3307 | 581 | } |
3308 | | |
3309 | 628 | if (FormatTok->is(tok::l_brace)) { |
3310 | 601 | FormatTok->setFinalizedType(TT_RequiresExpressionLBrace); |
3311 | 601 | parseChildBlock(/*CanContainBracedList=*/false, |
3312 | 601 | /*NextLBracesType=*/TT_CompoundRequirementLBrace); |
3313 | 601 | } |
3314 | 628 | } |
3315 | | |
3316 | | /// \brief Parses a constraint expression. |
3317 | | /// |
3318 | | /// This is either the definition of a concept, or the body of a requires |
3319 | | /// clause. It returns, when the parsing is complete, or the expression is |
3320 | | /// incorrect. |
3321 | 1.59k | void UnwrappedLineParser::parseConstraintExpression() { |
3322 | | // The special handling for lambdas is needed since tryToParseLambda() eats a |
3323 | | // token and if a requires expression is the last part of a requires clause |
3324 | | // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is |
3325 | | // not set on the correct token. Thus we need to be aware if we even expect a |
3326 | | // lambda to be possible. |
3327 | | // template <typename T> requires requires { ... } [[nodiscard]] ...; |
3328 | 1.59k | bool LambdaNextTimeAllowed = true; |
3329 | 5.86k | do { |
3330 | 5.86k | bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false); |
3331 | | |
3332 | 5.86k | switch (FormatTok->Tok.getKind()) { |
3333 | 492 | case tok::kw_requires: { |
3334 | 492 | auto RequiresToken = FormatTok; |
3335 | 492 | nextToken(); |
3336 | 492 | parseRequiresExpression(RequiresToken); |
3337 | 492 | break; |
3338 | 0 | } |
3339 | | |
3340 | 527 | case tok::l_paren: |
3341 | 527 | parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator); |
3342 | 527 | break; |
3343 | | |
3344 | 47 | case tok::l_square: |
3345 | 47 | if (!LambdaThisTimeAllowed || !tryToParseLambda()36 ) |
3346 | 11 | return; |
3347 | 36 | break; |
3348 | | |
3349 | 36 | case tok::kw_const: |
3350 | 861 | case tok::semi: |
3351 | 915 | case tok::kw_class: |
3352 | 1.03k | case tok::kw_struct: |
3353 | 1.03k | case tok::kw_union: |
3354 | 1.03k | return; |
3355 | | |
3356 | 210 | case tok::l_brace: |
3357 | | // Potential function body. |
3358 | 210 | return; |
3359 | | |
3360 | 724 | case tok::ampamp: |
3361 | 805 | case tok::pipepipe: |
3362 | 805 | FormatTok->setFinalizedType(TT_BinaryOperator); |
3363 | 805 | nextToken(); |
3364 | 805 | LambdaNextTimeAllowed = true; |
3365 | 805 | break; |
3366 | | |
3367 | 0 | case tok::comma: |
3368 | 0 | case tok::comment: |
3369 | 0 | LambdaNextTimeAllowed = LambdaThisTimeAllowed; |
3370 | 0 | nextToken(); |
3371 | 0 | break; |
3372 | | |
3373 | 189 | case tok::kw_sizeof: |
3374 | 216 | case tok::greater: |
3375 | 243 | case tok::greaterequal: |
3376 | 243 | case tok::greatergreater: |
3377 | 252 | case tok::less: |
3378 | 423 | case tok::lessequal: |
3379 | 423 | case tok::lessless: |
3380 | 432 | case tok::equalequal: |
3381 | 459 | case tok::exclaim: |
3382 | 459 | case tok::exclaimequal: |
3383 | 468 | case tok::plus: |
3384 | 468 | case tok::minus: |
3385 | 468 | case tok::star: |
3386 | 468 | case tok::slash: |
3387 | 513 | case tok::kw_decltype: |
3388 | 513 | LambdaNextTimeAllowed = true; |
3389 | | // Just eat them. |
3390 | 513 | nextToken(); |
3391 | 513 | break; |
3392 | | |
3393 | 306 | case tok::numeric_constant: |
3394 | 415 | case tok::coloncolon: |
3395 | 488 | case tok::kw_true: |
3396 | 506 | case tok::kw_false: |
3397 | | // Just eat them. |
3398 | 506 | nextToken(); |
3399 | 506 | break; |
3400 | | |
3401 | 9 | case tok::kw_static_cast: |
3402 | 9 | case tok::kw_const_cast: |
3403 | 9 | case tok::kw_reinterpret_cast: |
3404 | 9 | case tok::kw_dynamic_cast: |
3405 | 9 | nextToken(); |
3406 | 9 | if (!FormatTok->is(tok::less)) |
3407 | 0 | return; |
3408 | | |
3409 | 9 | nextToken(); |
3410 | 9 | parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, |
3411 | 9 | /*ClosingBraceKind=*/tok::greater); |
3412 | 9 | break; |
3413 | | |
3414 | 9 | case tok::kw_bool: |
3415 | | // bool is only allowed if it is directly followed by a paren for a cast: |
3416 | | // concept C = bool(...); |
3417 | | // and bool is the only type, all other types as cast must be inside a |
3418 | | // cast to bool an thus are handled by the other cases. |
3419 | 9 | nextToken(); |
3420 | 9 | if (FormatTok->isNot(tok::l_paren)) |
3421 | 0 | return; |
3422 | 9 | parseParens(); |
3423 | 9 | break; |
3424 | | |
3425 | 1.71k | default: |
3426 | 1.71k | if (!FormatTok->Tok.getIdentifierInfo()) { |
3427 | | // Identifiers are part of the default case, we check for more then |
3428 | | // tok::identifier to handle builtin type traits. |
3429 | 0 | return; |
3430 | 0 | } |
3431 | | |
3432 | | // We need to differentiate identifiers for a template deduction guide, |
3433 | | // variables, or function return types (the constraint expression has |
3434 | | // ended before that), and basically all other cases. But it's easier to |
3435 | | // check the other way around. |
3436 | 1.71k | assert(FormatTok->Previous); |
3437 | 0 | switch (FormatTok->Previous->Tok.getKind()) { |
3438 | 109 | case tok::coloncolon: // Nested identifier. |
3439 | 444 | case tok::ampamp: // Start of a function or variable for the |
3440 | 453 | case tok::pipepipe: // constraint expression. |
3441 | 1.22k | case tok::kw_requires: // Initial identifier of a requires clause. |
3442 | 1.38k | case tok::equal: // Initial identifier of a concept declaration. |
3443 | 1.38k | break; |
3444 | 326 | default: |
3445 | 326 | return; |
3446 | 1.71k | } |
3447 | | |
3448 | | // Read identifier with optional template declaration. |
3449 | 1.38k | nextToken(); |
3450 | 1.38k | if (FormatTok->is(tok::less)) { |
3451 | 1.20k | nextToken(); |
3452 | 1.20k | parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, |
3453 | 1.20k | /*ClosingBraceKind=*/tok::greater); |
3454 | 1.20k | } |
3455 | 1.38k | break; |
3456 | 5.86k | } |
3457 | 5.86k | } while (!eof()4.28k ); |
3458 | 1.59k | } |
3459 | | |
3460 | 2.62k | bool UnwrappedLineParser::parseEnum() { |
3461 | 2.62k | const FormatToken &InitialToken = *FormatTok; |
3462 | | |
3463 | | // Won't be 'enum' for NS_ENUMs. |
3464 | 2.62k | if (FormatTok->is(tok::kw_enum)) |
3465 | 2.54k | nextToken(); |
3466 | | |
3467 | | // In TypeScript, "enum" can also be used as property name, e.g. in interface |
3468 | | // declarations. An "enum" keyword followed by a colon would be a syntax |
3469 | | // error and thus assume it is just an identifier. |
3470 | 2.62k | if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question)48 ) |
3471 | 8 | return false; |
3472 | | |
3473 | | // In protobuf, "enum" can be used as a field name. |
3474 | 2.61k | if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal)8 ) |
3475 | 2 | return false; |
3476 | | |
3477 | | // Eat up enum class ... |
3478 | 2.61k | if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct)) |
3479 | 442 | nextToken(); |
3480 | | |
3481 | 5.35k | while (FormatTok->Tok.getIdentifierInfo() || |
3482 | 5.35k | FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, |
3483 | 3.01k | tok::greater, tok::comma, tok::question, |
3484 | 3.01k | tok::l_square, tok::r_square)) { |
3485 | 2.78k | nextToken(); |
3486 | | // We can have macros or attributes in between 'enum' and the enum name. |
3487 | 2.78k | if (FormatTok->is(tok::l_paren)) |
3488 | 128 | parseParens(); |
3489 | 2.78k | if (FormatTok->is(TT_AttributeSquare)) { |
3490 | 0 | parseSquare(); |
3491 | | // Consume the closing TT_AttributeSquare. |
3492 | 0 | if (FormatTok->Next && FormatTok->is(TT_AttributeSquare)) |
3493 | 0 | nextToken(); |
3494 | 0 | } |
3495 | 2.78k | if (FormatTok->is(tok::identifier)) { |
3496 | 360 | nextToken(); |
3497 | | // If there are two identifiers in a row, this is likely an elaborate |
3498 | | // return type. In Java, this can be "implements", etc. |
3499 | 360 | if (Style.isCpp() && FormatTok->is(tok::identifier)356 ) |
3500 | 36 | return false; |
3501 | 360 | } |
3502 | 2.78k | } |
3503 | | |
3504 | | // Just a declaration or something is wrong. |
3505 | 2.57k | if (FormatTok->isNot(tok::l_brace)) |
3506 | 46 | return true; |
3507 | 2.53k | FormatTok->setFinalizedType(TT_EnumLBrace); |
3508 | 2.53k | FormatTok->setBlockKind(BK_Block); |
3509 | | |
3510 | 2.53k | if (Style.Language == FormatStyle::LK_Java) { |
3511 | | // Java enums are different. |
3512 | 24 | parseJavaEnumBody(); |
3513 | 24 | return true; |
3514 | 24 | } |
3515 | 2.50k | if (Style.Language == FormatStyle::LK_Proto) { |
3516 | 6 | parseBlock(/*MustBeDeclaration=*/true); |
3517 | 6 | return true; |
3518 | 6 | } |
3519 | | |
3520 | 2.50k | if (!Style.AllowShortEnumsOnASingleLine && |
3521 | 2.50k | ShouldBreakBeforeBrace(Style, InitialToken)107 ) { |
3522 | 34 | addUnwrappedLine(); |
3523 | 34 | } |
3524 | | // Parse enum body. |
3525 | 2.50k | nextToken(); |
3526 | 2.50k | if (!Style.AllowShortEnumsOnASingleLine) { |
3527 | 107 | addUnwrappedLine(); |
3528 | 107 | Line->Level += 1; |
3529 | 107 | } |
3530 | 2.50k | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true, |
3531 | 2.50k | /*IsEnum=*/true); |
3532 | 2.50k | if (!Style.AllowShortEnumsOnASingleLine) |
3533 | 107 | Line->Level -= 1; |
3534 | 2.50k | if (HasError) { |
3535 | 18 | if (FormatTok->is(tok::semi)) |
3536 | 9 | nextToken(); |
3537 | 18 | addUnwrappedLine(); |
3538 | 18 | } |
3539 | 2.50k | return true; |
3540 | | |
3541 | | // There is no addUnwrappedLine() here so that we fall through to parsing a |
3542 | | // structural element afterwards. Thus, in "enum A {} n, m;", |
3543 | | // "} n, m;" will end up in one unwrapped line. |
3544 | 2.50k | } |
3545 | | |
3546 | 9.84k | bool UnwrappedLineParser::parseStructLike() { |
3547 | | // parseRecord falls through and does not yet add an unwrapped line as a |
3548 | | // record declaration or definition can start a structural element. |
3549 | 9.84k | parseRecord(); |
3550 | | // This does not apply to Java, JavaScript and C#. |
3551 | 9.84k | if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript()9.81k || |
3552 | 9.84k | Style.isCSharp()9.54k ) { |
3553 | 451 | if (FormatTok->is(tok::semi)) |
3554 | 12 | nextToken(); |
3555 | 451 | addUnwrappedLine(); |
3556 | 451 | return true; |
3557 | 451 | } |
3558 | 9.39k | return false; |
3559 | 9.84k | } |
3560 | | |
3561 | | namespace { |
3562 | | // A class used to set and restore the Token position when peeking |
3563 | | // ahead in the token source. |
3564 | | class ScopedTokenPosition { |
3565 | | unsigned StoredPosition; |
3566 | | FormatTokenSource *Tokens; |
3567 | | |
3568 | | public: |
3569 | 1.85k | ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) { |
3570 | 1.85k | assert(Tokens && "Tokens expected to not be null"); |
3571 | 0 | StoredPosition = Tokens->getPosition(); |
3572 | 1.85k | } |
3573 | | |
3574 | 1.85k | ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); } |
3575 | | }; |
3576 | | } // namespace |
3577 | | |
3578 | | // Look to see if we have [[ by looking ahead, if |
3579 | | // its not then rewind to the original position. |
3580 | 1.85k | bool UnwrappedLineParser::tryToParseSimpleAttribute() { |
3581 | 1.85k | ScopedTokenPosition AutoPosition(Tokens); |
3582 | 1.85k | FormatToken *Tok = Tokens->getNextToken(); |
3583 | | // We already read the first [ check for the second. |
3584 | 1.85k | if (!Tok->is(tok::l_square)) |
3585 | 1.39k | return false; |
3586 | | // Double check that the attribute is just something |
3587 | | // fairly simple. |
3588 | 1.89k | while (461 Tok->isNot(tok::eof)) { |
3589 | 1.89k | if (Tok->is(tok::r_square)) |
3590 | 461 | break; |
3591 | 1.43k | Tok = Tokens->getNextToken(); |
3592 | 1.43k | } |
3593 | 461 | if (Tok->is(tok::eof)) |
3594 | 0 | return false; |
3595 | 461 | Tok = Tokens->getNextToken(); |
3596 | 461 | if (!Tok->is(tok::r_square)) |
3597 | 111 | return false; |
3598 | 350 | Tok = Tokens->getNextToken(); |
3599 | 350 | if (Tok->is(tok::semi)) |
3600 | 70 | return false; |
3601 | 280 | return true; |
3602 | 350 | } |
3603 | | |
3604 | 24 | void UnwrappedLineParser::parseJavaEnumBody() { |
3605 | 24 | assert(FormatTok->is(tok::l_brace)); |
3606 | 0 | const FormatToken *OpeningBrace = FormatTok; |
3607 | | |
3608 | | // Determine whether the enum is simple, i.e. does not have a semicolon or |
3609 | | // constants with class bodies. Simple enums can be formatted like braced |
3610 | | // lists, contracted to a single line, etc. |
3611 | 24 | unsigned StoredPosition = Tokens->getPosition(); |
3612 | 24 | bool IsSimple = true; |
3613 | 24 | FormatToken *Tok = Tokens->getNextToken(); |
3614 | 146 | while (!Tok->is(tok::eof)) { |
3615 | 146 | if (Tok->is(tok::r_brace)) |
3616 | 8 | break; |
3617 | 138 | if (Tok->isOneOf(tok::l_brace, tok::semi)) { |
3618 | 16 | IsSimple = false; |
3619 | 16 | break; |
3620 | 16 | } |
3621 | | // FIXME: This will also mark enums with braces in the arguments to enum |
3622 | | // constants as "not simple". This is probably fine in practice, though. |
3623 | 122 | Tok = Tokens->getNextToken(); |
3624 | 122 | } |
3625 | 24 | FormatTok = Tokens->setPosition(StoredPosition); |
3626 | | |
3627 | 24 | if (IsSimple) { |
3628 | 8 | nextToken(); |
3629 | 8 | parseBracedList(); |
3630 | 8 | addUnwrappedLine(); |
3631 | 8 | return; |
3632 | 8 | } |
3633 | | |
3634 | | // Parse the body of a more complex enum. |
3635 | | // First add a line for everything up to the "{". |
3636 | 16 | nextToken(); |
3637 | 16 | addUnwrappedLine(); |
3638 | 16 | ++Line->Level; |
3639 | | |
3640 | | // Parse the enum constants. |
3641 | 86 | while (FormatTok->isNot(tok::eof)) { |
3642 | 84 | if (FormatTok->is(tok::l_brace)) { |
3643 | | // Parse the constant's class body. |
3644 | 10 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u, |
3645 | 10 | /*MunchSemi=*/false); |
3646 | 74 | } else if (FormatTok->is(tok::l_paren)) { |
3647 | 14 | parseParens(); |
3648 | 60 | } else if (FormatTok->is(tok::comma)) { |
3649 | 14 | nextToken(); |
3650 | 14 | addUnwrappedLine(); |
3651 | 46 | } else if (FormatTok->is(tok::semi)) { |
3652 | 14 | nextToken(); |
3653 | 14 | addUnwrappedLine(); |
3654 | 14 | break; |
3655 | 32 | } else if (FormatTok->is(tok::r_brace)) { |
3656 | 0 | addUnwrappedLine(); |
3657 | 0 | break; |
3658 | 32 | } else { |
3659 | 32 | nextToken(); |
3660 | 32 | } |
3661 | 84 | } |
3662 | | |
3663 | | // Parse the class body after the enum's ";" if any. |
3664 | 16 | parseLevel(OpeningBrace); |
3665 | 16 | nextToken(); |
3666 | 16 | --Line->Level; |
3667 | 16 | addUnwrappedLine(); |
3668 | 16 | } |
3669 | | |
3670 | 9.88k | void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { |
3671 | 9.88k | const FormatToken &InitialToken = *FormatTok; |
3672 | 9.88k | nextToken(); |
3673 | | |
3674 | | // The actual identifier can be a nested name specifier, and in macros |
3675 | | // it is often token-pasted. |
3676 | | // An [[attribute]] can be before the identifier. |
3677 | 20.7k | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, |
3678 | 20.7k | tok::kw___attribute, tok::kw___declspec, |
3679 | 20.7k | tok::kw_alignas, tok::l_square, tok::r_square) || |
3680 | 20.7k | (9.90k (9.90k Style.Language == FormatStyle::LK_Java9.90k || Style.isJavaScript()9.86k ) && |
3681 | 10.9k | FormatTok->isOneOf(tok::period, tok::comma)365 )) { |
3682 | 10.9k | if (Style.isJavaScript() && |
3683 | 10.9k | FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)376 ) { |
3684 | | // JavaScript/TypeScript supports inline object types in |
3685 | | // extends/implements positions: |
3686 | | // class Foo implements {bar: number} { } |
3687 | 52 | nextToken(); |
3688 | 52 | if (FormatTok->is(tok::l_brace)) { |
3689 | 12 | tryToParseBracedList(); |
3690 | 12 | continue; |
3691 | 12 | } |
3692 | 52 | } |
3693 | 10.8k | bool IsNonMacroIdentifier = |
3694 | 10.8k | FormatTok->is(tok::identifier) && |
3695 | 10.8k | FormatTok->TokenText != FormatTok->TokenText.upper()10.1k ; |
3696 | 10.8k | nextToken(); |
3697 | | // We can have macros or attributes in between 'class' and the class name. |
3698 | 10.8k | if (!IsNonMacroIdentifier) { |
3699 | 5.50k | if (FormatTok->is(tok::l_paren)) { |
3700 | 72 | parseParens(); |
3701 | 5.43k | } else if (FormatTok->is(TT_AttributeSquare)) { |
3702 | 0 | parseSquare(); |
3703 | | // Consume the closing TT_AttributeSquare. |
3704 | 0 | if (FormatTok->Next && FormatTok->is(TT_AttributeSquare)) |
3705 | 0 | nextToken(); |
3706 | 0 | } |
3707 | 5.50k | } |
3708 | 10.8k | } |
3709 | | |
3710 | | // Note that parsing away template declarations here leads to incorrectly |
3711 | | // accepting function declarations as record declarations. |
3712 | | // In general, we cannot solve this problem. Consider: |
3713 | | // class A<int> B() {} |
3714 | | // which can be a function definition or a class definition when B() is a |
3715 | | // macro. If we find enough real-world cases where this is a problem, we |
3716 | | // can parse for the 'template' keyword in the beginning of the statement, |
3717 | | // and thus rule out the record production in case there is no template |
3718 | | // (this would still leave us with an ambiguity between template function |
3719 | | // and class declarations). |
3720 | 9.88k | if (FormatTok->isOneOf(tok::colon, tok::less)) { |
3721 | 6.72k | do { |
3722 | 6.72k | if (FormatTok->is(tok::l_brace)) { |
3723 | 1.00k | calculateBraceTypes(/*ExpectClassBody=*/true); |
3724 | 1.00k | if (!tryToParseBracedList()) |
3725 | 974 | break; |
3726 | 1.00k | } |
3727 | 5.74k | if (FormatTok->is(tok::l_square)) { |
3728 | 54 | FormatToken *Previous = FormatTok->Previous; |
3729 | 54 | if (!Previous || |
3730 | 54 | !(Previous->is(tok::r_paren) || Previous->isTypeOrIdentifier()36 )) { |
3731 | | // Don't try parsing a lambda if we had a closing parenthesis before, |
3732 | | // it was probably a pointer to an array: int (*)[]. |
3733 | 27 | if (!tryToParseLambda()) |
3734 | 0 | break; |
3735 | 27 | } else { |
3736 | 27 | parseSquare(); |
3737 | 27 | continue; |
3738 | 27 | } |
3739 | 54 | } |
3740 | 5.72k | if (FormatTok->is(tok::semi)) |
3741 | 18 | return; |
3742 | 5.70k | if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)56 ) { |
3743 | 8 | addUnwrappedLine(); |
3744 | 8 | nextToken(); |
3745 | 8 | parseCSharpGenericTypeConstraint(); |
3746 | 8 | break; |
3747 | 8 | } |
3748 | 5.69k | nextToken(); |
3749 | 5.72k | } while (!eof()); |
3750 | 1.02k | } |
3751 | | |
3752 | 9.87k | auto GetBraceType = [](const FormatToken &RecordTok) { |
3753 | 8.01k | switch (RecordTok.Tok.getKind()) { |
3754 | 3.90k | case tok::kw_class: |
3755 | 3.90k | return TT_ClassLBrace; |
3756 | 3.76k | case tok::kw_struct: |
3757 | 3.76k | return TT_StructLBrace; |
3758 | 269 | case tok::kw_union: |
3759 | 269 | return TT_UnionLBrace; |
3760 | 80 | default: |
3761 | | // Useful for e.g. interface. |
3762 | 80 | return TT_RecordLBrace; |
3763 | 8.01k | } |
3764 | 8.01k | }; |
3765 | 9.87k | if (FormatTok->is(tok::l_brace)) { |
3766 | 8.01k | FormatTok->setFinalizedType(GetBraceType(InitialToken)); |
3767 | 8.01k | if (ParseAsExpr) { |
3768 | 4 | parseChildBlock(); |
3769 | 8.01k | } else { |
3770 | 8.01k | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
3771 | 550 | addUnwrappedLine(); |
3772 | | |
3773 | 8.01k | unsigned AddLevels = Style.IndentAccessModifiers ? 2u54 : 1u7.95k ; |
3774 | 8.01k | parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false); |
3775 | 8.01k | } |
3776 | 8.01k | } |
3777 | | // There is no addUnwrappedLine() here so that we fall through to parsing a |
3778 | | // structural element afterwards. Thus, in "class A {} n, m;", |
3779 | | // "} n, m;" will end up in one unwrapped line. |
3780 | 9.87k | } |
3781 | | |
3782 | 366 | void UnwrappedLineParser::parseObjCMethod() { |
3783 | 366 | assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) && |
3784 | 366 | "'(' or identifier expected."); |
3785 | 2.27k | do { |
3786 | 2.27k | if (FormatTok->is(tok::semi)) { |
3787 | 234 | nextToken(); |
3788 | 234 | addUnwrappedLine(); |
3789 | 234 | return; |
3790 | 2.04k | } else if (FormatTok->is(tok::l_brace)) { |
3791 | 132 | if (Style.BraceWrapping.AfterFunction) |
3792 | 6 | addUnwrappedLine(); |
3793 | 132 | parseBlock(); |
3794 | 132 | addUnwrappedLine(); |
3795 | 132 | return; |
3796 | 1.90k | } else { |
3797 | 1.90k | nextToken(); |
3798 | 1.90k | } |
3799 | 2.27k | } while (!eof()1.90k ); |
3800 | 366 | } |
3801 | | |
3802 | 90 | void UnwrappedLineParser::parseObjCProtocolList() { |
3803 | 90 | assert(FormatTok->is(tok::less) && "'<' expected."); |
3804 | 384 | do { |
3805 | 384 | nextToken(); |
3806 | | // Early exit in case someone forgot a close angle. |
3807 | 384 | if (FormatTok->isOneOf(tok::semi, tok::l_brace) || |
3808 | 384 | FormatTok->isObjCAtKeyword(tok::objc_end)) { |
3809 | 0 | return; |
3810 | 0 | } |
3811 | 384 | } while (!eof() && FormatTok->isNot(tok::greater)); |
3812 | 90 | nextToken(); // Skip '>'. |
3813 | 90 | } |
3814 | | |
3815 | 494 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
3816 | 1.34k | do { |
3817 | 1.34k | if (FormatTok->isObjCAtKeyword(tok::objc_end)) { |
3818 | 429 | nextToken(); |
3819 | 429 | addUnwrappedLine(); |
3820 | 429 | break; |
3821 | 429 | } |
3822 | 918 | if (FormatTok->is(tok::l_brace)) { |
3823 | 6 | parseBlock(); |
3824 | | // In ObjC interfaces, nothing should be following the "}". |
3825 | 6 | addUnwrappedLine(); |
3826 | 912 | } else if (FormatTok->is(tok::r_brace)) { |
3827 | | // Ignore stray "}". parseStructuralElement doesn't consume them. |
3828 | 6 | nextToken(); |
3829 | 6 | addUnwrappedLine(); |
3830 | 906 | } else if (FormatTok->isOneOf(tok::minus, tok::plus)) { |
3831 | 366 | nextToken(); |
3832 | 366 | parseObjCMethod(); |
3833 | 540 | } else { |
3834 | 540 | parseStructuralElement(); |
3835 | 540 | } |
3836 | 918 | } while (!eof()); |
3837 | 494 | } |
3838 | | |
3839 | 437 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
3840 | 437 | assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface || |
3841 | 437 | FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation); |
3842 | 0 | nextToken(); |
3843 | 437 | nextToken(); // interface name |
3844 | | |
3845 | | // @interface can be followed by a lightweight generic |
3846 | | // specialization list, then either a base class or a category. |
3847 | 437 | if (FormatTok->is(tok::less)) |
3848 | 20 | parseObjCLightweightGenerics(); |
3849 | 437 | if (FormatTok->is(tok::colon)) { |
3850 | 117 | nextToken(); |
3851 | 117 | nextToken(); // base class name |
3852 | | // The base class can also have lightweight generics applied to it. |
3853 | 117 | if (FormatTok->is(tok::less)) |
3854 | 70 | parseObjCLightweightGenerics(); |
3855 | 320 | } else if (FormatTok->is(tok::l_paren)) { |
3856 | | // Skip category, if present. |
3857 | 121 | parseParens(); |
3858 | 121 | } |
3859 | | |
3860 | 437 | if (FormatTok->is(tok::less)) |
3861 | 78 | parseObjCProtocolList(); |
3862 | | |
3863 | 437 | if (FormatTok->is(tok::l_brace)) { |
3864 | 161 | if (Style.BraceWrapping.AfterObjCDeclaration) |
3865 | 27 | addUnwrappedLine(); |
3866 | 161 | parseBlock(/*MustBeDeclaration=*/true); |
3867 | 161 | } |
3868 | | |
3869 | | // With instance variables, this puts '}' on its own line. Without instance |
3870 | | // variables, this ends the @interface line. |
3871 | 437 | addUnwrappedLine(); |
3872 | | |
3873 | 437 | parseObjCUntilAtEnd(); |
3874 | 437 | } |
3875 | | |
3876 | 90 | void UnwrappedLineParser::parseObjCLightweightGenerics() { |
3877 | 90 | assert(FormatTok->is(tok::less)); |
3878 | | // Unlike protocol lists, generic parameterizations support |
3879 | | // nested angles: |
3880 | | // |
3881 | | // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> : |
3882 | | // NSObject <NSCopying, NSSecureCoding> |
3883 | | // |
3884 | | // so we need to count how many open angles we have left. |
3885 | 0 | unsigned NumOpenAngles = 1; |
3886 | 322 | do { |
3887 | 322 | nextToken(); |
3888 | | // Early exit in case someone forgot a close angle. |
3889 | 322 | if (FormatTok->isOneOf(tok::semi, tok::l_brace) || |
3890 | 322 | FormatTok->isObjCAtKeyword(tok::objc_end)) { |
3891 | 0 | break; |
3892 | 0 | } |
3893 | 322 | if (FormatTok->is(tok::less)) { |
3894 | 12 | ++NumOpenAngles; |
3895 | 310 | } else if (FormatTok->is(tok::greater)) { |
3896 | 102 | assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative"); |
3897 | 0 | --NumOpenAngles; |
3898 | 102 | } |
3899 | 322 | } while (!eof() && NumOpenAngles != 0); |
3900 | 0 | nextToken(); // Skip '>'. |
3901 | 90 | } |
3902 | | |
3903 | | // Returns true for the declaration/definition form of @protocol, |
3904 | | // false for the expression form. |
3905 | 75 | bool UnwrappedLineParser::parseObjCProtocol() { |
3906 | 75 | assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol); |
3907 | 0 | nextToken(); |
3908 | | |
3909 | 75 | if (FormatTok->is(tok::l_paren)) { |
3910 | | // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);". |
3911 | 6 | return false; |
3912 | 6 | } |
3913 | | |
3914 | | // The definition/declaration form, |
3915 | | // @protocol Foo |
3916 | | // - (int)someMethod; |
3917 | | // @end |
3918 | | |
3919 | 69 | nextToken(); // protocol name |
3920 | | |
3921 | 69 | if (FormatTok->is(tok::less)) |
3922 | 12 | parseObjCProtocolList(); |
3923 | | |
3924 | | // Check for protocol declaration. |
3925 | 69 | if (FormatTok->is(tok::semi)) { |
3926 | 12 | nextToken(); |
3927 | 12 | addUnwrappedLine(); |
3928 | 12 | return true; |
3929 | 12 | } |
3930 | | |
3931 | 57 | addUnwrappedLine(); |
3932 | 57 | parseObjCUntilAtEnd(); |
3933 | 57 | return true; |
3934 | 69 | } |
3935 | | |
3936 | 554 | void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { |
3937 | 554 | bool IsImport = FormatTok->is(Keywords.kw_import); |
3938 | 554 | assert(IsImport || FormatTok->is(tok::kw_export)); |
3939 | 0 | nextToken(); |
3940 | | |
3941 | | // Consume the "default" in "export default class/function". |
3942 | 554 | if (FormatTok->is(tok::kw_default)) |
3943 | 42 | nextToken(); |
3944 | | |
3945 | | // Consume "async function", "function" and "default function", so that these |
3946 | | // get parsed as free-standing JS functions, i.e. do not require a trailing |
3947 | | // semicolon. |
3948 | 554 | if (FormatTok->is(Keywords.kw_async)) |
3949 | 4 | nextToken(); |
3950 | 554 | if (FormatTok->is(Keywords.kw_function)) { |
3951 | 58 | nextToken(); |
3952 | 58 | return; |
3953 | 58 | } |
3954 | | |
3955 | | // For imports, `export *`, `export {...}`, consume the rest of the line up |
3956 | | // to the terminating `;`. For everything else, just return and continue |
3957 | | // parsing the structural element, i.e. the declaration or expression for |
3958 | | // `export default`. |
3959 | 496 | if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star)174 && |
3960 | 496 | !FormatTok->isStringLiteral()125 ) { |
3961 | 125 | return; |
3962 | 125 | } |
3963 | | |
3964 | 1.50k | while (371 !eof()) { |
3965 | 1.50k | if (FormatTok->is(tok::semi)) |
3966 | 363 | return; |
3967 | 1.13k | if (Line->Tokens.empty()) { |
3968 | | // Common issue: Automatic Semicolon Insertion wrapped the line, so the |
3969 | | // import statement should terminate. |
3970 | 4 | return; |
3971 | 4 | } |
3972 | 1.13k | if (FormatTok->is(tok::l_brace)) { |
3973 | 330 | FormatTok->setBlockKind(BK_Block); |
3974 | 330 | nextToken(); |
3975 | 330 | parseBracedList(); |
3976 | 805 | } else { |
3977 | 805 | nextToken(); |
3978 | 805 | } |
3979 | 1.13k | } |
3980 | 371 | } |
3981 | | |
3982 | 99 | void UnwrappedLineParser::parseStatementMacro() { |
3983 | 99 | nextToken(); |
3984 | 99 | if (FormatTok->is(tok::l_paren)) |
3985 | 90 | parseParens(); |
3986 | 99 | if (FormatTok->is(tok::semi)) |
3987 | 9 | nextToken(); |
3988 | 99 | addUnwrappedLine(); |
3989 | 99 | } |
3990 | | |
3991 | | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
3992 | 0 | StringRef Prefix = "") { |
3993 | 0 | llvm::dbgs() << Prefix << "Line(" << Line.Level |
3994 | 0 | << ", FSC=" << Line.FirstStartColumn << ")" |
3995 | 0 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
3996 | 0 | for (const auto &Node : Line.Tokens) { |
3997 | 0 | llvm::dbgs() << Node.Tok->Tok.getName() << "[" |
3998 | 0 | << "T=" << static_cast<unsigned>(Node.Tok->getType()) |
3999 | 0 | << ", OC=" << Node.Tok->OriginalColumn << "] "; |
4000 | 0 | } |
4001 | 0 | for (const auto &Node : Line.Tokens) |
4002 | 0 | for (const auto &ChildNode : Node.Children) |
4003 | 0 | printDebugInfo(ChildNode, "\nChild: "); |
4004 | |
|
4005 | 0 | llvm::dbgs() << "\n"; |
4006 | 0 | } |
4007 | | |
4008 | 452k | void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) { |
4009 | 452k | if (Line->Tokens.empty()) |
4010 | 198k | return; |
4011 | 254k | LLVM_DEBUG({ |
4012 | 254k | if (CurrentLines == &Lines) |
4013 | 254k | printDebugInfo(*Line); |
4014 | 254k | }); |
4015 | | |
4016 | | // If this line closes a block when in Whitesmiths mode, remember that |
4017 | | // information so that the level can be decreased after the line is added. |
4018 | | // This has to happen after the addition of the line since the line itself |
4019 | | // needs to be indented. |
4020 | 254k | bool ClosesWhitesmithsBlock = |
4021 | 254k | Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex && |
4022 | 254k | Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths36.8k ; |
4023 | | |
4024 | 254k | CurrentLines->push_back(std::move(*Line)); |
4025 | 254k | Line->Tokens.clear(); |
4026 | 254k | Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; |
4027 | 254k | Line->FirstStartColumn = 0; |
4028 | | |
4029 | 254k | if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove702 ) |
4030 | 666 | --Line->Level; |
4031 | 254k | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()241k ) { |
4032 | 2.23k | CurrentLines->append( |
4033 | 2.23k | std::make_move_iterator(PreprocessorDirectives.begin()), |
4034 | 2.23k | std::make_move_iterator(PreprocessorDirectives.end())); |
4035 | 2.23k | PreprocessorDirectives.clear(); |
4036 | 2.23k | } |
4037 | | // Disconnect the current token from the last token on the previous line. |
4038 | 254k | FormatTok->Previous = nullptr; |
4039 | 254k | } |
4040 | | |
4041 | 1.86M | bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); } |
4042 | | |
4043 | 1.07M | bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { |
4044 | 1.07M | return (Line->InPPDirective || FormatTok.HasUnescapedNewline1.01M ) && |
4045 | 1.07M | FormatTok.NewlinesBefore > 0150k ; |
4046 | 1.07M | } |
4047 | | |
4048 | | // Checks if \p FormatTok is a line comment that continues the line comment |
4049 | | // section on \p Line. |
4050 | | static bool |
4051 | | continuesLineCommentSection(const FormatToken &FormatTok, |
4052 | | const UnwrappedLine &Line, |
4053 | 23.3k | const llvm::Regex &CommentPragmasRegex) { |
4054 | 23.3k | if (Line.Tokens.empty()) |
4055 | 7.56k | return false; |
4056 | | |
4057 | 15.7k | StringRef IndentContent = FormatTok.TokenText; |
4058 | 15.7k | if (FormatTok.TokenText.startswith("//") || |
4059 | 15.7k | FormatTok.TokenText.startswith("/*")2.47k ) { |
4060 | 15.7k | IndentContent = FormatTok.TokenText.substr(2); |
4061 | 15.7k | } |
4062 | 15.7k | if (CommentPragmasRegex.match(IndentContent)) |
4063 | 7 | return false; |
4064 | | |
4065 | | // If Line starts with a line comment, then FormatTok continues the comment |
4066 | | // section if its original column is greater or equal to the original start |
4067 | | // column of the line. |
4068 | | // |
4069 | | // Define the min column token of a line as follows: if a line ends in '{' or |
4070 | | // contains a '{' followed by a line comment, then the min column token is |
4071 | | // that '{'. Otherwise, the min column token of the line is the first token of |
4072 | | // the line. |
4073 | | // |
4074 | | // If Line starts with a token other than a line comment, then FormatTok |
4075 | | // continues the comment section if its original column is greater than the |
4076 | | // original start column of the min column token of the line. |
4077 | | // |
4078 | | // For example, the second line comment continues the first in these cases: |
4079 | | // |
4080 | | // // first line |
4081 | | // // second line |
4082 | | // |
4083 | | // and: |
4084 | | // |
4085 | | // // first line |
4086 | | // // second line |
4087 | | // |
4088 | | // and: |
4089 | | // |
4090 | | // int i; // first line |
4091 | | // // second line |
4092 | | // |
4093 | | // and: |
4094 | | // |
4095 | | // do { // first line |
4096 | | // // second line |
4097 | | // int i; |
4098 | | // } while (true); |
4099 | | // |
4100 | | // and: |
4101 | | // |
4102 | | // enum { |
4103 | | // a, // first line |
4104 | | // // second line |
4105 | | // b |
4106 | | // }; |
4107 | | // |
4108 | | // The second line comment doesn't continue the first in these cases: |
4109 | | // |
4110 | | // // first line |
4111 | | // // second line |
4112 | | // |
4113 | | // and: |
4114 | | // |
4115 | | // int i; // first line |
4116 | | // // second line |
4117 | | // |
4118 | | // and: |
4119 | | // |
4120 | | // do { // first line |
4121 | | // // second line |
4122 | | // int i; |
4123 | | // } while (true); |
4124 | | // |
4125 | | // and: |
4126 | | // |
4127 | | // enum { |
4128 | | // a, // first line |
4129 | | // // second line |
4130 | | // }; |
4131 | 15.7k | const FormatToken *MinColumnToken = Line.Tokens.front().Tok; |
4132 | | |
4133 | | // Scan for '{//'. If found, use the column of '{' as a min column for line |
4134 | | // comment section continuation. |
4135 | 15.7k | const FormatToken *PreviousToken = nullptr; |
4136 | 76.4k | for (const UnwrappedLineNode &Node : Line.Tokens) { |
4137 | 76.4k | if (PreviousToken && PreviousToken->is(tok::l_brace)60.7k && |
4138 | 76.4k | isLineComment(*Node.Tok)2.18k ) { |
4139 | 281 | MinColumnToken = PreviousToken; |
4140 | 281 | break; |
4141 | 281 | } |
4142 | 76.2k | PreviousToken = Node.Tok; |
4143 | | |
4144 | | // Grab the last newline preceding a token in this unwrapped line. |
4145 | 76.2k | if (Node.Tok->NewlinesBefore > 0) |
4146 | 16.6k | MinColumnToken = Node.Tok; |
4147 | 76.2k | } |
4148 | 15.7k | if (PreviousToken && PreviousToken->is(tok::l_brace)) |
4149 | 3.09k | MinColumnToken = PreviousToken; |
4150 | | |
4151 | 15.7k | return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, |
4152 | 15.7k | MinColumnToken); |
4153 | 15.7k | } |
4154 | | |
4155 | 1.11M | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
4156 | 1.11M | bool JustComments = Line->Tokens.empty(); |
4157 | 1.11M | for (FormatToken *Tok : CommentsBeforeNextToken) { |
4158 | | // Line comments that belong to the same line comment section are put on the |
4159 | | // same line since later we might want to reflow content between them. |
4160 | | // Additional fine-grained breaking of line comment sections is controlled |
4161 | | // by the class BreakableLineCommentSection in case it is desirable to keep |
4162 | | // several line comment sections in the same unwrapped line. |
4163 | | // |
4164 | | // FIXME: Consider putting separate line comment sections as children to the |
4165 | | // unwrapped line instead. |
4166 | 7.92k | Tok->ContinuesLineCommentSection = |
4167 | 7.92k | continuesLineCommentSection(*Tok, *Line, CommentPragmasRegex); |
4168 | 7.92k | if (isOnNewLine(*Tok) && JustComments6.36k && !Tok->ContinuesLineCommentSection5.80k ) |
4169 | 3.49k | addUnwrappedLine(); |
4170 | 7.92k | pushToken(Tok); |
4171 | 7.92k | } |
4172 | 1.11M | if (NewlineBeforeNext && JustComments157k ) |
4173 | 133k | addUnwrappedLine(); |
4174 | 1.11M | CommentsBeforeNextToken.clear(); |
4175 | 1.11M | } |
4176 | | |
4177 | 1.03M | void UnwrappedLineParser::nextToken(int LevelDifference) { |
4178 | 1.03M | if (eof()) |
4179 | 4.87k | return; |
4180 | 1.03M | flushComments(isOnNewLine(*FormatTok)); |
4181 | 1.03M | pushToken(FormatTok); |
4182 | 1.03M | FormatToken *Previous = FormatTok; |
4183 | 1.03M | if (!Style.isJavaScript()) |
4184 | 996k | readToken(LevelDifference); |
4185 | 34.1k | else |
4186 | 34.1k | readTokenWithJavaScriptASI(); |
4187 | 1.03M | FormatTok->Previous = Previous; |
4188 | 1.03M | if (Style.isVerilog()) { |
4189 | | // Blocks in Verilog can have `begin` and `end` instead of braces. For |
4190 | | // keywords like `begin`, we can't treat them the same as left braces |
4191 | | // because some contexts require one of them. For example structs use |
4192 | | // braces and if blocks use keywords, and a left brace can occur in an if |
4193 | | // statement, but it is not a block. For keywords like `end`, we simply |
4194 | | // treat them the same as right braces. |
4195 | 920 | if (Keywords.isVerilogEnd(*FormatTok)) |
4196 | 28 | FormatTok->Tok.setKind(tok::r_brace); |
4197 | 920 | } |
4198 | 1.03M | } |
4199 | | |
4200 | | void UnwrappedLineParser::distributeComments( |
4201 | | const SmallVectorImpl<FormatToken *> &Comments, |
4202 | 1.10M | const FormatToken *NextTok) { |
4203 | | // Whether or not a line comment token continues a line is controlled by |
4204 | | // the method continuesLineCommentSection, with the following caveat: |
4205 | | // |
4206 | | // Define a trail of Comments to be a nonempty proper postfix of Comments such |
4207 | | // that each comment line from the trail is aligned with the next token, if |
4208 | | // the next token exists. If a trail exists, the beginning of the maximal |
4209 | | // trail is marked as a start of a new comment section. |
4210 | | // |
4211 | | // For example in this code: |
4212 | | // |
4213 | | // int a; // line about a |
4214 | | // // line 1 about b |
4215 | | // // line 2 about b |
4216 | | // int b; |
4217 | | // |
4218 | | // the two lines about b form a maximal trail, so there are two sections, the |
4219 | | // first one consisting of the single comment "// line about a" and the |
4220 | | // second one consisting of the next two comments. |
4221 | 1.10M | if (Comments.empty()) |
4222 | 1.09M | return; |
4223 | 12.7k | bool ShouldPushCommentsInCurrentLine = true; |
4224 | 12.7k | bool HasTrailAlignedWithNextToken = false; |
4225 | 12.7k | unsigned StartOfTrailAlignedWithNextToken = 0; |
4226 | 12.7k | if (NextTok) { |
4227 | | // We are skipping the first element intentionally. |
4228 | 16.1k | for (unsigned i = Comments.size() - 1; i > 0; --i3.37k ) { |
4229 | 3.37k | if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { |
4230 | 2.55k | HasTrailAlignedWithNextToken = true; |
4231 | 2.55k | StartOfTrailAlignedWithNextToken = i; |
4232 | 2.55k | } |
4233 | 3.37k | } |
4234 | 12.7k | } |
4235 | 28.8k | for (unsigned i = 0, e = Comments.size(); i < e; ++i16.1k ) { |
4236 | 16.1k | FormatToken *FormatTok = Comments[i]; |
4237 | 16.1k | if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken3.33k ) { |
4238 | 674 | FormatTok->ContinuesLineCommentSection = false; |
4239 | 15.4k | } else { |
4240 | 15.4k | FormatTok->ContinuesLineCommentSection = |
4241 | 15.4k | continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); |
4242 | 15.4k | } |
4243 | 16.1k | if (!FormatTok->ContinuesLineCommentSection && |
4244 | 16.1k | (15.8k isOnNewLine(*FormatTok)15.8k || FormatTok->IsFirst9.54k )) { |
4245 | 7.87k | ShouldPushCommentsInCurrentLine = false; |
4246 | 7.87k | } |
4247 | 16.1k | if (ShouldPushCommentsInCurrentLine) |
4248 | 8.17k | pushToken(FormatTok); |
4249 | 7.92k | else |
4250 | 7.92k | CommentsBeforeNextToken.push_back(FormatTok); |
4251 | 16.1k | } |
4252 | 12.7k | } |
4253 | | |
4254 | 1.09M | void UnwrappedLineParser::readToken(int LevelDifference) { |
4255 | 1.09M | SmallVector<FormatToken *, 1> Comments; |
4256 | 1.09M | bool PreviousWasComment = false; |
4257 | 1.09M | bool FirstNonCommentOnLine = false; |
4258 | 1.12M | do { |
4259 | 1.12M | FormatTok = Tokens->getNextToken(); |
4260 | 1.12M | assert(FormatTok); |
4261 | 1.12M | while (FormatTok->getType() == TT_ConflictStart || |
4262 | 1.12M | FormatTok->getType() == TT_ConflictEnd1.12M || |
4263 | 1.12M | FormatTok->getType() == TT_ConflictAlternative1.12M ) { |
4264 | 126 | if (FormatTok->getType() == TT_ConflictStart) |
4265 | 27 | conditionalCompilationStart(/*Unreachable=*/false); |
4266 | 99 | else if (FormatTok->getType() == TT_ConflictAlternative) |
4267 | 72 | conditionalCompilationAlternative(); |
4268 | 27 | else if (FormatTok->getType() == TT_ConflictEnd) |
4269 | 27 | conditionalCompilationEnd(); |
4270 | 126 | FormatTok = Tokens->getNextToken(); |
4271 | 126 | FormatTok->MustBreakBefore = true; |
4272 | 126 | } |
4273 | | |
4274 | 1.12M | auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine, |
4275 | 1.12M | const FormatToken &Tok, |
4276 | 1.13M | bool PreviousWasComment) { |
4277 | 1.13M | auto IsFirstOnLine = [](const FormatToken &Tok) { |
4278 | 1.12M | return Tok.HasUnescapedNewline || Tok.IsFirst1.04M ; |
4279 | 1.12M | }; |
4280 | | |
4281 | | // Consider preprocessor directives preceded by block comments as first |
4282 | | // on line. |
4283 | 1.13M | if (PreviousWasComment) |
4284 | 16.5k | return FirstNonCommentOnLine || IsFirstOnLine(Tok)8.01k ; |
4285 | 1.11M | return IsFirstOnLine(Tok); |
4286 | 1.13M | }; |
4287 | | |
4288 | 1.12M | FirstNonCommentOnLine = IsFirstNonCommentOnLine( |
4289 | 1.12M | FirstNonCommentOnLine, *FormatTok, PreviousWasComment); |
4290 | 1.12M | PreviousWasComment = FormatTok->is(tok::comment); |
4291 | | |
4292 | 1.13M | while (!Line->InPPDirective && FormatTok->is(tok::hash)1.07M && |
4293 | 1.13M | (13.4k !Style.isVerilog()13.4k || |
4294 | 13.4k | Keywords.isVerilogPPDirective(*Tokens->peekNextToken())43 ) && |
4295 | 1.13M | FirstNonCommentOnLine13.4k ) { |
4296 | 13.4k | distributeComments(Comments, FormatTok); |
4297 | 13.4k | Comments.clear(); |
4298 | | // If there is an unfinished unwrapped line, we flush the preprocessor |
4299 | | // directives only after that unwrapped line was finished later. |
4300 | 13.4k | bool SwitchToPreprocessorLines = !Line->Tokens.empty(); |
4301 | 13.4k | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
4302 | 13.4k | assert((LevelDifference >= 0 || |
4303 | 13.4k | static_cast<unsigned>(-LevelDifference) <= Line->Level) && |
4304 | 13.4k | "LevelDifference makes Line->Level negative"); |
4305 | 0 | Line->Level += LevelDifference; |
4306 | | // Comments stored before the preprocessor directive need to be output |
4307 | | // before the preprocessor directive, at the same level as the |
4308 | | // preprocessor directive, as we consider them to apply to the directive. |
4309 | 13.4k | if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && |
4310 | 13.4k | PPBranchLevel > 0312 ) { |
4311 | 96 | Line->Level += PPBranchLevel; |
4312 | 96 | } |
4313 | 13.4k | flushComments(isOnNewLine(*FormatTok)); |
4314 | 13.4k | parsePPDirective(); |
4315 | 13.4k | PreviousWasComment = FormatTok->is(tok::comment); |
4316 | 13.4k | FirstNonCommentOnLine = IsFirstNonCommentOnLine( |
4317 | 13.4k | FirstNonCommentOnLine, *FormatTok, PreviousWasComment); |
4318 | 13.4k | } |
4319 | | |
4320 | 1.12M | if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable)29.0k && |
4321 | 1.12M | !Line->InPPDirective13.4k ) { |
4322 | 7.49k | continue; |
4323 | 7.49k | } |
4324 | | |
4325 | 1.11M | if (!FormatTok->is(tok::comment)) { |
4326 | 1.09M | distributeComments(Comments, FormatTok); |
4327 | 1.09M | Comments.clear(); |
4328 | 1.09M | return; |
4329 | 1.09M | } |
4330 | | |
4331 | 16.1k | Comments.push_back(FormatTok); |
4332 | 23.5k | } while (!eof()); |
4333 | | |
4334 | 0 | distributeComments(Comments, nullptr); |
4335 | 0 | Comments.clear(); |
4336 | 0 | } |
4337 | | |
4338 | 1.11M | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
4339 | 1.11M | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
4340 | 1.11M | if (MustBreakBeforeNextToken) { |
4341 | 4.84k | Line->Tokens.back().Tok->MustBreakBefore = true; |
4342 | 4.84k | MustBreakBeforeNextToken = false; |
4343 | 4.84k | } |
4344 | 1.11M | } |
4345 | | |
4346 | | } // end namespace format |
4347 | | } // end namespace clang |