diff --git a/lib/Parse/ParseIfConfig.cpp b/lib/Parse/ParseIfConfig.cpp index 9795897f1f6..43bcc5905ad 100644 --- a/lib/Parse/ParseIfConfig.cpp +++ b/lib/Parse/ParseIfConfig.cpp @@ -278,7 +278,8 @@ public: } // 'swift' '(' '>=' float-literal ( '.' integer-literal )* ')' - if (*KindName == "swift") { + // 'compiler' '(' '>=' float-literal ( '.' integer-literal )* ')' + if (*KindName == "swift" || *KindName == "compiler") { auto PUE = dyn_cast(Arg); llvm::Optional PrefixName = PUE ? getDeclRefStr(PUE->getFn(), DeclRefKind::PrefixOperator) : None; @@ -452,6 +453,13 @@ public: Str, SourceLoc(), nullptr).getValue(); auto thisVersion = Ctx.LangOpts.EffectiveLanguageVersion; return thisVersion >= Val; + } else if (KindName == "compiler") { + auto PUE = cast(Arg); + auto Str = extractExprSource(Ctx.SourceMgr, PUE->getArg()); + auto Val = version::Version::parseVersionString( + Str, SourceLoc(), nullptr).getValue(); + auto thisVersion = version::Version::getCurrentLanguageVersion(); + return thisVersion >= Val; } else if (KindName == "canImport") { auto Str = extractExprSource(Ctx.SourceMgr, Arg); return Ctx.canImportModule({ Ctx.getIdentifier(Str) , E->getLoc() }); @@ -539,7 +547,8 @@ public: 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()); } diff --git a/test/Compatibility/conditional_compiliation_expr.swift b/test/Compatibility/conditional_compilation_expr.swift similarity index 79% rename from test/Compatibility/conditional_compiliation_expr.swift rename to test/Compatibility/conditional_compilation_expr.swift index 1c6e987b5fe..a8d1c92448d 100644 --- a/test/Compatibility/conditional_compiliation_expr.swift +++ b/test/Compatibility/conditional_compilation_expr.swift @@ -69,6 +69,13 @@ 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 @@ -76,6 +83,13 @@ let _: Int = 1 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}} @@ -83,12 +97,25 @@ foo bar baz // expected-error 2 {{consecutive statements}} 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}} @@ -96,6 +123,13 @@ foo bar baz // expected-error 2 {{consecutive statements}} 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 @@ -103,6 +137,13 @@ undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}} 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 @@ -110,6 +151,13 @@ undefinedIf() // expected-error {{use of unresolved identifier 'undefinedIf'}} 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