26
26
27
27
using namespace swift ;
28
28
29
- static const StringRef SupportedConditionalCompilationOSs[] = {
29
+ struct SupportedConditionalValue {
30
+ StringRef value;
31
+
32
+ // / If the value has been deprecated, the new value to replace it with.
33
+ StringRef replacement = " " ;
34
+
35
+ SupportedConditionalValue (const char *value) : value(value) {}
36
+ SupportedConditionalValue (const char *value, const char *replacement)
37
+ : value(value), replacement(replacement) {}
38
+ };
39
+
40
+ static const SupportedConditionalValue SupportedConditionalCompilationOSs[] = {
30
41
" OSX" ,
31
42
" macOS" ,
32
43
" tvOS" ,
@@ -42,7 +53,7 @@ static const StringRef SupportedConditionalCompilationOSs[] = {
42
53
" WASI" ,
43
54
};
44
55
45
- static const StringRef SupportedConditionalCompilationArches[] = {
56
+ static const SupportedConditionalValue SupportedConditionalCompilationArches[] = {
46
57
" arm" ,
47
58
" arm64" ,
48
59
" i386" ,
@@ -53,18 +64,20 @@ static const StringRef SupportedConditionalCompilationArches[] = {
53
64
" wasm32" ,
54
65
};
55
66
56
- static const StringRef SupportedConditionalCompilationEndianness[] = {
67
+ static const SupportedConditionalValue SupportedConditionalCompilationEndianness[] = {
57
68
" little" ,
58
69
" big"
59
70
};
60
71
61
- static const StringRef SupportedConditionalCompilationRuntimes[] = {
72
+ static const SupportedConditionalValue SupportedConditionalCompilationRuntimes[] = {
62
73
" _ObjC" ,
63
74
" _Native" ,
64
75
};
65
76
66
- static const StringRef SupportedConditionalCompilationTargetEnvironments[] = {
77
+ static const SupportedConditionalValue SupportedConditionalCompilationTargetEnvironments[] = {
67
78
" simulator" ,
79
+ { " macabi" , " macCatalyst" },
80
+ " macCatalyst" , // A synonym for "macabi" when compiling for iOS
68
81
};
69
82
70
83
static const PlatformConditionKind AllPublicPlatformConditionKinds[] = {
@@ -73,7 +86,7 @@ static const PlatformConditionKind AllPublicPlatformConditionKinds[] = {
73
86
#include " swift/AST/PlatformConditionKinds.def"
74
87
};
75
88
76
- ArrayRef<StringRef > getSupportedConditionalCompilationValues (const PlatformConditionKind &Kind) {
89
+ ArrayRef<SupportedConditionalValue > getSupportedConditionalCompilationValues (const PlatformConditionKind &Kind) {
77
90
switch (Kind) {
78
91
case PlatformConditionKind::OS:
79
92
return SupportedConditionalCompilationOSs;
@@ -97,11 +110,11 @@ PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind,
97
110
for (const PlatformConditionKind& candidateKind : AllPublicPlatformConditionKinds) {
98
111
if (candidateKind != Kind) {
99
112
auto supportedValues = getSupportedConditionalCompilationValues (candidateKind);
100
- for (const StringRef & candidateValue : supportedValues) {
101
- if (candidateValue.lower () == lower) {
113
+ for (const SupportedConditionalValue & candidateValue : supportedValues) {
114
+ if (candidateValue.value . lower () == lower) {
102
115
suggestedValues.clear ();
103
- if (candidateValue != V) {
104
- suggestedValues.emplace_back (candidateValue);
116
+ if (candidateValue. value != V) {
117
+ suggestedValues.emplace_back (candidateValue. value );
105
118
}
106
119
return candidateKind;
107
120
}
@@ -118,19 +131,21 @@ bool isMatching(PlatformConditionKind Kind, const StringRef &V,
118
131
unsigned minDistance = std::numeric_limits<unsigned >::max ();
119
132
std::string lower = V.lower ();
120
133
auto supportedValues = getSupportedConditionalCompilationValues (Kind);
121
- for (const StringRef & candidate : supportedValues) {
122
- if (candidate == V) {
134
+ for (const SupportedConditionalValue & candidate : supportedValues) {
135
+ if (candidate. value == V) {
123
136
suggestedKind = Kind;
124
137
suggestions.clear ();
138
+ if (!candidate.replacement .empty ())
139
+ suggestions.push_back (candidate.replacement );
125
140
return true ;
126
141
}
127
- unsigned distance = StringRef (lower).edit_distance (candidate.lower ());
142
+ unsigned distance = StringRef (lower).edit_distance (candidate.value . lower ());
128
143
if (distance < minDistance) {
129
144
suggestions.clear ();
130
145
minDistance = distance;
131
146
}
132
147
if (distance == minDistance)
133
- suggestions.emplace_back (candidate);
148
+ suggestions.emplace_back (candidate. value );
134
149
}
135
150
suggestedKind = suggestedPlatformConditionKind (Kind, V, suggestions);
136
151
return false ;
@@ -171,6 +186,16 @@ checkPlatformCondition(PlatformConditionKind Kind, StringRef Value) const {
171
186
if (Kind == PlatformConditionKind::OS && Value == " macOS" )
172
187
return checkPlatformCondition (Kind, " OSX" );
173
188
189
+ // When compiling for iOS we consider "macCatalyst" to be a
190
+ // synonym of "macabi". This enables the use of
191
+ // #if targetEnvironment(macCatalyst) as a compilation
192
+ // condition for macCatalyst.
193
+
194
+ if (Kind == PlatformConditionKind::TargetEnvironment &&
195
+ Value == " macCatalyst" && Target.isiOS ()) {
196
+ return checkPlatformCondition (Kind, " macabi" );
197
+ }
198
+
174
199
for (auto &Opt : llvm::reverse (PlatformConditionValues)) {
175
200
if (Opt.first == Kind)
176
201
if (Opt.second == Value)
@@ -321,6 +346,10 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
321
346
addPlatformConditionValue (PlatformConditionKind::TargetEnvironment,
322
347
" simulator" );
323
348
349
+ if (tripleIsMacCatalystEnvironment (Target))
350
+ addPlatformConditionValue (PlatformConditionKind::TargetEnvironment,
351
+ " macabi" );
352
+
324
353
// If you add anything to this list, change the default size of
325
354
// PlatformConditionValues to not require an extra allocation
326
355
// in the common case.
0 commit comments