Skip to content

Commit 6867669

Browse files
author
git apple-llvm automerger
committed
Merge commit 'c093fd93b89b' from swift/release/6.2 into stable/20240723
2 parents bbd33b4 + c093fd9 commit 6867669

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ def err_drv_cannot_open_randomize_layout_seed_file : Error<
205205
"cannot read randomize layout seed file '%0'">;
206206
def err_drv_invalid_version_number : Error<
207207
"invalid version number in '%0'">;
208+
def err_drv_invalid_version_number_inferred
209+
: Error<"invalid version number '%0' inferred from '%1'">;
208210
def err_drv_missing_version_number : Error<"missing version number in '%0'">;
209211
def err_drv_kcfi_arity_unsupported_target : Error<
210212
"target '%0' is unsupported by -fsanitize-kcfi-arity">;

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,16 +1888,23 @@ struct DarwinPlatform {
18881888
case TargetArg:
18891889
case MTargetOSArg:
18901890
case OSVersionArg:
1891-
case InferredFromSDK:
1892-
case InferredFromArch:
18931891
assert(Arg && "OS version argument not yet inferred");
18941892
return Arg->getAsString(Args);
18951893
case DeploymentTargetEnv:
18961894
return (llvm::Twine(EnvVarName) + "=" + OSVersionStr).str();
1895+
case InferredFromSDK:
1896+
case InferredFromArch:
1897+
llvm_unreachable("Cannot print arguments for inferred OS version");
18971898
}
18981899
llvm_unreachable("Unsupported Darwin Source Kind");
18991900
}
19001901

1902+
// Returns the inferred source of how the OS version was resolved.
1903+
std::string getInferredSource() {
1904+
assert(!isExplicitlySpecified() && "OS version was not inferred");
1905+
return InferredSource.str();
1906+
}
1907+
19011908
void setEnvironment(llvm::Triple::EnvironmentType EnvType,
19021909
const VersionTuple &OSVersion,
19031910
const std::optional<DarwinSDKInfo> &SDKInfo) {
@@ -1971,19 +1978,24 @@ struct DarwinPlatform {
19711978
Result.EnvVarName = EnvVarName;
19721979
return Result;
19731980
}
1974-
static DarwinPlatform createFromSDK(DarwinPlatformKind Platform,
1981+
static DarwinPlatform createFromSDK(StringRef SDKRoot,
1982+
DarwinPlatformKind Platform,
19751983
StringRef Value,
19761984
bool IsSimulator = false) {
19771985
DarwinPlatform Result(InferredFromSDK, Platform,
19781986
getVersionFromString(Value));
19791987
if (IsSimulator)
19801988
Result.Environment = DarwinEnvironmentKind::Simulator;
19811989
Result.InferSimulatorFromArch = false;
1990+
Result.InferredSource = SDKRoot;
19821991
return Result;
19831992
}
1984-
static DarwinPlatform createFromArch(llvm::Triple::OSType OS,
1993+
static DarwinPlatform createFromArch(StringRef Arch, llvm::Triple::OSType OS,
19851994
VersionTuple Version) {
1986-
return DarwinPlatform(InferredFromArch, getPlatformFromOS(OS), Version);
1995+
auto Result =
1996+
DarwinPlatform(InferredFromArch, getPlatformFromOS(OS), Version);
1997+
Result.InferredSource = Arch;
1998+
return Result;
19871999
}
19882000

19892001
/// Constructs an inferred SDKInfo value based on the version inferred from
@@ -2069,6 +2081,9 @@ struct DarwinPlatform {
20692081
bool InferSimulatorFromArch = true;
20702082
std::pair<Arg *, std::string> Arguments;
20712083
StringRef EnvVarName;
2084+
// If the DarwinPlatform information is derived from an inferred source, this
2085+
// captures what that source input was for error reporting.
2086+
StringRef InferredSource;
20722087
// When compiling for a zippered target, this value represents the target
20732088
// triple encoded in the target variant.
20742089
std::optional<llvm::Triple> TargetVariantTriple;
@@ -2237,26 +2252,27 @@ inferDeploymentTargetFromSDK(DerivedArgList &Args,
22372252
[&](StringRef SDK) -> std::optional<DarwinPlatform> {
22382253
if (SDK.starts_with("iPhoneOS") || SDK.starts_with("iPhoneSimulator"))
22392254
return DarwinPlatform::createFromSDK(
2240-
Darwin::IPhoneOS, Version,
2255+
isysroot, Darwin::IPhoneOS, Version,
22412256
/*IsSimulator=*/SDK.starts_with("iPhoneSimulator"));
22422257
else if (SDK.starts_with("MacOSX"))
2243-
return DarwinPlatform::createFromSDK(Darwin::MacOS,
2258+
return DarwinPlatform::createFromSDK(isysroot, Darwin::MacOS,
22442259
getSystemOrSDKMacOSVersion(Version));
22452260
else if (SDK.starts_with("WatchOS") || SDK.starts_with("WatchSimulator"))
22462261
return DarwinPlatform::createFromSDK(
2247-
Darwin::WatchOS, Version,
2262+
isysroot, Darwin::WatchOS, Version,
22482263
/*IsSimulator=*/SDK.starts_with("WatchSimulator"));
22492264
else if (SDK.starts_with("AppleTVOS") ||
22502265
SDK.starts_with("AppleTVSimulator"))
22512266
return DarwinPlatform::createFromSDK(
2252-
Darwin::TvOS, Version,
2267+
isysroot, Darwin::TvOS, Version,
22532268
/*IsSimulator=*/SDK.starts_with("AppleTVSimulator"));
22542269
else if (SDK.starts_with("XR"))
22552270
return DarwinPlatform::createFromSDK(
2256-
Darwin::XROS, Version,
2271+
isysroot, Darwin::XROS, Version,
22572272
/*IsSimulator=*/SDK.contains("Simulator"));
22582273
else if (SDK.starts_with("DriverKit"))
2259-
return DarwinPlatform::createFromSDK(Darwin::DriverKit, Version);
2274+
return DarwinPlatform::createFromSDK(isysroot, Darwin::DriverKit,
2275+
Version);
22602276
return std::nullopt;
22612277
};
22622278
if (auto Result = CreatePlatformFromSDKName(SDK))
@@ -2329,7 +2345,7 @@ inferDeploymentTargetFromArch(DerivedArgList &Args, const Darwin &Toolchain,
23292345
if (OSTy == llvm::Triple::UnknownOS)
23302346
return std::nullopt;
23312347
return DarwinPlatform::createFromArch(
2332-
OSTy, getInferredOSVersion(OSTy, Triple, TheDriver));
2348+
MachOArchName, OSTy, getInferredOSVersion(OSTy, Triple, TheDriver));
23332349
}
23342350

23352351
/// Returns the deployment target that's specified using the -target option.
@@ -2548,9 +2564,15 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
25482564
}
25492565

25502566
assert(PlatformAndVersion && "Unable to infer Darwin variant");
2551-
if (!PlatformAndVersion->isValidOSVersion())
2552-
getDriver().Diag(diag::err_drv_invalid_version_number)
2553-
<< PlatformAndVersion->getAsString(Args, Opts);
2567+
if (!PlatformAndVersion->isValidOSVersion()) {
2568+
if (PlatformAndVersion->isExplicitlySpecified())
2569+
getDriver().Diag(diag::err_drv_invalid_version_number)
2570+
<< PlatformAndVersion->getAsString(Args, Opts);
2571+
else
2572+
getDriver().Diag(diag::err_drv_invalid_version_number_inferred)
2573+
<< PlatformAndVersion->getOSVersion().getAsString()
2574+
<< PlatformAndVersion->getInferredSource();
2575+
}
25542576
// After the deployment OS version has been resolved, set it to the canonical
25552577
// version before further error detection and converting to a proper target
25562578
// triple.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// This test validates that the various ways to assign an invalid deployment version are captured and detected.
2+
// REQUIRES: system-darwin && native
3+
4+
// RUN: rm -rf %t
5+
// RUN: split-file %s %t
6+
7+
// RUN: env SDKROOT=%t/iPhoneOS21.0.sdk not %clang -m64 -c -### %s 2>&1 \
8+
// RUN: | FileCheck %s --check-prefix=SDKROOT
9+
10+
// RUN: not %clang -isysroot %t/iPhoneOS21.0.sdk -m64 -c -### %s 2>&1 \
11+
// RUN: | FileCheck %s --check-prefix=SYSROOT
12+
13+
// RUN: not %clang -target arm64-apple-ios21 -c -### %s 2>&1 \
14+
// RUN: | FileCheck %s --check-prefix=TARGET
15+
16+
// RUN: not %clang -mtargetos=ios21 -arch arm64 -c -### %s 2>&1 \
17+
// RUN: | FileCheck %s --check-prefix=MTARGET
18+
19+
// RUN: env IPHONEOS_DEPLOYMENT_TARGET=21.0 not %clang -arch arm64 -c -### %s 2>&1 \
20+
// RUN: | FileCheck %s --check-prefix=DEPLOY_VAR
21+
22+
// SDKROOT: error: invalid version number '21.0' inferred from '{{.*}}.sdk'
23+
// SYSROOT: error: invalid version number '21.0' inferred from '{{.*}}.sdk'
24+
// TARGET: error: invalid version number in '-target arm64-apple-ios21'
25+
// MTARGET: error: invalid version number in '-mtargetos=ios21'
26+
// DEPLOY_VAR: error: invalid version number in 'IPHONEOS_DEPLOYMENT_TARGET=21.0'
27+
28+
//--- iPhoneOS21.0.sdk/SDKSettings.json
29+
{"Version":"21.0", "MaximumDeploymentTarget": "21.0.99"}

0 commit comments

Comments
 (0)