Skip to content

Commit 0a1c3b2

Browse files
cyndyishidatomtor
authored andcommitted
[clang][Darwin] Align all OS Versions for 26 (llvm#143548)
* 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 48c9a90 commit 0a1c3b2

21 files changed

+648
-41
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,8 @@ static llvm::StringRef canonicalizePlatformName(llvm::StringRef Platform) {
11111111
return llvm::StringSwitch<llvm::StringRef>(Platform)
11121112
.Case("iOS", "ios")
11131113
.Case("macOS", "macos")
1114+
.Case("macOSX", "macos")
1115+
.Case("macosx", "macos")
11141116
.Case("tvOS", "tvos")
11151117
.Case("watchOS", "watchos")
11161118
.Case("iOSApplicationExtension", "ios_app_extension")
@@ -1175,6 +1177,26 @@ static llvm::Triple::EnvironmentType getEnvironmentType(llvm::StringRef Environm
11751177
.Case("library", llvm::Triple::Library)
11761178
.Default(llvm::Triple::UnknownEnvironment);
11771179
}
1180+
1181+
static llvm::Triple::OSType getOSType(llvm::StringRef Platform) {
1182+
using OSType = llvm::Triple::OSType;
1183+
return llvm::StringSwitch<OSType>(Platform)
1184+
.Case("ios", OSType::IOS)
1185+
.Case("macos", OSType::MacOSX)
1186+
.Case("maccatalyst", OSType::IOS)
1187+
.Case("tvos", OSType::TvOS)
1188+
.Case("watchos", OSType::WatchOS)
1189+
.Case("bridgeos", OSType::BridgeOS)
1190+
.Case("ios_app_extension", OSType::IOS)
1191+
.Case("maccatalyst_app_extension", OSType::IOS)
1192+
.Case("macos_app_extension", OSType::MacOSX)
1193+
.Case("tvos_app_extension", OSType::TvOS)
1194+
.Case("watchos_app_extension", OSType::WatchOS)
1195+
.Case("xros", OSType::XROS)
1196+
.Case("xros_app_extension", OSType::XROS)
1197+
.Default(OSType::UnknownOS);
1198+
}
1199+
11781200
}];
11791201
let HasCustomParsing = 1;
11801202
let InheritEvenIfAlreadyPresent = 1;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,6 +4063,9 @@ def warn_at_available_unchecked_use : Warning<
40634063
"%select{@available|__builtin_available}0 does not guard availability here; "
40644064
"use if (%select{@available|__builtin_available}0) instead">,
40654065
InGroup<DiagGroup<"unsupported-availability-guard">>;
4066+
def warn_availability_invalid_os_version
4067+
: Warning<"invalid %1 version '%0' in availability attribute">, InGroup<DiagGroup<"invalid-version-availability">>;
4068+
def note_availability_invalid_os_version_adjusted: Note<"implicitly treating version as '%0'">;
40664069

40674070
def warn_missing_sdksettings_for_availability_checking : Warning<
40684071
"%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
@@ -1721,10 +1721,14 @@ struct DarwinPlatform {
17211721
UnderlyingOSVersion.reset();
17221722
return Result;
17231723
}
1724+
bool isValidOSVersion() const {
1725+
return llvm::Triple::isValidVersionForOS(getOSFromPlatform(Platform),
1726+
getOSVersion());
1727+
}
17241728

17251729
VersionTuple getCanonicalOSVersion() const {
1726-
return llvm::Triple::getCanonicalVersionForOS(getOSFromPlatform(Platform),
1727-
getOSVersion());
1730+
return llvm::Triple::getCanonicalVersionForOS(
1731+
getOSFromPlatform(Platform), getOSVersion(), /*IsInValidRange=*/true);
17281732
}
17291733

17301734
void setOSVersion(const VersionTuple &Version) {
@@ -2451,6 +2455,9 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
24512455
}
24522456

24532457
assert(PlatformAndVersion && "Unable to infer Darwin variant");
2458+
if (!PlatformAndVersion->isValidOSVersion())
2459+
getDriver().Diag(diag::err_drv_invalid_version_number)
2460+
<< PlatformAndVersion->getAsString(Args, Opts);
24542461
// After the deployment OS version has been resolved, set it to the canonical
24552462
// version before further error detection and converting to a proper target
24562463
// triple.
@@ -2552,6 +2559,12 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
25522559
ZipperedOSVersion = PlatformAndVersion->getZipperedOSVersion();
25532560
setTarget(Platform, Environment, Major, Minor, Micro, ZipperedOSVersion);
25542561
TargetVariantTriple = PlatformAndVersion->getTargetVariantTriple();
2562+
if (TargetVariantTriple &&
2563+
!llvm::Triple::isValidVersionForOS(TargetVariantTriple->getOS(),
2564+
TargetVariantTriple->getOSVersion())) {
2565+
getDriver().Diag(diag::err_drv_invalid_version_number)
2566+
<< TargetVariantTriple->str();
2567+
}
25552568

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

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,8 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
23742374
IdentifierLoc *Platform = AL.getArgAsIdent(0);
23752375

23762376
IdentifierInfo *II = Platform->getIdentifierInfo();
2377-
if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2377+
StringRef PrettyName = AvailabilityAttr::getPrettyPlatformName(II->getName());
2378+
if (PrettyName.empty())
23782379
S.Diag(Platform->getLoc(), diag::warn_availability_unknown_platform)
23792380
<< Platform->getIdentifierInfo();
23802381

@@ -2385,6 +2386,32 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
23852386
AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
23862387
AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
23872388
AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2389+
2390+
const llvm::Triple::OSType PlatformOS = AvailabilityAttr::getOSType(
2391+
AvailabilityAttr::canonicalizePlatformName(II->getName()));
2392+
2393+
auto reportAndUpdateIfInvalidOS = [&](auto &InputVersion) -> void {
2394+
const bool IsInValidRange =
2395+
llvm::Triple::isValidVersionForOS(PlatformOS, InputVersion);
2396+
// Canonicalize availability versions.
2397+
auto CanonicalVersion = llvm::Triple::getCanonicalVersionForOS(
2398+
PlatformOS, InputVersion, IsInValidRange);
2399+
if (!IsInValidRange) {
2400+
S.Diag(Platform->getLoc(), diag::warn_availability_invalid_os_version)
2401+
<< InputVersion.getAsString() << PrettyName;
2402+
S.Diag(Platform->getLoc(),
2403+
diag::note_availability_invalid_os_version_adjusted)
2404+
<< CanonicalVersion.getAsString();
2405+
}
2406+
InputVersion = CanonicalVersion;
2407+
};
2408+
2409+
if (PlatformOS != llvm::Triple::OSType::UnknownOS) {
2410+
reportAndUpdateIfInvalidOS(Introduced.Version);
2411+
reportAndUpdateIfInvalidOS(Deprecated.Version);
2412+
reportAndUpdateIfInvalidOS(Obsoleted.Version);
2413+
}
2414+
23882415
bool IsUnavailable = AL.getUnavailableLoc().isValid();
23892416
bool IsStrict = AL.getStrictLoc().isValid();
23902417
StringRef Str;
@@ -2476,7 +2503,11 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
24762503
}
24772504

24782505
auto Major = Version.getMajor();
2479-
auto NewMajor = Major >= 9 ? Major - 7 : 0;
2506+
auto NewMajor = Major;
2507+
if (Major < 9)
2508+
NewMajor = 0;
2509+
else if (Major < 12)
2510+
NewMajor = Major - 7;
24802511
if (NewMajor >= 2) {
24812512
if (Version.getMinor()) {
24822513
if (Version.getSubminor())

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5151,7 +5151,8 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
51515151
SourceLocation RParen) {
51525152
ASTContext &Context = getASTContext();
51535153
auto FindSpecVersion =
5154-
[&](StringRef Platform) -> std::optional<VersionTuple> {
5154+
[&](StringRef Platform,
5155+
const llvm::Triple::OSType &OS) -> std::optional<VersionTuple> {
51555156
auto Spec = llvm::find_if(AvailSpecs, [&](const AvailabilitySpec &Spec) {
51565157
return Spec.getPlatform() == Platform;
51575158
});
@@ -5164,12 +5165,16 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
51645165
}
51655166
if (Spec == AvailSpecs.end())
51665167
return std::nullopt;
5167-
return Spec->getVersion();
5168+
5169+
return llvm::Triple::getCanonicalVersionForOS(
5170+
OS, Spec->getVersion(),
5171+
llvm::Triple::isValidVersionForOS(OS, Spec->getVersion()));
51685172
};
51695173

51705174
VersionTuple Version;
51715175
if (auto MaybeVersion =
5172-
FindSpecVersion(Context.getTargetInfo().getPlatformName()))
5176+
FindSpecVersion(Context.getTargetInfo().getPlatformName(),
5177+
Context.getTargetInfo().getTriple().getOS()))
51735178
Version = *MaybeVersion;
51745179

51755180
// The use of `@available` in the enclosing context should be analyzed to
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"

clang/test/ExtractAPI/availability.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void a(void) __attribute__((availability(macos, introduced=12.0)));
1717
// A-NEXT: ]
1818

1919
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix B
20-
void b(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=20.0)));
20+
void b(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=30.0)));
2121
// B-LABEL: "!testLabel": "c:@F@b"
2222
// B: "availability": [
2323
// B-NEXT: {
@@ -33,15 +33,15 @@ void b(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0
3333
// B-NEXT: "patch": 0
3434
// B-NEXT: },
3535
// B-NEXT: "obsoleted": {
36-
// B-NEXT: "major": 20,
36+
// B-NEXT: "major": 30,
3737
// B-NEXT: "minor": 0,
3838
// B-NEXT: "patch": 0
3939
// B-NEXT: }
4040
// B-NEXT: }
4141
// B-NEXT: ]
4242

4343
// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix E
44-
void c(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=20.0))) __attribute__((availability(ios, introduced=13.0)));
44+
void c(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=30.0))) __attribute__((availability(ios, introduced=13.0)));
4545
// C-LABEL: "!testLabel": "c:@F@c"
4646
// C: "availability": [
4747
// C-NEXT: {
@@ -57,7 +57,7 @@ void c(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0
5757
// C-NEXT: "patch": 0
5858
// C-NEXT: },
5959
// C-NEXT: "obsoleted": {
60-
// C-NEXT: "major": 20,
60+
// C-NEXT: "major": 30,
6161
// C-NEXT: "minor": 0,
6262
// C-NEXT: "patch": 0
6363
// C-NEXT: }

0 commit comments

Comments
 (0)