Skip to content

Cherry-pick from main for x2 #2642

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 2 commits into from
Mar 10, 2021
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
52 changes: 35 additions & 17 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,15 @@ getDeploymentTargetFromEnvironmentVariables(const Driver &TheDriver,
return None;
}

/// Returns the SDK name without the optional prefix that ends with a '.' or an
/// empty string otherwise.
static StringRef dropSDKNamePrefix(StringRef SDKName) {
size_t PrefixPos = SDKName.find('.');
if (PrefixPos == StringRef::npos)
return "";
return SDKName.substr(PrefixPos + 1);
}

/// Tries to infer the deployment target from the SDK specified by -isysroot
/// (or SDKROOT). Uses the version specified in the SDKSettings.json file if
/// it's available.
Expand Down Expand Up @@ -1719,22 +1728,29 @@ inferDeploymentTargetFromSDK(DerivedArgList &Args,
if (Version.empty())
return None;

if (SDK.startswith("iPhoneOS") || SDK.startswith("iPhoneSimulator"))
return DarwinPlatform::createFromSDK(
Darwin::IPhoneOS, Version,
/*IsSimulator=*/SDK.startswith("iPhoneSimulator"));
else if (SDK.startswith("MacOSX"))
return DarwinPlatform::createFromSDK(Darwin::MacOS,
getSystemOrSDKMacOSVersion(Version));
else if (SDK.startswith("WatchOS") || SDK.startswith("WatchSimulator"))
return DarwinPlatform::createFromSDK(
Darwin::WatchOS, Version,
/*IsSimulator=*/SDK.startswith("WatchSimulator"));
else if (SDK.startswith("AppleTVOS") || SDK.startswith("AppleTVSimulator"))
return DarwinPlatform::createFromSDK(
Darwin::TvOS, Version,
/*IsSimulator=*/SDK.startswith("AppleTVSimulator"));
return None;
auto CreatePlatformFromSDKName =
[&](StringRef SDK) -> Optional<DarwinPlatform> {
if (SDK.startswith("iPhoneOS") || SDK.startswith("iPhoneSimulator"))
return DarwinPlatform::createFromSDK(
Darwin::IPhoneOS, Version,
/*IsSimulator=*/SDK.startswith("iPhoneSimulator"));
else if (SDK.startswith("MacOSX"))
return DarwinPlatform::createFromSDK(Darwin::MacOS,
getSystemOrSDKMacOSVersion(Version));
else if (SDK.startswith("WatchOS") || SDK.startswith("WatchSimulator"))
return DarwinPlatform::createFromSDK(
Darwin::WatchOS, Version,
/*IsSimulator=*/SDK.startswith("WatchSimulator"));
else if (SDK.startswith("AppleTVOS") || SDK.startswith("AppleTVSimulator"))
return DarwinPlatform::createFromSDK(
Darwin::TvOS, Version,
/*IsSimulator=*/SDK.startswith("AppleTVSimulator"));
return None;
};
if (auto Result = CreatePlatformFromSDKName(SDK))
return Result;
// The SDK can be an SDK variant with a name like `<prefix>.<platform>`.
return CreatePlatformFromSDKName(dropSDKNamePrefix(SDK));
}

std::string getOSVersion(llvm::Triple::OSType OS, const llvm::Triple &Triple,
Expand Down Expand Up @@ -2000,7 +2016,9 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
StringRef SDKName = SDK.slice(0, StartVer);
// Don't warn about the macabi SDK.
// FIXME: Can we warn here?
if (!SDKName.startswith(getPlatformFamily()) && Environment != MacABI)
if (!SDKName.startswith(getPlatformFamily()) &&
!dropSDKNamePrefix(SDKName).startswith(getPlatformFamily())
&& Environment != MacABI)
getDriver().Diag(diag::warn_incompatible_sysroot)
<< SDKName << getPlatformFamily();
}
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ static bool isInstanceMethod(const Decl *D) {
return false;
}

static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
static inline bool isNSStringType(QualType T, ASTContext &Ctx,
bool AllowNSAttributedString = false) {
const auto *PT = T->getAs<ObjCObjectPointerType>();
if (!PT)
return false;
Expand All @@ -164,6 +165,9 @@ static inline bool isNSStringType(QualType T, ASTContext &Ctx) {

IdentifierInfo* ClsName = Cls->getIdentifier();

if (AllowNSAttributedString &&
ClsName == &Ctx.Idents.get("NSAttributedString"))
return true;
// FIXME: Should we walk the chain of classes?
return ClsName == &Ctx.Idents.get("NSString") ||
ClsName == &Ctx.Idents.get("NSMutableString");
Expand Down Expand Up @@ -3328,7 +3332,7 @@ static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
return;
}
Ty = getFunctionOrMethodResultType(D);
if (!isNSStringType(Ty, S.Context) &&
if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true) &&
!isCFStringType(Ty, S.Context) &&
(!Ty->isPointerType() ||
!Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Driver/darwin-sdk-with-prefix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: rm -rf %t.dir
// RUN: mkdir -p %t.dir

// RUN: rm -rf %t.dir/prefix.iPhoneOS12.0.0.sdk
// RUN: mkdir -p %t.dir/prefix.iPhoneOS12.0.0.sdk
// RUN: %clang -c -isysroot %t.dir/prefix.iPhoneOS12.0.0.sdk -target arm64-apple-darwin %s -### 2>&1 | FileCheck %s
// RUN: env SDKROOT=%t.dir/prefix.iPhoneOS12.0.0.sdk %clang -c -target arm64-apple-darwin %s -### 2>&1 | FileCheck %s
//
// CHECK-NOT: warning: using sysroot for
// CHECK: "-triple" "arm64-apple-ios12.0.0"
3 changes: 3 additions & 0 deletions clang/test/SemaObjC/format-arg-attribute.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %clang_cc1 -verify -fsyntax-only %s

@class NSString;
@class NSAttributedString;

extern NSString *fa2 (const NSString *) __attribute__((format_arg(1)));
extern NSString *fa3 (NSString *) __attribute__((format_arg(1)));
Expand All @@ -25,3 +26,5 @@
extern int fi3 (const NSString *) __attribute__((format_arg(1))); // expected-error {{function does not return NSString}}
extern NSString *fi4 (const NSString *) __attribute__((format_arg(1)));
extern NSString *fi5 (const NSString *) __attribute__((format_arg(1)));

extern NSAttributedString *fattrs (const NSString *) __attribute__((format_arg(1)));