Skip to content

Commit bdb6ad6

Browse files
committed
[clang][Darwin] Align all OS Versions for 26
* Translate the following versions to 26. * watchOS 12 -> 26 * visionOS 3 -> 26 * macos 16 -> 26 * iOS 19 -> 26 * tvOS 19 -> 26 * Emit diagnostics, but allow conversion when clients attempt to use invalid gaps in OS versioning in availability. * For target-triples, only allow "valid" versions for implicit conversions.
1 parent 59ca4d2 commit bdb6ad6

21 files changed

+647
-56
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,8 @@ static llvm::StringRef canonicalizePlatformName(llvm::StringRef Platform) {
11281128
return llvm::StringSwitch<llvm::StringRef>(Platform)
11291129
.Case("iOS", "ios")
11301130
.Case("macOS", "macos")
1131+
.Case("macOSX", "macos")
1132+
.Case("macosx", "macos")
11311133
.Case("tvOS", "tvos")
11321134
.Case("watchOS", "watchos")
11331135
.Case("iOSApplicationExtension", "ios_app_extension")
@@ -1192,6 +1194,26 @@ static llvm::Triple::EnvironmentType getEnvironmentType(llvm::StringRef Environm
11921194
.Case("library", llvm::Triple::Library)
11931195
.Default(llvm::Triple::UnknownEnvironment);
11941196
}
1197+
1198+
static llvm::Triple::OSType getOSType(llvm::StringRef Platform) {
1199+
using OSType = llvm::Triple::OSType;
1200+
return llvm::StringSwitch<OSType>(Platform)
1201+
.Case("ios", OSType::IOS)
1202+
.Case("macos", OSType::MacOSX)
1203+
.Case("maccatalyst", OSType::IOS)
1204+
.Case("tvos", OSType::TvOS)
1205+
.Case("watchos", OSType::WatchOS)
1206+
.Case("bridgeos", OSType::BridgeOS)
1207+
.Case("ios_app_extension", OSType::IOS)
1208+
.Case("maccatalyst_app_extension", OSType::IOS)
1209+
.Case("macos_app_extension", OSType::MacOSX)
1210+
.Case("tvos_app_extension", OSType::TvOS)
1211+
.Case("watchos_app_extension", OSType::WatchOS)
1212+
.Case("xros", OSType::XROS)
1213+
.Case("xros_app_extension", OSType::XROS)
1214+
.Default(OSType::UnknownOS);
1215+
}
1216+
11951217
}];
11961218
let HasCustomParsing = 1;
11971219
let InheritEvenIfAlreadyPresent = 1;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4100,6 +4100,9 @@ def warn_at_available_unchecked_use : Warning<
41004100
"%select{@available|__builtin_available}0 does not guard availability here; "
41014101
"use if (%select{@available|__builtin_available}0) instead">,
41024102
InGroup<DiagGroup<"unsupported-availability-guard">>;
4103+
def warn_availability_invalid_os_version
4104+
: Warning<"invalid %1 version '%0' in availability attribute">, InGroup<DiagGroup<"invalid-version-availability">>;
4105+
def note_availability_invalid_os_version_adjusted: Note<"implicitly treating version as '%0'">;
41034106

41044107
def warn_missing_sdksettings_for_availability_checking : Warning<
41054108
"%0 availability is ignored without a valid 'SDKSettings.json' in the SDK">,

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,10 +1861,14 @@ struct DarwinPlatform {
18611861
UnderlyingOSVersion.reset();
18621862
return Result;
18631863
}
1864+
bool isValidOSVersion() const {
1865+
return llvm::Triple::isValidVersionForOS(getOSFromPlatform(Platform),
1866+
getOSVersion());
1867+
}
18641868

18651869
VersionTuple getCanonicalOSVersion() const {
1866-
return llvm::Triple::getCanonicalVersionForOS(getOSFromPlatform(Platform),
1867-
getOSVersion());
1870+
return llvm::Triple::getCanonicalVersionForOS(
1871+
getOSFromPlatform(Platform), getOSVersion(), /*IsInValidRange=*/true);
18681872
}
18691873

18701874
void setOSVersion(const VersionTuple &Version) {
@@ -2591,6 +2595,9 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
25912595
}
25922596

25932597
assert(PlatformAndVersion && "Unable to infer Darwin variant");
2598+
if (!PlatformAndVersion->isValidOSVersion())
2599+
getDriver().Diag(diag::err_drv_invalid_version_number)
2600+
<< PlatformAndVersion->getAsString(Args, Opts);
25942601
// After the deployment OS version has been resolved, set it to the canonical
25952602
// version before further error detection and converting to a proper target
25962603
// triple.
@@ -2692,6 +2699,12 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
26922699
ZipperedOSVersion = PlatformAndVersion->getZipperedOSVersion();
26932700
setTarget(Platform, Environment, Major, Minor, Micro, ZipperedOSVersion);
26942701
TargetVariantTriple = PlatformAndVersion->getTargetVariantTriple();
2702+
if (TargetVariantTriple &&
2703+
!llvm::Triple::isValidVersionForOS(TargetVariantTriple->getOS(),
2704+
TargetVariantTriple->getOSVersion())) {
2705+
getDriver().Diag(diag::err_drv_invalid_version_number)
2706+
<< TargetVariantTriple->str();
2707+
}
26952708

26962709
if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
26972710
StringRef SDK = getSDKName(A->getValue());

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,8 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
24302430
IdentifierLoc *Platform = AL.getArgAsIdent(0);
24312431

24322432
IdentifierInfo *II = Platform->getIdentifierInfo();
2433-
if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2433+
StringRef PrettyName = AvailabilityAttr::getPrettyPlatformName(II->getName());
2434+
if (PrettyName.empty())
24342435
S.Diag(Platform->getLoc(), diag::warn_availability_unknown_platform)
24352436
<< Platform->getIdentifierInfo();
24362437

@@ -2441,15 +2442,32 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
24412442
AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
24422443
AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
24432444
AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2444-
if (II->getName() == "macos" || II->getName() == "macos_app_extension") {
2445-
// Canonicalize macOS availability versions.
2446-
Introduced.Version = llvm::Triple::getCanonicalVersionForOS(
2447-
llvm::Triple::MacOSX, Introduced.Version);
2448-
Deprecated.Version = llvm::Triple::getCanonicalVersionForOS(
2449-
llvm::Triple::MacOSX, Deprecated.Version);
2450-
Obsoleted.Version = llvm::Triple::getCanonicalVersionForOS(
2451-
llvm::Triple::MacOSX, Obsoleted.Version);
2445+
2446+
const llvm::Triple::OSType PlatformOS = AvailabilityAttr::getOSType(
2447+
AvailabilityAttr::canonicalizePlatformName(II->getName()));
2448+
2449+
auto reportAndUpdateIfInvalidOS = [&](auto &InputVersion) -> void {
2450+
const bool IsInValidRange =
2451+
llvm::Triple::isValidVersionForOS(PlatformOS, InputVersion);
2452+
// Canonicalize availability versions.
2453+
auto CanonicalVersion = llvm::Triple::getCanonicalVersionForOS(
2454+
PlatformOS, InputVersion, IsInValidRange);
2455+
if (!IsInValidRange) {
2456+
S.Diag(Platform->getLoc(), diag::warn_availability_invalid_os_version)
2457+
<< InputVersion.getAsString() << PrettyName;
2458+
S.Diag(Platform->getLoc(),
2459+
diag::note_availability_invalid_os_version_adjusted)
2460+
<< CanonicalVersion.getAsString();
2461+
}
2462+
InputVersion = CanonicalVersion;
2463+
};
2464+
2465+
if (PlatformOS != llvm::Triple::OSType::UnknownOS) {
2466+
reportAndUpdateIfInvalidOS(Introduced.Version);
2467+
reportAndUpdateIfInvalidOS(Deprecated.Version);
2468+
reportAndUpdateIfInvalidOS(Obsoleted.Version);
24522469
}
2470+
24532471
bool IsUnavailable = AL.getUnavailableLoc().isValid();
24542472
bool IsStrict = AL.getStrictLoc().isValid();
24552473
StringRef Str;
@@ -2541,7 +2559,11 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
25412559
}
25422560

25432561
auto Major = Version.getMajor();
2544-
auto NewMajor = Major >= 9 ? Major - 7 : 0;
2562+
auto NewMajor = Major;
2563+
if (Major < 9)
2564+
NewMajor = 0;
2565+
else if (Major < 12)
2566+
NewMajor = Major - 7;
25452567
if (NewMajor >= 2) {
25462568
if (Version.getMinor()) {
25472569
if (Version.getSubminor())

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5163,7 +5163,7 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
51635163
AtLoc, RParen, Context.BoolTy, Spec.getDomainName(), Context);
51645164
}
51655165

5166-
auto FindSpecVersion = [&](StringRef Platform)
5166+
auto FindSpecVersion = [&](StringRef Platform, const llvm::Triple::OSType &OS)
51675167
-> std::optional<ObjCAvailabilityCheckExpr::VersionAsWritten> {
51685168
auto Spec = llvm::find_if(AvailSpecs, [&](const AvailabilitySpec &Spec) {
51695169
return Spec.getPlatform() == Platform;
@@ -5177,18 +5177,16 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
51775177
}
51785178
if (Spec == AvailSpecs.end())
51795179
return std::nullopt;
5180-
if (Platform == "macos") {
5181-
return ObjCAvailabilityCheckExpr::VersionAsWritten{
5182-
llvm::Triple::getCanonicalVersionForOS(llvm::Triple::MacOSX,
5183-
Spec->getVersion()),
5184-
Spec->getVersion()};
5185-
}
5186-
return ObjCAvailabilityCheckExpr::VersionAsWritten{Spec->getVersion(),
5187-
Spec->getVersion()};
5180+
return ObjCAvailabilityCheckExpr::VersionAsWritten{
5181+
llvm::Triple::getCanonicalVersionForOS(
5182+
OS, Spec->getVersion(),
5183+
llvm::Triple::isValidVersionForOS(OS, Spec->getVersion())),
5184+
Spec->getVersion()};
51885185
};
51895186

51905187
auto MaybeVersion =
5191-
FindSpecVersion(Context.getTargetInfo().getPlatformName());
5188+
FindSpecVersion(Context.getTargetInfo().getPlatformName(),
5189+
Context.getTargetInfo().getTriple().getOS());
51925190
ObjCAvailabilityCheckExpr::VersionAsWritten Version;
51935191
if (MaybeVersion)
51945192
Version = *MaybeVersion;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// This test verifies IR generated for APIs protected with availability annotations with a common versions.
2+
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-ios26.0" -emit-llvm -o - %s | FileCheck %s
3+
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-tvos26" -emit-llvm -o - %s | FileCheck %s
4+
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-watchos26" -emit-llvm -o - %s | FileCheck %s
5+
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-ios18" -emit-llvm -o - %s | FileCheck -check-prefix=OLD %s
6+
7+
__attribute__((availability(ios,introduced=19)))
8+
void f0(void);
9+
10+
__attribute__((availability(ios,introduced=26)))
11+
void f1(void);
12+
13+
__attribute__((availability(ios,introduced=27)))
14+
void f2(void);
15+
16+
// OLD: declare extern_weak void @f0
17+
// OLD: declare extern_weak void @f1
18+
// OLD: declare extern_weak void @f2
19+
20+
// CHECK: declare void @f0
21+
// CHECK: declare void @f1
22+
// CHECK: declare extern_weak void @f2
23+
24+
void test() {
25+
f0();
26+
f1();
27+
f2();
28+
}

clang/test/Driver/darwin-infer-simulator-sdkroot.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
//
4242
// RUN: rm -rf %t/SDKs/WatchOS3.0.sdk
4343
// RUN: mkdir -p %t/SDKs/WatchOS3.0.sdk
44-
// RUN: env SDKROOT=%t/SDKs/WatchOS3.0.sdk %clang %s -fuse-ld= -mlinker-version=400 -### 2>&1 \
44+
// RUN: env SDKROOT=%t/SDKs/WatchOS3.0.sdk %clang %s -fuse-ld= -arch arm64_32 -mlinker-version=400 -### 2>&1 \
4545
// RUN: | FileCheck --check-prefix=CHECK-WATCH %s
46-
// RUN: env WATCHOS_DEPLOYMENT_TARGET=3.0 %clang %s -fuse-ld= -isysroot %t/SDKs/WatchOS3.0.sdk -mlinker-version=400 -### 2>&1 \
46+
// RUN: env WATCHOS_DEPLOYMENT_TARGET=3.0 %clang %s -fuse-ld= -arch arm64_32 -isysroot %t/SDKs/WatchOS3.0.sdk -mlinker-version=400 -### 2>&1 \
4747
// RUN: | FileCheck --check-prefix=CHECK-WATCH %s
4848
//
4949
// CHECK-WATCH: clang
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// Verify invalid OSVersions are diagnosed.
2+
3+
// RUN: not %clang -target arm64-apple-ios20 -c %s 2>&1 | FileCheck %s --check-prefix=IOS
4+
// IOS: error: invalid version number in '-target arm64-apple-ios20'
5+
6+
// RUN: not %clang -target arm64-apple-watchos20 -c %s 2>&1 | FileCheck %s --check-prefix=WATCHOS
7+
// WATCHOS: error: invalid version number in '-target arm64-apple-watchos20'
8+
9+
// RUN: not %clang -target arm64-apple-macosx19 -c %s 2>&1 | FileCheck %s --check-prefix=MAC
10+
// MAC: error: invalid version number in '-target arm64-apple-macosx19'
11+
12+
// RUN: not %clang -target arm64-apple-ios22-macabi -c %s 2>&1 | FileCheck %s --check-prefix=IOSMAC
13+
// IOSMAC: error: invalid version number in '-target arm64-apple-ios22-macabi'
14+
15+
// RUN: not %clang -target arm64-apple-macosx16 -darwin-target-variant arm64-apple-ios22-macabi -c %s 2>&1 | FileCheck %s --check-prefix=ZIPPERED
16+
// ZIPPERED: error: invalid version number in 'arm64-apple-ios22-macabi'
17+
18+
// RUN: not %clang -target arm64-apple-visionos5 -c %s 2>&1 | FileCheck %s --check-prefix=VISION
19+
// VISION: error: invalid version number in '-target arm64-apple-visionos5'
20+
21+
// RUN: not %clang -target arm64-apple-tvos21 -c %s 2>&1 | FileCheck %s --check-prefix=TV
22+
// TV: error: invalid version number in '-target arm64-apple-tvos21'

clang/test/Driver/darwin-ld-platform-version-macos.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@
4848
// RUN: -### %t.o 2>&1 \
4949
// RUN: | FileCheck --check-prefix=NOSDK %s
5050
// NOSDK: "-platform_version" "macos" "10.13.0" "10.13.0"
51+
52+
// RUN: %clang -target arm64-apple-macos26 -mlinker-version=520 \
53+
// RUN: -### %t.o 2>&1 \
54+
// RUN: | FileCheck --check-prefix=VERSION_BUMP %s
55+
// VERSION_BUMP: "-platform_version" "macos" "26.0.0" "26.0.0"

clang/test/Driver/darwin-ld-platform-version-watchos.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,54 @@
1717
// RUN: -### %t.o 2>&1 \
1818
// RUN: | FileCheck --check-prefix=SIMUL %s
1919

20+
// RUN: %clang -target arm64-apple-watchos6.3 -fuse-ld= \
21+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=400 \
22+
// RUN: -### %t.o 2>&1 \
23+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-OLD %s
24+
25+
// RUN: %clang -target arm64e-apple-watchos6.3 -fuse-ld= \
26+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=400 \
27+
// RUN: -### %t.o 2>&1 \
28+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-OLD %s
29+
30+
// RUN: %clang -target arm64-apple-watchos26.1 -fuse-ld= \
31+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=400 \
32+
// RUN: -### %t.o 2>&1 \
33+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-OLD-261 %s
34+
35+
// RUN: %clang -target arm64-apple-watchos6.3 -fuse-ld=lld \
36+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=0 \
37+
// RUN: -### %t.o -B%S/Inputs/lld 2>&1 \
38+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW %s
39+
40+
// RUN: %clang -target arm64e-apple-watchos6.3 -fuse-ld=lld \
41+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=0 \
42+
// RUN: -### %t.o -B%S/Inputs/lld 2>&1 \
43+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW %s
44+
45+
// RUN: %clang -target arm64-apple-watchos6.3 -fuse-ld= \
46+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=520 \
47+
// RUN: -### %t.o 2>&1 \
48+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW %s
49+
50+
// RUN: %clang -target arm64-apple-watchos26.1 -fuse-ld= \
51+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=520 \
52+
// RUN: -### %t.o 2>&1 \
53+
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW-261 %s
54+
55+
// RUN: %clang -target arm64-apple-watchos6-simulator -fuse-ld= \
56+
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=520 \
57+
// RUN: -### %t.o 2>&1 \
58+
// RUN: | FileCheck --check-prefix=ARM64-SIMUL %s
59+
2060
// LINKER-OLD: "-watchos_version_min" "5.2.0"
2161
// LINKER-NEW: "-platform_version" "watchos" "5.2.0" "6.0"
2262
// SIMUL: "-platform_version" "watchos-simulator" "6.0.0" "6.0"
63+
64+
// ARM64-LINKER-OLD: "-watchos_version_min" "26.0.0"
65+
// ARM64-LINKER-OLD-261: "-watchos_version_min" "26.1.0"
66+
67+
// ARM64-LINKER-NEW: "-platform_version" "watchos" "26.0.0" "6.0"
68+
// ARM64-LINKER-NEW-261: "-platform_version" "watchos" "26.1.0" "6.0"
69+
70+
// ARM64-SIMUL: "-platform_version" "watchos-simulator" "7.0.0" "6.0"

0 commit comments

Comments
 (0)