@@ -74,8 +74,9 @@ class ScopedMacroState : public FormatTokenSource {
74
74
UnwrappedLineParser::UnwrappedLineParser (const FormatStyle &Style,
75
75
FormatTokenSource &Tokens,
76
76
UnwrappedLineConsumer &Callback)
77
- : RootTokenInitialized(false ), Style(Style), Tokens(&Tokens),
78
- Callback (Callback) {
77
+ : Line(new UnwrappedLine), RootTokenInitialized(false ),
78
+ LastInCurrentLine (NULL ), MustBreakBeforeNextToken(false ), Style(Style),
79
+ Tokens(&Tokens), Callback(Callback) {
79
80
}
80
81
81
82
bool UnwrappedLineParser::parse () {
@@ -126,9 +127,9 @@ bool UnwrappedLineParser::parseBlock(unsigned AddLevels) {
126
127
127
128
addUnwrappedLine ();
128
129
129
- Line. Level += AddLevels;
130
+ Line-> Level += AddLevels;
130
131
parseLevel (/* HasOpeningBrace=*/ true );
131
- Line. Level -= AddLevels;
132
+ Line-> Level -= AddLevels;
132
133
133
134
if (!FormatTok.Tok .is (tok::r_brace))
134
135
return true ;
@@ -139,7 +140,7 @@ bool UnwrappedLineParser::parseBlock(unsigned AddLevels) {
139
140
140
141
void UnwrappedLineParser::parsePPDirective () {
141
142
assert (FormatTok.Tok .is (tok::hash) && " '#' expected" );
142
- ScopedMacroState MacroState (Line, Tokens, FormatTok);
143
+ ScopedMacroState MacroState (* Line, Tokens, FormatTok);
143
144
nextToken ();
144
145
145
146
if (FormatTok.Tok .getIdentifierInfo () == NULL ) {
@@ -169,7 +170,7 @@ void UnwrappedLineParser::parsePPDefine() {
169
170
parseParens ();
170
171
}
171
172
addUnwrappedLine ();
172
- Line. Level = 1 ;
173
+ Line-> Level = 1 ;
173
174
174
175
// Errors during a preprocessor directive can only affect the layout of the
175
176
// preprocessor directive, and thus we ignore them. An alternative approach
@@ -319,9 +320,9 @@ void UnwrappedLineParser::parseIfThenElse() {
319
320
NeedsUnwrappedLine = true ;
320
321
} else {
321
322
addUnwrappedLine ();
322
- ++Line. Level ;
323
+ ++Line-> Level ;
323
324
parseStructuralElement ();
324
- --Line. Level ;
325
+ --Line-> Level ;
325
326
}
326
327
if (FormatTok.Tok .is (tok::kw_else)) {
327
328
nextToken ();
@@ -332,9 +333,9 @@ void UnwrappedLineParser::parseIfThenElse() {
332
333
parseIfThenElse ();
333
334
} else {
334
335
addUnwrappedLine ();
335
- ++Line. Level ;
336
+ ++Line-> Level ;
336
337
parseStructuralElement ();
337
- --Line. Level ;
338
+ --Line-> Level ;
338
339
}
339
340
} else if (NeedsUnwrappedLine) {
340
341
addUnwrappedLine ();
@@ -363,9 +364,9 @@ void UnwrappedLineParser::parseForOrWhileLoop() {
363
364
addUnwrappedLine ();
364
365
} else {
365
366
addUnwrappedLine ();
366
- ++Line. Level ;
367
+ ++Line-> Level ;
367
368
parseStructuralElement ();
368
- --Line. Level ;
369
+ --Line-> Level ;
369
370
}
370
371
}
371
372
@@ -376,9 +377,9 @@ void UnwrappedLineParser::parseDoWhile() {
376
377
parseBlock ();
377
378
} else {
378
379
addUnwrappedLine ();
379
- ++Line. Level ;
380
+ ++Line-> Level ;
380
381
parseStructuralElement ();
381
- --Line. Level ;
382
+ --Line-> Level ;
382
383
}
383
384
384
385
// FIXME: Add error handling.
@@ -395,14 +396,14 @@ void UnwrappedLineParser::parseLabel() {
395
396
// FIXME: remove all asserts.
396
397
assert (FormatTok.Tok .is (tok::colon) && " ':' expected" );
397
398
nextToken ();
398
- unsigned OldLineLevel = Line. Level ;
399
- if (Line. Level > 0 )
400
- --Line. Level ;
399
+ unsigned OldLineLevel = Line-> Level ;
400
+ if (Line-> Level > 0 )
401
+ --Line-> Level ;
401
402
if (FormatTok.Tok .is (tok::l_brace)) {
402
403
parseBlock ();
403
404
}
404
405
addUnwrappedLine ();
405
- Line. Level = OldLineLevel;
406
+ Line-> Level = OldLineLevel;
406
407
}
407
408
408
409
void UnwrappedLineParser::parseCaseLabel () {
@@ -423,9 +424,9 @@ void UnwrappedLineParser::parseSwitch() {
423
424
addUnwrappedLine ();
424
425
} else {
425
426
addUnwrappedLine ();
426
- Line. Level += (Style.IndentCaseLabels ? 2 : 1 );
427
+ Line-> Level += (Style.IndentCaseLabels ? 2 : 1 );
427
428
parseStructuralElement ();
428
- Line. Level -= (Style.IndentCaseLabels ? 2 : 1 );
429
+ Line-> Level -= (Style.IndentCaseLabels ? 2 : 1 );
429
430
}
430
431
}
431
432
@@ -444,7 +445,7 @@ void UnwrappedLineParser::parseEnum() {
444
445
case tok::l_brace:
445
446
nextToken ();
446
447
addUnwrappedLine ();
447
- ++Line. Level ;
448
+ ++Line-> Level ;
448
449
parseComments ();
449
450
break ;
450
451
case tok::l_paren:
@@ -458,7 +459,7 @@ void UnwrappedLineParser::parseEnum() {
458
459
case tok::r_brace:
459
460
if (HasContents)
460
461
addUnwrappedLine ();
461
- --Line. Level ;
462
+ --Line-> Level ;
462
463
nextToken ();
463
464
break ;
464
465
case tok::semi:
@@ -501,8 +502,9 @@ void UnwrappedLineParser::addUnwrappedLine() {
501
502
FormatTok.Tok .is (tok::comment)) {
502
503
nextToken ();
503
504
}
504
- Callback.consumeUnwrappedLine (Line);
505
+ Callback.consumeUnwrappedLine (* Line);
505
506
RootTokenInitialized = false ;
507
+ LastInCurrentLine = NULL ;
506
508
}
507
509
508
510
bool UnwrappedLineParser::eof () const {
@@ -513,26 +515,42 @@ void UnwrappedLineParser::nextToken() {
513
515
if (eof ())
514
516
return ;
515
517
if (RootTokenInitialized) {
518
+ assert (LastInCurrentLine->Children .empty ());
516
519
LastInCurrentLine->Children .push_back (FormatTok);
517
520
LastInCurrentLine = &LastInCurrentLine->Children .back ();
518
521
} else {
519
- Line. RootToken = FormatTok;
522
+ Line-> RootToken = FormatTok;
520
523
RootTokenInitialized = true ;
521
- LastInCurrentLine = &Line.RootToken ;
524
+ LastInCurrentLine = &Line->RootToken ;
525
+ }
526
+ if (MustBreakBeforeNextToken) {
527
+ LastInCurrentLine->MustBreakBefore = true ;
528
+ MustBreakBeforeNextToken = false ;
522
529
}
523
530
readToken ();
524
531
}
525
532
526
533
void UnwrappedLineParser::readToken () {
527
534
FormatTok = Tokens->getNextToken ();
528
- while (!Line. InPPDirective && FormatTok.Tok .is (tok::hash) &&
535
+ while (!Line-> InPPDirective && FormatTok.Tok .is (tok::hash) &&
529
536
((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline ) ||
530
537
FormatTok.IsFirst )) {
531
- // FIXME: This is incorrect - the correct way is to create a
532
- // data structure that will construct the parts around the preprocessor
533
- // directive as a structured \c UnwrappedLine.
534
- addUnwrappedLine ();
538
+ UnwrappedLine* StoredLine = Line.take ();
539
+ Line.reset (new UnwrappedLine (*StoredLine));
540
+ assert (LastInCurrentLine == NULL || LastInCurrentLine->Children .empty ());
541
+ FormatToken *StoredLastInCurrentLine = LastInCurrentLine;
542
+ bool PreviousInitialized = RootTokenInitialized;
543
+ RootTokenInitialized = false ;
544
+ LastInCurrentLine = NULL ;
545
+
535
546
parsePPDirective ();
547
+
548
+ assert (!RootTokenInitialized);
549
+ Line.reset (StoredLine);
550
+ RootTokenInitialized = PreviousInitialized;
551
+ LastInCurrentLine = StoredLastInCurrentLine;
552
+ assert (LastInCurrentLine == NULL || LastInCurrentLine->Children .empty ());
553
+ MustBreakBeforeNextToken = true ;
536
554
}
537
555
}
538
556
0 commit comments