Coverage Report

Created: 2020-02-18 08:44

/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 "llvm/ADT/STLExtras.h"
17
#include "llvm/Support/Debug.h"
18
#include "llvm/Support/raw_ostream.h"
19
20
#include <algorithm>
21
22
#define DEBUG_TYPE "format-parser"
23
24
namespace clang {
25
namespace format {
26
27
class FormatTokenSource {
28
public:
29
41.0k
  virtual ~FormatTokenSource() {}
30
  virtual FormatToken *getNextToken() = 0;
31
32
  virtual unsigned getPosition() = 0;
33
  virtual FormatToken *setPosition(unsigned Position) = 0;
34
};
35
36
namespace {
37
38
class ScopedDeclarationState {
39
public:
40
  ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
41
                         bool MustBeDeclaration)
42
59.1k
      : Line(Line), Stack(Stack) {
43
59.1k
    Line.MustBeDeclaration = MustBeDeclaration;
44
59.1k
    Stack.push_back(MustBeDeclaration);
45
59.1k
  }
46
59.1k
  ~ScopedDeclarationState() {
47
59.1k
    Stack.pop_back();
48
59.1k
    if (!Stack.empty())
49
24.1k
      Line.MustBeDeclaration = Stack.back();
50
34.9k
    else
51
34.9k
      Line.MustBeDeclaration = true;
52
59.1k
  }
53
54
private:
55
  UnwrappedLine &Line;
56
  std::vector<bool> &Stack;
57
};
58
59
36.9k
static bool isLineComment(const FormatToken &FormatTok) {
60
36.9k
  return FormatTok.is(tok::comment) && 
!FormatTok.TokenText.startswith("/*")13.0k
;
61
36.9k
}
62
63
// Checks if \p FormatTok is a line comment that continues the line comment
64
// \p Previous. The original column of \p MinColumnToken is used to determine
65
// whether \p FormatTok is indented enough to the right to continue \p Previous.
66
static bool continuesLineComment(const FormatToken &FormatTok,
67
                                 const FormatToken *Previous,
68
16.0k
                                 const FormatToken *MinColumnToken) {
69
16.0k
  if (!Previous || 
!MinColumnToken16.0k
)
70
10
    return false;
71
16.0k
  unsigned MinContinueColumn =
72
16.0k
      MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 
01.68k
:
114.3k
);
73
16.0k
  return isLineComment(FormatTok) && 
FormatTok.NewlinesBefore == 18.29k
&&
74
16.0k
         
isLineComment(*Previous)3.85k
&&
75
16.0k
         
FormatTok.OriginalColumn >= MinContinueColumn1.72k
;
76
16.0k
}
77
78
class ScopedMacroState : public FormatTokenSource {
79
public:
80
  ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
81
                   FormatToken *&ResetToken)
82
      : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
83
        PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
84
8.25k
        Token(nullptr), PreviousToken(nullptr) {
85
8.25k
    FakeEOF.Tok.startToken();
86
8.25k
    FakeEOF.Tok.setKind(tok::eof);
87
8.25k
    TokenSource = this;
88
8.25k
    Line.Level = 0;
89
8.25k
    Line.InPPDirective = true;
90
8.25k
  }
91
92
8.25k
  ~ScopedMacroState() override {
93
8.25k
    TokenSource = PreviousTokenSource;
94
8.25k
    ResetToken = Token;
95
8.25k
    Line.InPPDirective = false;
96
8.25k
    Line.Level = PreviousLineLevel;
97
8.25k
  }
98
99
38.8k
  FormatToken *getNextToken() override {
100
38.8k
    // The \c UnwrappedLineParser guards against this by never calling
101
38.8k
    // \c getNextToken() after it has encountered the first eof token.
102
38.8k
    assert(!eof());
103
38.8k
    PreviousToken = Token;
104
38.8k
    Token = PreviousTokenSource->getNextToken();
105
38.8k
    if (eof())
106
6.89k
      return &FakeEOF;
107
31.9k
    return Token;
108
31.9k
  }
109
110
1.04k
  unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
111
112
103
  FormatToken *setPosition(unsigned Position) override {
113
103
    PreviousToken = nullptr;
114
103
    Token = PreviousTokenSource->setPosition(Position);
115
103
    return Token;
116
103
  }
117
118
private:
119
77.7k
  bool eof() {
120
77.7k
    return Token && 
Token->HasUnescapedNewline69.5k
&&
121
77.7k
           !continuesLineComment(*Token, PreviousToken,
122
6.93k
                                 /*MinColumnToken=*/PreviousToken);
123
77.7k
  }
124
125
  FormatToken FakeEOF;
126
  UnwrappedLine &Line;
127
  FormatTokenSource *&TokenSource;
128
  FormatToken *&ResetToken;
129
  unsigned PreviousLineLevel;
130
  FormatTokenSource *PreviousTokenSource;
131
132
  FormatToken *Token;
133
  FormatToken *PreviousToken;
134
};
135
136
} // end anonymous namespace
137
138
class ScopedLineState {
139
public:
140
  ScopedLineState(UnwrappedLineParser &Parser,
141
                  bool SwitchToPreprocessorLines = false)
142
10.7k
      : Parser(Parser), OriginalLines(Parser.CurrentLines) {
143
10.7k
    if (SwitchToPreprocessorLines)
144
1.72k
      Parser.CurrentLines = &Parser.PreprocessorDirectives;
145
8.98k
    else if (!Parser.Line->Tokens.empty())
146
2.45k
      Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
147
10.7k
    PreBlockLine = std::move(Parser.Line);
148
10.7k
    Parser.Line = std::make_unique<UnwrappedLine>();
149
10.7k
    Parser.Line->Level = PreBlockLine->Level;
150
10.7k
    Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
151
10.7k
  }
152
153
10.7k
  ~ScopedLineState() {
154
10.7k
    if (!Parser.Line->Tokens.empty()) {
155
3
      Parser.addUnwrappedLine();
156
3
    }
157
10.7k
    assert(Parser.Line->Tokens.empty());
158
10.7k
    Parser.Line = std::move(PreBlockLine);
159
10.7k
    if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
160
1.72k
      Parser.MustBreakBeforeNextToken = true;
161
10.7k
    Parser.CurrentLines = OriginalLines;
162
10.7k
  }
163
164
private:
165
  UnwrappedLineParser &Parser;
166
167
  std::unique_ptr<UnwrappedLine> PreBlockLine;
168
  SmallVectorImpl<UnwrappedLine> *OriginalLines;
169
};
170
171
class CompoundStatementIndenter {
172
public:
173
  CompoundStatementIndenter(UnwrappedLineParser *Parser,
174
                            const FormatStyle &Style, unsigned &LineLevel)
175
      : CompoundStatementIndenter(Parser, LineLevel,
176
                                  Style.BraceWrapping.AfterControlStatement,
177
2.83k
                                  Style.BraceWrapping.IndentBraces) {}
178
  CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel,
179
                            bool WrapBrace, bool IndentBrace)
180
2.95k
      : LineLevel(LineLevel), OldLineLevel(LineLevel) {
181
2.95k
    if (WrapBrace)
182
566
      Parser->addUnwrappedLine();
183
2.95k
    if (IndentBrace)
184
84
      ++LineLevel;
185
2.95k
  }
186
2.95k
  ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
187
188
private:
189
  unsigned &LineLevel;
190
  unsigned OldLineLevel;
191
};
192
193
namespace {
194
195
class IndexedTokenSource : public FormatTokenSource {
196
public:
197
  IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
198
32.7k
      : Tokens(Tokens), Position(-1) {}
199
200
653k
  FormatToken *getNextToken() override {
201
653k
    ++Position;
202
653k
    return Tokens[Position];
203
653k
  }
204
205
12.5k
  unsigned getPosition() override {
206
12.5k
    assert(Position >= 0);
207
12.5k
    return Position;
208
12.5k
  }
209
210
11.5k
  FormatToken *setPosition(unsigned P) override {
211
11.5k
    Position = P;
212
11.5k
    return Tokens[Position];
213
11.5k
  }
214
215
33.0k
  void reset() { Position = -1; }
216
217
private:
218
  ArrayRef<FormatToken *> Tokens;
219
  int Position;
220
};
221
222
} // end anonymous namespace
223
224
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
225
                                         const AdditionalKeywords &Keywords,
226
                                         unsigned FirstStartColumn,
227
                                         ArrayRef<FormatToken *> Tokens,
228
                                         UnwrappedLineConsumer &Callback)
229
    : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
230
      CurrentLines(&Lines), Style(Style), Keywords(Keywords),
231
      CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
232
      Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
233
      IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
234
                       ? IG_Rejected
235
                       : IG_Inited),
236
32.7k
      IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
237
238
33.0k
void UnwrappedLineParser::reset() {
239
33.0k
  PPBranchLevel = -1;
240
33.0k
  IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
241
33.0k
                     ? 
IG_Rejected32.8k
242
33.0k
                     : 
IG_Inited207
;
243
33.0k
  IncludeGuardToken = nullptr;
244
33.0k
  Line.reset(new UnwrappedLine);
245
33.0k
  CommentsBeforeNextToken.clear();
246
33.0k
  FormatTok = nullptr;
247
33.0k
  MustBreakBeforeNextToken = false;
248
33.0k
  PreprocessorDirectives.clear();
249
33.0k
  CurrentLines = &Lines;
250
33.0k
  DeclarationScopeStack.clear();
251
33.0k
  PPStack.clear();
252
33.0k
  Line->FirstStartColumn = FirstStartColumn;
253
33.0k
}
254
255
32.7k
void UnwrappedLineParser::parse() {
256
32.7k
  IndexedTokenSource TokenSource(AllTokens);
257
32.7k
  Line->FirstStartColumn = FirstStartColumn;
258
33.0k
  do {
259
33.0k
    LLVM_DEBUG(llvm::dbgs() << "----\n");
260
33.0k
    reset();
261
33.0k
    Tokens = &TokenSource;
262
33.0k
    TokenSource.reset();
263
33.0k
264
33.0k
    readToken();
265
33.0k
    parseFile();
266
33.0k
267
33.0k
    // If we found an include guard then all preprocessor directives (other than
268
33.0k
    // the guard) are over-indented by one.
269
33.0k
    if (IncludeGuard == IG_Found)
270
34
      for (auto &Line : Lines)
271
177
        if (Line.InPPDirective && 
Line.Level > 0130
)
272
62
          --Line.Level;
273
33.0k
274
33.0k
    // Create line with eof token.
275
33.0k
    pushToken(FormatTok);
276
33.0k
    addUnwrappedLine();
277
33.0k
278
33.0k
    for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
279
33.0k
                                                  E = Lines.end();
280
163k
         I != E; 
++I130k
) {
281
130k
      Callback.consumeUnwrappedLine(*I);
282
130k
    }
283
33.0k
    Callback.finishRun();
284
33.0k
    Lines.clear();
285
33.7k
    while (!PPLevelBranchIndex.empty() &&
286
33.7k
           
PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()962
) {
287
686
      PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
288
686
      PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
289
686
    }
290
33.0k
    if (!PPLevelBranchIndex.empty()) {
291
276
      ++PPLevelBranchIndex.back();
292
276
      assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
293
276
      assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
294
276
    }
295
33.0k
  } while (!PPLevelBranchIndex.empty());
296
32.7k
}
297
298
35.2k
void UnwrappedLineParser::parseFile() {
299
35.2k
  // The top-level context in a file always has declarations, except for pre-
300
35.2k
  // processor directives and JavaScript files.
301
35.2k
  bool MustBeDeclaration =
302
35.2k
      !Line->InPPDirective && 
Style.Language != FormatStyle::LK_JavaScript33.0k
;
303
35.2k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
304
35.2k
                                          MustBeDeclaration);
305
35.2k
  if (Style.Language == FormatStyle::LK_TextProto)
306
606
    parseBracedList();
307
34.6k
  else
308
34.6k
    parseLevel(/*HasOpeningBrace=*/false);
309
35.2k
  // Make sure to format the remaining tokens.
310
35.2k
  //
311
35.2k
  // LK_TextProto is special since its top-level is parsed as the body of a
312
35.2k
  // braced list, which does not necessarily have natural line separators such
313
35.2k
  // as a semicolon. Comments after the last entry that have been determined to
314
35.2k
  // not belong to that line, as in:
315
35.2k
  //   key: value
316
35.2k
  //   // endfile comment
317
35.2k
  // do not have a chance to be put on a line of their own until this point.
318
35.2k
  // Here we add this newline before end-of-file comments.
319
35.2k
  if (Style.Language == FormatStyle::LK_TextProto &&
320
35.2k
      
!CommentsBeforeNextToken.empty()606
)
321
12
    addUnwrappedLine();
322
35.2k
  flushComments(true);
323
35.2k
  addUnwrappedLine();
324
35.2k
}
325
326
50
void UnwrappedLineParser::parseCSharpAttribute() {
327
208
  do {
328
208
    switch (FormatTok->Tok.getKind()) {
329
50
    case tok::r_square:
330
50
      nextToken();
331
50
      addUnwrappedLine();
332
50
      return;
333
158
    default:
334
158
      nextToken();
335
158
      break;
336
158
    }
337
158
  } while (!eof());
338
50
}
339
340
58.5k
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
341
58.5k
  bool SwitchLabelEncountered = false;
342
90.1k
  do {
343
90.1k
    tok::TokenKind kind = FormatTok->Tok.getKind();
344
90.1k
    if (FormatTok->Type == TT_MacroBlockBegin) {
345
28
      kind = tok::l_brace;
346
90.1k
    } else if (FormatTok->Type == TT_MacroBlockEnd) {
347
28
      kind = tok::r_brace;
348
28
    }
349
90.1k
350
90.1k
    switch (kind) {
351
0
    case tok::comment:
352
0
      nextToken();
353
0
      addUnwrappedLine();
354
0
      break;
355
482
    case tok::l_brace:
356
482
      // FIXME: Add parameter whether this can happen - if this happens, we must
357
482
      // be in a non-declaration context.
358
482
      if (!FormatTok->is(TT_MacroBlockBegin) && 
tryToParseBracedList()454
)
359
7
        continue;
360
475
      parseBlock(/*MustBeDeclaration=*/false);
361
475
      addUnwrappedLine();
362
475
      break;
363
23.8k
    case tok::r_brace:
364
23.8k
      if (HasOpeningBrace)
365
23.7k
        return;
366
62
      nextToken();
367
62
      addUnwrappedLine();
368
62
      break;
369
107
    case tok::kw_default: {
370
107
      unsigned StoredPosition = Tokens->getPosition();
371
107
      FormatToken *Next;
372
110
      do {
373
110
        Next = Tokens->getNextToken();
374
110
      } while (Next && Next->is(tok::comment));
375
107
      FormatTok = Tokens->setPosition(StoredPosition);
376
107
      if (Next && Next->isNot(tok::colon)) {
377
2
        // default not followed by ':' is not a case label; treat it like
378
2
        // an identifier.
379
2
        parseStructuralElement();
380
2
        break;
381
2
      }
382
105
      // Else, if it is 'default:', fall through to the case handling.
383
105
      LLVM_FALLTHROUGH;
384
105
    }
385
552
    case tok::kw_case:
386
552
      if (Style.Language == FormatStyle::LK_JavaScript &&
387
552
          
Line->MustBeDeclaration12
) {
388
8
        // A 'case: string' style field declaration.
389
8
        parseStructuralElement();
390
8
        break;
391
8
      }
392
544
      if (!SwitchLabelEncountered &&
393
544
          
(331
Style.IndentCaseLabels331
||
(284
Line->InPPDirective284
&&
Line->Level == 121
)))
394
61
        ++Line->Level;
395
544
      SwitchLabelEncountered = true;
396
544
      parseStructuralElement();
397
544
      break;
398
794
    case tok::l_square:
399
794
      if (Style.isCSharp()) {
400
50
        nextToken();
401
50
        parseCSharpAttribute();
402
50
        break;
403
50
      }
404
744
      LLVM_FALLTHROUGH;
405
65.2k
    default:
406
65.2k
      parseStructuralElement();
407
65.2k
      break;
408
66.3k
    }
409
66.3k
  } while (!eof());
410
58.5k
}
411
412
11.4k
void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
413
11.4k
  // We'll parse forward through the tokens until we hit
414
11.4k
  // a closing brace or eof - note that getNextToken() will
415
11.4k
  // parse macros, so this will magically work inside macro
416
11.4k
  // definitions, too.
417
11.4k
  unsigned StoredPosition = Tokens->getPosition();
418
11.4k
  FormatToken *Tok = FormatTok;
419
11.4k
  const FormatToken *PrevTok = Tok->Previous;
420
11.4k
  // Keep a stack of positions of lbrace tokens. We will
421
11.4k
  // update information about whether an lbrace starts a
422
11.4k
  // braced init list or a different block during the loop.
423
11.4k
  SmallVector<FormatToken *, 8> LBraceStack;
424
11.4k
  assert(Tok->Tok.is(tok::l_brace));
425
82.4k
  do {
426
82.4k
    // Get next non-comment token.
427
82.4k
    FormatToken *NextTok;
428
82.4k
    unsigned ReadTokens = 0;
429
83.7k
    do {
430
83.7k
      NextTok = Tokens->getNextToken();
431
83.7k
      ++ReadTokens;
432
83.7k
    } while (NextTok->is(tok::comment));
433
82.4k
434
82.4k
    switch (Tok->Tok.getKind()) {
435
13.1k
    case tok::l_brace:
436
13.1k
      if (Style.Language == FormatStyle::LK_JavaScript && 
PrevTok736
) {
437
726
        if (PrevTok->isOneOf(tok::colon, tok::less))
438
94
          // A ':' indicates this code is in a type, or a braced list
439
94
          // following a label in an object literal ({a: {b: 1}}).
440
94
          // A '<' could be an object used in a comparison, but that is nonsense
441
94
          // code (can never return true), so more likely it is a generic type
442
94
          // argument (`X<{a: string; b: number}>`).
443
94
          // The code below could be confused by semicolons between the
444
94
          // individual members in a type member list, which would normally
445
94
          // trigger BK_Block. In both cases, this must be parsed as an inline
446
94
          // braced init.
447
94
          Tok->BlockKind = BK_BracedInit;
448
632
        else if (PrevTok->is(tok::r_paren))
449
336
          // `) { }` can only occur in function or method declarations in JS.
450
336
          Tok->BlockKind = BK_Block;
451
12.4k
      } else {
452
12.4k
        Tok->BlockKind = BK_Unknown;
453
12.4k
      }
454
13.1k
      LBraceStack.push_back(Tok);
455
13.1k
      break;
456
13.0k
    case tok::r_brace:
457
13.0k
      if (LBraceStack.empty())
458
0
        break;
459
13.0k
      if (LBraceStack.back()->BlockKind == BK_Unknown) {
460
6.54k
        bool ProbablyBracedList = false;
461
6.54k
        if (Style.Language == FormatStyle::LK_Proto) {
462
36
          ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
463
6.51k
        } else {
464
6.51k
          // Using OriginalColumn to distinguish between ObjC methods and
465
6.51k
          // binary operators is a bit hacky.
466
6.51k
          bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
467
6.51k
                                  
NextTok->OriginalColumn == 014
;
468
6.51k
469
6.51k
          // If there is a comma, semicolon or right paren after the closing
470
6.51k
          // brace, we assume this is a braced initializer list.  Note that
471
6.51k
          // regardless how we mark inner braces here, we will overwrite the
472
6.51k
          // BlockKind later if we parse a braced list (where all blocks
473
6.51k
          // inside are by default braced lists), or when we explicitly detect
474
6.51k
          // blocks (for example while parsing lambdas).
475
6.51k
          // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
476
6.51k
          // braced list in JS.
477
6.51k
          ProbablyBracedList =
478
6.51k
              (Style.Language == FormatStyle::LK_JavaScript &&
479
6.51k
               NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
480
242
                                Keywords.kw_as)) ||
481
6.51k
              
(6.49k
Style.isCpp()6.49k
&&
NextTok->is(tok::l_paren)6.08k
) ||
482
6.51k
              NextTok->isOneOf(tok::comma, tok::period, tok::colon,
483
6.48k
                               tok::r_paren, tok::r_square, tok::l_brace,
484
6.48k
                               tok::ellipsis) ||
485
6.51k
              
(5.92k
NextTok->is(tok::identifier)5.92k
&&
486
5.92k
               
!PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)141
) ||
487
6.51k
              
(5.91k
NextTok->is(tok::semi)5.91k
&&
488
5.91k
               
(1.51k
!ExpectClassBody1.51k
||
LBraceStack.size() != 1536
)) ||
489
6.51k
              
(4.90k
NextTok->isBinaryOperator()4.90k
&&
!NextIsObjCMethod59
);
490
6.51k
          if (!Style.isCSharp() && 
NextTok->is(tok::l_square)6.41k
) {
491
14
            // We can have an array subscript after a braced init
492
14
            // list, but C++11 attributes are expected after blocks.
493
14
            NextTok = Tokens->getNextToken();
494
14
            ++ReadTokens;
495
14
            ProbablyBracedList = NextTok->isNot(tok::l_square);
496
14
          }
497
6.51k
        }
498
6.54k
        if (ProbablyBracedList) {
499
1.69k
          Tok->BlockKind = BK_BracedInit;
500
1.69k
          LBraceStack.back()->BlockKind = BK_BracedInit;
501
4.85k
        } else {
502
4.85k
          Tok->BlockKind = BK_Block;
503
4.85k
          LBraceStack.back()->BlockKind = BK_Block;
504
4.85k
        }
505
6.54k
      }
506
13.0k
      LBraceStack.pop_back();
507
13.0k
      break;
508
16.7k
    case tok::identifier:
509
16.7k
      if (!Tok->is(TT_StatementMacro))
510
16.6k
        break;
511
14
      LLVM_FALLTHROUGH;
512
8.13k
    case tok::at:
513
8.13k
    case tok::semi:
514
8.13k
    case tok::kw_if:
515
8.13k
    case tok::kw_while:
516
8.13k
    case tok::kw_for:
517
8.13k
    case tok::kw_switch:
518
8.13k
    case tok::kw_try:
519
8.13k
    case tok::kw___try:
520
8.13k
      if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
521
6.08k
        LBraceStack.back()->BlockKind = BK_Block;
522
8.13k
      break;
523
31.4k
    default:
524
31.4k
      break;
525
82.4k
    }
526
82.4k
    PrevTok = Tok;
527
82.4k
    Tok = NextTok;
528
82.4k
  } while (Tok->Tok.isNot(tok::eof) && 
!LBraceStack.empty()76.3k
);
529
11.4k
530
11.4k
  // Assume other blocks for all unclosed opening braces.
531
11.5k
  
for (unsigned i = 0, e = LBraceStack.size(); 11.4k
i != e;
++i109
) {
532
109
    if (LBraceStack[i]->BlockKind == BK_Unknown)
533
70
      LBraceStack[i]->BlockKind = BK_Block;
534
109
  }
535
11.4k
536
11.4k
  FormatTok = Tokens->setPosition(StoredPosition);
537
11.4k
}
538
539
template <class T>
540
636
static inline void hash_combine(std::size_t &seed, const T &v) {
541
636
  std::hash<T> hasher;
542
636
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
543
636
}
544
545
42.7k
size_t UnwrappedLineParser::computePPHash() const {
546
42.7k
  size_t h = 0;
547
42.7k
  for (const auto &i : PPStack) {
548
318
    hash_combine(h, size_t(i.Kind));
549
318
    hash_combine(h, i.Line);
550
318
  }
551
42.7k
  return h;
552
42.7k
}
553
554
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
555
21.4k
                                     bool MunchSemi) {
556
21.4k
  assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
557
21.4k
         "'{' or macro block token expected");
558
21.4k
  const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
559
21.4k
  FormatTok->BlockKind = BK_Block;
560
21.4k
561
21.4k
  size_t PPStartHash = computePPHash();
562
21.4k
563
21.4k
  unsigned InitialLevel = Line->Level;
564
21.4k
  nextToken(/*LevelDifference=*/AddLevel ? 
118.1k
:
03.30k
);
565
21.4k
566
21.4k
  if (MacroBlock && 
FormatTok->is(tok::l_paren)28
)
567
7
    parseParens();
568
21.4k
569
21.4k
  size_t NbPreprocessorDirectives =
570
21.4k
      CurrentLines == &Lines ? 
PreprocessorDirectives.size()21.2k
:
0139
;
571
21.4k
  addUnwrappedLine();
572
21.4k
  size_t OpeningLineIndex =
573
21.4k
      CurrentLines->empty()
574
21.4k
          ? 
(UnwrappedLine::kInvalidIndex)0
575
21.4k
          : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
576
21.4k
577
21.4k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
578
21.4k
                                          MustBeDeclaration);
579
21.4k
  if (AddLevel)
580
18.1k
    ++Line->Level;
581
21.4k
  parseLevel(/*HasOpeningBrace=*/true);
582
21.4k
583
21.4k
  if (eof())
584
128
    return;
585
21.3k
586
21.3k
  if (MacroBlock ? 
!FormatTok->is(TT_MacroBlockEnd)28
587
21.3k
                 : 
!FormatTok->is(tok::r_brace)21.2k
) {
588
0
    Line->Level = InitialLevel;
589
0
    FormatTok->BlockKind = BK_Block;
590
0
    return;
591
0
  }
592
21.3k
593
21.3k
  size_t PPEndHash = computePPHash();
594
21.3k
595
21.3k
  // Munch the closing brace.
596
21.3k
  nextToken(/*LevelDifference=*/AddLevel ? 
-118.0k
:
03.29k
);
597
21.3k
598
21.3k
  if (MacroBlock && 
FormatTok->is(tok::l_paren)28
)
599
7
    parseParens();
600
21.3k
601
21.3k
  if (MunchSemi && 
FormatTok->Tok.is(tok::semi)15.7k
)
602
311
    nextToken();
603
21.3k
  Line->Level = InitialLevel;
604
21.3k
605
21.3k
  if (PPStartHash == PPEndHash) {
606
21.2k
    Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
607
21.2k
    if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
608
21.2k
      // Update the opening line to add the forward reference as well
609
21.2k
      (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
610
21.2k
          CurrentLines->size() - 1;
611
21.2k
    }
612
21.2k
  }
613
21.3k
}
614
615
380
static bool isGoogScope(const UnwrappedLine &Line) {
616
380
  // FIXME: Closure-library specific stuff should not be hard-coded but be
617
380
  // configurable.
618
380
  if (Line.Tokens.size() < 4)
619
0
    return false;
620
380
  auto I = Line.Tokens.begin();
621
380
  if (I->Tok->TokenText != "goog")
622
372
    return false;
623
8
  ++I;
624
8
  if (I->Tok->isNot(tok::period))
625
0
    return false;
626
8
  ++I;
627
8
  if (I->Tok->TokenText != "scope")
628
0
    return false;
629
8
  ++I;
630
8
  return I->Tok->is(tok::l_paren);
631
8
}
632
633
static bool isIIFE(const UnwrappedLine &Line,
634
372
                   const AdditionalKeywords &Keywords) {
635
372
  // Look for the start of an immediately invoked anonymous function.
636
372
  // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
637
372
  // This is commonly done in JavaScript to create a new, anonymous scope.
638
372
  // Example: (function() { ... })()
639
372
  if (Line.Tokens.size() < 3)
640
0
    return false;
641
372
  auto I = Line.Tokens.begin();
642
372
  if (I->Tok->isNot(tok::l_paren))
643
352
    return false;
644
20
  ++I;
645
20
  if (I->Tok->isNot(Keywords.kw_function))
646
0
    return false;
647
20
  ++I;
648
20
  return I->Tok->is(tok::l_paren);
649
20
}
650
651
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
652
8.95k
                                   const FormatToken &InitialToken) {
653
8.95k
  if (InitialToken.isOneOf(tok::kw_namespace, TT_NamespaceMacro))
654
3.38k
    return Style.BraceWrapping.AfterNamespace;
655
5.57k
  if (InitialToken.is(tok::kw_class))
656
2.82k
    return Style.BraceWrapping.AfterClass;
657
2.74k
  if (InitialToken.is(tok::kw_union))
658
116
    return Style.BraceWrapping.AfterUnion;
659
2.63k
  if (InitialToken.is(tok::kw_struct))
660
2.59k
    return Style.BraceWrapping.AfterStruct;
661
36
  return false;
662
36
}
663
664
2.45k
void UnwrappedLineParser::parseChildBlock() {
665
2.45k
  FormatTok->BlockKind = BK_Block;
666
2.45k
  nextToken();
667
2.45k
  {
668
2.45k
    bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
669
2.45k
                       
(380
isGoogScope(*Line)380
||
isIIFE(*Line, Keywords)372
));
670
2.45k
    ScopedLineState LineState(*this);
671
2.45k
    ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
672
2.45k
                                            /*MustBeDeclaration=*/false);
673
2.45k
    Line->Level += SkipIndent ? 
024
:
12.43k
;
674
2.45k
    parseLevel(/*HasOpeningBrace=*/true);
675
2.45k
    flushComments(isOnNewLine(*FormatTok));
676
2.45k
    Line->Level -= SkipIndent ? 
024
:
12.43k
;
677
2.45k
  }
678
2.45k
  nextToken();
679
2.45k
}
680
681
8.25k
void UnwrappedLineParser::parsePPDirective() {
682
8.25k
  assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
683
8.25k
  ScopedMacroState MacroState(*Line, Tokens, FormatTok);
684
8.25k
685
8.25k
  nextToken();
686
8.25k
687
8.25k
  if (!FormatTok->Tok.getIdentifierInfo()) {
688
34
    parsePPUnknown();
689
34
    return;
690
34
  }
691
8.21k
692
8.21k
  switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
693
2.17k
  case tok::pp_define:
694
2.17k
    parsePPDefine();
695
2.17k
    return;
696
385
  case tok::pp_if:
697
385
    parsePPIf(/*IfDef=*/false);
698
385
    break;
699
609
  case tok::pp_ifdef:
700
609
  case tok::pp_ifndef:
701
609
    parsePPIf(/*IfDef=*/true);
702
609
    break;
703
609
  case tok::pp_else:
704
487
    parsePPElse();
705
487
    break;
706
609
  case tok::pp_elif:
707
70
    parsePPElIf();
708
70
    break;
709
946
  case tok::pp_endif:
710
946
    parsePPEndIf();
711
946
    break;
712
3.54k
  default:
713
3.54k
    parsePPUnknown();
714
3.54k
    break;
715
8.21k
  }
716
8.21k
}
717
718
1.63k
void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
719
1.63k
  size_t Line = CurrentLines->size();
720
1.63k
  if (CurrentLines == &PreprocessorDirectives)
721
758
    Line += Lines.size();
722
1.63k
723
1.63k
  if (Unreachable ||
724
1.63k
      
(977
!PPStack.empty()977
&&
PPStack.back().Kind == PP_Unreachable146
))
725
723
    PPStack.push_back({PP_Unreachable, Line});
726
909
  else
727
909
    PPStack.push_back({PP_Conditional, Line});
728
1.63k
}
729
730
1.02k
void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
731
1.02k
  ++PPBranchLevel;
732
1.02k
  assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
733
1.02k
  if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
734
686
    PPLevelBranchIndex.push_back(0);
735
686
    PPLevelBranchCount.push_back(0);
736
686
  }
737
1.02k
  PPChainBranchIndex.push(0);
738
1.02k
  bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
739
1.02k
  conditionalCompilationCondition(Unreachable || 
Skip964
);
740
1.02k
}
741
742
611
void UnwrappedLineParser::conditionalCompilationAlternative() {
743
611
  if (!PPStack.empty())
744
604
    PPStack.pop_back();
745
611
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
746
611
  if (!PPChainBranchIndex.empty())
747
604
    ++PPChainBranchIndex.top();
748
611
  conditionalCompilationCondition(
749
611
      PPBranchLevel >= 0 && 
!PPChainBranchIndex.empty()604
&&
750
611
      
PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()604
);
751
611
}
752
753
973
void UnwrappedLineParser::conditionalCompilationEnd() {
754
973
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
755
973
  if (PPBranchLevel >= 0 && 
!PPChainBranchIndex.empty()960
) {
756
960
    if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
757
632
      PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
758
632
    }
759
960
  }
760
973
  // Guard against #endif's without #if.
761
973
  if (PPBranchLevel > -1)
762
960
    --PPBranchLevel;
763
973
  if (!PPChainBranchIndex.empty())
764
960
    PPChainBranchIndex.pop();
765
973
  if (!PPStack.empty())
766
960
    PPStack.pop_back();
767
973
}
768
769
994
void UnwrappedLineParser::parsePPIf(bool IfDef) {
770
994
  bool IfNDef = FormatTok->is(tok::pp_ifndef);
771
994
  nextToken();
772
994
  bool Unreachable = false;
773
994
  if (!IfDef && 
(385
FormatTok->is(tok::kw_false)385
||
FormatTok->TokenText == "0"382
))
774
54
    Unreachable = true;
775
994
  if (IfDef && 
!IfNDef609
&&
FormatTok->TokenText == "SWIG"499
)
776
3
    Unreachable = true;
777
994
  conditionalCompilationStart(Unreachable);
778
994
  FormatToken *IfCondition = FormatTok;
779
994
  // If there's a #ifndef on the first line, and the only lines before it are
780
994
  // comments, it could be an include guard.
781
994
  bool MaybeIncludeGuard = IfNDef;
782
994
  if (IncludeGuard == IG_Inited && 
MaybeIncludeGuard258
)
783
93
    for (auto &Line : Lines) {
784
17
      if (!Line.Tokens.front().Tok->is(tok::comment)) {
785
10
        MaybeIncludeGuard = false;
786
10
        IncludeGuard = IG_Rejected;
787
10
        break;
788
10
      }
789
17
    }
790
994
  --PPBranchLevel;
791
994
  parsePPUnknown();
792
994
  ++PPBranchLevel;
793
994
  if (IncludeGuard == IG_Inited && 
MaybeIncludeGuard248
) {
794
83
    IncludeGuard = IG_IfNdefed;
795
83
    IncludeGuardToken = IfCondition;
796
83
  }
797
994
}
798
799
557
void UnwrappedLineParser::parsePPElse() {
800
557
  // If a potential include guard has an #else, it's not an include guard.
801
557
  if (IncludeGuard == IG_Defined && 
PPBranchLevel == 028
)
802
28
    IncludeGuard = IG_Rejected;
803
557
  conditionalCompilationAlternative();
804
557
  if (PPBranchLevel > -1)
805
550
    --PPBranchLevel;
806
557
  parsePPUnknown();
807
557
  ++PPBranchLevel;
808
557
}
809
810
70
void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
811
812
946
void UnwrappedLineParser::parsePPEndIf() {
813
946
  conditionalCompilationEnd();
814
946
  parsePPUnknown();
815
946
  // If the #endif of a potential include guard is the last thing in the file,
816
946
  // then we found an include guard.
817
946
  unsigned TokenPosition = Tokens->getPosition();
818
946
  FormatToken *PeekNext = AllTokens[TokenPosition];
819
946
  if (IncludeGuard == IG_Defined && 
PPBranchLevel == -148
&&
820
946
      
PeekNext->is(tok::eof)41
&&
821
946
      
Style.IndentPPDirectives != FormatStyle::PPDIS_None34
)
822
34
    IncludeGuard = IG_Found;
823
946
}
824
825
2.17k
void UnwrappedLineParser::parsePPDefine() {
826
2.17k
  nextToken();
827
2.17k
828
2.17k
  if (!FormatTok->Tok.getIdentifierInfo()) {
829
7
    IncludeGuard = IG_Rejected;
830
7
    IncludeGuardToken = nullptr;
831
7
    parsePPUnknown();
832
7
    return;
833
7
  }
834
2.17k
835
2.17k
  if (IncludeGuard == IG_IfNdefed &&
836
2.17k
      
IncludeGuardToken->TokenText == FormatTok->TokenText76
) {
837
69
    IncludeGuard = IG_Defined;
838
69
    IncludeGuardToken = nullptr;
839
73
    for (auto &Line : Lines) {
840
73
      if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
841
0
        IncludeGuard = IG_Rejected;
842
0
        break;
843
0
      }
844
73
    }
845
69
  }
846
2.17k
847
2.17k
  nextToken();
848
2.17k
  if (FormatTok->Tok.getKind() == tok::l_paren &&
849
2.17k
      FormatTok->WhitespaceRange.getBegin() ==
850
1.07k
          FormatTok->WhitespaceRange.getEnd()) {
851
945
    parseParens();
852
945
  }
853
2.17k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
854
341
    Line->Level += PPBranchLevel + 1;
855
2.17k
  addUnwrappedLine();
856
2.17k
  ++Line->Level;
857
2.17k
858
2.17k
  // Errors during a preprocessor directive can only affect the layout of the
859
2.17k
  // preprocessor directive, and thus we ignore them. An alternative approach
860
2.17k
  // would be to use the same approach we use on the file level (no
861
2.17k
  // re-indentation if there was a structural error) within the macro
862
2.17k
  // definition.
863
2.17k
  parseFile();
864
2.17k
}
865
866
6.07k
void UnwrappedLineParser::parsePPUnknown() {
867
12.4k
  do {
868
12.4k
    nextToken();
869
12.4k
  } while (!eof());
870
6.07k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
871
694
    Line->Level += PPBranchLevel + 1;
872
6.07k
  addUnwrappedLine();
873
6.07k
}
874
875
// Here we blacklist certain tokens that are not usually the first token in an
876
// unwrapped line. This is used in attempt to distinguish macro calls without
877
// trailing semicolons from other constructs split to several lines.
878
449
static bool tokenCanStartNewLine(const clang::Token &Tok) {
879
449
  // Semicolon can be a null-statement, l_square can be a start of a macro or
880
449
  // a C++11 attribute, but this doesn't seem to be common.
881
449
  return Tok.isNot(tok::semi) && 
Tok.isNot(tok::l_brace)446
&&
882
449
         
Tok.isNot(tok::l_square)440
&&
883
449
         // Tokens that can only be used as binary operators and a part of
884
449
         // overloaded operator names.
885
449
         
Tok.isNot(tok::period)419
&&
Tok.isNot(tok::periodstar)374
&&
886
449
         
Tok.isNot(tok::arrow)374
&&
Tok.isNot(tok::arrowstar)368
&&
887
449
         
Tok.isNot(tok::less)365
&&
Tok.isNot(tok::greater)365
&&
888
449
         
Tok.isNot(tok::slash)362
&&
Tok.isNot(tok::percent)362
&&
889
449
         
Tok.isNot(tok::lessless)362
&&
Tok.isNot(tok::greatergreater)332
&&
890
449
         
Tok.isNot(tok::equal)332
&&
Tok.isNot(tok::plusequal)323
&&
891
449
         
Tok.isNot(tok::minusequal)320
&&
Tok.isNot(tok::starequal)317
&&
892
449
         
Tok.isNot(tok::slashequal)314
&&
Tok.isNot(tok::percentequal)311
&&
893
449
         
Tok.isNot(tok::ampequal)308
&&
Tok.isNot(tok::pipeequal)305
&&
894
449
         
Tok.isNot(tok::caretequal)302
&&
Tok.isNot(tok::greatergreaterequal)299
&&
895
449
         
Tok.isNot(tok::lesslessequal)296
&&
896
449
         // Colon is used in labels, base class lists, initializer lists,
897
449
         // range-based for loops, ternary operator, but should never be the
898
449
         // first token in an unwrapped line.
899
449
         
Tok.isNot(tok::colon)293
&&
900
449
         // 'noexcept' is a trailing annotation.
901
449
         
Tok.isNot(tok::kw_noexcept)184
;
902
449
}
903
904
static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
905
4.91k
                          const FormatToken *FormatTok) {
906
4.91k
  // FIXME: This returns true for C/C++ keywords like 'struct'.
907
4.91k
  return FormatTok->is(tok::identifier) &&
908
4.91k
         
(1.19k
FormatTok->Tok.getIdentifierInfo() == nullptr1.19k
||
909
1.19k
          !FormatTok->isOneOf(
910
1.18k
              Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
911
1.18k
              Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
912
1.18k
              Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
913
1.18k
              Keywords.kw_let, Keywords.kw_var, tok::kw_const,
914
1.18k
              Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
915
1.18k
              Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
916
1.18k
              Keywords.kw_from));
917
4.91k
}
918
919
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
920
5.06k
                                 const FormatToken *FormatTok) {
921
5.06k
  return FormatTok->Tok.isLiteral() ||
922
5.06k
         
FormatTok->isOneOf(tok::kw_true, tok::kw_false)4.86k
||
923
5.06k
         
mustBeJSIdent(Keywords, FormatTok)4.86k
;
924
5.06k
}
925
926
// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
927
// when encountered after a value (see mustBeJSIdentOrValue).
928
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
929
176
                           const FormatToken *FormatTok) {
930
176
  return FormatTok->isOneOf(
931
176
      tok::kw_return, Keywords.kw_yield,
932
176
      // conditionals
933
176
      tok::kw_if, tok::kw_else,
934
176
      // loops
935
176
      tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
936
176
      // switch/case
937
176
      tok::kw_switch, tok::kw_case,
938
176
      // exceptions
939
176
      tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
940
176
      // declaration
941
176
      tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
942
176
      Keywords.kw_async, Keywords.kw_function,
943
176
      // import/export
944
176
      Keywords.kw_import, tok::kw_export);
945
176
}
946
947
// readTokenWithJavaScriptASI reads the next token and terminates the current
948
// line if JavaScript Automatic Semicolon Insertion must
949
// happen between the current token and the next token.
950
//
951
// This method is conservative - it cannot cover all edge cases of JavaScript,
952
// but only aims to correctly handle certain well known cases. It *must not*
953
// return true in speculative cases.
954
30.2k
void UnwrappedLineParser::readTokenWithJavaScriptASI() {
955
30.2k
  FormatToken *Previous = FormatTok;
956
30.2k
  readToken();
957
30.2k
  FormatToken *Next = FormatTok;
958
30.2k
959
30.2k
  bool IsOnSameLine =
960
30.2k
      CommentsBeforeNextToken.empty()
961
30.2k
          ? 
Next->NewlinesBefore == 030.2k
962
30.2k
          : 
CommentsBeforeNextToken.front()->NewlinesBefore == 072
;
963
30.2k
  if (IsOnSameLine)
964
27.7k
    return;
965
2.54k
966
2.54k
  bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
967
2.54k
  bool PreviousStartsTemplateExpr =
968
2.54k
      Previous->is(TT_TemplateString) && 
Previous->TokenText.endswith("${")14
;
969
2.54k
  if (PreviousMustBeValue || 
Previous->is(tok::r_paren)2.37k
) {
970
222
    // If the line contains an '@' sign, the previous token might be an
971
222
    // annotation, which can precede another identifier/value.
972
222
    bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
973
1.34k
                              [](UnwrappedLineNode &LineNode) {
974
1.34k
                                return LineNode.Tok->is(tok::at);
975
1.34k
                              }) != Line->Tokens.end();
976
222
    if (HasAt)
977
16
      return;
978
2.52k
  }
979
2.52k
  if (Next->is(tok::exclaim) && 
PreviousMustBeValue4
)
980
4
    return addUnwrappedLine();
981
2.52k
  bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
982
2.52k
  bool NextEndsTemplateExpr =
983
2.52k
      Next->is(TT_TemplateString) && 
Next->TokenText.startswith("}")26
;
984
2.52k
  if (NextMustBeValue && 
!NextEndsTemplateExpr855
&&
!PreviousStartsTemplateExpr853
&&
985
2.52k
      
(841
PreviousMustBeValue841
||
986
841
       Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
987
825
                         tok::minusminus)))
988
34
    return addUnwrappedLine();
989
2.48k
  if ((PreviousMustBeValue || 
Previous->is(tok::r_paren)2.35k
) &&
990
2.48k
      
isJSDeclOrStmt(Keywords, Next)176
)
991
28
    return addUnwrappedLine();
992
2.48k
}
993
994
68.3k
void UnwrappedLineParser::parseStructuralElement() {
995
68.3k
  assert(!FormatTok->is(tok::l_brace));
996
68.3k
  if (Style.Language == FormatStyle::LK_TableGen &&
997
68.3k
      
FormatTok->is(tok::pp_include)10
) {
998
6
    nextToken();
999
6
    if (FormatTok->is(tok::string_literal))
1000
6
      nextToken();
1001
6
    addUnwrappedLine();
1002
6
    return;
1003
6
  }
1004
68.3k
  switch (FormatTok->Tok.getKind()) {
1005
43
  case tok::kw_asm:
1006
43
    nextToken();
1007
43
    if (FormatTok->is(tok::l_brace)) {
1008
9
      FormatTok->Type = TT_InlineASMBrace;
1009
9
      nextToken();
1010
87
      while (FormatTok && FormatTok->isNot(tok::eof)) {
1011
87
        if (FormatTok->is(tok::r_brace)) {
1012
9
          FormatTok->Type = TT_InlineASMBrace;
1013
9
          nextToken();
1014
9
          addUnwrappedLine();
1015
9
          break;
1016
9
        }
1017
78
        FormatTok->Finalized = true;
1018
78
        nextToken();
1019
78
      }
1020
9
    }
1021
43
    break;
1022
3.20k
  case tok::kw_namespace:
1023
3.20k
    parseNamespace();
1024
3.20k
    return;
1025
1.03k
  case tok::kw_public:
1026
1.03k
  case tok::kw_protected:
1027
1.03k
  case tok::kw_private:
1028
1.03k
    if (Style.Language == FormatStyle::LK_Java ||
1029
1.03k
        
Style.Language == FormatStyle::LK_JavaScript968
||
Style.isCSharp()944
)
1030
165
      nextToken();
1031
872
    else
1032
872
      parseAccessSpecifier();
1033
1.03k
    return;
1034
1.91k
  case tok::kw_if:
1035
1.91k
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration40
)
1036
4
      // field/method declaration.
1037
4
      break;
1038
1.91k
    parseIfThenElse();
1039
1.91k
    return;
1040
1.91k
  case tok::kw_for:
1041
905
  case tok::kw_while:
1042
905
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration56
)
1043
8
      // field/method declaration.
1044
8
      break;
1045
897
    parseForOrWhileLoop();
1046
897
    return;
1047
897
  case tok::kw_do:
1048
146
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration4
)
1049
4
      // field/method declaration.
1050
4
      break;
1051
142
    parseDoWhile();
1052
142
    return;
1053
345
  case tok::kw_switch:
1054
345
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration8
)
1055
4
      // 'switch: string' field declaration.
1056
4
      break;
1057
341
    parseSwitch();
1058
341
    return;
1059
341
  case tok::kw_default:
1060
175
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration4
)
1061
4
      // 'default: string' field declaration.
1062
4
      break;
1063
171
    nextToken();
1064
171
    if (FormatTok->is(tok::colon)) {
1065
169
      parseLabel();
1066
169
      return;
1067
169
    }
1068
2
    // e.g. "default void f() {}" in a Java interface.
1069
2
    break;
1070
578
  case tok::kw_case:
1071
578
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration8
)
1072
4
      // 'case: string' field declaration.
1073
4
      break;
1074
574
    parseCaseLabel();
1075
574
    return;
1076
574
  case tok::kw_try:
1077
160
  case tok::kw___try:
1078
160
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration8
)
1079
4
      // field/method declaration.
1080
4
      break;
1081
156
    parseTryCatch();
1082
156
    return;
1083
156
  case tok::kw_extern:
1084
139
    nextToken();
1085
139
    if (FormatTok->Tok.is(tok::string_literal)) {
1086
90
      nextToken();
1087
90
      if (FormatTok->Tok.is(tok::l_brace)) {
1088
38
        if (Style.BraceWrapping.AfterExternBlock) {
1089
14
          addUnwrappedLine();
1090
14
          parseBlock(/*MustBeDeclaration=*/true);
1091
24
        } else {
1092
24
          parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1093
24
        }
1094
38
        addUnwrappedLine();
1095
38
        return;
1096
38
      }
1097
101
    }
1098
101
    break;
1099
211
  case tok::kw_export:
1100
211
    if (Style.Language == FormatStyle::LK_JavaScript) {
1101
182
      parseJavaScriptEs6ImportExport();
1102
182
      return;
1103
182
    }
1104
29
    if (!Style.isCpp())
1105
0
      break;
1106
29
    // Handle C++ "(inline|export) namespace".
1107
29
    LLVM_FALLTHROUGH;
1108
81
  case tok::kw_inline:
1109
81
    nextToken();
1110
81
    if (FormatTok->Tok.is(tok::kw_namespace)) {
1111
51
      parseNamespace();
1112
51
      return;
1113
51
    }
1114
30
    break;
1115
18.6k
  case tok::identifier:
1116
18.6k
    if (FormatTok->is(TT_ForEachMacro)) {
1117
21
      parseForOrWhileLoop();
1118
21
      return;
1119
21
    }
1120
18.6k
    if (FormatTok->is(TT_MacroBlockBegin)) {
1121
0
      parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1122
0
                 /*MunchSemi=*/false);
1123
0
      return;
1124
0
    }
1125
18.6k
    if (FormatTok->is(Keywords.kw_import)) {
1126
307
      if (Style.Language == FormatStyle::LK_JavaScript) {
1127
221
        parseJavaScriptEs6ImportExport();
1128
221
        return;
1129
221
      }
1130
86
      if (Style.Language == FormatStyle::LK_Proto) {
1131
12
        nextToken();
1132
12
        if (FormatTok->is(tok::kw_public))
1133
2
          nextToken();
1134
12
        if (!FormatTok->is(tok::string_literal))
1135
0
          return;
1136
12
        nextToken();
1137
12
        if (FormatTok->is(tok::semi))
1138
8
          nextToken();
1139
12
        addUnwrappedLine();
1140
12
        return;
1141
12
      }
1142
86
    }
1143
18.4k
    if (Style.isCpp() &&
1144
18.4k
        FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1145
15.3k
                           Keywords.kw_slots, Keywords.kw_qslots)) {
1146
35
      nextToken();
1147
35
      if (FormatTok->is(tok::colon)) {
1148
14
        nextToken();
1149
14
        addUnwrappedLine();
1150
14
        return;
1151
14
      }
1152
18.4k
    }
1153
18.4k
    if (Style.isCpp() && 
FormatTok->is(TT_StatementMacro)15.3k
) {
1154
70
      parseStatementMacro();
1155
70
      return;
1156
70
    }
1157
18.3k
    if (Style.isCpp() && 
FormatTok->is(TT_NamespaceMacro)15.3k
) {
1158
138
      parseNamespace();
1159
138
      return;
1160
138
    }
1161
18.2k
    // In all other cases, parse the declaration.
1162
18.2k
    break;
1163
40.7k
  default:
1164
40.7k
    break;
1165
59.1k
  }
1166
250k
  
do 59.1k
{
1167
250k
    const FormatToken *Previous = FormatTok->Previous;
1168
250k
    switch (FormatTok->Tok.getKind()) {
1169
862
    case tok::at:
1170
862
      nextToken();
1171
862
      if (FormatTok->Tok.is(tok::l_brace)) {
1172
47
        nextToken();
1173
47
        parseBracedList();
1174
47
        break;
1175
815
      } else if (Style.Language == FormatStyle::LK_Java &&
1176
815
                 
FormatTok->is(Keywords.kw_interface)82
) {
1177
8
        nextToken();
1178
8
        break;
1179
8
      }
1180
807
      switch (FormatTok->Tok.getObjCKeywordID()) {
1181
61
      case tok::objc_public:
1182
61
      case tok::objc_protected:
1183
61
      case tok::objc_package:
1184
61
      case tok::objc_private:
1185
61
        return parseAccessSpecifier();
1186
175
      case tok::objc_interface:
1187
175
      case tok::objc_implementation:
1188
175
        return parseObjCInterfaceOrImplementation();
1189
175
      case tok::objc_protocol:
1190
25
        if (parseObjCProtocol())
1191
23
          return;
1192
2
        break;
1193
171
      case tok::objc_end:
1194
171
        return; // Handled by the caller.
1195
12
      case tok::objc_optional:
1196
12
      case tok::objc_required:
1197
12
        nextToken();
1198
12
        addUnwrappedLine();
1199
12
        return;
1200
26
      case tok::objc_autoreleasepool:
1201
26
        nextToken();
1202
26
        if (FormatTok->Tok.is(tok::l_brace)) {
1203
24
          if (Style.BraceWrapping.AfterControlStatement ==
1204
24
              FormatStyle::BWACS_Always)
1205
4
            addUnwrappedLine();
1206
24
          parseBlock(/*MustBeDeclaration=*/false);
1207
24
        }
1208
26
        addUnwrappedLine();
1209
26
        return;
1210
12
      case tok::objc_synchronized:
1211
12
        nextToken();
1212
12
        if (FormatTok->Tok.is(tok::l_paren))
1213
10
          // Skip synchronization object
1214
10
          parseParens();
1215
12
        if (FormatTok->Tok.is(tok::l_brace)) {
1216
10
          if (Style.BraceWrapping.AfterControlStatement ==
1217
10
              FormatStyle::BWACS_Always)
1218
4
            addUnwrappedLine();
1219
10
          parseBlock(/*MustBeDeclaration=*/false);
1220
10
        }
1221
12
        addUnwrappedLine();
1222
12
        return;
1223
14
      case tok::objc_try:
1224
14
        // This branch isn't strictly necessary (the kw_try case below would
1225
14
        // do this too after the tok::at is parsed above).  But be explicit.
1226
14
        parseTryCatch();
1227
14
        return;
1228
311
      default:
1229
311
        break;
1230
313
      }
1231
313
      break;
1232
1.87k
    case tok::kw_enum:
1233
1.87k
      // Ignore if this is part of "template <enum ...".
1234
1.87k
      if (Previous && 
Previous->is(tok::less)96
) {
1235
14
        nextToken();
1236
14
        break;
1237
14
      }
1238
1.86k
1239
1.86k
      // parseEnum falls through and does not yet add an unwrapped line as an
1240
1.86k
      // enum definition can start a structural element.
1241
1.86k
      if (!parseEnum())
1242
42
        break;
1243
1.82k
      // This only applies for C++.
1244
1.82k
      if (!Style.isCpp()) {
1245
56
        addUnwrappedLine();
1246
56
        return;
1247
56
      }
1248
1.76k
      break;
1249
1.76k
    case tok::kw_typedef:
1250
806
      nextToken();
1251
806
      if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1252
806
                             Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1253
806
                             Keywords.kw_CF_CLOSED_ENUM,
1254
806
                             Keywords.kw_NS_CLOSED_ENUM))
1255
58
        parseEnum();
1256
806
      break;
1257
6.53k
    case tok::kw_struct:
1258
6.53k
    case tok::kw_union:
1259
6.53k
    case tok::kw_class:
1260
6.53k
      // parseRecord falls through and does not yet add an unwrapped line as a
1261
6.53k
      // record declaration or definition can start a structural element.
1262
6.53k
      parseRecord();
1263
6.53k
      // This does not apply for Java, JavaScript and C#.
1264
6.53k
      if (Style.Language == FormatStyle::LK_Java ||
1265
6.53k
          
Style.Language == FormatStyle::LK_JavaScript6.49k
||
Style.isCSharp()6.25k
) {
1266
299
        if (FormatTok->is(tok::semi))
1267
8
          nextToken();
1268
299
        addUnwrappedLine();
1269
299
        return;
1270
299
      }
1271
6.23k
      break;
1272
6.23k
    case tok::period:
1273
1.92k
      nextToken();
1274
1.92k
      // In Java, classes have an implicit static member "class".
1275
1.92k
      if (Style.Language == FormatStyle::LK_Java && 
FormatTok160
&&
1276
1.92k
          
FormatTok->is(tok::kw_class)160
)
1277
4
        nextToken();
1278
1.92k
      if (Style.Language == FormatStyle::LK_JavaScript && 
FormatTok458
&&
1279
1.92k
          
FormatTok->Tok.getIdentifierInfo()458
)
1280
438
        // JavaScript only has pseudo keywords, all keywords are allowed to
1281
438
        // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1282
438
        nextToken();
1283
1.92k
      break;
1284
42.1k
    case tok::semi:
1285
42.1k
      nextToken();
1286
42.1k
      addUnwrappedLine();
1287
42.1k
      return;
1288
6.23k
    case tok::r_brace:
1289
324
      addUnwrappedLine();
1290
324
      return;
1291
24.7k
    case tok::l_paren:
1292
24.7k
      parseParens();
1293
24.7k
      break;
1294
6.23k
    case tok::kw_operator:
1295
1.30k
      nextToken();
1296
1.30k
      if (FormatTok->isBinaryOperator())
1297
537
        nextToken();
1298
1.30k
      break;
1299
6.23k
    case tok::caret:
1300
116
      nextToken();
1301
116
      if (FormatTok->Tok.isAnyIdentifier() ||
1302
116
          
FormatTok->isSimpleTypeSpecifier()95
)
1303
35
        nextToken();
1304
116
      if (FormatTok->is(tok::l_paren))
1305
46
        parseParens();
1306
116
      if (FormatTok->is(tok::l_brace))
1307
79
        parseChildBlock();
1308
116
      break;
1309
9.92k
    case tok::l_brace:
1310
9.92k
      if (!tryToParseBracedList()) {
1311
8.84k
        // A block outside of parentheses must be the last part of a
1312
8.84k
        // structural element.
1313
8.84k
        // FIXME: Figure out cases where this is not true, and add projections
1314
8.84k
        // for them (the one we know is missing are lambdas).
1315
8.84k
        if (Style.BraceWrapping.AfterFunction)
1316
911
          addUnwrappedLine();
1317
8.84k
        FormatTok->Type = TT_FunctionLBrace;
1318
8.84k
        parseBlock(/*MustBeDeclaration=*/false);
1319
8.84k
        addUnwrappedLine();
1320
8.84k
        return;
1321
8.84k
      }
1322
1.07k
      // Otherwise this was a braced init list, and the structural
1323
1.07k
      // element continues.
1324
1.07k
      break;
1325
1.07k
    case tok::kw_try:
1326
21
      if (Style.Language == FormatStyle::LK_JavaScript &&
1327
21
          
Line->MustBeDeclaration4
) {
1328
4
        // field/method declaration.
1329
4
        nextToken();
1330
4
        break;
1331
4
      }
1332
17
      // We arrive here when parsing function-try blocks.
1333
17
      if (Style.BraceWrapping.AfterFunction)
1334
3
        addUnwrappedLine();
1335
17
      parseTryCatch();
1336
17
      return;
1337
70.9k
    case tok::identifier: {
1338
70.9k
      if (FormatTok->is(TT_MacroBlockEnd)) {
1339
8
        addUnwrappedLine();
1340
8
        return;
1341
8
      }
1342
70.9k
1343
70.9k
      // Function declarations (as opposed to function expressions) are parsed
1344
70.9k
      // on their own unwrapped line by continuing this loop. Function
1345
70.9k
      // expressions (functions that are not on their own line) must not create
1346
70.9k
      // a new unwrapped line, so they are special cased below.
1347
70.9k
      size_t TokenCount = Line->Tokens.size();
1348
70.9k
      if (Style.Language == FormatStyle::LK_JavaScript &&
1349
70.9k
          
FormatTok->is(Keywords.kw_function)5.40k
&&
1350
70.9k
          
(276
TokenCount > 1276
||
(208
TokenCount == 1208
&& !Line->Tokens.front().Tok->is(
1351
88
                                                     Keywords.kw_async)))) {
1352
88
        tryToParseJSFunction();
1353
88
        break;
1354
88
      }
1355
70.8k
      if ((Style.Language == FormatStyle::LK_JavaScript ||
1356
70.8k
           
Style.Language == FormatStyle::LK_Java65.5k
) &&
1357
70.8k
          
FormatTok->is(Keywords.kw_interface)6.21k
) {
1358
52
        if (Style.Language == FormatStyle::LK_JavaScript) {
1359
48
          // In JavaScript/TypeScript, "interface" can be used as a standalone
1360
48
          // identifier, e.g. in `var interface = 1;`. If "interface" is
1361
48
          // followed by another identifier, it is very like to be an actual
1362
48
          // interface declaration.
1363
48
          unsigned StoredPosition = Tokens->getPosition();
1364
48
          FormatToken *Next = Tokens->getNextToken();
1365
48
          FormatTok = Tokens->setPosition(StoredPosition);
1366
48
          if (Next && !mustBeJSIdent(Keywords, Next)) {
1367
16
            nextToken();
1368
16
            break;
1369
16
          }
1370
36
        }
1371
36
        parseRecord();
1372
36
        addUnwrappedLine();
1373
36
        return;
1374
36
      }
1375
70.7k
1376
70.7k
      if (Style.isCpp() && 
FormatTok->is(TT_StatementMacro)63.8k
) {
1377
7
        parseStatementMacro();
1378
7
        return;
1379
7
      }
1380
70.7k
1381
70.7k
      // See if the following token should start a new unwrapped line.
1382
70.7k
      StringRef Text = FormatTok->TokenText;
1383
70.7k
      nextToken();
1384
70.7k
1385
70.7k
      // JS doesn't have macros, and within classes colons indicate fields, not
1386
70.7k
      // labels.
1387
70.7k
      if (Style.Language == FormatStyle::LK_JavaScript)
1388
5.26k
        break;
1389
65.4k
1390
65.4k
      TokenCount = Line->Tokens.size();
1391
65.4k
      if (TokenCount == 1 ||
1392
65.4k
          
(49.9k
TokenCount == 249.9k
&&
Line->Tokens.front().Tok->is(tok::comment)21.5k
)) {
1393
15.5k
        if (FormatTok->Tok.is(tok::colon) && 
!Line->MustBeDeclaration126
) {
1394
112
          Line->Tokens.begin()->Tok->MustBreakBefore = true;
1395
112
          parseLabel(!Style.IndentGotoLabels);
1396
112
          return;
1397
112
        }
1398
15.4k
        // Recognize function-like macro usages without trailing semicolon as
1399
15.4k
        // well as free-standing macros like Q_OBJECT.
1400
15.4k
        bool FunctionLike = FormatTok->is(tok::l_paren);
1401
15.4k
        if (FunctionLike)
1402
6.48k
          parseParens();
1403
15.4k
1404
15.4k
        bool FollowedByNewline =
1405
15.4k
            CommentsBeforeNextToken.empty()
1406
15.4k
                ? 
FormatTok->NewlinesBefore > 015.4k
1407
15.4k
                : 
CommentsBeforeNextToken.front()->NewlinesBefore > 012
;
1408
15.4k
1409
15.4k
        if (FollowedByNewline && 
(455
Text.size() >= 5455
||
FunctionLike132
) &&
1410
15.4k
            
tokenCanStartNewLine(FormatTok->Tok)449
&&
Text == Text.upper()181
) {
1411
86
          addUnwrappedLine();
1412
86
          return;
1413
86
        }
1414
65.3k
      }
1415
65.3k
      break;
1416
65.3k
    }
1417
65.3k
    case tok::equal:
1418
9.05k
      // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1419
9.05k
      // TT_JsFatArrow. The always start an expression or a child block if
1420
9.05k
      // followed by a curly.
1421
9.05k
      if (FormatTok->is(TT_JsFatArrow)) {
1422
90
        nextToken();
1423
90
        if (FormatTok->is(tok::l_brace))
1424
64
          parseChildBlock();
1425
90
        break;
1426
90
      }
1427
8.96k
1428
8.96k
      nextToken();
1429
8.96k
      if (FormatTok->Tok.is(tok::l_brace)) {
1430
936
        nextToken();
1431
936
        parseBracedList();
1432
8.03k
      } else if (Style.Language == FormatStyle::LK_Proto &&
1433
8.03k
                 
FormatTok->Tok.is(tok::less)95
) {
1434
22
        nextToken();
1435
22
        parseBracedList(/*ContinueOnSemicolons=*/false,
1436
22
                        /*ClosingBraceKind=*/tok::greater);
1437
22
      }
1438
8.96k
      break;
1439
8.96k
    case tok::l_square:
1440
2.82k
      parseSquare();
1441
2.82k
      break;
1442
8.96k
    case tok::kw_new:
1443
269
      parseNew();
1444
269
      break;
1445
76.9k
    default:
1446
76.9k
      nextToken();
1447
76.9k
      break;
1448
198k
    }
1449
198k
  } while (!eof());
1450
59.1k
}
1451
1452
4.63k
bool UnwrappedLineParser::tryToParseLambda() {
1453
4.63k
  if (!Style.isCpp()) {
1454
556
    nextToken();
1455
556
    return false;
1456
556
  }
1457
4.08k
  assert(FormatTok->is(tok::l_square));
1458
4.08k
  FormatToken &LSquare = *FormatTok;
1459
4.08k
  if (!tryToParseLambdaIntroducer())
1460
1.68k
    return false;
1461
2.39k
1462
2.39k
  bool SeenArrow = false;
1463
2.39k
1464
6.56k
  while (FormatTok->isNot(tok::l_brace)) {
1465
5.03k
    if (FormatTok->isSimpleTypeSpecifier()) {
1466
70
      nextToken();
1467
70
      continue;
1468
70
    }
1469
4.96k
    switch (FormatTok->Tok.getKind()) {
1470
0
    case tok::l_brace:
1471
0
      break;
1472
1.26k
    case tok::l_paren:
1473
1.26k
      parseParens();
1474
1.26k
      break;
1475
2.04k
    case tok::amp:
1476
2.04k
    case tok::star:
1477
2.04k
    case tok::kw_const:
1478
2.04k
    case tok::comma:
1479
2.04k
    case tok::less:
1480
2.04k
    case tok::greater:
1481
2.04k
    case tok::identifier:
1482
2.04k
    case tok::numeric_constant:
1483
2.04k
    case tok::coloncolon:
1484
2.04k
    case tok::kw_class:
1485
2.04k
    case tok::kw_mutable:
1486
2.04k
    case tok::kw_noexcept:
1487
2.04k
    case tok::kw_template:
1488
2.04k
    case tok::kw_typename:
1489
2.04k
      nextToken();
1490
2.04k
      break;
1491
2.04k
    // Specialization of a template with an integer parameter can contain
1492
2.04k
    // arithmetic, logical, comparison and ternary operators.
1493
2.04k
    //
1494
2.04k
    // FIXME: This also accepts sequences of operators that are not in the scope
1495
2.04k
    // of a template argument list.
1496
2.04k
    //
1497
2.04k
    // In a C++ lambda a template type can only occur after an arrow. We use
1498
2.04k
    // this as an heuristic to distinguish between Objective-C expressions
1499
2.04k
    // followed by an `a->b` expression, such as:
1500
2.04k
    // ([obj func:arg] + a->b)
1501
2.04k
    // Otherwise the code below would parse as a lambda.
1502
2.04k
    //
1503
2.04k
    // FIXME: This heuristic is incorrect for C++20 generic lambdas with
1504
2.04k
    // explicit template lists: []<bool b = true && false>(U &&u){}
1505
2.04k
    case tok::plus:
1506
383
    case tok::minus:
1507
383
    case tok::exclaim:
1508
383
    case tok::tilde:
1509
383
    case tok::slash:
1510
383
    case tok::percent:
1511
383
    case tok::lessless:
1512
383
    case tok::pipe:
1513
383
    case tok::pipepipe:
1514
383
    case tok::ampamp:
1515
383
    case tok::caret:
1516
383
    case tok::equalequal:
1517
383
    case tok::exclaimequal:
1518
383
    case tok::greaterequal:
1519
383
    case tok::lessequal:
1520
383
    case tok::question:
1521
383
    case tok::colon:
1522
383
    case tok::kw_true:
1523
383
    case tok::kw_false:
1524
383
      if (SeenArrow) {
1525
294
        nextToken();
1526
294
        break;
1527
294
      }
1528
89
      return true;
1529
499
    case tok::arrow:
1530
499
      // This might or might not actually be a lambda arrow (this could be an
1531
499
      // ObjC method invocation followed by a dereferencing arrow). We might
1532
499
      // reset this back to TT_Unknown in TokenAnnotator.
1533
499
      FormatTok->Type = TT_LambdaArrow;
1534
499
      SeenArrow = true;
1535
499
      nextToken();
1536
499
      break;
1537
774
    default:
1538
774
      return true;
1539
4.96k
    }
1540
4.96k
  }
1541
2.39k
  FormatTok->Type = TT_LambdaLBrace;
1542
1.52k
  LSquare.Type = TT_LambdaLSquare;
1543
1.52k
  parseChildBlock();
1544
1.52k
  return true;
1545
2.39k
}
1546
1547
4.08k
bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1548
4.08k
  const FormatToken *Previous = FormatTok->Previous;
1549
4.08k
  if (Previous &&
1550
4.08k
      
(3.41k
Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1551
3.41k
                         tok::kw_delete, tok::l_square) ||
1552
3.41k
       
FormatTok->isCppStructuredBinding(Style)2.41k
||
Previous->closesScope()2.20k
||
1553
3.41k
       
Previous->isSimpleTypeSpecifier()1.88k
)) {
1554
1.56k
    nextToken();
1555
1.56k
    return false;
1556
1.56k
  }
1557
2.51k
  nextToken();
1558
2.51k
  if (FormatTok->is(tok::l_square)) {
1559
124
    return false;
1560
124
  }
1561
2.39k
  parseSquare(/*LambdaIntroducer=*/true);
1562
2.39k
  return true;
1563
2.39k
}
1564
1565
288
void UnwrappedLineParser::tryToParseJSFunction() {
1566
288
  assert(FormatTok->is(Keywords.kw_function) ||
1567
288
         FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
1568
288
  if (FormatTok->is(Keywords.kw_async))
1569
0
    nextToken();
1570
288
  // Consume "function".
1571
288
  nextToken();
1572
288
1573
288
  // Consume * (generator function). Treat it like C++'s overloaded operators.
1574
288
  if (FormatTok->is(tok::star)) {
1575
4
    FormatTok->Type = TT_OverloadedOperator;
1576
4
    nextToken();
1577
4
  }
1578
288
1579
288
  // Consume function name.
1580
288
  if (FormatTok->is(tok::identifier))
1581
48
    nextToken();
1582
288
1583
288
  if (FormatTok->isNot(tok::l_paren))
1584
0
    return;
1585
288
1586
288
  // Parse formal parameter list.
1587
288
  parseParens();
1588
288
1589
288
  if (FormatTok->is(tok::colon)) {
1590
16
    // Parse a type definition.
1591
16
    nextToken();
1592
16
1593
16
    // Eat the type declaration. For braced inline object types, balance braces,
1594
16
    // otherwise just parse until finding an l_brace for the function body.
1595
16
    if (FormatTok->is(tok::l_brace))
1596
8
      tryToParseBracedList();
1597
8
    else
1598
16
      
while (8
!FormatTok->isOneOf(tok::l_brace, tok::semi) &&
!eof()8
)
1599
8
        nextToken();
1600
16
  }
1601
288
1602
288
  if (FormatTok->is(tok::semi))
1603
16
    return;
1604
272
1605
272
  parseChildBlock();
1606
272
}
1607
1608
12.0k
bool UnwrappedLineParser::tryToParseBracedList() {
1609
12.0k
  if (FormatTok->BlockKind == BK_Unknown)
1610
10.6k
    calculateBraceTypes();
1611
12.0k
  assert(FormatTok->BlockKind != BK_Unknown);
1612
12.0k
  if (FormatTok->BlockKind == BK_Block)
1613
10.4k
    return false;
1614
1.52k
  nextToken();
1615
1.52k
  parseBracedList();
1616
1.52k
  return true;
1617
1.52k
}
1618
1619
bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1620
6.38k
                                          tok::TokenKind ClosingBraceKind) {
1621
6.38k
  bool HasError = false;
1622
6.38k
1623
6.38k
  // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1624
6.38k
  // replace this by using parseAssigmentExpression() inside.
1625
34.9k
  do {
1626
34.9k
    if (Style.Language == FormatStyle::LK_JavaScript) {
1627
3.69k
      if (FormatTok->is(Keywords.kw_function) ||
1628
3.69k
          
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)3.63k
) {
1629
60
        tryToParseJSFunction();
1630
60
        continue;
1631
60
      }
1632
3.63k
      if (FormatTok->is(TT_JsFatArrow)) {
1633
12
        nextToken();
1634
12
        // Fat arrows can be followed by simple expressions or by child blocks
1635
12
        // in curly braces.
1636
12
        if (FormatTok->is(tok::l_brace)) {
1637
4
          parseChildBlock();
1638
4
          continue;
1639
4
        }
1640
3.63k
      }
1641
3.63k
      if (FormatTok->is(tok::l_brace)) {
1642
32
        // Could be a method inside of a braced list `{a() { return 1; }}`.
1643
32
        if (tryToParseBracedList())
1644
28
          continue;
1645
4
        parseChildBlock();
1646
4
      }
1647
3.63k
    }
1648
34.9k
    
if (34.8k
FormatTok->Tok.getKind() == ClosingBraceKind34.8k
) {
1649
5.75k
      nextToken();
1650
5.75k
      return !HasError;
1651
5.75k
    }
1652
29.0k
    switch (FormatTok->Tok.getKind()) {
1653
0
    case tok::caret:
1654
0
      nextToken();
1655
0
      if (FormatTok->is(tok::l_brace)) {
1656
0
        parseChildBlock();
1657
0
      }
1658
0
      break;
1659
481
    case tok::l_square:
1660
481
      tryToParseLambda();
1661
481
      break;
1662
310
    case tok::l_paren:
1663
310
      parseParens();
1664
310
      // JavaScript can just have free standing methods and getters/setters in
1665
310
      // object literals. Detect them by a "{" following ")".
1666
310
      if (Style.Language == FormatStyle::LK_JavaScript) {
1667
40
        if (FormatTok->is(tok::l_brace))
1668
24
          parseChildBlock();
1669
40
        break;
1670
40
      }
1671
270
      break;
1672
1.10k
    case tok::l_brace:
1673
1.10k
      // Assume there are no blocks inside a braced init list apart
1674
1.10k
      // from the ones we explicitly parse out (like lambdas).
1675
1.10k
      FormatTok->BlockKind = BK_BracedInit;
1676
1.10k
      nextToken();
1677
1.10k
      parseBracedList();
1678
1.10k
      break;
1679
293
    case tok::less:
1680
293
      if (Style.Language == FormatStyle::LK_Proto) {
1681
56
        nextToken();
1682
56
        parseBracedList(/*ContinueOnSemicolons=*/false,
1683
56
                        /*ClosingBraceKind=*/tok::greater);
1684
237
      } else {
1685
237
        nextToken();
1686
237
      }
1687
293
      break;
1688
270
    case tok::semi:
1689
76
      // JavaScript (or more precisely TypeScript) can have semicolons in braced
1690
76
      // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1691
76
      // used for error recovery if we have otherwise determined that this is
1692
76
      // a braced list.
1693
76
      if (Style.Language == FormatStyle::LK_JavaScript) {
1694
36
        nextToken();
1695
36
        break;
1696
36
      }
1697
40
      HasError = true;
1698
40
      if (!ContinueOnSemicolons)
1699
19
        return !HasError;
1700
21
      nextToken();
1701
21
      break;
1702
6.82k
    case tok::comma:
1703
6.82k
      nextToken();
1704
6.82k
      break;
1705
19.9k
    default:
1706
19.9k
      nextToken();
1707
19.9k
      break;
1708
29.1k
    }
1709
29.1k
  } while (!eof());
1710
6.38k
  
return false611
;
1711
6.38k
}
1712
1713
42.2k
void UnwrappedLineParser::parseParens() {
1714
42.2k
  assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1715
42.2k
  nextToken();
1716
111k
  do {
1717
111k
    switch (FormatTok->Tok.getKind()) {
1718
3.88k
    case tok::l_paren:
1719
3.88k
      parseParens();
1720
3.88k
      if (Style.Language == FormatStyle::LK_Java && 
FormatTok->is(tok::l_brace)22
)
1721
10
        parseChildBlock();
1722
3.88k
      break;
1723
42.2k
    case tok::r_paren:
1724
42.2k
      nextToken();
1725
42.2k
      return;
1726
3
    case tok::r_brace:
1727
3
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
1728
3
      return;
1729
1.08k
    case tok::l_square:
1730
1.08k
      tryToParseLambda();
1731
1.08k
      break;
1732
639
    case tok::l_brace:
1733
639
      if (!tryToParseBracedList())
1734
294
        parseChildBlock();
1735
639
      break;
1736
30
    case tok::at:
1737
30
      nextToken();
1738
30
      if (FormatTok->Tok.is(tok::l_brace)) {
1739
2
        nextToken();
1740
2
        parseBracedList();
1741
2
      }
1742
30
      break;
1743
67
    case tok::kw_class:
1744
67
      if (Style.Language == FormatStyle::LK_JavaScript)
1745
4
        parseRecord(/*ParseAsExpr=*/true);
1746
63
      else
1747
63
        nextToken();
1748
67
      break;
1749
32.9k
    case tok::identifier:
1750
32.9k
      if (Style.Language == FormatStyle::LK_JavaScript &&
1751
32.9k
          
(1.65k
FormatTok->is(Keywords.kw_function)1.65k
||
1752
1.65k
           
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)1.51k
))
1753
140
        tryToParseJSFunction();
1754
32.8k
      else
1755
32.8k
        nextToken();
1756
32.9k
      break;
1757
30.9k
    default:
1758
30.9k
      nextToken();
1759
30.9k
      break;
1760
69.6k
    }
1761
69.6k
  } while (!eof());
1762
42.2k
}
1763
1764
5.46k
void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1765
5.46k
  if (!LambdaIntroducer) {
1766
3.07k
    assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1767
3.07k
    if (tryToParseLambda())
1768
1.37k
      return;
1769
4.09k
  }
1770
12.4k
  
do 4.09k
{
1771
12.4k
    switch (FormatTok->Tok.getKind()) {
1772
327
    case tok::l_paren:
1773
327
      parseParens();
1774
327
      break;
1775
4.08k
    case tok::r_square:
1776
4.08k
      nextToken();
1777
4.08k
      return;
1778
0
    case tok::r_brace:
1779
0
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
1780
0
      return;
1781
241
    case tok::l_square:
1782
241
      parseSquare();
1783
241
      break;
1784
202
    case tok::l_brace: {
1785
202
      if (!tryToParseBracedList())
1786
168
        parseChildBlock();
1787
202
      break;
1788
0
    }
1789
63
    case tok::at:
1790
63
      nextToken();
1791
63
      if (FormatTok->Tok.is(tok::l_brace)) {
1792
25
        nextToken();
1793
25
        parseBracedList();
1794
25
      }
1795
63
      break;
1796
7.51k
    default:
1797
7.51k
      nextToken();
1798
7.51k
      break;
1799
8.34k
    }
1800
8.34k
  } while (!eof());
1801
4.09k
}
1802
1803
2.10k
void UnwrappedLineParser::parseIfThenElse() {
1804
2.10k
  assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1805
2.10k
  nextToken();
1806
2.10k
  if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier))
1807
385
    nextToken();
1808
2.10k
  if (FormatTok->Tok.is(tok::l_paren))
1809
2.09k
    parseParens();
1810
2.10k
  bool NeedsUnwrappedLine = false;
1811
2.10k
  if (FormatTok->Tok.is(tok::l_brace)) {
1812
1.13k
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
1813
1.13k
    parseBlock(/*MustBeDeclaration=*/false);
1814
1.13k
    if (Style.BraceWrapping.BeforeElse)
1815
114
      addUnwrappedLine();
1816
1.01k
    else
1817
1.01k
      NeedsUnwrappedLine = true;
1818
1.13k
  } else {
1819
976
    addUnwrappedLine();
1820
976
    ++Line->Level;
1821
976
    parseStructuralElement();
1822
976
    --Line->Level;
1823
976
  }
1824
2.10k
  if (FormatTok->Tok.is(tok::kw_else)) {
1825
497
    nextToken();
1826
497
    if (FormatTok->Tok.is(tok::l_brace)) {
1827
220
      CompoundStatementIndenter Indenter(this, Style, Line->Level);
1828
220
      parseBlock(/*MustBeDeclaration=*/false);
1829
220
      addUnwrappedLine();
1830
277
    } else if (FormatTok->Tok.is(tok::kw_if)) {
1831
193
      parseIfThenElse();
1832
193
    } else {
1833
84
      addUnwrappedLine();
1834
84
      ++Line->Level;
1835
84
      parseStructuralElement();
1836
84
      if (FormatTok->is(tok::eof))
1837
49
        addUnwrappedLine();
1838
84
      --Line->Level;
1839
84
    }
1840
1.60k
  } else if (NeedsUnwrappedLine) {
1841
770
    addUnwrappedLine();
1842
770
  }
1843
2.10k
}
1844
1845
187
void UnwrappedLineParser::parseTryCatch() {
1846
187
  assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
1847
187
  nextToken();
1848
187
  bool NeedsUnwrappedLine = false;
1849
187
  if (FormatTok->is(tok::colon)) {
1850
10
    // We are in a function try block, what comes is an initializer list.
1851
10
    nextToken();
1852
20
    while (FormatTok->is(tok::identifier)) {
1853
10
      nextToken();
1854
10
      if (FormatTok->is(tok::l_paren))
1855
10
        parseParens();
1856
10
      if (FormatTok->is(tok::comma))
1857
0
        nextToken();
1858
10
    }
1859
10
  }
1860
187
  // Parse try with resource.
1861
187
  if (Style.Language == FormatStyle::LK_Java && 
FormatTok->is(tok::l_paren)12
) {
1862
4
    parseParens();
1863
4
  }
1864
187
  if (FormatTok->is(tok::l_brace)) {
1865
185
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
1866
185
    parseBlock(/*MustBeDeclaration=*/false);
1867
185
    if (Style.BraceWrapping.BeforeCatch) {
1868
52
      addUnwrappedLine();
1869
133
    } else {
1870
133
      NeedsUnwrappedLine = true;
1871
133
    }
1872
185
  } else 
if (2
!FormatTok->is(tok::kw_catch)2
) {
1873
2
    // The C++ standard requires a compound-statement after a try.
1874
2
    // If there's none, we try to assume there's a structuralElement
1875
2
    // and try to continue.
1876
2
    addUnwrappedLine();
1877
2
    ++Line->Level;
1878
2
    parseStructuralElement();
1879
2
    --Line->Level;
1880
2
  }
1881
386
  while (1) {
1882
386
    if (FormatTok->is(tok::at))
1883
14
      nextToken();
1884
386
    if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1885
386
                             tok::kw___finally) ||
1886
386
          
(204
(204
Style.Language == FormatStyle::LK_Java204
||
1887
204
            
Style.Language == FormatStyle::LK_JavaScript188
) &&
1888
204
           
FormatTok->is(Keywords.kw_finally)24
) ||
1889
386
          
(196
FormatTok->Tok.isObjCAtKeyword(tok::objc_catch)196
||
1890
196
           FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1891
184
      break;
1892
202
    nextToken();
1893
384
    while (FormatTok->isNot(tok::l_brace)) {
1894
185
      if (FormatTok->is(tok::l_paren)) {
1895
168
        parseParens();
1896
168
        continue;
1897
168
      }
1898
17
      if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
1899
3
        return;
1900
14
      nextToken();
1901
14
    }
1902
202
    NeedsUnwrappedLine = false;
1903
199
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
1904
199
    parseBlock(/*MustBeDeclaration=*/false);
1905
199
    if (Style.BraceWrapping.BeforeCatch)
1906
52
      addUnwrappedLine();
1907
147
    else
1908
147
      NeedsUnwrappedLine = true;
1909
199
  }
1910
187
  
if (184
NeedsUnwrappedLine184
)
1911
130
    addUnwrappedLine();
1912
184
}
1913
1914
3.39k
void UnwrappedLineParser::parseNamespace() {
1915
3.39k
  assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
1916
3.39k
         "'namespace' expected");
1917
3.39k
1918
3.39k
  const FormatToken &InitialToken = *FormatTok;
1919
3.39k
  nextToken();
1920
3.39k
  if (InitialToken.is(TT_NamespaceMacro)) {
1921
138
    parseParens();
1922
3.25k
  } else {
1923
6.48k
    while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
1924
6.48k
                              tok::l_square)) {
1925
3.23k
      if (FormatTok->is(tok::l_square))
1926
8
        parseSquare();
1927
3.22k
      else
1928
3.22k
        nextToken();
1929
3.23k
    }
1930
3.25k
  }
1931
3.39k
  if (FormatTok->Tok.is(tok::l_brace)) {
1932
3.38k
    if (ShouldBreakBeforeBrace(Style, InitialToken))
1933
85
      addUnwrappedLine();
1934
3.38k
1935
3.38k
    bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1936
3.38k
                    
(3.32k
Style.NamespaceIndentation == FormatStyle::NI_Inner3.32k
&&
1937
3.32k
                     
DeclarationScopeStack.size() > 178
);
1938
3.38k
    parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1939
3.38k
    // Munch the semicolon after a namespace. This is more common than one would
1940
3.38k
    // think. Puttin the semicolon into its own line is very ugly.
1941
3.38k
    if (FormatTok->Tok.is(tok::semi))
1942
1
      nextToken();
1943
3.38k
    addUnwrappedLine();
1944
3.38k
  }
1945
3.39k
  // FIXME: Add error handling.
1946
3.39k
}
1947
1948
269
void UnwrappedLineParser::parseNew() {
1949
269
  assert(FormatTok->is(tok::kw_new) && "'new' expected");
1950
269
  nextToken();
1951
269
  if (Style.Language != FormatStyle::LK_Java)
1952
259
    return;
1953
10
1954
10
  // In Java, we can parse everything up to the parens, which aren't optional.
1955
32
  
do 10
{
1956
32
    // There should not be a ;, { or } before the new's open paren.
1957
32
    if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1958
6
      return;
1959
26
1960
26
    // Consume the parens.
1961
26
    if (FormatTok->is(tok::l_paren)) {
1962
4
      parseParens();
1963
4
1964
4
      // If there is a class body of an anonymous class, consume that as child.
1965
4
      if (FormatTok->is(tok::l_brace))
1966
4
        parseChildBlock();
1967
4
      return;
1968
4
    }
1969
22
    nextToken();
1970
22
  } while (!eof());
1971
10
}
1972
1973
918
void UnwrappedLineParser::parseForOrWhileLoop() {
1974
918
  assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1975
918
         "'for', 'while' or foreach macro expected");
1976
918
  nextToken();
1977
918
  // JS' for await ( ...
1978
918
  if (Style.Language == FormatStyle::LK_JavaScript &&
1979
918
      
FormatTok->is(Keywords.kw_await)48
)
1980
8
    nextToken();
1981
918
  if (FormatTok->Tok.is(tok::l_paren))
1982
901
    parseParens();
1983
918
  if (FormatTok->Tok.is(tok::l_brace)) {
1984
644
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
1985
644
    parseBlock(/*MustBeDeclaration=*/false);
1986
644
    addUnwrappedLine();
1987
644
  } else {
1988
274
    addUnwrappedLine();
1989
274
    ++Line->Level;
1990
274
    parseStructuralElement();
1991
274
    --Line->Level;
1992
274
  }
1993
918
}
1994
1995
142
void UnwrappedLineParser::parseDoWhile() {
1996
142
  assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1997
142
  nextToken();
1998
142
  if (FormatTok->Tok.is(tok::l_brace)) {
1999
135
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2000
135
    parseBlock(/*MustBeDeclaration=*/false);
2001
135
    if (Style.BraceWrapping.IndentBraces)
2002
7
      addUnwrappedLine();
2003
135
  } else {
2004
7
    addUnwrappedLine();
2005
7
    ++Line->Level;
2006
7
    parseStructuralElement();
2007
7
    --Line->Level;
2008
7
  }
2009
142
2010
142
  // FIXME: Add error handling.
2011
142
  if (!FormatTok->Tok.is(tok::kw_while)) {
2012
28
    addUnwrappedLine();
2013
28
    return;
2014
28
  }
2015
114
2016
114
  nextToken();
2017
114
  parseStructuralElement();
2018
114
}
2019
2020
855
void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
2021
855
  nextToken();
2022
855
  unsigned OldLineLevel = Line->Level;
2023
855
  if (Line->Level > 1 || 
(624
!Line->InPPDirective624
&&
Line->Level > 0624
))
2024
848
    --Line->Level;
2025
855
  if (LeftAlignLabel)
2026
21
    Line->Level = 0;
2027
855
  if (!Style.IndentCaseBlocks && 
CommentsBeforeNextToken.empty()837
&&
2028
855
      
FormatTok->Tok.is(tok::l_brace)810
) {
2029
122
    CompoundStatementIndenter Indenter(this, Line->Level,
2030
122
                                       Style.BraceWrapping.AfterCaseLabel,
2031
122
                                       Style.BraceWrapping.IndentBraces);
2032
122
    parseBlock(/*MustBeDeclaration=*/false);
2033
122
    if (FormatTok->Tok.is(tok::kw_break)) {
2034
21
      if (Style.BraceWrapping.AfterControlStatement ==
2035
21
          FormatStyle::BWACS_Always)
2036
14
        addUnwrappedLine();
2037
21
      parseStructuralElement();
2038
21
    }
2039
122
    addUnwrappedLine();
2040
733
  } else {
2041
733
    if (FormatTok->is(tok::semi))
2042
28
      nextToken();
2043
733
    addUnwrappedLine();
2044
733
  }
2045
855
  Line->Level = OldLineLevel;
2046
855
  if (FormatTok->isNot(tok::l_brace)) {
2047
836
    parseStructuralElement();
2048
836
    addUnwrappedLine();
2049
836
  }
2050
855
}
2051
2052
574
void UnwrappedLineParser::parseCaseLabel() {
2053
574
  assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
2054
574
  // FIXME: fix handling of complex expressions here.
2055
1.27k
  do {
2056
1.27k
    nextToken();
2057
1.27k
  } while (!eof() && 
!FormatTok->Tok.is(tok::colon)1.26k
);
2058
574
  parseLabel();
2059
574
}
2060
2061
341
void UnwrappedLineParser::parseSwitch() {
2062
341
  assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
2063
341
  nextToken();
2064
341
  if (FormatTok->Tok.is(tok::l_paren))
2065
334
    parseParens();
2066
341
  if (FormatTok->Tok.is(tok::l_brace)) {
2067
320
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2068
320
    parseBlock(/*MustBeDeclaration=*/false);
2069
320
    addUnwrappedLine();
2070
320
  } else {
2071
21
    addUnwrappedLine();
2072
21
    ++Line->Level;
2073
21
    parseStructuralElement();
2074
21
    --Line->Level;
2075
21
  }
2076
341
}
2077
2078
933
void UnwrappedLineParser::parseAccessSpecifier() {
2079
933
  nextToken();
2080
933
  // Understand Qt's slots.
2081
933
  if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
2082
42
    nextToken();
2083
933
  // Otherwise, we don't know what it is, and we'd better keep the next token.
2084
933
  if (FormatTok->Tok.is(tok::colon))
2085
837
    nextToken();
2086
933
  addUnwrappedLine();
2087
933
}
2088
2089
1.92k
bool UnwrappedLineParser::parseEnum() {
2090
1.92k
  // Won't be 'enum' for NS_ENUMs.
2091
1.92k
  if (FormatTok->Tok.is(tok::kw_enum))
2092
1.86k
    nextToken();
2093
1.92k
2094
1.92k
  // In TypeScript, "enum" can also be used as property name, e.g. in interface
2095
1.92k
  // declarations. An "enum" keyword followed by a colon would be a syntax
2096
1.92k
  // error and thus assume it is just an identifier.
2097
1.92k
  if (Style.Language == FormatStyle::LK_JavaScript &&
2098
1.92k
      
FormatTok->isOneOf(tok::colon, tok::question)36
)
2099
8
    return false;
2100
1.91k
2101
1.91k
  // In protobuf, "enum" can be used as a field name.
2102
1.91k
  if (Style.Language == FormatStyle::LK_Proto && 
FormatTok->is(tok::equal)8
)
2103
2
    return false;
2104
1.91k
2105
1.91k
  // Eat up enum class ...
2106
1.91k
  if (FormatTok->Tok.is(tok::kw_class) || 
FormatTok->Tok.is(tok::kw_struct)1.60k
)
2107
351
    nextToken();
2108
1.91k
2109
3.77k
  while (FormatTok->Tok.getIdentifierInfo() ||
2110
3.77k
         FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
2111
1.95k
                            tok::greater, tok::comma, tok::question)) {
2112
1.90k
    nextToken();
2113
1.90k
    // We can have macros or attributes in between 'enum' and the enum name.
2114
1.90k
    if (FormatTok->is(tok::l_paren))
2115
100
      parseParens();
2116
1.90k
    if (FormatTok->is(tok::identifier)) {
2117
158
      nextToken();
2118
158
      // If there are two identifiers in a row, this is likely an elaborate
2119
158
      // return type. In Java, this can be "implements", etc.
2120
158
      if (Style.isCpp() && 
FormatTok->is(tok::identifier)154
)
2121
32
        return false;
2122
158
    }
2123
1.90k
  }
2124
1.91k
2125
1.91k
  // Just a declaration or something is wrong.
2126
1.91k
  
if (1.87k
FormatTok->isNot(tok::l_brace)1.87k
)
2127
38
    return true;
2128
1.84k
  FormatTok->BlockKind = BK_Block;
2129
1.84k
2130
1.84k
  if (Style.Language == FormatStyle::LK_Java) {
2131
20
    // Java enums are different.
2132
20
    parseJavaEnumBody();
2133
20
    return true;
2134
20
  }
2135
1.82k
  if (Style.Language == FormatStyle::LK_Proto) {
2136
6
    parseBlock(/*MustBeDeclaration=*/true);
2137
6
    return true;
2138
6
  }
2139
1.81k
2140
1.81k
  // Parse enum body.
2141
1.81k
  nextToken();
2142
1.81k
  bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
2143
1.81k
  if (HasError) {
2144
14
    if (FormatTok->is(tok::semi))
2145
7
      nextToken();
2146
14
    addUnwrappedLine();
2147
14
  }
2148
1.81k
  return true;
2149
1.81k
2150
1.81k
  // There is no addUnwrappedLine() here so that we fall through to parsing a
2151
1.81k
  // structural element afterwards. Thus, in "enum A {} n, m;",
2152
1.81k
  // "} n, m;" will end up in one unwrapped line.
2153
1.81k
}
2154
2155
20
void UnwrappedLineParser::parseJavaEnumBody() {
2156
20
  // Determine whether the enum is simple, i.e. does not have a semicolon or
2157
20
  // constants with class bodies. Simple enums can be formatted like braced
2158
20
  // lists, contracted to a single line, etc.
2159
20
  unsigned StoredPosition = Tokens->getPosition();
2160
20
  bool IsSimple = true;
2161
20
  FormatToken *Tok = Tokens->getNextToken();
2162
128
  while (Tok) {
2163
128
    if (Tok->is(tok::r_brace))
2164
8
      break;
2165
120
    if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2166
12
      IsSimple = false;
2167
12
      break;
2168
12
    }
2169
108
    // FIXME: This will also mark enums with braces in the arguments to enum
2170
108
    // constants as "not simple". This is probably fine in practice, though.
2171
108
    Tok = Tokens->getNextToken();
2172
108
  }
2173
20
  FormatTok = Tokens->setPosition(StoredPosition);
2174
20
2175
20
  if (IsSimple) {
2176
8
    nextToken();
2177
8
    parseBracedList();
2178
8
    addUnwrappedLine();
2179
8
    return;
2180
8
  }
2181
12
2182
12
  // Parse the body of a more complex enum.
2183
12
  // First add a line for everything up to the "{".
2184
12
  nextToken();
2185
12
  addUnwrappedLine();
2186
12
  ++Line->Level;
2187
12
2188
12
  // Parse the enum constants.
2189
68
  while (FormatTok) {
2190
68
    if (FormatTok->is(tok::l_brace)) {
2191
8
      // Parse the constant's class body.
2192
8
      parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2193
8
                 /*MunchSemi=*/false);
2194
60
    } else if (FormatTok->is(tok::l_paren)) {
2195
12
      parseParens();
2196
48
    } else if (FormatTok->is(tok::comma)) {
2197
12
      nextToken();
2198
12
      addUnwrappedLine();
2199
36
    } else if (FormatTok->is(tok::semi)) {
2200
12
      nextToken();
2201
12
      addUnwrappedLine();
2202
12
      break;
2203
24
    } else if (FormatTok->is(tok::r_brace)) {
2204
0
      addUnwrappedLine();
2205
0
      break;
2206
24
    } else {
2207
24
      nextToken();
2208
24
    }
2209
68
  }
2210
12
2211
12
  // Parse the class body after the enum's ";" if any.
2212
12
  parseLevel(/*HasOpeningBrace=*/true);
2213
12
  nextToken();
2214
12
  --Line->Level;
2215
12
  addUnwrappedLine();
2216
12
}
2217
2218
6.57k
void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
2219
6.57k
  const FormatToken &InitialToken = *FormatTok;
2220
6.57k
  nextToken();
2221
6.57k
2222
6.57k
  // The actual identifier can be a nested name specifier, and in macros
2223
6.57k
  // it is often token-pasted.
2224
13.4k
  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2225
13.4k
                            tok::kw___attribute, tok::kw___declspec,
2226
13.4k
                            tok::kw_alignas) ||
2227
13.4k
         
(6.58k
(6.58k
Style.Language == FormatStyle::LK_Java6.58k
||
2228
6.58k
           
Style.Language == FormatStyle::LK_JavaScript6.54k
) &&
2229
6.91k
          
FormatTok->isOneOf(tok::period, tok::comma)329
)) {
2230
6.91k
    if (Style.Language == FormatStyle::LK_JavaScript &&
2231
6.91k
        
FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)324
) {
2232
36
      // JavaScript/TypeScript supports inline object types in
2233
36
      // extends/implements positions:
2234
36
      //     class Foo implements {bar: number} { }
2235
36
      nextToken();
2236
36
      if (FormatTok->is(tok::l_brace)) {
2237
12
        tryToParseBracedList();
2238
12
        continue;
2239
12
      }
2240
6.90k
    }
2241
6.90k
    bool IsNonMacroIdentifier =
2242
6.90k
        FormatTok->is(tok::identifier) &&
2243
6.90k
        
FormatTok->TokenText != FormatTok->TokenText.upper()6.77k
;
2244
6.90k
    nextToken();
2245
6.90k
    // We can have macros or attributes in between 'class' and the class name.
2246
6.90k
    if (!IsNonMacroIdentifier && 
FormatTok->Tok.is(tok::l_paren)3.48k
)
2247
49
      parseParens();
2248
6.90k
  }
2249
6.57k
2250
6.57k
  // Note that parsing away template declarations here leads to incorrectly
2251
6.57k
  // accepting function declarations as record declarations.
2252
6.57k
  // In general, we cannot solve this problem. Consider:
2253
6.57k
  // class A<int> B() {}
2254
6.57k
  // which can be a function definition or a class definition when B() is a
2255
6.57k
  // macro. If we find enough real-world cases where this is a problem, we
2256
6.57k
  // can parse for the 'template' keyword in the beginning of the statement,
2257
6.57k
  // and thus rule out the record production in case there is no template
2258
6.57k
  // (this would still leave us with an ambiguity between template function
2259
6.57k
  // and class declarations).
2260
6.57k
  if (FormatTok->isOneOf(tok::colon, tok::less)) {
2261
4.90k
    while (!eof()) {
2262
4.90k
      if (FormatTok->is(tok::l_brace)) {
2263
752
        calculateBraceTypes(/*ExpectClassBody=*/true);
2264
752
        if (!tryToParseBracedList())
2265
740
          break;
2266
4.16k
      }
2267
4.16k
      if (FormatTok->Tok.is(tok::semi))
2268
14
        return;
2269
4.14k
      nextToken();
2270
4.14k
    }
2271
761
  }
2272
6.57k
  
if (6.55k
FormatTok->Tok.is(tok::l_brace)6.55k
) {
2273
5.57k
    if (ParseAsExpr) {
2274
4
      parseChildBlock();
2275
5.57k
    } else {
2276
5.57k
      if (ShouldBreakBeforeBrace(Style, InitialToken))
2277
242
        addUnwrappedLine();
2278
5.57k
2279
5.57k
      parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2280
5.57k
                 /*MunchSemi=*/false);
2281
5.57k
    }
2282
5.57k
  }
2283
6.55k
  // There is no addUnwrappedLine() here so that we fall through to parsing a
2284
6.55k
  // structural element afterwards. Thus, in "class A {} n, m;",
2285
6.55k
  // "} n, m;" will end up in one unwrapped line.
2286
6.55k
}
2287
2288
120
void UnwrappedLineParser::parseObjCMethod() {
2289
120
  assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2290
120
         "'(' or identifier expected.");
2291
748
  do {
2292
748
    if (FormatTok->Tok.is(tok::semi)) {
2293
76
      nextToken();
2294
76
      addUnwrappedLine();
2295
76
      return;
2296
672
    } else if (FormatTok->Tok.is(tok::l_brace)) {
2297
44
      if (Style.BraceWrapping.AfterFunction)
2298
2
        addUnwrappedLine();
2299
44
      parseBlock(/*MustBeDeclaration=*/false);
2300
44
      addUnwrappedLine();
2301
44
      return;
2302
628
    } else {
2303
628
      nextToken();
2304
628
    }
2305
748
  } while (
!eof()628
);
2306
120
}
2307
2308
40
void UnwrappedLineParser::parseObjCProtocolList() {
2309
40
  assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
2310
162
  do {
2311
162
    nextToken();
2312
162
    // Early exit in case someone forgot a close angle.
2313
162
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2314
162
        FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2315
0
      return;
2316
162
  } while (!eof() && FormatTok->Tok.isNot(tok::greater));
2317
40
  nextToken(); // Skip '>'.
2318
40
}
2319
2320
194
void UnwrappedLineParser::parseObjCUntilAtEnd() {
2321
503
  do {
2322
503
    if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
2323
171
      nextToken();
2324
171
      addUnwrappedLine();
2325
171
      break;
2326
171
    }
2327
332
    if (FormatTok->is(tok::l_brace)) {
2328
2
      parseBlock(/*MustBeDeclaration=*/false);
2329
2
      // In ObjC interfaces, nothing should be following the "}".
2330
2
      addUnwrappedLine();
2331
330
    } else if (FormatTok->is(tok::r_brace)) {
2332
2
      // Ignore stray "}". parseStructuralElement doesn't consume them.
2333
2
      nextToken();
2334
2
      addUnwrappedLine();
2335
328
    } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2336
120
      nextToken();
2337
120
      parseObjCMethod();
2338
208
    } else {
2339
208
      parseStructuralElement();
2340
208
    }
2341
332
  } while (!eof());
2342
194
}
2343
2344
175
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2345
175
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2346
175
         FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
2347
175
  nextToken();
2348
175
  nextToken(); // interface name
2349
175
2350
175
  // @interface can be followed by a lightweight generic
2351
175
  // specialization list, then either a base class or a category.
2352
175
  if (FormatTok->Tok.is(tok::less)) {
2353
6
    // Unlike protocol lists, generic parameterizations support
2354
6
    // nested angles:
2355
6
    //
2356
6
    // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2357
6
    //     NSObject <NSCopying, NSSecureCoding>
2358
6
    //
2359
6
    // so we need to count how many open angles we have left.
2360
6
    unsigned NumOpenAngles = 1;
2361
42
    do {
2362
42
      nextToken();
2363
42
      // Early exit in case someone forgot a close angle.
2364
42
      if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2365
42
          FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2366
0
        break;
2367
42
      if (FormatTok->Tok.is(tok::less))
2368
2
        ++NumOpenAngles;
2369
40
      else if (FormatTok->Tok.is(tok::greater)) {
2370
8
        assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2371
8
        --NumOpenAngles;
2372
8
      }
2373
42
    } while (!eof() && NumOpenAngles != 0);
2374
6
    nextToken(); // Skip '>'.
2375
6
  }
2376
175
  if (FormatTok->Tok.is(tok::colon)) {
2377
39
    nextToken();
2378
39
    nextToken(); // base class name
2379
136
  } else if (FormatTok->Tok.is(tok::l_paren))
2380
59
    // Skip category, if present.
2381
59
    parseParens();
2382
175
2383
175
  if (FormatTok->Tok.is(tok::less))
2384
36
    parseObjCProtocolList();
2385
175
2386
175
  if (FormatTok->Tok.is(tok::l_brace)) {
2387
69
    if (Style.BraceWrapping.AfterObjCDeclaration)
2388
21
      addUnwrappedLine();
2389
69
    parseBlock(/*MustBeDeclaration=*/true);
2390
69
  }
2391
175
2392
175
  // With instance variables, this puts '}' on its own line.  Without instance
2393
175
  // variables, this ends the @interface line.
2394
175
  addUnwrappedLine();
2395
175
2396
175
  parseObjCUntilAtEnd();
2397
175
}
2398
2399
// Returns true for the declaration/definition form of @protocol,
2400
// false for the expression form.
2401
25
bool UnwrappedLineParser::parseObjCProtocol() {
2402
25
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
2403
25
  nextToken();
2404
25
2405
25
  if (FormatTok->is(tok::l_paren))
2406
2
    // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2407
2
    return false;
2408
23
2409
23
  // The definition/declaration form,
2410
23
  // @protocol Foo
2411
23
  // - (int)someMethod;
2412
23
  // @end
2413
23
2414
23
  nextToken(); // protocol name
2415
23
2416
23
  if (FormatTok->Tok.is(tok::less))
2417
4
    parseObjCProtocolList();
2418
23
2419
23
  // Check for protocol declaration.
2420
23
  if (FormatTok->Tok.is(tok::semi)) {
2421
4
    nextToken();
2422
4
    addUnwrappedLine();
2423
4
    return true;
2424
4
  }
2425
19
2426
19
  addUnwrappedLine();
2427
19
  parseObjCUntilAtEnd();
2428
19
  return true;
2429
19
}
2430
2431
403
void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2432
403
  bool IsImport = FormatTok->is(Keywords.kw_import);
2433
403
  assert(IsImport || FormatTok->is(tok::kw_export));
2434
403
  nextToken();
2435
403
2436
403
  // Consume the "default" in "export default class/function".
2437
403
  if (FormatTok->is(tok::kw_default))
2438
36
    nextToken();
2439
403
2440
403
  // Consume "async function", "function" and "default function", so that these
2441
403
  // get parsed as free-standing JS functions, i.e. do not require a trailing
2442
403
  // semicolon.
2443
403
  if (FormatTok->is(Keywords.kw_async))
2444
4
    nextToken();
2445
403
  if (FormatTok->is(Keywords.kw_function)) {
2446
40
    nextToken();
2447
40
    return;
2448
40
  }
2449
363
2450
363
  // For imports, `export *`, `export {...}`, consume the rest of the line up
2451
363
  // to the terminating `;`. For everything else, just return and continue
2452
363
  // parsing the structural element, i.e. the declaration or expression for
2453
363
  // `export default`.
2454
363
  if (!IsImport && 
!FormatTok->isOneOf(tok::l_brace, tok::star)142
&&
2455
363
      
!FormatTok->isStringLiteral()103
)
2456
103
    return;
2457
260
2458
1.05k
  
while (260
!eof()) {
2459
1.05k
    if (FormatTok->is(tok::semi))
2460
252
      return;
2461
802
    if (Line->Tokens.empty()) {
2462
4
      // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2463
4
      // import statement should terminate.
2464
4
      return;
2465
4
    }
2466
798
    if (FormatTok->is(tok::l_brace)) {
2467
238
      FormatTok->BlockKind = BK_Block;
2468
238
      nextToken();
2469
238
      parseBracedList();
2470
560
    } else {
2471
560
      nextToken();
2472
560
    }
2473
798
  }
2474
260
}
2475
2476
77
void UnwrappedLineParser::parseStatementMacro() {
2477
77
  nextToken();
2478
77
  if (FormatTok->is(tok::l_paren))
2479
70
    parseParens();
2480
77
  if (FormatTok->is(tok::semi))
2481
7
    nextToken();
2482
77
  addUnwrappedLine();
2483
77
}
2484
2485
LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2486
0
                                                 StringRef Prefix = "") {
2487
0
  llvm::dbgs() << Prefix << "Line(" << Line.Level
2488
0
               << ", FSC=" << Line.FirstStartColumn << ")"
2489
0
               << (Line.InPPDirective ? " MACRO" : "") << ": ";
2490
0
  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2491
0
                                                    E = Line.Tokens.end();
2492
0
       I != E; ++I) {
2493
0
    llvm::dbgs() << I->Tok->Tok.getName() << "["
2494
0
                 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2495
0
                 << "] ";
2496
0
  }
2497
0
  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2498
0
                                                    E = Line.Tokens.end();
2499
0
       I != E; ++I) {
2500
0
    const UnwrappedLineNode &Node = *I;
2501
0
    for (SmallVectorImpl<UnwrappedLine>::const_iterator
2502
0
             I = Node.Children.begin(),
2503
0
             E = Node.Children.end();
2504
0
         I != E; ++I) {
2505
0
      printDebugInfo(*I, "\nChild: ");
2506
0
    }
2507
0
  }
2508
0
  llvm::dbgs() << "\n";
2509
0
}
2510
2511
239k
void UnwrappedLineParser::addUnwrappedLine() {
2512
239k
  if (Line->Tokens.empty())
2513
105k
    return;
2514
133k
  LLVM_DEBUG({
2515
133k
    if (CurrentLines == &Lines)
2516
133k
      printDebugInfo(*Line);
2517
133k
  });
2518
133k
  CurrentLines->push_back(std::move(*Line));
2519
133k
  Line->Tokens.clear();
2520
133k
  Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
2521
133k
  Line->FirstStartColumn = 0;
2522
133k
  if (CurrentLines == &Lines && 
!PreprocessorDirectives.empty()128k
) {
2523
1.02k
    CurrentLines->append(
2524
1.02k
        std::make_move_iterator(PreprocessorDirectives.begin()),
2525
1.02k
        std::make_move_iterator(PreprocessorDirectives.end()));
2526
1.02k
    PreprocessorDirectives.clear();
2527
1.02k
  }
2528
133k
  // Disconnect the current token from the last token on the previous line.
2529
133k
  FormatTok->Previous = nullptr;
2530
133k
}
2531
2532
953k
bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
2533
2534
548k
bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
2535
548k
  return (Line->InPPDirective || 
FormatTok.HasUnescapedNewline510k
) &&
2536
548k
         
FormatTok.NewlinesBefore > 095.2k
;
2537
548k
}
2538
2539
// Checks if \p FormatTok is a line comment that continues the line comment
2540
// section on \p Line.
2541
static bool
2542
continuesLineCommentSection(const FormatToken &FormatTok,
2543
                            const UnwrappedLine &Line,
2544
13.2k
                            const llvm::Regex &CommentPragmasRegex) {
2545
13.2k
  if (Line.Tokens.empty())
2546
4.06k
    return false;
2547
9.16k
2548
9.16k
  StringRef IndentContent = FormatTok.TokenText;
2549
9.16k
  if (FormatTok.TokenText.startswith("//") ||
2550
9.16k
      
FormatTok.TokenText.startswith("/*")1.09k
)
2551
9.09k
    IndentContent = FormatTok.TokenText.substr(2);
2552
9.16k
  if (CommentPragmasRegex.match(IndentContent))
2553
7
    return false;
2554
9.15k
2555
9.15k
  // If Line starts with a line comment, then FormatTok continues the comment
2556
9.15k
  // section if its original column is greater or equal to the original start
2557
9.15k
  // column of the line.
2558
9.15k
  //
2559
9.15k
  // Define the min column token of a line as follows: if a line ends in '{' or
2560
9.15k
  // contains a '{' followed by a line comment, then the min column token is
2561
9.15k
  // that '{'. Otherwise, the min column token of the line is the first token of
2562
9.15k
  // the line.
2563
9.15k
  //
2564
9.15k
  // If Line starts with a token other than a line comment, then FormatTok
2565
9.15k
  // continues the comment section if its original column is greater than the
2566
9.15k
  // original start column of the min column token of the line.
2567
9.15k
  //
2568
9.15k
  // For example, the second line comment continues the first in these cases:
2569
9.15k
  //
2570
9.15k
  // // first line
2571
9.15k
  // // second line
2572
9.15k
  //
2573
9.15k
  // and:
2574
9.15k
  //
2575
9.15k
  // // first line
2576
9.15k
  //  // second line
2577
9.15k
  //
2578
9.15k
  // and:
2579
9.15k
  //
2580
9.15k
  // int i; // first line
2581
9.15k
  //  // second line
2582
9.15k
  //
2583
9.15k
  // and:
2584
9.15k
  //
2585
9.15k
  // do { // first line
2586
9.15k
  //      // second line
2587
9.15k
  //   int i;
2588
9.15k
  // } while (true);
2589
9.15k
  //
2590
9.15k
  // and:
2591
9.15k
  //
2592
9.15k
  // enum {
2593
9.15k
  //   a, // first line
2594
9.15k
  //    // second line
2595
9.15k
  //   b
2596
9.15k
  // };
2597
9.15k
  //
2598
9.15k
  // The second line comment doesn't continue the first in these cases:
2599
9.15k
  //
2600
9.15k
  //   // first line
2601
9.15k
  //  // second line
2602
9.15k
  //
2603
9.15k
  // and:
2604
9.15k
  //
2605
9.15k
  // int i; // first line
2606
9.15k
  // // second line
2607
9.15k
  //
2608
9.15k
  // and:
2609
9.15k
  //
2610
9.15k
  // do { // first line
2611
9.15k
  //   // second line
2612
9.15k
  //   int i;
2613
9.15k
  // } while (true);
2614
9.15k
  //
2615
9.15k
  // and:
2616
9.15k
  //
2617
9.15k
  // enum {
2618
9.15k
  //   a, // first line
2619
9.15k
  //   // second line
2620
9.15k
  // };
2621
9.15k
  const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2622
9.15k
2623
9.15k
  // Scan for '{//'. If found, use the column of '{' as a min column for line
2624
9.15k
  // comment section continuation.
2625
9.15k
  const FormatToken *PreviousToken = nullptr;
2626
38.9k
  for (const UnwrappedLineNode &Node : Line.Tokens) {
2627
38.9k
    if (PreviousToken && 
PreviousToken->is(tok::l_brace)29.7k
&&
2628
38.9k
        
isLineComment(*Node.Tok)902
) {
2629
201
      MinColumnToken = PreviousToken;
2630
201
      break;
2631
201
    }
2632
38.7k
    PreviousToken = Node.Tok;
2633
38.7k
2634
38.7k
    // Grab the last newline preceding a token in this unwrapped line.
2635
38.7k
    if (Node.Tok->NewlinesBefore > 0) {
2636
9.50k
      MinColumnToken = Node.Tok;
2637
9.50k
    }
2638
38.7k
  }
2639
9.15k
  if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2640
1.63k
    MinColumnToken = PreviousToken;
2641
1.63k
  }
2642
9.15k
2643
9.15k
  return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2644
9.15k
                              MinColumnToken);
2645
9.15k
}
2646
2647
570k
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2648
570k
  bool JustComments = Line->Tokens.empty();
2649
570k
  for (SmallVectorImpl<FormatToken *>::const_iterator
2650
570k
           I = CommentsBeforeNextToken.begin(),
2651
570k
           E = CommentsBeforeNextToken.end();
2652
574k
       I != E; 
++I4.18k
) {
2653
4.18k
    // Line comments that belong to the same line comment section are put on the
2654
4.18k
    // same line since later we might want to reflow content between them.
2655
4.18k
    // Additional fine-grained breaking of line comment sections is controlled
2656
4.18k
    // by the class BreakableLineCommentSection in case it is desirable to keep
2657
4.18k
    // several line comment sections in the same unwrapped line.
2658
4.18k
    //
2659
4.18k
    // FIXME: Consider putting separate line comment sections as children to the
2660
4.18k
    // unwrapped line instead.
2661
4.18k
    (*I)->ContinuesLineCommentSection =
2662
4.18k
        continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
2663
4.18k
    if (isOnNewLine(**I) && 
JustComments3.24k
&&
!(*I)->ContinuesLineCommentSection2.83k
)
2664
1.43k
      addUnwrappedLine();
2665
4.18k
    pushToken(*I);
2666
4.18k
  }
2667
570k
  if (NewlineBeforeNext && 
JustComments90.8k
)
2668
74.8k
    addUnwrappedLine();
2669
570k
  CommentsBeforeNextToken.clear();
2670
570k
}
2671
2672
527k
void UnwrappedLineParser::nextToken(int LevelDifference) {
2673
527k
  if (eof())
2674
2.83k
    return;
2675
524k
  flushComments(isOnNewLine(*FormatTok));
2676
524k
  pushToken(FormatTok);
2677
524k
  FormatToken *Previous = FormatTok;
2678
524k
  if (Style.Language != FormatStyle::LK_JavaScript)
2679
494k
    readToken(LevelDifference);
2680
30.2k
  else
2681
30.2k
    readTokenWithJavaScriptASI();
2682
524k
  FormatTok->Previous = Previous;
2683
524k
}
2684
2685
void UnwrappedLineParser::distributeComments(
2686
    const SmallVectorImpl<FormatToken *> &Comments,
2687
565k
    const FormatToken *NextTok) {
2688
565k
  // Whether or not a line comment token continues a line is controlled by
2689
565k
  // the method continuesLineCommentSection, with the following caveat:
2690
565k
  //
2691
565k
  // Define a trail of Comments to be a nonempty proper postfix of Comments such
2692
565k
  // that each comment line from the trail is aligned with the next token, if
2693
565k
  // the next token exists. If a trail exists, the beginning of the maximal
2694
565k
  // trail is marked as a start of a new comment section.
2695
565k
  //
2696
565k
  // For example in this code:
2697
565k
  //
2698
565k
  // int a; // line about a
2699
565k
  //   // line 1 about b
2700
565k
  //   // line 2 about b
2701
565k
  //   int b;
2702
565k
  //
2703
565k
  // the two lines about b form a maximal trail, so there are two sections, the
2704
565k
  // first one consisting of the single comment "// line about a" and the
2705
565k
  // second one consisting of the next two comments.
2706
565k
  if (Comments.empty())
2707
558k
    return;
2708
7.61k
  bool ShouldPushCommentsInCurrentLine = true;
2709
7.61k
  bool HasTrailAlignedWithNextToken = false;
2710
7.61k
  unsigned StartOfTrailAlignedWithNextToken = 0;
2711
7.61k
  if (NextTok) {
2712
7.61k
    // We are skipping the first element intentionally.
2713
9.42k
    for (unsigned i = Comments.size() - 1; i > 0; 
--i1.81k
) {
2714
1.81k
      if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2715
1.28k
        HasTrailAlignedWithNextToken = true;
2716
1.28k
        StartOfTrailAlignedWithNextToken = i;
2717
1.28k
      }
2718
1.81k
    }
2719
7.61k
  }
2720
17.0k
  for (unsigned i = 0, e = Comments.size(); i < e; 
++i9.42k
) {
2721
9.42k
    FormatToken *FormatTok = Comments[i];
2722
9.42k
    if (HasTrailAlignedWithNextToken && 
i == StartOfTrailAlignedWithNextToken1.70k
) {
2723
377
      FormatTok->ContinuesLineCommentSection = false;
2724
9.04k
    } else {
2725
9.04k
      FormatTok->ContinuesLineCommentSection =
2726
9.04k
          continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
2727
9.04k
    }
2728
9.42k
    if (!FormatTok->ContinuesLineCommentSection &&
2729
9.42k
        
(9.25k
isOnNewLine(*FormatTok)9.25k
||
FormatTok->IsFirst6.01k
)) {
2730
4.15k
      ShouldPushCommentsInCurrentLine = false;
2731
4.15k
    }
2732
9.42k
    if (ShouldPushCommentsInCurrentLine) {
2733
5.24k
      pushToken(FormatTok);
2734
5.24k
    } else {
2735
4.18k
      CommentsBeforeNextToken.push_back(FormatTok);
2736
4.18k
    }
2737
9.42k
  }
2738
7.61k
}
2739
2740
557k
void UnwrappedLineParser::readToken(int LevelDifference) {
2741
557k
  SmallVector<FormatToken *, 1> Comments;
2742
569k
  do {
2743
569k
    FormatTok = Tokens->getNextToken();
2744
569k
    assert(FormatTok);
2745
577k
    while (!Line->InPPDirective && 
FormatTok->Tok.is(tok::hash)539k
&&
2746
577k
           
(8.26k
FormatTok->HasUnescapedNewline8.26k
||
FormatTok->IsFirst4.07k
)) {
2747
8.25k
      distributeComments(Comments, FormatTok);
2748
8.25k
      Comments.clear();
2749
8.25k
      // If there is an unfinished unwrapped line, we flush the preprocessor
2750
8.25k
      // directives only after that unwrapped line was finished later.
2751
8.25k
      bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2752
8.25k
      ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
2753
8.25k
      assert((LevelDifference >= 0 ||
2754
8.25k
              static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2755
8.25k
             "LevelDifference makes Line->Level negative");
2756
8.25k
      Line->Level += LevelDifference;
2757
8.25k
      // Comments stored before the preprocessor directive need to be output
2758
8.25k
      // before the preprocessor directive, at the same level as the
2759
8.25k
      // preprocessor directive, as we consider them to apply to the directive.
2760
8.25k
      if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
2761
8.25k
          
PPBranchLevel > 0235
)
2762
80
        Line->Level += PPBranchLevel;
2763
8.25k
      flushComments(isOnNewLine(*FormatTok));
2764
8.25k
      parsePPDirective();
2765
8.25k
    }
2766
569k
    while (FormatTok->Type == TT_ConflictStart ||
2767
569k
           
FormatTok->Type == TT_ConflictEnd569k
||
2768
569k
           
FormatTok->Type == TT_ConflictAlternative569k
) {
2769
108
      if (FormatTok->Type == TT_ConflictStart) {
2770
27
        conditionalCompilationStart(/*Unreachable=*/false);
2771
81
      } else if (FormatTok->Type == TT_ConflictAlternative) {
2772
54
        conditionalCompilationAlternative();
2773
54
      } else 
if (27
FormatTok->Type == TT_ConflictEnd27
) {
2774
27
        conditionalCompilationEnd();
2775
27
      }
2776
108
      FormatTok = Tokens->getNextToken();
2777
108
      FormatTok->MustBreakBefore = true;
2778
108
    }
2779
569k
2780
569k
    if (!PPStack.empty() && 
(PPStack.back().Kind == PP_Unreachable)12.3k
&&
2781
569k
        
!Line->InPPDirective5.13k
) {
2782
2.51k
      continue;
2783
2.51k
    }
2784
567k
2785
567k
    if (!FormatTok->Tok.is(tok::comment)) {
2786
557k
      distributeComments(Comments, FormatTok);
2787
557k
      Comments.clear();
2788
557k
      return;
2789
557k
    }
2790
9.42k
2791
9.42k
    Comments.push_back(FormatTok);
2792
11.9k
  } while (!eof());
2793
557k
2794
557k
  distributeComments(Comments, nullptr);
2795
0
  Comments.clear();
2796
0
}
2797
2798
567k
void UnwrappedLineParser::pushToken(FormatToken *Tok) {
2799
567k
  Line->Tokens.push_back(UnwrappedLineNode(Tok));
2800
567k
  if (MustBreakBeforeNextToken) {
2801
1.72k
    Line->Tokens.back().Tok->MustBreakBefore = true;
2802
1.72k
    MustBreakBeforeNextToken = false;
2803
1.72k
  }
2804
567k
}
2805
2806
} // end namespace format
2807
} // end namespace clang