@@ -549,129 +549,74 @@ static bool isVersionIfConfigCondition(Expr *Condition) {
549
549
550
550
} // end anonymous namespace
551
551
552
- // / Parse and populate a list of #if/#elseif/#else/# endif clauses .
552
+ // / Parse and populate a #if ... # endif directive .
553
553
// / Delegate callback function to parse elements in the blocks.
554
- template <unsigned N>
555
- static ParserStatus parseIfConfig (
556
- Parser &P, SmallVectorImpl<IfConfigClause> &Clauses,
557
- SourceLoc &EndLoc, bool HadMissingEnd,
554
+ ParserResult<IfConfigDecl> Parser::parseIfConfig (
558
555
llvm::function_ref<void (SmallVectorImpl<ASTNode> &, bool )> parseElements) {
556
+
557
+ SmallVector<IfConfigClause, 4 > Clauses;
559
558
Parser::StructureMarkerRAII ParsingDecl (
560
- P, P. Tok .getLoc (), Parser::StructureMarkerKind::IfConfig);
559
+ * this , Tok.getLoc (), Parser::StructureMarkerKind::IfConfig);
561
560
562
561
bool foundActive = false ;
563
562
bool isVersionCondition = false ;
564
563
while (1 ) {
565
- bool isElse = P. Tok .is (tok::pound_else);
566
- SourceLoc ClauseLoc = P. consumeToken ();
564
+ bool isElse = Tok.is (tok::pound_else);
565
+ SourceLoc ClauseLoc = consumeToken ();
567
566
Expr *Condition = nullptr ;
568
567
bool isActive = false ;
569
568
570
569
// Parse and evaluate the directive.
571
570
if (isElse) {
572
571
isActive = !foundActive;
573
572
} else {
574
- llvm::SaveAndRestore<bool > S (P. InPoundIfEnvironment , true );
575
- ParserResult<Expr> Result = P. parseExprSequence (diag::expected_expr,
573
+ llvm::SaveAndRestore<bool > S (InPoundIfEnvironment, true );
574
+ ParserResult<Expr> Result = parseExprSequence (diag::expected_expr,
576
575
/* isBasic*/ true ,
577
576
/* isForDirective*/ true );
578
577
if (Result.isNull ())
579
578
return makeParserError ();
580
579
Condition = Result.get ();
581
- if (validateIfConfigCondition (Condition, P. Context , P. Diags )) {
580
+ if (validateIfConfigCondition (Condition, Context, Diags)) {
582
581
// Error in the condition;
583
582
isActive = false ;
584
583
isVersionCondition = false ;
585
584
} else if (!foundActive) {
586
585
// Evaludate the condition only if we haven't found any active one.
587
- isActive = evaluateIfConfigCondition (Condition, P. Context );
586
+ isActive = evaluateIfConfigCondition (Condition, Context);
588
587
isVersionCondition = isVersionIfConfigCondition (Condition);
589
588
}
590
589
}
591
590
592
591
foundActive |= isActive;
593
592
594
- if (!P. Tok .isAtStartOfLine () && P. Tok .isNot (tok::eof)) {
595
- P. diagnose (P. Tok .getLoc (),
596
- diag::extra_tokens_conditional_compilation_directive);
593
+ if (!Tok.isAtStartOfLine () && Tok.isNot (tok::eof)) {
594
+ diagnose (Tok.getLoc (),
595
+ diag::extra_tokens_conditional_compilation_directive);
597
596
}
598
597
599
598
// Parse elements
600
- SmallVector<ASTNode, N > Elements;
599
+ SmallVector<ASTNode, 16 > Elements;
601
600
if (isActive || !isVersionCondition) {
602
601
parseElements (Elements, isActive);
603
602
} else {
604
- DiagnosticTransaction DT (P. Diags );
605
- P. skipUntilConditionalBlockClose ();
603
+ DiagnosticTransaction DT (Diags);
604
+ skipUntilConditionalBlockClose ();
606
605
DT.abort ();
607
606
}
608
607
609
608
Clauses.emplace_back (ClauseLoc, Condition,
610
- P. Context .AllocateCopy (Elements), isActive);
609
+ Context.AllocateCopy (Elements), isActive);
611
610
612
- if (P. Tok .isNot (tok::pound_elseif, tok::pound_else))
611
+ if (Tok.isNot (tok::pound_elseif, tok::pound_else))
613
612
break ;
614
613
615
614
if (isElse)
616
- P. diagnose (P. Tok , diag::expected_close_after_else_directive);
615
+ diagnose (Tok, diag::expected_close_after_else_directive);
617
616
}
618
617
619
- HadMissingEnd = P.parseEndIfDirective (EndLoc);
620
- return makeParserSuccess ();
621
- }
622
-
623
- // / Parse #if ... #endif in declarations position.
624
- ParserResult<IfConfigDecl> Parser::parseDeclIfConfig (ParseDeclOptions Flags) {
625
- SmallVector<IfConfigClause, 4 > Clauses;
626
- SourceLoc EndLoc;
627
- bool HadMissingEnd = false ;
628
- auto Status = parseIfConfig<8 >(
629
- *this , Clauses, EndLoc, HadMissingEnd,
630
- [&](SmallVectorImpl<ASTNode> &Decls, bool IsActive) {
631
- Optional<Scope> scope;
632
- if (!IsActive)
633
- scope.emplace (this , getScopeInfo ().getCurrentScope ()->getKind (),
634
- /* inactiveConfigBlock=*/ true );
635
-
636
- ParserStatus Status;
637
- bool PreviousHadSemi = true ;
638
- while (Tok.isNot (tok::pound_else, tok::pound_endif, tok::pound_elseif,
639
- tok::eof)) {
640
- if (Tok.is (tok::r_brace)) {
641
- diagnose (Tok.getLoc (),
642
- diag::unexpected_rbrace_in_conditional_compilation_block);
643
- // If we see '}', following declarations don't look like belong to
644
- // the current decl context; skip them.
645
- skipUntilConditionalBlockClose ();
646
- break ;
647
- }
648
- Status |= parseDeclItem (PreviousHadSemi, Flags,
649
- [&](Decl *D) {Decls.emplace_back (D);});
650
- }
651
- });
652
- if (Status.isError ())
653
- return makeParserErrorResult<IfConfigDecl>();
654
-
655
- IfConfigDecl *ICD = new (Context) IfConfigDecl (CurDeclContext,
656
- Context.AllocateCopy (Clauses),
657
- EndLoc, HadMissingEnd);
658
- return makeParserResult (ICD);
659
- }
660
-
661
- // / Parse #if ... #endif in statements position.
662
- ParserResult<IfConfigDecl> Parser::parseStmtIfConfig (BraceItemListKind Kind) {
663
- SmallVector<IfConfigClause, 4 > Clauses;
664
618
SourceLoc EndLoc;
665
- bool HadMissingEnd = false ;
666
- auto Status = parseIfConfig<16 >(
667
- *this , Clauses, EndLoc, HadMissingEnd,
668
- [&](SmallVectorImpl<ASTNode> &Elements, bool IsActive) {
669
- parseBraceItems (Elements, Kind, IsActive
670
- ? BraceItemListKind::ActiveConditionalBlock
671
- : BraceItemListKind::InactiveConditionalBlock);
672
- });
673
- if (Status.isError ())
674
- return makeParserErrorResult<IfConfigDecl>();
619
+ bool HadMissingEnd = parseEndIfDirective (EndLoc);
675
620
676
621
auto *ICD = new (Context) IfConfigDecl (CurDeclContext,
677
622
Context.AllocateCopy (Clauses),
0 commit comments