Skip to content

[clang][Darwin] Align all OS Versions for 26 #143548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,8 @@ static llvm::StringRef canonicalizePlatformName(llvm::StringRef Platform) {
return llvm::StringSwitch<llvm::StringRef>(Platform)
.Case("iOS", "ios")
.Case("macOS", "macos")
.Case("macOSX", "macos")
.Case("macosx", "macos")
.Case("tvOS", "tvos")
.Case("watchOS", "watchos")
.Case("iOSApplicationExtension", "ios_app_extension")
Expand Down Expand Up @@ -1175,6 +1177,26 @@ static llvm::Triple::EnvironmentType getEnvironmentType(llvm::StringRef Environm
.Case("library", llvm::Triple::Library)
.Default(llvm::Triple::UnknownEnvironment);
}

static llvm::Triple::OSType getOSType(llvm::StringRef Platform) {
using OSType = llvm::Triple::OSType;
return llvm::StringSwitch<OSType>(Platform)
.Case("ios", OSType::IOS)
.Case("macos", OSType::MacOSX)
.Case("maccatalyst", OSType::IOS)
.Case("tvos", OSType::TvOS)
.Case("watchos", OSType::WatchOS)
.Case("bridgeos", OSType::BridgeOS)
.Case("ios_app_extension", OSType::IOS)
.Case("maccatalyst_app_extension", OSType::IOS)
.Case("macos_app_extension", OSType::MacOSX)
.Case("tvos_app_extension", OSType::TvOS)
.Case("watchos_app_extension", OSType::WatchOS)
.Case("xros", OSType::XROS)
.Case("xros_app_extension", OSType::XROS)
.Default(OSType::UnknownOS);
}

}];
let HasCustomParsing = 1;
let InheritEvenIfAlreadyPresent = 1;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4063,6 +4063,9 @@ def warn_at_available_unchecked_use : Warning<
"%select{@available|__builtin_available}0 does not guard availability here; "
"use if (%select{@available|__builtin_available}0) instead">,
InGroup<DiagGroup<"unsupported-availability-guard">>;
def warn_availability_invalid_os_version
: Warning<"invalid %1 version '%0' in availability attribute">, InGroup<DiagGroup<"invalid-version-availability">>;
def note_availability_invalid_os_version_adjusted: Note<"implicitly treating version as '%0'">;

def warn_missing_sdksettings_for_availability_checking : Warning<
"%0 availability is ignored without a valid 'SDKSettings.json' in the SDK">,
Expand Down
17 changes: 15 additions & 2 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1721,10 +1721,14 @@ struct DarwinPlatform {
UnderlyingOSVersion.reset();
return Result;
}
bool isValidOSVersion() const {
return llvm::Triple::isValidVersionForOS(getOSFromPlatform(Platform),
getOSVersion());
}

VersionTuple getCanonicalOSVersion() const {
return llvm::Triple::getCanonicalVersionForOS(getOSFromPlatform(Platform),
getOSVersion());
return llvm::Triple::getCanonicalVersionForOS(
getOSFromPlatform(Platform), getOSVersion(), /*IsInValidRange=*/true);
}

void setOSVersion(const VersionTuple &Version) {
Expand Down Expand Up @@ -2451,6 +2455,9 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
}

assert(PlatformAndVersion && "Unable to infer Darwin variant");
if (!PlatformAndVersion->isValidOSVersion())
getDriver().Diag(diag::err_drv_invalid_version_number)
<< PlatformAndVersion->getAsString(Args, Opts);
// After the deployment OS version has been resolved, set it to the canonical
// version before further error detection and converting to a proper target
// triple.
Expand Down Expand Up @@ -2552,6 +2559,12 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
ZipperedOSVersion = PlatformAndVersion->getZipperedOSVersion();
setTarget(Platform, Environment, Major, Minor, Micro, ZipperedOSVersion);
TargetVariantTriple = PlatformAndVersion->getTargetVariantTriple();
if (TargetVariantTriple &&
!llvm::Triple::isValidVersionForOS(TargetVariantTriple->getOS(),
TargetVariantTriple->getOSVersion())) {
getDriver().Diag(diag::err_drv_invalid_version_number)
<< TargetVariantTriple->str();
}

if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
StringRef SDK = getSDKName(A->getValue());
Expand Down
35 changes: 33 additions & 2 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,8 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
IdentifierLoc *Platform = AL.getArgAsIdent(0);

IdentifierInfo *II = Platform->getIdentifierInfo();
if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
StringRef PrettyName = AvailabilityAttr::getPrettyPlatformName(II->getName());
if (PrettyName.empty())
S.Diag(Platform->getLoc(), diag::warn_availability_unknown_platform)
<< Platform->getIdentifierInfo();

Expand All @@ -2385,6 +2386,32 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();

const llvm::Triple::OSType PlatformOS = AvailabilityAttr::getOSType(
AvailabilityAttr::canonicalizePlatformName(II->getName()));

auto reportAndUpdateIfInvalidOS = [&](auto &InputVersion) -> void {
const bool IsInValidRange =
llvm::Triple::isValidVersionForOS(PlatformOS, InputVersion);
// Canonicalize availability versions.
auto CanonicalVersion = llvm::Triple::getCanonicalVersionForOS(
PlatformOS, InputVersion, IsInValidRange);
if (!IsInValidRange) {
S.Diag(Platform->getLoc(), diag::warn_availability_invalid_os_version)
<< InputVersion.getAsString() << PrettyName;
S.Diag(Platform->getLoc(),
diag::note_availability_invalid_os_version_adjusted)
<< CanonicalVersion.getAsString();
}
InputVersion = CanonicalVersion;
};

if (PlatformOS != llvm::Triple::OSType::UnknownOS) {
reportAndUpdateIfInvalidOS(Introduced.Version);
reportAndUpdateIfInvalidOS(Deprecated.Version);
reportAndUpdateIfInvalidOS(Obsoleted.Version);
}

bool IsUnavailable = AL.getUnavailableLoc().isValid();
bool IsStrict = AL.getStrictLoc().isValid();
StringRef Str;
Expand Down Expand Up @@ -2476,7 +2503,11 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
}

auto Major = Version.getMajor();
auto NewMajor = Major >= 9 ? Major - 7 : 0;
auto NewMajor = Major;
if (Major < 9)
NewMajor = 0;
else if (Major < 12)
NewMajor = Major - 7;
if (NewMajor >= 2) {
if (Version.getMinor()) {
if (Version.getSubminor())
Expand Down
11 changes: 8 additions & 3 deletions clang/lib/Sema/SemaExprObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5151,7 +5151,8 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
SourceLocation RParen) {
ASTContext &Context = getASTContext();
auto FindSpecVersion =
[&](StringRef Platform) -> std::optional<VersionTuple> {
[&](StringRef Platform,
const llvm::Triple::OSType &OS) -> std::optional<VersionTuple> {
auto Spec = llvm::find_if(AvailSpecs, [&](const AvailabilitySpec &Spec) {
return Spec.getPlatform() == Platform;
});
Expand All @@ -5164,12 +5165,16 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
}
if (Spec == AvailSpecs.end())
return std::nullopt;
return Spec->getVersion();

return llvm::Triple::getCanonicalVersionForOS(
OS, Spec->getVersion(),
llvm::Triple::isValidVersionForOS(OS, Spec->getVersion()));
};

VersionTuple Version;
if (auto MaybeVersion =
FindSpecVersion(Context.getTargetInfo().getPlatformName()))
FindSpecVersion(Context.getTargetInfo().getPlatformName(),
Context.getTargetInfo().getTriple().getOS()))
Version = *MaybeVersion;

// The use of `@available` in the enclosing context should be analyzed to
Expand Down
28 changes: 28 additions & 0 deletions clang/test/CodeGen/attr-availability-aligned-versions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// This test verifies IR generated for APIs protected with availability annotations with a common versions.
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-ios26.0" -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-tvos26" -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-watchos26" -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -fvisibility=hidden "-triple" "arm64-apple-ios18" -emit-llvm -o - %s | FileCheck -check-prefix=OLD %s

__attribute__((availability(ios,introduced=19)))
void f0(void);

__attribute__((availability(ios,introduced=26)))
void f1(void);

__attribute__((availability(ios,introduced=27)))
void f2(void);

// OLD: declare extern_weak void @f0
// OLD: declare extern_weak void @f1
// OLD: declare extern_weak void @f2

// CHECK: declare void @f0
// CHECK: declare void @f1
// CHECK: declare extern_weak void @f2

void test() {
f0();
f1();
f2();
}
4 changes: 2 additions & 2 deletions clang/test/Driver/darwin-infer-simulator-sdkroot.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
//
// RUN: rm -rf %t/SDKs/WatchOS3.0.sdk
// RUN: mkdir -p %t/SDKs/WatchOS3.0.sdk
// RUN: env SDKROOT=%t/SDKs/WatchOS3.0.sdk %clang %s -fuse-ld= -mlinker-version=400 -### 2>&1 \
// RUN: env SDKROOT=%t/SDKs/WatchOS3.0.sdk %clang %s -fuse-ld= -arch arm64_32 -mlinker-version=400 -### 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-WATCH %s
// RUN: env WATCHOS_DEPLOYMENT_TARGET=3.0 %clang %s -fuse-ld= -isysroot %t/SDKs/WatchOS3.0.sdk -mlinker-version=400 -### 2>&1 \
// 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 \
// RUN: | FileCheck --check-prefix=CHECK-WATCH %s
//
// CHECK-WATCH: clang
Expand Down
22 changes: 22 additions & 0 deletions clang/test/Driver/darwin-invalid-os-versions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// Verify invalid OSVersions are diagnosed.

// RUN: not %clang -target arm64-apple-ios20 -c %s 2>&1 | FileCheck %s --check-prefix=IOS
// IOS: error: invalid version number in '-target arm64-apple-ios20'

// RUN: not %clang -target arm64-apple-watchos20 -c %s 2>&1 | FileCheck %s --check-prefix=WATCHOS
// WATCHOS: error: invalid version number in '-target arm64-apple-watchos20'

// RUN: not %clang -target arm64-apple-macosx19 -c %s 2>&1 | FileCheck %s --check-prefix=MAC
// MAC: error: invalid version number in '-target arm64-apple-macosx19'

// RUN: not %clang -target arm64-apple-ios22-macabi -c %s 2>&1 | FileCheck %s --check-prefix=IOSMAC
// IOSMAC: error: invalid version number in '-target arm64-apple-ios22-macabi'

// RUN: not %clang -target arm64-apple-macosx16 -darwin-target-variant arm64-apple-ios22-macabi -c %s 2>&1 | FileCheck %s --check-prefix=ZIPPERED
// ZIPPERED: error: invalid version number in 'arm64-apple-ios22-macabi'

// RUN: not %clang -target arm64-apple-visionos5 -c %s 2>&1 | FileCheck %s --check-prefix=VISION
// VISION: error: invalid version number in '-target arm64-apple-visionos5'

// RUN: not %clang -target arm64-apple-tvos21 -c %s 2>&1 | FileCheck %s --check-prefix=TV
// TV: error: invalid version number in '-target arm64-apple-tvos21'
5 changes: 5 additions & 0 deletions clang/test/Driver/darwin-ld-platform-version-macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=NOSDK %s
// NOSDK: "-platform_version" "macos" "10.13.0" "10.13.0"

// RUN: %clang -target arm64-apple-macos26 -mlinker-version=520 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=VERSION_BUMP %s
// VERSION_BUMP: "-platform_version" "macos" "26.0.0" "26.0.0"
48 changes: 48 additions & 0 deletions clang/test/Driver/darwin-ld-platform-version-watchos.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,54 @@
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=SIMUL %s

// RUN: %clang -target arm64-apple-watchos6.3 -fuse-ld= \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=400 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-OLD %s

// RUN: %clang -target arm64e-apple-watchos6.3 -fuse-ld= \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=400 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-OLD %s

// RUN: %clang -target arm64-apple-watchos26.1 -fuse-ld= \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=400 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-OLD-261 %s

// RUN: %clang -target arm64-apple-watchos6.3 -fuse-ld=lld \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=0 \
// RUN: -### %t.o -B%S/Inputs/lld 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW %s

// RUN: %clang -target arm64e-apple-watchos6.3 -fuse-ld=lld \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=0 \
// RUN: -### %t.o -B%S/Inputs/lld 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW %s

// RUN: %clang -target arm64-apple-watchos6.3 -fuse-ld= \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=520 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW %s

// RUN: %clang -target arm64-apple-watchos26.1 -fuse-ld= \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=520 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-LINKER-NEW-261 %s

// RUN: %clang -target arm64-apple-watchos6-simulator -fuse-ld= \
// RUN: -isysroot %S/Inputs/WatchOS6.0.sdk -mlinker-version=520 \
// RUN: -### %t.o 2>&1 \
// RUN: | FileCheck --check-prefix=ARM64-SIMUL %s

// LINKER-OLD: "-watchos_version_min" "5.2.0"
// LINKER-NEW: "-platform_version" "watchos" "5.2.0" "6.0"
// SIMUL: "-platform_version" "watchos-simulator" "6.0.0" "6.0"

// ARM64-LINKER-OLD: "-watchos_version_min" "26.0.0"
// ARM64-LINKER-OLD-261: "-watchos_version_min" "26.1.0"

// ARM64-LINKER-NEW: "-platform_version" "watchos" "26.0.0" "6.0"
// ARM64-LINKER-NEW-261: "-platform_version" "watchos" "26.1.0" "6.0"

// ARM64-SIMUL: "-platform_version" "watchos-simulator" "7.0.0" "6.0"
8 changes: 4 additions & 4 deletions clang/test/ExtractAPI/availability.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void a(void) __attribute__((availability(macos, introduced=12.0)));
// A-NEXT: ]

// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix B
void b(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=20.0)));
void b(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=30.0)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are supposed to be future versions, they should likely be 99.0 instead of something we're likely to hit in the next few years.

Copy link
Member Author

@cyndyishida cyndyishida Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I just incremented it by the same value as the bump to avoid a warning on an invalid range.

// B-LABEL: "!testLabel": "c:@F@b"
// B: "availability": [
// B-NEXT: {
Expand All @@ -33,15 +33,15 @@ void b(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0
// B-NEXT: "patch": 0
// B-NEXT: },
// B-NEXT: "obsoleted": {
// B-NEXT: "major": 20,
// B-NEXT: "major": 30,
// B-NEXT: "minor": 0,
// B-NEXT: "patch": 0
// B-NEXT: }
// B-NEXT: }
// B-NEXT: ]

// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix E
void c(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=20.0))) __attribute__((availability(ios, introduced=13.0)));
void c(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0, obsoleted=30.0))) __attribute__((availability(ios, introduced=13.0)));
// C-LABEL: "!testLabel": "c:@F@c"
// C: "availability": [
// C-NEXT: {
Expand All @@ -57,7 +57,7 @@ void c(void) __attribute__((availability(macos, introduced=11.0, deprecated=12.0
// C-NEXT: "patch": 0
// C-NEXT: },
// C-NEXT: "obsoleted": {
// C-NEXT: "major": 20,
// C-NEXT: "major": 30,
// C-NEXT: "minor": 0,
// C-NEXT: "patch": 0
// C-NEXT: }
Expand Down
Loading
Loading