forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseIfConfig.cpp
679 lines (586 loc) · 23.6 KB
/
ParseIfConfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//===--- ParseIfConfig.cpp - Swift Language Parser for #if directives -----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Conditional Compilation Block Parsing and AST Building
//
//===----------------------------------------------------------------------===//
#include "swift/Parse/Parser.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/Version.h"
#include "swift/Parse/Lexer.h"
#include "swift/Parse/SyntaxParsingContext.h"
#include "swift/Syntax/SyntaxFactory.h"
#include "swift/Syntax/TokenSyntax.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace swift;
using namespace swift::syntax;
namespace {
/// Get PlatformConditionKind from platform condition name.
static
Optional<PlatformConditionKind> getPlatformConditionKind(StringRef Name) {
return llvm::StringSwitch<Optional<PlatformConditionKind>>(Name)
.Case("os", PlatformConditionKind::OS)
.Case("arch", PlatformConditionKind::Arch)
.Case("_endian", PlatformConditionKind::Endianness)
.Case("_runtime", PlatformConditionKind::Runtime)
.Case("canImport", PlatformConditionKind::CanImport)
.Case("targetEnvironment", PlatformConditionKind::TargetEnvironment)
.Default(None);
}
/// Extract source text of the expression.
static StringRef extractExprSource(SourceManager &SM, Expr *E) {
CharSourceRange Range =
Lexer::getCharSourceRangeFromSourceRange(SM, E->getSourceRange());
return SM.extractText(Range);
}
static bool isValidPrefixUnaryOperator(Optional<StringRef> UnaryOperator) {
return UnaryOperator != None &&
(UnaryOperator.getValue() == ">=" || UnaryOperator.getValue() == "<");
}
static bool isValidVersion(const version::Version &Version,
const version::Version &ExpectedVersion,
StringRef UnaryOperator) {
if (UnaryOperator == ">=")
return Version >= ExpectedVersion;
if (UnaryOperator == "<")
return Version < ExpectedVersion;
llvm_unreachable("unsupported unary operator");
}
/// The condition validator.
class ValidateIfConfigCondition :
public ExprVisitor<ValidateIfConfigCondition, Expr*> {
ASTContext &Ctx;
DiagnosticEngine &D;
bool HasError;
/// Get the identifier string of the UnresolvedDeclRefExpr.
Optional<StringRef> getDeclRefStr(Expr *E, DeclRefKind Kind) {
auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(E);
if (!UDRE ||
!UDRE->hasName() ||
UDRE->getRefKind() != Kind ||
UDRE->getName().isCompoundName())
return None;
return UDRE->getName().getBaseIdentifier().str();
}
Expr *diagnoseUnsupportedExpr(Expr *E) {
D.diagnose(E->getLoc(),
diag::unsupported_conditional_compilation_expression_type);
return nullptr;
}
// Support '||' and '&&' operator. The procedence of '&&' is higher than '||'.
// Invalid operator and the next operand are diagnosed and removed from AST.
Expr *foldSequence(Expr *LHS, ArrayRef<Expr*> &S, bool isRecurse = false) {
assert(!S.empty() && ((S.size() & 1) == 0));
auto getNextOperator = [&]() -> Optional<StringRef> {
assert((S.size() & 1) == 0);
while (!S.empty()) {
auto Name = getDeclRefStr(S[0], DeclRefKind::BinaryOperator);
if (Name.hasValue() && (*Name == "||" || *Name == "&&"))
return Name;
auto DiagID = isa<UnresolvedDeclRefExpr>(S[0])
? diag::unsupported_conditional_compilation_binary_expression
: diag::unsupported_conditional_compilation_expression_type;
D.diagnose(S[0]->getLoc(), DiagID);
HasError |= true;
// Consume invalid operator and the immediate RHS.
S = S.slice(2);
}
return None;
};
// Extract out the first operator name.
auto OpName = getNextOperator();
if (!OpName.hasValue())
// If failed, it's not a sequence anymore.
return LHS;
Expr *Op = S[0];
// We will definitely be consuming at least one operator.
// Pull out the prospective RHS and slice off the first two elements.
Expr *RHS = validate(S[1]);
S = S.slice(2);
while (true) {
// Pull out the next binary operator.
auto NextOpName = getNextOperator();
bool IsEnd = !NextOpName.hasValue();
if (!IsEnd && *OpName == "||" && *NextOpName == "&&") {
RHS = foldSequence(RHS, S, /*isRecurse*/true);
continue;
}
// Apply the operator with left-associativity by folding the first two
// operands.
TupleExpr *Arg = TupleExpr::create(Ctx, SourceLoc(), { LHS, RHS },
{ }, { }, SourceLoc(),
/*HasTrailingClosure=*/false,
/*Implicit=*/true);
LHS = new (Ctx) BinaryExpr(Op, Arg, /*implicit*/false);
// If we don't have the next operator, we're done.
if (IsEnd)
break;
if (isRecurse && *OpName == "&&" && *NextOpName == "||")
break;
OpName = NextOpName;
Op = S[0];
RHS = validate(S[1]);
S = S.slice(2);
}
return LHS;
}
public:
ValidateIfConfigCondition(ASTContext &Ctx, DiagnosticEngine &D)
: Ctx(Ctx), D(D), HasError(false) {}
// Explicit configuration flag.
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E) {
if (!getDeclRefStr(E, DeclRefKind::Ordinary).hasValue())
return diagnoseUnsupportedExpr(E);
return E;
}
// 'true' or 'false' constant.
Expr *visitBooleanLiteralExpr(BooleanLiteralExpr *E) {
return E;
}
// '0' and '1' are warned, but we accept it.
Expr *visitIntegerLiteralExpr(IntegerLiteralExpr *E) {
if (E->isNegative() ||
(E->getDigitsText() != "0" && E->getDigitsText() != "1")) {
return diagnoseUnsupportedExpr(E);
}
// "#if 0" isn't valid, but it is common, so recognize it and handle it
// with a fixit.
StringRef replacement = E->getDigitsText() == "0" ? "false" :"true";
D.diagnose(E->getLoc(), diag::unsupported_conditional_compilation_integer,
E->getDigitsText(), replacement)
.fixItReplace(E->getLoc(), replacement);
return E;
}
// Platform conditions.
Expr *visitCallExpr(CallExpr *E) {
auto KindName = getDeclRefStr(E->getFn(), DeclRefKind::Ordinary);
if (!KindName.hasValue()) {
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
return nullptr;
}
auto *ArgP = dyn_cast<ParenExpr>(E->getArg());
if (!ArgP) {
D.diagnose(E->getLoc(), diag::platform_condition_expected_one_argument);
return nullptr;
}
Expr *Arg = ArgP->getSubExpr();
// '_compiler_version' '(' string-literal ')'
if (*KindName == "_compiler_version") {
auto SLE = dyn_cast<StringLiteralExpr>(Arg);
if (!SLE) {
D.diagnose(Arg->getLoc(),
diag::unsupported_platform_condition_argument,
"string literal");
return nullptr;
}
auto ValStr = SLE->getValue();
if (ValStr.empty()) {
D.diagnose(SLE->getLoc(), diag::empty_version_string);
return nullptr;
}
auto Val = version::Version::parseCompilerVersionString(
SLE->getValue(), SLE->getLoc(), &D);
if (!Val.hasValue())
return nullptr;
return E;
}
// 'swift' '(' ('>=' | '<') float-literal ( '.' integer-literal )* ')'
// 'compiler' '(' ('>=' | '<') float-literal ( '.' integer-literal )* ')'
if (*KindName == "swift" || *KindName == "compiler") {
auto PUE = dyn_cast<PrefixUnaryExpr>(Arg);
Optional<StringRef> PrefixName =
PUE ? getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
if (!isValidPrefixUnaryOperator(PrefixName)) {
D.diagnose(
Arg->getLoc(), diag::unsupported_platform_condition_argument,
"a unary comparison '>=' or '<'; for example, '>=2.2' or '<2.2'");
return nullptr;
}
auto versionString = extractExprSource(Ctx.SourceMgr, PUE->getArg());
auto Val = version::Version::parseVersionString(
versionString, PUE->getArg()->getStartLoc(), &D);
if (!Val.hasValue())
return nullptr;
return E;
}
// ( 'os' | 'arch' | '_endian' | '_runtime' | 'canImport') '(' identifier ')''
auto Kind = getPlatformConditionKind(*KindName);
if (!Kind.hasValue()) {
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
return nullptr;
}
auto ArgStr = getDeclRefStr(Arg, DeclRefKind::Ordinary);
if (!ArgStr.hasValue()) {
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_argument,
"identifier");
return nullptr;
}
std::vector<StringRef> suggestions;
if (!LangOptions::checkPlatformConditionSupported(*Kind, *ArgStr,
suggestions)) {
if (Kind == PlatformConditionKind::Runtime) {
// Error for _runtime()
D.diagnose(Arg->getLoc(),
diag::unsupported_platform_runtime_condition_argument);
return nullptr;
}
// Just a warning for other unsupported arguments.
StringRef DiagName;
switch (*Kind) {
case PlatformConditionKind::OS:
DiagName = "operating system"; break;
case PlatformConditionKind::Arch:
DiagName = "architecture"; break;
case PlatformConditionKind::Endianness:
DiagName = "endianness"; break;
case PlatformConditionKind::CanImport:
DiagName = "import conditional"; break;
case PlatformConditionKind::TargetEnvironment:
DiagName = "target environment"; break;
case PlatformConditionKind::Runtime:
llvm_unreachable("handled above");
}
auto Loc = Arg->getLoc();
D.diagnose(Loc, diag::unknown_platform_condition_argument,
DiagName, *KindName);
for (auto suggestion : suggestions)
D.diagnose(Loc, diag::note_typo_candidate, suggestion)
.fixItReplace(Arg->getSourceRange(), suggestion);
}
return E;
}
// Grouped condition. e.g. '(FLAG)'
Expr *visitParenExpr(ParenExpr *E) {
E->setSubExpr(validate(E->getSubExpr()));
return E;
}
// Prefix '!'. Other prefix operators are rejected.
Expr *visitPrefixUnaryExpr(PrefixUnaryExpr *E) {
auto OpName = getDeclRefStr(E->getFn(), DeclRefKind::PrefixOperator);
if (!OpName.hasValue() || *OpName != "!") {
D.diagnose(E->getLoc(),
diag::unsupported_conditional_compilation_unary_expression);
return nullptr;
}
E->setArg(validate(E->getArg()));
return E;
}
// Fold sequence expression for non-Swift3 mode.
Expr *visitSequenceExpr(SequenceExpr *E) {
ArrayRef<Expr*> Elts = E->getElements();
Expr *foldedExpr = validate(Elts[0]);
Elts = Elts.slice(1);
foldedExpr = foldSequence(foldedExpr, Elts);
assert(Elts.empty());
return foldedExpr;
}
// Other expression types are unsupported.
Expr *visitExpr(Expr *E) {
return diagnoseUnsupportedExpr(E);
}
Expr *validate(Expr *E) {
if (auto E2 = visit(E))
return E2;
HasError |= true;
return E;
}
bool hasError() const {
return HasError;
}
};
/// Validate and modify the condition expression.
/// Returns \c true if the condition contains any error.
static bool validateIfConfigCondition(Expr *&condition,
ASTContext &Context,
DiagnosticEngine &D) {
ValidateIfConfigCondition Validator(Context, D);
condition = Validator.validate(condition);
return Validator.hasError();
}
/// The condition evaluator.
/// The condition must be validated with validateIfConfigCondition().
class EvaluateIfConfigCondition :
public ExprVisitor<EvaluateIfConfigCondition, bool> {
ASTContext &Ctx;
/// Get the identifier string from an \c Expr assuming it's an
/// \c UnresolvedDeclRefExpr.
StringRef getDeclRefStr(Expr *E) {
return cast<UnresolvedDeclRefExpr>(E)->getName().getBaseIdentifier().str();
}
public:
EvaluateIfConfigCondition(ASTContext &Ctx) : Ctx(Ctx) {}
bool visitBooleanLiteralExpr(BooleanLiteralExpr *E) {
return E->getValue();
}
bool visitIntegerLiteralExpr(IntegerLiteralExpr *E) {
return E->getDigitsText() != "0";
}
bool visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E) {
auto Name = getDeclRefStr(E);
return Ctx.LangOpts.isCustomConditionalCompilationFlagSet(Name);
}
bool visitCallExpr(CallExpr *E) {
auto KindName = getDeclRefStr(E->getFn());
auto *Arg = cast<ParenExpr>(E->getArg())->getSubExpr();
if (KindName == "_compiler_version") {
auto Str = cast<StringLiteralExpr>(Arg)->getValue();
auto Val = version::Version::parseCompilerVersionString(
Str, SourceLoc(), nullptr).getValue();
auto thisVersion = version::Version::getCurrentCompilerVersion();
return thisVersion >= Val;
} else if ((KindName == "swift") || (KindName == "compiler")) {
auto PUE = cast<PrefixUnaryExpr>(Arg);
auto PrefixName = getDeclRefStr(PUE->getFn());
auto Str = extractExprSource(Ctx.SourceMgr, PUE->getArg());
auto Val = version::Version::parseVersionString(
Str, SourceLoc(), nullptr).getValue();
if (KindName == "swift") {
return isValidVersion(Ctx.LangOpts.EffectiveLanguageVersion, Val,
PrefixName);
} else if (KindName == "compiler") {
auto currentLanguageVersion =
version::Version::getCurrentLanguageVersion();
return isValidVersion(currentLanguageVersion, Val, PrefixName);
} else {
llvm_unreachable("unsupported version conditional");
}
} else if (KindName == "canImport") {
auto Str = extractExprSource(Ctx.SourceMgr, Arg);
return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() });
}
auto Val = getDeclRefStr(Arg);
auto Kind = getPlatformConditionKind(KindName).getValue();
return Ctx.LangOpts.checkPlatformCondition(Kind, Val);
}
bool visitPrefixUnaryExpr(PrefixUnaryExpr *E) {
return !visit(E->getArg());
}
bool visitParenExpr(ParenExpr *E) {
return visit(E->getSubExpr());
}
bool visitBinaryExpr(BinaryExpr *E) {
auto OpName = getDeclRefStr(E->getFn());
auto Args = E->getArg()->getElements();
if (OpName == "||") return visit(Args[0]) || visit(Args[1]);
if (OpName == "&&") return visit(Args[0]) && visit(Args[1]);
llvm_unreachable("unsupported binary operator");
}
bool visitExpr(Expr *E) { llvm_unreachable("Unvalidated condition?"); }
};
/// Evaluate the condition.
/// \c true if success, \c false if failed.
static bool evaluateIfConfigCondition(Expr *Condition, ASTContext &Context) {
return EvaluateIfConfigCondition(Context).visit(Condition);
}
/// Version condition checker.
class IsVersionIfConfigCondition :
public ExprVisitor<IsVersionIfConfigCondition, bool> {
/// Get the identifier string from an \c Expr assuming it's an
/// \c UnresolvedDeclRefExpr.
StringRef getDeclRefStr(Expr *E) {
return cast<UnresolvedDeclRefExpr>(E)->getName().getBaseIdentifier().str();
}
public:
IsVersionIfConfigCondition() {}
bool visitBinaryExpr(BinaryExpr *E) {
auto OpName = getDeclRefStr(E->getFn());
auto Args = E->getArg()->getElements();
if (OpName == "||") return visit(Args[0]) && visit(Args[1]);
if (OpName == "&&") return visit(Args[0]) || visit(Args[1]);
llvm_unreachable("unsupported binary operator");
}
bool visitCallExpr(CallExpr *E) {
auto KindName = getDeclRefStr(E->getFn());
return KindName == "_compiler_version" || KindName == "swift" ||
KindName == "compiler";
}
bool visitPrefixUnaryExpr(PrefixUnaryExpr *E) { return visit(E->getArg()); }
bool visitParenExpr(ParenExpr *E) { return visit(E->getSubExpr()); }
bool visitExpr(Expr *E) { return false; }
};
/// Returns \c true if the condition is a version check.
static bool isVersionIfConfigCondition(Expr *Condition) {
return IsVersionIfConfigCondition().visit(Condition);
}
/// Get the identifier string from an \c Expr if it's an
/// \c UnresolvedDeclRefExpr, otherwise the empty string.
static StringRef getDeclRefStr(Expr *E) {
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E)) {
return UDRE->getName().getBaseIdentifier().str();
}
return "";
}
static bool isPlatformConditionDisjunction(Expr *E, PlatformConditionKind Kind,
ArrayRef<StringRef> Vals) {
if (auto *Or = dyn_cast<BinaryExpr>(E)) {
if (getDeclRefStr(Or->getFn()) == "||") {
auto Args = Or->getArg()->getElements();
return (isPlatformConditionDisjunction(Args[0], Kind, Vals) &&
isPlatformConditionDisjunction(Args[1], Kind, Vals));
}
} else if (auto *P = dyn_cast<ParenExpr>(E)) {
return isPlatformConditionDisjunction(P->getSubExpr(), Kind, Vals);
} else if (auto *C = dyn_cast<CallExpr>(E)) {
if (getPlatformConditionKind(getDeclRefStr(C->getFn())) != Kind)
return false;
if (auto *ArgP = dyn_cast<ParenExpr>(C->getArg())) {
if (auto *Arg = ArgP->getSubExpr()) {
auto ArgStr = getDeclRefStr(Arg);
for (auto V : Vals) {
if (ArgStr == V)
return true;
}
}
}
}
return false;
}
// Search for the first occurrence of a _likely_ (but not definite) implicit
// simulator-environment platform condition, or negation thereof. This is
// defined as any logical conjunction of one or more os() platform conditions
// _strictly_ from the set {iOS, tvOS, watchOS} and one or more arch() platform
// conditions _strictly_ from the set {i386, x86_64}.
//
// These are (at the time of writing) defined as de-facto simulators in
// Platform.cpp, and if a user is testing them they're _likely_ looking for
// simulator-ness indirectly. If there is anything else in the condition aside
// from these conditions (or the negation of such a conjunction), we
// conservatively assume the user is testing something other than
// simulator-ness.
static Expr *findAnyLikelySimulatorEnvironmentTest(Expr *Condition) {
if (!Condition)
return nullptr;
if (auto *N = dyn_cast<PrefixUnaryExpr>(Condition)) {
return findAnyLikelySimulatorEnvironmentTest(N->getArg());
} else if (auto *P = dyn_cast<ParenExpr>(Condition)) {
return findAnyLikelySimulatorEnvironmentTest(P->getSubExpr());
}
// We assume the user is writing the condition in CNF -- say (os(iOS) ||
// os(tvOS)) && (arch(i386) || arch(x86_64)) -- rather than DNF, as the former
// is exponentially more terse, and these conditions are already quite
// unwieldy. If field evidence shows people using other variants, possibly add
// them here.
auto isSimulatorPlatformOSTest = [](Expr *E) -> bool {
return isPlatformConditionDisjunction(
E, PlatformConditionKind::OS, {"iOS", "tvOS", "watchOS"});
};
auto isSimulatorPlatformArchTest = [](Expr *E) -> bool {
return isPlatformConditionDisjunction(
E, PlatformConditionKind::Arch, {"i386", "x86_64"});
};
if (auto *And = dyn_cast<BinaryExpr>(Condition)) {
if (getDeclRefStr(And->getFn()) == "&&") {
auto Args = And->getArg()->getElements();
if ((isSimulatorPlatformOSTest(Args[0]) &&
isSimulatorPlatformArchTest(Args[1])) ||
(isSimulatorPlatformOSTest(Args[1]) &&
isSimulatorPlatformArchTest(Args[0]))) {
return And;
}
}
}
return nullptr;
}
} // end anonymous namespace
/// Parse and populate a #if ... #endif directive.
/// Delegate callback function to parse elements in the blocks.
ParserResult<IfConfigDecl> Parser::parseIfConfig(
llvm::function_ref<void(SmallVectorImpl<ASTNode> &, bool)> parseElements) {
SyntaxParsingContext IfConfigCtx(SyntaxContext, SyntaxKind::IfConfigDecl);
SmallVector<IfConfigClause, 4> Clauses;
Parser::StructureMarkerRAII ParsingDecl(
*this, Tok.getLoc(), Parser::StructureMarkerKind::IfConfig);
bool shouldEvaluate =
// Don't evaluate if it's in '-parse' mode, etc.
State->PerformConditionEvaluation &&
// If it's in inactive #if ... #endif block, there's no point to do it.
!getScopeInfo().isInactiveConfigBlock();
bool foundActive = false;
bool isVersionCondition = false;
while (1) {
SyntaxParsingContext ClauseContext(SyntaxContext,
SyntaxKind::IfConfigClause);
bool isElse = Tok.is(tok::pound_else);
SourceLoc ClauseLoc = consumeToken();
Expr *Condition = nullptr;
bool isActive = false;
if (!Tok.isAtStartOfLine() && isElse && Tok.is(tok::kw_if)) {
diagnose(Tok, diag::unexpected_if_following_else_compilation_directive)
.fixItReplace(SourceRange(ClauseLoc, consumeToken()), "#elseif");
isElse = false;
}
// Parse the condition. Evaluate it to determine the active
// clause unless we're doing a parse-only pass.
if (isElse) {
isActive = !foundActive && shouldEvaluate;
} else {
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
/*isBasic*/true,
/*isForDirective*/true);
if (Result.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (Result.isNull())
return makeParserError();
Condition = Result.get();
if (validateIfConfigCondition(Condition, Context, Diags)) {
// Error in the condition;
isActive = false;
isVersionCondition = false;
} else if (!foundActive && shouldEvaluate) {
// Evaluate the condition only if we haven't found any active one and
// we're not in parse-only mode.
isActive = evaluateIfConfigCondition(Condition, Context);
isVersionCondition = isVersionIfConfigCondition(Condition);
}
}
foundActive |= isActive;
if (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
diagnose(Tok.getLoc(),
diag::extra_tokens_conditional_compilation_directive);
}
if (Expr *Test = findAnyLikelySimulatorEnvironmentTest(Condition)) {
diagnose(Test->getLoc(),
diag::likely_simulator_platform_condition)
.fixItReplace(Test->getSourceRange(),
"targetEnvironment(simulator)");
}
// Parse elements
SmallVector<ASTNode, 16> Elements;
if (isActive || !isVersionCondition) {
parseElements(Elements, isActive);
} else {
// The parser will keep running and we just discard the AST part.
DiagnosticSuppression suppression(Context.Diags);
SmallVector<ASTNode, 16> dropedElements;
parseElements(dropedElements, false);
}
Clauses.emplace_back(ClauseLoc, Condition,
Context.AllocateCopy(Elements), isActive);
if (Tok.isNot(tok::pound_elseif, tok::pound_else))
break;
if (isElse)
diagnose(Tok, diag::expected_close_after_else_directive);
}
SyntaxContext->collectNodesInPlace(SyntaxKind::IfConfigClauseList);
SourceLoc EndLoc;
bool HadMissingEnd = parseEndIfDirective(EndLoc);
auto *ICD = new (Context) IfConfigDecl(CurDeclContext,
Context.AllocateCopy(Clauses),
EndLoc, HadMissingEnd);
return makeParserResult(ICD);
}