diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index 97b4cffe8632a..97000a47c72f0 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -293,6 +293,9 @@ namespace swift { /// Returns the value for the given platform condition or an empty string. StringRef getPlatformConditionValue(PlatformConditionKind Kind) const; + /// Check whether the given platform condition matches the given value. + bool checkPlatformCondition(PlatformConditionKind Kind, StringRef Value) const; + /// Explicit conditional compilation flags, initialized via the '-D' /// compiler flag. void addCustomConditionalCompilationFlag(StringRef Name) { diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index 0e8ea3d1be67a..10dd1a1ea9e48 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -116,6 +116,21 @@ LangOptions::getPlatformConditionValue(PlatformConditionKind Kind) const { return StringRef(); } +bool LangOptions:: +checkPlatformCondition(PlatformConditionKind Kind, StringRef Value) const { + // Check a special case that "macOS" is an alias of "OSX". + if (Kind == PlatformConditionKind::OS && Value == "macOS") + return checkPlatformCondition(Kind, "OSX"); + + for (auto &Opt : reversed(PlatformConditionValues)) { + if (Opt.first == Kind) + if (Opt.second == Value) + return true; + } + + return false; +} + bool LangOptions::isCustomConditionalCompilationFlagSet(StringRef Name) const { return std::find(CustomConditionalCompilationFlags.begin(), CustomConditionalCompilationFlags.end(), Name) diff --git a/lib/Parse/ParseIfConfig.cpp b/lib/Parse/ParseIfConfig.cpp index f9943f66be264..49457e8495fa0 100644 --- a/lib/Parse/ParseIfConfig.cpp +++ b/lib/Parse/ParseIfConfig.cpp @@ -304,15 +304,6 @@ class ValidateIfConfigCondition : return nullptr; } - // FIXME: Perform the replacement macOS -> OSX elsewhere. - if (Kind == PlatformConditionKind::OS && *ArgStr == "macOS") { - *ArgStr = "OSX"; - ArgP->setSubExpr( - new (Ctx) UnresolvedDeclRefExpr(Ctx.getIdentifier(*ArgStr), - DeclRefKind::Ordinary, - DeclNameLoc(Arg->getLoc()))); - } - std::vector suggestions; if (!LangOptions::checkPlatformConditionSupported(*Kind, *ArgStr, suggestions)) { @@ -460,8 +451,7 @@ class EvaluateIfConfigCondition : auto Val = getDeclRefStr(Arg); auto Kind = getPlatformConditionKind(KindName).getValue(); - auto Target = Ctx.LangOpts.getPlatformConditionValue(Kind); - return Target == Val; + return Ctx.LangOpts.checkPlatformCondition(Kind, Val); } bool visitPrefixUnaryExpr(PrefixUnaryExpr *E) { diff --git a/test/IDE/coloring.swift b/test/IDE/coloring.swift index e5c9ff83f2e69..24e915f5e9d1c 100644 --- a/test/IDE/coloring.swift +++ b/test/IDE/coloring.swift @@ -608,6 +608,10 @@ func keywordInCaseAndLocalArgLabel(_ for: Int, for in: Int, class _: Int) { } } +#if os(macOS) +#endif +// CHECK: <#kw>#if <#id>os(<#id>macOS) + // Keep this as the last test /** Trailing off ...