Skip to content

[6.2][clang][Darwin] Align all OS Versions for 26 #10820

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
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 @@ -1083,6 +1083,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 @@ -1117,6 +1119,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 @@ -4006,6 +4006,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 @@ -1816,10 +1816,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 @@ -2544,6 +2548,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 @@ -2645,6 +2652,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
41 changes: 31 additions & 10 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,8 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
IdentifierLoc *Platform = AL.getArgAsIdent(0);

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

Expand All @@ -2355,15 +2356,31 @@ static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
if (II->getName() == "macos" || II->getName() == "macos_app_extension") {
// Canonicalize macOS availability versions.
Introduced.Version = llvm::Triple::getCanonicalVersionForOS(
llvm::Triple::MacOSX, Introduced.Version);
Deprecated.Version = llvm::Triple::getCanonicalVersionForOS(
llvm::Triple::MacOSX, Deprecated.Version);
Obsoleted.Version = llvm::Triple::getCanonicalVersionForOS(
llvm::Triple::MacOSX, Obsoleted.Version);

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->Loc, diag::warn_availability_invalid_os_version)
<< InputVersion.getAsString() << PrettyName;
S.Diag(Platform->Loc, 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 @@ -2453,7 +2470,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
18 changes: 8 additions & 10 deletions clang/lib/Sema/SemaExprObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5161,7 +5161,7 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
AtLoc, RParen, Context.BoolTy, Spec.getDomainName(), Context);
}

auto FindSpecVersion = [&](StringRef Platform)
auto FindSpecVersion = [&](StringRef Platform, const llvm::Triple::OSType &OS)
-> std::optional<ObjCAvailabilityCheckExpr::VersionAsWritten> {
auto Spec = llvm::find_if(AvailSpecs, [&](const AvailabilitySpec &Spec) {
return Spec.getPlatform() == Platform;
Expand All @@ -5175,18 +5175,16 @@ ExprResult SemaObjC::ActOnObjCAvailabilityCheckExpr(
}
if (Spec == AvailSpecs.end())
return std::nullopt;
if (Platform == "macos") {
return ObjCAvailabilityCheckExpr::VersionAsWritten{
llvm::Triple::getCanonicalVersionForOS(llvm::Triple::MacOSX,
Spec->getVersion()),
Spec->getVersion()};
}
return ObjCAvailabilityCheckExpr::VersionAsWritten{Spec->getVersion(),
Spec->getVersion()};
return ObjCAvailabilityCheckExpr::VersionAsWritten{
llvm::Triple::getCanonicalVersionForOS(
OS, Spec->getVersion(),
llvm::Triple::isValidVersionForOS(OS, Spec->getVersion())),
Spec->getVersion()};
};

auto MaybeVersion =
FindSpecVersion(Context.getTargetInfo().getPlatformName());
FindSpecVersion(Context.getTargetInfo().getPlatformName(),
Context.getTargetInfo().getTriple().getOS());
ObjCAvailabilityCheckExpr::VersionAsWritten Version;
if (MaybeVersion)
Version = *MaybeVersion;
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)));
// 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