Skip to content

Commit cc2a00e

Browse files
authored
Merge pull request #5183 from apple/eng/revert_ns_error_enum
Revert back to previous ns_Error_enum behavior
2 parents 03bd949 + c6660e1 commit cc2a00e

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ def ObjCBridgeRelated : InheritableAttr {
20902090
def NSErrorDomain : InheritableAttr {
20912091
let Spellings = [GNU<"ns_error_domain">];
20922092
let Subjects = SubjectList<[Enum], ErrorDiag>;
2093-
let Args = [DeclArgument<Var, "ErrorDomain">];
2093+
let Args = [IdentifierArgument<"ErrorDomain">];
20942094
let Documentation = [NSErrorDomainDocs];
20952095
}
20962096

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,11 @@ static void ProcessAPINotes(Sema &S, Decl *D,
348348

349349
// ns_error_domain
350350
if (auto nsErrorDomain = info.getNSErrorDomain()) {
351-
handleAPINotedAttribute<NSErrorDomainAttr>(
352-
S, D, !nsErrorDomain->empty(), metadata, [&]() -> NSErrorDomainAttr * {
353-
LookupResult lookupResult(
354-
S, DeclarationName(&S.Context.Idents.get(*nsErrorDomain)),
355-
SourceLocation(), Sema::LookupNameKind::LookupOrdinaryName);
356-
S.LookupName(lookupResult, S.TUScope);
357-
auto *VD = lookupResult.getAsSingle<VarDecl>();
358-
359-
if (!VD) {
360-
S.Diag(D->getLocation(), diag::err_nserrordomain_invalid_decl) << 0;
361-
return nullptr;
362-
}
363-
364-
return new (S.Context)
365-
NSErrorDomainAttr(S.Context, getDummyAttrInfo(), VD);
366-
});
351+
handleAPINotedAttribute<NSErrorDomainAttr>(S, D, !nsErrorDomain->empty(),
352+
metadata, [&] {
353+
return new (S.Context) NSErrorDomainAttr(
354+
S.Context, getDummyAttrInfo(), &S.Context.Idents.get(*nsErrorDomain));
355+
});
367356
}
368357

369358
ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(info),

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5944,29 +5944,37 @@ static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
59445944
D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
59455945
}
59465946

5947-
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &AL) {
5948-
auto *E = AL.getArgAsExpr(0);
5949-
auto Loc = E ? E->getBeginLoc() : AL.getLoc();
5950-
5951-
auto *DRE = dyn_cast<DeclRefExpr>(AL.getArgAsExpr(0));
5952-
if (!DRE) {
5953-
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 0;
5947+
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &Attr) {
5948+
if (!isa<TagDecl>(D)) {
5949+
S.Diag(D->getBeginLoc(), diag::err_nserrordomain_invalid_decl)
5950+
<< 0;
59545951
return;
59555952
}
5953+
IdentifierLoc *identLoc =
5954+
Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
5955+
if (!identLoc || !identLoc->Ident) {
5956+
// Try to locate the argument directly
5957+
SourceLocation loc = Attr.getLoc();
5958+
if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0))
5959+
loc = Attr.getArgAsExpr(0)->getBeginLoc();
59565960

5957-
auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
5958-
if (!VD) {
5959-
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 1 << DRE->getDecl();
5961+
S.Diag(loc, diag::err_nserrordomain_invalid_decl) << 0;
59605962
return;
59615963
}
59625964

5963-
if (!isNSStringType(VD->getType(), S.Context) &&
5964-
!isCFStringType(VD->getType(), S.Context)) {
5965-
S.Diag(Loc, diag::err_nserrordomain_wrong_type) << VD;
5965+
// Verify that the identifier is a valid decl in the C decl namespace
5966+
LookupResult lookupResult(S, DeclarationName(identLoc->Ident),
5967+
SourceLocation(),
5968+
Sema::LookupNameKind::LookupOrdinaryName);
5969+
if (!S.LookupName(lookupResult, S.TUScope) ||
5970+
!lookupResult.getAsSingle<VarDecl>()) {
5971+
S.Diag(identLoc->Loc, diag::err_nserrordomain_invalid_decl)
5972+
<< 1 << identLoc->Ident;
59665973
return;
59675974
}
59685975

5969-
D->addAttr(::new (S.Context) NSErrorDomainAttr(S.Context, AL, VD));
5976+
D->addAttr(::new (S.Context)
5977+
NSErrorDomainAttr(S.Context, Attr, identLoc->Ident));
59705978
}
59715979

59725980
static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {

clang/test/Sema/ns_error_enum.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ typedef NS_ERROR_ENUM(unsigned char, MyCFTypedefErrorEnum, MyCFTypedefErrorDomai
5353

5454
extern char *const WrongErrorDomainType;
5555
enum __attribute__((ns_error_domain(WrongErrorDomainType))) MyWrongErrorDomainType { MyWrongErrorDomain };
56-
// expected-error@-1{{domain argument 'WrongErrorDomainType' does not point to an NSString or CFString constant}}
5756

5857
struct __attribute__((ns_error_domain(MyErrorDomain))) MyStructWithErrorDomain {};
5958
// expected-error@-1{{'ns_error_domain' attribute only applies to enums}}
@@ -68,7 +67,7 @@ typedef NS_ERROR_ENUM(unsigned char, MyCFTypedefErrorEnum, MyCFTypedefErrorDomai
6867
// expected-error@-1{{'ns_error_domain' attribute takes one argument}}
6968

7069
typedef NS_ERROR_ENUM(unsigned char, MyErrorEnumInvalid, InvalidDomain) {
71-
// expected-error@-1{{use of undeclared identifier 'InvalidDomain'}}
70+
// expected-error@-1{{domain argument 'InvalidDomain' does not refer to global constant}}
7271
MyErrFirstInvalid,
7372
MyErrSecondInvalid,
7473
};

0 commit comments

Comments
 (0)