Skip to content

Commit f3a8c9c

Browse files
authored
Merge pull request #2642 from apple/eng/2021al3
Cherry-pick from main for x2
2 parents 9cc7b9a + dd3ac30 commit f3a8c9c

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,15 @@ getDeploymentTargetFromEnvironmentVariables(const Driver &TheDriver,
16901690
return None;
16911691
}
16921692

1693+
/// Returns the SDK name without the optional prefix that ends with a '.' or an
1694+
/// empty string otherwise.
1695+
static StringRef dropSDKNamePrefix(StringRef SDKName) {
1696+
size_t PrefixPos = SDKName.find('.');
1697+
if (PrefixPos == StringRef::npos)
1698+
return "";
1699+
return SDKName.substr(PrefixPos + 1);
1700+
}
1701+
16931702
/// Tries to infer the deployment target from the SDK specified by -isysroot
16941703
/// (or SDKROOT). Uses the version specified in the SDKSettings.json file if
16951704
/// it's available.
@@ -1719,22 +1728,29 @@ inferDeploymentTargetFromSDK(DerivedArgList &Args,
17191728
if (Version.empty())
17201729
return None;
17211730

1722-
if (SDK.startswith("iPhoneOS") || SDK.startswith("iPhoneSimulator"))
1723-
return DarwinPlatform::createFromSDK(
1724-
Darwin::IPhoneOS, Version,
1725-
/*IsSimulator=*/SDK.startswith("iPhoneSimulator"));
1726-
else if (SDK.startswith("MacOSX"))
1727-
return DarwinPlatform::createFromSDK(Darwin::MacOS,
1728-
getSystemOrSDKMacOSVersion(Version));
1729-
else if (SDK.startswith("WatchOS") || SDK.startswith("WatchSimulator"))
1730-
return DarwinPlatform::createFromSDK(
1731-
Darwin::WatchOS, Version,
1732-
/*IsSimulator=*/SDK.startswith("WatchSimulator"));
1733-
else if (SDK.startswith("AppleTVOS") || SDK.startswith("AppleTVSimulator"))
1734-
return DarwinPlatform::createFromSDK(
1735-
Darwin::TvOS, Version,
1736-
/*IsSimulator=*/SDK.startswith("AppleTVSimulator"));
1737-
return None;
1731+
auto CreatePlatformFromSDKName =
1732+
[&](StringRef SDK) -> Optional<DarwinPlatform> {
1733+
if (SDK.startswith("iPhoneOS") || SDK.startswith("iPhoneSimulator"))
1734+
return DarwinPlatform::createFromSDK(
1735+
Darwin::IPhoneOS, Version,
1736+
/*IsSimulator=*/SDK.startswith("iPhoneSimulator"));
1737+
else if (SDK.startswith("MacOSX"))
1738+
return DarwinPlatform::createFromSDK(Darwin::MacOS,
1739+
getSystemOrSDKMacOSVersion(Version));
1740+
else if (SDK.startswith("WatchOS") || SDK.startswith("WatchSimulator"))
1741+
return DarwinPlatform::createFromSDK(
1742+
Darwin::WatchOS, Version,
1743+
/*IsSimulator=*/SDK.startswith("WatchSimulator"));
1744+
else if (SDK.startswith("AppleTVOS") || SDK.startswith("AppleTVSimulator"))
1745+
return DarwinPlatform::createFromSDK(
1746+
Darwin::TvOS, Version,
1747+
/*IsSimulator=*/SDK.startswith("AppleTVSimulator"));
1748+
return None;
1749+
};
1750+
if (auto Result = CreatePlatformFromSDKName(SDK))
1751+
return Result;
1752+
// The SDK can be an SDK variant with a name like `<prefix>.<platform>`.
1753+
return CreatePlatformFromSDKName(dropSDKNamePrefix(SDK));
17381754
}
17391755

17401756
std::string getOSVersion(llvm::Triple::OSType OS, const llvm::Triple &Triple,
@@ -2000,7 +2016,9 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
20002016
StringRef SDKName = SDK.slice(0, StartVer);
20012017
// Don't warn about the macabi SDK.
20022018
// FIXME: Can we warn here?
2003-
if (!SDKName.startswith(getPlatformFamily()) && Environment != MacABI)
2019+
if (!SDKName.startswith(getPlatformFamily()) &&
2020+
!dropSDKNamePrefix(SDKName).startswith(getPlatformFamily())
2021+
&& Environment != MacABI)
20042022
getDriver().Diag(diag::warn_incompatible_sysroot)
20052023
<< SDKName << getPlatformFamily();
20062024
}

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ static bool isInstanceMethod(const Decl *D) {
153153
return false;
154154
}
155155

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

165166
IdentifierInfo* ClsName = Cls->getIdentifier();
166167

168+
if (AllowNSAttributedString &&
169+
ClsName == &Ctx.Idents.get("NSAttributedString"))
170+
return true;
167171
// FIXME: Should we walk the chain of classes?
168172
return ClsName == &Ctx.Idents.get("NSString") ||
169173
ClsName == &Ctx.Idents.get("NSMutableString");
@@ -3328,7 +3332,7 @@ static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
33283332
return;
33293333
}
33303334
Ty = getFunctionOrMethodResultType(D);
3331-
if (!isNSStringType(Ty, S.Context) &&
3335+
if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true) &&
33323336
!isCFStringType(Ty, S.Context) &&
33333337
(!Ty->isPointerType() ||
33343338
!Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: rm -rf %t.dir
2+
// RUN: mkdir -p %t.dir
3+
4+
// RUN: rm -rf %t.dir/prefix.iPhoneOS12.0.0.sdk
5+
// RUN: mkdir -p %t.dir/prefix.iPhoneOS12.0.0.sdk
6+
// RUN: %clang -c -isysroot %t.dir/prefix.iPhoneOS12.0.0.sdk -target arm64-apple-darwin %s -### 2>&1 | FileCheck %s
7+
// RUN: env SDKROOT=%t.dir/prefix.iPhoneOS12.0.0.sdk %clang -c -target arm64-apple-darwin %s -### 2>&1 | FileCheck %s
8+
//
9+
// CHECK-NOT: warning: using sysroot for
10+
// CHECK: "-triple" "arm64-apple-ios12.0.0"

clang/test/SemaObjC/format-arg-attribute.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clang_cc1 -verify -fsyntax-only %s
22

33
@class NSString;
4+
@class NSAttributedString;
45

56
extern NSString *fa2 (const NSString *) __attribute__((format_arg(1)));
67
extern NSString *fa3 (NSString *) __attribute__((format_arg(1)));
@@ -25,3 +26,5 @@
2526
extern int fi3 (const NSString *) __attribute__((format_arg(1))); // expected-error {{function does not return NSString}}
2627
extern NSString *fi4 (const NSString *) __attribute__((format_arg(1)));
2728
extern NSString *fi5 (const NSString *) __attribute__((format_arg(1)));
29+
30+
extern NSAttributedString *fattrs (const NSString *) __attribute__((format_arg(1)));

0 commit comments

Comments
 (0)