Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
[ObjC] Avoid the -Wunguarded-availability warnings for protocol
Browse files Browse the repository at this point in the history
requirements in protocol/class/category declarations

The unguarded availability warnings in the protocol requirements of a protocol
/class/category declaration can be avoided. This matches the behaviour of
Swift's diagnostics. The warnings for deprecated/unavailable protocols are
preserved.

rdar://33156429

Differential Revision: https://reviews.llvm.org/D35061


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@307368 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
hyp committed Jul 7, 2017
1 parent a903edb commit bee6a76
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 3 additions & 2 deletions include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -3900,8 +3900,9 @@ class Sema {

bool CanUseDecl(NamedDecl *D, bool TreatUnavailableAsInvalid);
bool DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc,
const ObjCInterfaceDecl *UnknownObjCClass=nullptr,
bool ObjCPropertyAccess=false);
const ObjCInterfaceDecl *UnknownObjCClass = nullptr,
bool ObjCPropertyAccess = false,
bool AvoidPartialAvailabilityChecks = false);
void NoteDeletedFunction(FunctionDecl *FD);
void NoteDeletedInheritingConstructor(CXXConstructorDecl *CD);
std::string getDeletedOrUnavailableSuffix(const FunctionDecl *FD);
Expand Down
5 changes: 4 additions & 1 deletion lib/Sema/SemaDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ static void diagnoseUseOfProtocols(Sema &TheSema,
// Diagnose availability in the context of the ObjC container.
Sema::ContextRAII SavedContext(TheSema, CD);
for (unsigned i = 0; i < NumProtoRefs; ++i) {
(void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i]);
(void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i],
/*UnknownObjCClass=*/nullptr,
/*ObjCPropertyAccess=*/false,
/*AvoidPartialAvailabilityChecks=*/true);
}
}

Expand Down
11 changes: 8 additions & 3 deletions lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ Sema::ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D,
static void
DiagnoseAvailabilityOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc,
const ObjCInterfaceDecl *UnknownObjCClass,
bool ObjCPropertyAccess) {
bool ObjCPropertyAccess,
bool AvoidPartialAvailabilityChecks = false) {
std::string Message;
AvailabilityResult Result;
const NamedDecl* OffendingDecl;
Expand All @@ -138,6 +139,8 @@ DiagnoseAvailabilityOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc,
return;

if (Result == AR_NotYetIntroduced) {
if (AvoidPartialAvailabilityChecks)
return;
if (S.getCurFunctionOrMethodDecl()) {
S.getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
return;
Expand Down Expand Up @@ -275,7 +278,8 @@ void Sema::MaybeSuggestAddingStaticToDecl(const FunctionDecl *Cur) {
///
bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc,
const ObjCInterfaceDecl *UnknownObjCClass,
bool ObjCPropertyAccess) {
bool ObjCPropertyAccess,
bool AvoidPartialAvailabilityChecks) {
if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) {
// If there were any diagnostics suppressed by template argument deduction,
// emit them now.
Expand Down Expand Up @@ -360,7 +364,8 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc,
}

DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass,
ObjCPropertyAccess);
ObjCPropertyAccess,
AvoidPartialAvailabilityChecks);

DiagnoseUnusedOfDecl(*this, D, Loc);

Expand Down
24 changes: 24 additions & 0 deletions test/SemaObjC/unguarded-availability.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,27 @@ void with_local_struct() {
new_int x; // expected-warning{{'new_int' is partial}}
};
}

// rdar://33156429:
// Avoid the warning on protocol requirements.

AVAILABLE_10_12
@protocol NewProtocol // expected-note {{'NewProtocol' has been explicitly marked partial here}}
@end

@protocol ProtocolWithNewProtocolRequirement <NewProtocol> // expected-note {{annotate 'ProtocolWithNewProtocolRequirement' with an availability attribute to silence}}

@property(copy) id<NewProtocol> prop; // expected-warning {{'NewProtocol' is partial: introduced in macOS 10.12}}

@end

@interface BaseClass
@end

@interface ClassWithNewProtocolRequirement : BaseClass <NewProtocol>

@end

@interface BaseClass (CategoryWithNewProtocolRequirement) <NewProtocol>

@end

0 comments on commit bee6a76

Please sign in to comment.