Skip to content

New compiler directive to replace _compiler_version #15977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ class ValidateIfConfigCondition :
}

// 'swift' '(' '>=' float-literal ( '.' integer-literal )* ')'
if (*KindName == "swift") {
// 'compiler' '(' '>=' float-literal ( '.' integer-literal )* ')'
if (*KindName == "swift" || *KindName == "compiler") {
auto PUE = dyn_cast<PrefixUnaryExpr>(Arg);
llvm::Optional<StringRef> PrefixName = PUE ?
getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None;
Expand Down Expand Up @@ -445,13 +446,18 @@ class EvaluateIfConfigCondition :
Str, SourceLoc(), nullptr).getValue();
auto thisVersion = version::Version::getCurrentCompilerVersion();
return thisVersion >= Val;
} else if (KindName == "swift") {
} else if ((KindName == "swift") || (KindName == "compiler")) {
auto PUE = cast<PrefixUnaryExpr>(Arg);
auto Str = extractExprSource(Ctx.SourceMgr, PUE->getArg());
auto Val = version::Version::parseVersionString(
Str, SourceLoc(), nullptr).getValue();
auto thisVersion = Ctx.LangOpts.EffectiveLanguageVersion;
return thisVersion >= Val;
if (KindName == "swift") {
return Ctx.LangOpts.EffectiveLanguageVersion >= Val;
} else if (KindName == "compiler") {
return version::Version::getCurrentLanguageVersion() >= Val;
} else {
llvm_unreachable("unsupported version conditional");
}
} else if (KindName == "canImport") {
auto Str = extractExprSource(Ctx.SourceMgr, Arg);
return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() });
Expand Down Expand Up @@ -539,7 +545,8 @@ class IsVersionIfConfigCondition :

bool visitCallExpr(CallExpr *E) {
auto KindName = getDeclRefStr(E->getFn());
return KindName == "_compiler_version" || KindName == "swift";
return KindName == "_compiler_version" || KindName == "swift" ||
KindName == "compiler";
}

bool visitPrefixUnaryExpr(PrefixUnaryExpr *E) { return visit(E->getArg()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,47 +69,95 @@ foo bar
let _: Int = 1
#endif

#if !compiler(>=4.1)
// There should be no error here.
foo bar
#else
let _: Int = 1
#endif

#if (swift(>=2.2))
let _: Int = 1
#else
// There should be no error here.
foo bar
#endif

#if (compiler(>=4.1))
let _: Int = 1
#else
// There should be no error here.
foo bar
#endif

#if swift(>=99.0) || swift(>=88.1.1)
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#else
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
#endif

#if compiler(>=99.0) || compiler(>=88.1.1)
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#else
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
#endif

#if swift(>=99.0) || FOO
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
#else
undefinedElse()
#endif

#if compiler(>=99.0) || FOO
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
#else
undefinedElse()
#endif

#if swift(>=99.0) && FOO
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#else
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
#endif

#if compiler(>=99.0) && FOO
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#else
undefinedElse() // expected-error {{use of unresolved identifier 'undefinedElse'}}
#endif

#if FOO && swift(>=2.2)
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
#else
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#endif

#if FOO && compiler(>=4.0)
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
#else
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#endif

#if swift(>=2.2) && swift(>=1)
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
#else
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#endif

#if compiler(>=4.1) && compiler(>=4)
undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}}
#else
// There should be no error here.
foo bar baz // expected-error 2 {{consecutive statements}}
#endif

// ---------------------------------------------------------------------------
// SR-4031: Compound name in compilation condition
// See test/Parse/ConditionalCompilation/compoundName_swift4.swift for Swfit 4 behavior
Expand Down