Skip to content

Commit 81658f4

Browse files
authored
Merge pull request #15977 from hartbit/compiler-directive
New compiler directive to replace _compiler_version
2 parents 0747546 + 2abb931 commit 81658f4

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

lib/Parse/ParseIfConfig.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ class ValidateIfConfigCondition :
278278
}
279279

280280
// 'swift' '(' '>=' float-literal ( '.' integer-literal )* ')'
281-
if (*KindName == "swift") {
281+
// 'compiler' '(' '>=' float-literal ( '.' integer-literal )* ')'
282+
if (*KindName == "swift" || *KindName == "compiler") {
282283
auto PUE = dyn_cast<PrefixUnaryExpr>(Arg);
283284
llvm::Optional<StringRef> PrefixName = PUE ?
284285
getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
@@ -445,13 +446,18 @@ class EvaluateIfConfigCondition :
445446
Str, SourceLoc(), nullptr).getValue();
446447
auto thisVersion = version::Version::getCurrentCompilerVersion();
447448
return thisVersion >= Val;
448-
} else if (KindName == "swift") {
449+
} else if ((KindName == "swift") || (KindName == "compiler")) {
449450
auto PUE = cast<PrefixUnaryExpr>(Arg);
450451
auto Str = extractExprSource(Ctx.SourceMgr, PUE->getArg());
451452
auto Val = version::Version::parseVersionString(
452453
Str, SourceLoc(), nullptr).getValue();
453-
auto thisVersion = Ctx.LangOpts.EffectiveLanguageVersion;
454-
return thisVersion >= Val;
454+
if (KindName == "swift") {
455+
return Ctx.LangOpts.EffectiveLanguageVersion >= Val;
456+
} else if (KindName == "compiler") {
457+
return version::Version::getCurrentLanguageVersion() >= Val;
458+
} else {
459+
llvm_unreachable("unsupported version conditional");
460+
}
455461
} else if (KindName == "canImport") {
456462
auto Str = extractExprSource(Ctx.SourceMgr, Arg);
457463
return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() });
@@ -539,7 +545,8 @@ class IsVersionIfConfigCondition :
539545

540546
bool visitCallExpr(CallExpr *E) {
541547
auto KindName = getDeclRefStr(E->getFn());
542-
return KindName == "_compiler_version" || KindName == "swift";
548+
return KindName == "_compiler_version" || KindName == "swift" ||
549+
KindName == "compiler";
543550
}
544551

545552
bool visitPrefixUnaryExpr(PrefixUnaryExpr *E) { return visit(E->getArg()); }

test/Compatibility/conditional_compiliation_expr.swift renamed to test/Compatibility/conditional_compilation_expr.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,47 +69,95 @@ foo bar
6969
let _: Int = 1
7070
#endif
7171

72+
#if !compiler(>=4.1)
73+
// There should be no error here.
74+
foo bar
75+
#else
76+
let _: Int = 1
77+
#endif
78+
7279
#if (swift(>=2.2))
7380
let _: Int = 1
7481
#else
7582
// There should be no error here.
7683
foo bar
7784
#endif
7885

86+
#if (compiler(>=4.1))
87+
let _: Int = 1
88+
#else
89+
// There should be no error here.
90+
foo bar
91+
#endif
92+
7993
#if swift(>=99.0) || swift(>=88.1.1)
8094
// There should be no error here.
8195
foo bar baz // expected-error 2 {{consecutive statements}}
8296
#else
8397
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
8498
#endif
8599

100+
#if compiler(>=99.0) || compiler(>=88.1.1)
101+
// There should be no error here.
102+
foo bar baz // expected-error 2 {{consecutive statements}}
103+
#else
104+
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
105+
#endif
106+
86107
#if swift(>=99.0) || FOO
87108
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
88109
#else
89110
undefinedElse()
90111
#endif
91112

113+
#if compiler(>=99.0) || FOO
114+
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
115+
#else
116+
undefinedElse()
117+
#endif
118+
92119
#if swift(>=99.0) && FOO
93120
// There should be no error here.
94121
foo bar baz // expected-error 2 {{consecutive statements}}
95122
#else
96123
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
97124
#endif
98125

126+
#if compiler(>=99.0) && FOO
127+
// There should be no error here.
128+
foo bar baz // expected-error 2 {{consecutive statements}}
129+
#else
130+
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
131+
#endif
132+
99133
#if FOO && swift(>=2.2)
100134
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
101135
#else
102136
// There should be no error here.
103137
foo bar baz // expected-error 2 {{consecutive statements}}
104138
#endif
105139

140+
#if FOO && compiler(>=4.0)
141+
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
142+
#else
143+
// There should be no error here.
144+
foo bar baz // expected-error 2 {{consecutive statements}}
145+
#endif
146+
106147
#if swift(>=2.2) && swift(>=1)
107148
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
108149
#else
109150
// There should be no error here.
110151
foo bar baz // expected-error 2 {{consecutive statements}}
111152
#endif
112153

154+
#if compiler(>=4.1) && compiler(>=4)
155+
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
156+
#else
157+
// There should be no error here.
158+
foo bar baz // expected-error 2 {{consecutive statements}}
159+
#endif
160+
113161
// ---------------------------------------------------------------------------
114162
// SR-4031: Compound name in compilation condition
115163
// See test/Parse/ConditionalCompilation/compoundName_swift4.swift for Swfit 4 behavior

0 commit comments

Comments
 (0)