Skip to content

basic: introduce the _environment compilation conditional #3337

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ namespace swift {
/// a supported target endianness.
static bool isPlatformConditionEndiannessSupported(StringRef endianness);

/// Returns true if the 'environment' platform condition argument represents
/// a supported target environment.
static bool isPlatformConditionEnvironmentSupported(StringRef Environment);

private:
llvm::SmallVector<std::pair<std::string, std::string>, 3>
PlatformConditionValues;
Expand Down
30 changes: 30 additions & 0 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ static const StringRef SupportedConditionalCompilationEndianness[] = {
"big"
};

static const StringRef SupportedConditionalCompilationEnvironment[] = {
"cygnus",
"gnu",
"msvc",
"itanium",
};

template <typename Type, size_t N>
bool contains(const Type (&Array)[N], const Type &V) {
return std::find(std::begin(Array), std::end(Array), V) != std::end(Array);
Expand All @@ -69,6 +76,11 @@ LangOptions::isPlatformConditionEndiannessSupported(StringRef Endianness) {
return contains(SupportedConditionalCompilationEndianness, Endianness);
}

bool
LangOptions::isPlatformConditionEnvironmentSupported(StringRef Environment) {
return contains(SupportedConditionalCompilationEnvironment, Environment);
}

StringRef
LangOptions::getPlatformConditionValue(StringRef Name) const {
// Last one wins.
Expand Down Expand Up @@ -191,6 +203,24 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
llvm_unreachable("undefined architecture endianness");
}

// Set the "_environment" platform condition.
switch (Target.getEnvironment()) {
default: break;
case llvm::Triple::MSVC:
addPlatformConditionValue("_environment", "msvc");
Copy link
Contributor

@gribozavr gribozavr Jul 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for these values to appropriate files in test/Parse/ConditionalCompilation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im confused. How is the test below now sufficient?

break;
case llvm::Triple::Itanium:
addPlatformConditionValue("_environment", "itanium");
break;
case llvm::Triple::Cygnus:
addPlatformConditionValue("_environment", "cygnus");
break;
case llvm::Triple::GNU:
if (triple.isOSWindows())
addPlatformConditionValue("_environment", "gnu");
break;
}

// Set the "runtime" platform condition.
if (EnableObjCInterop)
addPlatformConditionValue("_runtime", "_ObjC");
Expand Down
10 changes: 7 additions & 3 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1603,9 +1603,8 @@ Parser::evaluateConditionalCompilationExpr(Expr *condition) {
}

if (!fnName.equals("arch") && !fnName.equals("os") &&
!fnName.equals("_endian") &&
!fnName.equals("_runtime") &&
!fnName.equals("swift") &&
!fnName.equals("_endian") && !fnName.equals("_environment") &&
!fnName.equals("_runtime") && !fnName.equals("swift") &&
!fnName.equals("_compiler_version")) {
diagnose(CE->getLoc(), diag::unsupported_platform_condition_expression);
return ConditionalCompilationExprState::error();
Expand Down Expand Up @@ -1699,6 +1698,11 @@ Parser::evaluateConditionalCompilationExpr(Expr *condition) {
diagnose(UDRE->getLoc(), diag::unknown_platform_condition_argument,
"endianness", fnName);
}
} else if (fnName == "_environment") {
if (!LangOptions::isPlatformConditionEnvironmentSupported(argument)) {
diagnose(UDRE->getLoc(), diag::unknown_platform_condition_argument,
"environment", fnName);
}
}
auto target = Context.LangOpts.getPlatformConditionValue(fnName);
return {target == argument, ConditionalCompilationExprKind::DeclRef};
Expand Down
9 changes: 9 additions & 0 deletions test/Parse/ConditionalCompilation/cygwinTarget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %swift -parse %s -target x86_64-unknown-windows-cygnus -parse-as-library -parse-stdlib -verify

#if _environment(cygnus)
public class cygwin {
}
#endif

let instance = cygwin()

9 changes: 9 additions & 0 deletions test/Parse/ConditionalCompilation/mingwTarget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %swift -parse %s -target i686-unknown-windows-gnu -parse-as-library -parse-stdlib -verify

#if _environment(gnu)
public class MinGW {
}
#endif

let instance = MinGW()

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %swift -parse %s -target i686-unknown-windows-itanium -parse-as-library -parse-stdlib -verify

#if _environment(itanium)
public class itanium {
}
#endif

let instance = itanium()

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %swift -parse %s -target i686-unknown-windows-msvc -parse-as-library -parse-stdlib -verify

#if _environment(msvc)
public class msvc {
}
#endif

let instance = msvc()