Skip to content

Commit bb3486d

Browse files
committed
Add new _endian() platform condition check; valid values are "little" and "big".
1 parent b4a2b53 commit bb3486d

20 files changed

+68
-17
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ namespace swift {
233233
/// a supported target architecture.
234234
static bool isPlatformConditionArchSupported(StringRef ArchName);
235235

236+
/// Returns true if the 'endian' platform condition argument represents
237+
/// a supported target endianness.
238+
static bool isPlatformConditionEndiannessSupported(StringRef endianness);
239+
236240
private:
237241
llvm::SmallVector<std::pair<std::string, std::string>, 3>
238242
PlatformConditionValues;

lib/Basic/LangOptions.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ static const StringRef SupportedConditionalCompilationArches[] = {
4343
"powerpc64le"
4444
};
4545

46+
static const StringRef SupportedConditionalCompilationEndianness[] = {
47+
"little",
48+
"big"
49+
};
50+
4651
bool LangOptions::isPlatformConditionOSSupported(StringRef OSName) {
4752
auto foundIt = std::find(std::begin(SupportedConditionalCompilationOSs),
4853
std::end(SupportedConditionalCompilationOSs),
@@ -58,6 +63,14 @@ LangOptions::isPlatformConditionArchSupported(StringRef ArchName) {
5863
return foundIt != std::end(SupportedConditionalCompilationArches);
5964
}
6065

66+
bool
67+
LangOptions::isPlatformConditionEndiannessSupported(StringRef Endianness) {
68+
auto foundIt = std::find(std::begin(SupportedConditionalCompilationEndianness),
69+
std::end(SupportedConditionalCompilationEndianness),
70+
Endianness);
71+
return foundIt != std::end(SupportedConditionalCompilationEndianness);
72+
}
73+
6174
StringRef
6275
LangOptions::getPlatformConditionValue(StringRef Name) const {
6376
// Last one wins.
@@ -148,6 +161,31 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
148161
if (UnsupportedOS || UnsupportedArch)
149162
return { UnsupportedOS, UnsupportedArch };
150163

164+
// Set the "_endian" platform condition.
165+
switch (Target.getArch()) {
166+
case llvm::Triple::ArchType::arm:
167+
case llvm::Triple::ArchType::thumb:
168+
addPlatformConditionValue("_endian", "little");
169+
break;
170+
case llvm::Triple::ArchType::aarch64:
171+
addPlatformConditionValue("_endian", "little");
172+
break;
173+
case llvm::Triple::ArchType::ppc64:
174+
addPlatformConditionValue("_endian", "big");
175+
break;
176+
case llvm::Triple::ArchType::ppc64le:
177+
addPlatformConditionValue("_endian", "little");
178+
break;
179+
case llvm::Triple::ArchType::x86:
180+
addPlatformConditionValue("_endian", "little");
181+
break;
182+
case llvm::Triple::ArchType::x86_64:
183+
addPlatformConditionValue("_endian", "little");
184+
break;
185+
default:
186+
llvm_unreachable("undefined architecture endianness");
187+
}
188+
151189
// Set the "runtime" platform condition.
152190
if (EnableObjCInterop)
153191
addPlatformConditionValue("_runtime", "_ObjC");

lib/Parse/ParseStmt.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ ParserResult<Stmt> Parser::parseStmtGuard() {
16081608
// "_compiler_version"), and whose argument is a named decl ref expression
16091609
ConditionalCompilationExprState
16101610
Parser::evaluateConditionalCompilationExpr(Expr *condition) {
1611-
// Evaluate a ParenExpr.
1611+
// Evaluate a ParenExpr.
16121612
if (auto *PE = dyn_cast<ParenExpr>(condition))
16131613
return evaluateConditionalCompilationExpr(PE->getSubExpr());
16141614

@@ -1709,6 +1709,7 @@ Parser::evaluateConditionalCompilationExpr(Expr *condition) {
17091709
}
17101710

17111711
if (!fnName.equals("arch") && !fnName.equals("os") &&
1712+
!fnName.equals("_endian") &&
17121713
!fnName.equals("_runtime") &&
17131714
!fnName.equals("swift") &&
17141715
!fnName.equals("_compiler_version")) {
@@ -1799,6 +1800,11 @@ Parser::evaluateConditionalCompilationExpr(Expr *condition) {
17991800
"architecture", fnName);
18001801
return ConditionalCompilationExprState::error();
18011802
}
1803+
} else if (fnName == "_endian") {
1804+
if (!LangOptions::isPlatformConditionEndiannessSupported(argument)) {
1805+
diagnose(UDRE->getLoc(), diag::unknown_platform_condition_argument,
1806+
"endianness", fnName);
1807+
}
18021808
}
18031809
auto target = Context.LangOpts.getPlatformConditionValue(fnName);
18041810
return {target == argument, ConditionalCompilationExprKind::DeclRef};

test/Parse/ConditionalCompilation/arm64AppleTVOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(tvOS) && _runtime(_ObjC)
10+
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/arm64IOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(iOS) && _runtime(_ObjC)
10+
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/armAndroidTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(Android) && _runtime(_Native)
10+
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/armIOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(iOS) && _runtime(_ObjC)
10+
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/armWatchOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(watchOS) && _runtime(_ObjC)
10+
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/basicParseErrors.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ struct S {
6161

6262
#if arch(leg) // expected-warning {{unknown architecture for build configuration 'arch'}}
6363
#endif
64+
65+
#if _endian(mid) // expected-warning {{unknown endianness for build configuration '_endian'}}
66+
#endif

test/Parse/ConditionalCompilation/i386AppleTVOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(tvOS) && _runtime(_ObjC)
10+
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/i386IOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(iOS) && _runtime(_ObjC)
10+
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/i386WatchOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(watchOS) && _runtime(_ObjC)
10+
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/powerpc64LinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -parse %s -verify -D FOO -D BAR -target powerpc64-unknown-linux-gnu -disable-objc-interop -D FOO -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target powerpc64-unknown-linux-gnu
33

4-
#if arch(powerpc64) && os(Linux) && _runtime(_Native)
4+
#if arch(powerpc64) && os(Linux) && _runtime(_Native) && _endian(big)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/powerpc64leLinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -parse %s -verify -D FOO -D BAR -target powerpc64le-unknown-linux-gnu -disable-objc-interop -D FOO -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target powerpc64le-unknown-linux-gnu
33

4-
#if arch(powerpc64le) && os(Linux) && _runtime(_Native)
4+
#if arch(powerpc64le) && os(Linux) && _runtime(_Native) && _endian(little)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64AppleTVOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(x86_64) && os(tvOS) && _runtime(_ObjC)
10+
#if arch(x86_64) && os(tvOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/x64FreeBSDTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-unknown-freebsd10 -disable-objc-interop -D FOO -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-freebsd10
33

4-
#if arch(x86_64) && os(FreeBSD) && _runtime(_Native)
4+
#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) && _endian(little)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64IOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(x86_64) && os(iOS) && _runtime(_ObjC)
10+
#if arch(x86_64) && os(iOS) && _runtime(_ObjC) && _endian(little)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/x64LinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-unknown-linux-gnu -disable-objc-interop -D FOO -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-linux-gnu
33

4-
#if arch(x86_64) && os(Linux) && _runtime(_Native)
4+
#if arch(x86_64) && os(Linux) && _runtime(_Native) && _endian(little)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64OSXTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-apple-macosx10.9 -D FOO -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-apple-macosx10.9
33

4-
#if arch(x86_64) && os(OSX) && _runtime(_ObjC)
4+
#if arch(x86_64) && os(OSX) && _runtime(_ObjC) && _endian(little)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64WindowsTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-unknown-windows-cygnus -disable-objc-interop -D FOO -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-windows-cygnus
33

4-
#if arch(x86_64) && os(Windows) && _runtime(_Native)
4+
#if arch(x86_64) && os(Windows) && _runtime(_Native) && _endian(little)
55
class C {}
66
var x = C()
77
#endif

0 commit comments

Comments
 (0)