Skip to content

Commit 3807b3f

Browse files
committed
[Parse] Add platform conditional targetEnvironment(simulator)
1 parent b41c939 commit 3807b3f

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace swift {
4646
Runtime,
4747
/// Conditional import of module
4848
CanImport,
49+
/// Target Environment (currently just 'simulator' or absent)
50+
TargetEnvironment,
4951
};
5052

5153
/// Describes which Swift 3 Objective-C inference warnings should be
@@ -351,7 +353,7 @@ namespace swift {
351353
}
352354

353355
private:
354-
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 4>
356+
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 5>
355357
PlatformConditionValues;
356358
llvm::SmallVector<std::string, 2> CustomConditionalCompilationFlags;
357359
};

lib/Basic/LangOptions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "swift/Basic/LangOptions.h"
19+
#include "swift/Basic/Platform.h"
1920
#include "swift/Basic/Range.h"
2021
#include "swift/Config.h"
2122
#include "llvm/ADT/Hashing.h"
@@ -60,6 +61,10 @@ static const StringRef SupportedConditionalCompilationRuntimes[] = {
6061
"_Native",
6162
};
6263

64+
static const StringRef SupportedConditionalCompilationTargetEnvironments[] = {
65+
"simulator",
66+
};
67+
6368
template <size_t N>
6469
bool contains(const StringRef (&Array)[N], const StringRef &V,
6570
std::vector<StringRef> &suggestions) {
@@ -99,6 +104,9 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
99104
case PlatformConditionKind::Runtime:
100105
return contains(SupportedConditionalCompilationRuntimes, Value,
101106
suggestions);
107+
case PlatformConditionKind::TargetEnvironment:
108+
return contains(SupportedConditionalCompilationTargetEnvironments, Value,
109+
suggestions);
102110
case PlatformConditionKind::CanImport:
103111
// All importable names are valid.
104112
// FIXME: Perform some kind of validation of the string?
@@ -254,6 +262,13 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
254262
else
255263
addPlatformConditionValue(PlatformConditionKind::Runtime, "_Native");
256264

265+
// Set the "targetEnvironment" platform condition if targeting a simulator
266+
// environmet. Otherwise _no_ value is present for targetEnvironment; it's
267+
// an optional disambiguating refinement of the triple.
268+
if (swift::tripleIsAnySimulator(Target))
269+
addPlatformConditionValue(PlatformConditionKind::TargetEnvironment,
270+
"simulator");
271+
257272
// If you add anything to this list, change the default size of
258273
// PlatformConditionValues to not require an extra allocation
259274
// in the common case.

lib/Parse/ParseIfConfig.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Optional<PlatformConditionKind> getPlatformConditionKind(StringRef Name) {
3737
.Case("_endian", PlatformConditionKind::Endianness)
3838
.Case("_runtime", PlatformConditionKind::Runtime)
3939
.Case("canImport", PlatformConditionKind::CanImport)
40+
.Case("targetEnvironment", PlatformConditionKind::TargetEnvironment)
4041
.Default(None);
4142
}
4243

@@ -325,6 +326,8 @@ class ValidateIfConfigCondition :
325326
DiagName = "endianness"; break;
326327
case PlatformConditionKind::CanImport:
327328
DiagName = "import conditional"; break;
329+
case PlatformConditionKind::TargetEnvironment:
330+
DiagName = "target environment"; break;
328331
case PlatformConditionKind::Runtime:
329332
llvm_unreachable("handled above");
330333
}

test/Parse/ConditionalCompilation/identifierName.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ func f2(
66
FOO: Int,
77
swift: Int, _compiler_version: Int,
88
os: Int, arch: Int, _endian: Int, _runtime: Int,
9+
targetEnvironment: Int,
910
arm: Int, i386: Int, macOS: Int, OSX: Int, Linux: Int,
1011
big: Int, little: Int,
11-
_ObjC: Int, _Native: Int
12+
_ObjC: Int, _Native: Int,
13+
simulator: Int
1214
) {
1315

1416
#if FOO
@@ -23,6 +25,8 @@ func f2(
2325
_ = _runtime + _ObjC + _Native
2426
#elseif swift(>=1.0) && _compiler_version("3.*.0")
2527
_ = swift + _compiler_version
28+
#elseif targetEnvironment(simulator)
29+
_ = targetEnvironment + simulator
2630
#endif
2731

2832
}
@@ -31,9 +35,11 @@ func f2() {
3135
let
3236
FOO = 1, swift = 1, _compiler_version = 1,
3337
os = 1, arch = 1, _endian = 1, _runtime = 1,
38+
targetEnvironment = 1,
3439
arm = 1, i386 = 1, macOS = 1, OSX = 1, Linux = 1,
3540
big = 1, little = 1,
36-
_ObjC = 1, _Native = 1
41+
_ObjC = 1, _Native = 1,
42+
simulator = 1
3743

3844
#if FOO
3945
_ = FOO
@@ -47,6 +53,8 @@ func f2() {
4753
_ = _runtime + _ObjC + _Native
4854
#elseif swift(>=1.0) && _compiler_version("3.*.0")
4955
_ = swift + _compiler_version
56+
#elseif targetEnvironment(simulator)
57+
_ = targetEnvironment + simulator
5058
#endif
5159

5260
}
@@ -55,16 +63,19 @@ struct S {
5563
let
5664
FOO = 1, swift = 1, _compiler_version = 1,
5765
os = 1, arch = 1, _endian = 1, _runtime = 1,
66+
targetEnvironment = 1,
5867
arm = 1, i386 = 1, macOS = 1, OSX = 1, Linux = 1,
5968
big = 1, little = 1,
60-
_ObjC = 1, _Native = 1
69+
_ObjC = 1, _Native = 1,
70+
simulator = 1
6171

6272
#if FOO
6373
#elseif os(macOS) && os(OSX) && os(Linux)
6474
#elseif arch(i386) && arch(arm)
6575
#elseif _endian(big) && _endian(little)
6676
#elseif _runtime(_ObjC) && _runtime(_Native)
6777
#elseif swift(>=1.0) && _compiler_version("3.*.0")
78+
#elseif targetEnvironment(simulator)
6879
#endif
6980

7081
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %swift -typecheck %s -verify -target x86_64-apple-ios7.0 -parse-stdlib
2+
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-apple-ios7.0
3+
4+
#if !targetEnvironment(simulator)
5+
// This block should not parse.
6+
let i: Int = "Hello"
7+
#endif
8+
9+
#if targetEnvironment(simulator)
10+
class C {}
11+
var x = C()
12+
#endif
13+
var y = x

0 commit comments

Comments
 (0)