Skip to content

Commit 3a71538

Browse files
committed
Revert back to previous ns_Error_enum behavior
(cherry picked from commit 759ba98)
1 parent dbc6d74 commit 3a71538

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
@@ -2056,7 +2056,7 @@ def ObjCBridgeRelated : InheritableAttr {
20562056
def NSErrorDomain : InheritableAttr {
20572057
let Spellings = [GNU<"ns_error_domain">];
20582058
let Subjects = SubjectList<[Enum], ErrorDiag>;
2059-
let Args = [DeclArgument<Var, "ErrorDomain">];
2059+
let Args = [IdentifierArgument<"ErrorDomain">];
20602060
let Documentation = [NSErrorDomainDocs];
20612061
}
20622062

clang/lib/Sema/SemaAPINotes.cpp

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

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

368357
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
@@ -5656,29 +5656,37 @@ static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
56565656
D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
56575657
}
56585658

5659-
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &AL) {
5660-
auto *E = AL.getArgAsExpr(0);
5661-
auto Loc = E ? E->getBeginLoc() : AL.getLoc();
5662-
5663-
auto *DRE = dyn_cast<DeclRefExpr>(AL.getArgAsExpr(0));
5664-
if (!DRE) {
5665-
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 0;
5659+
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &Attr) {
5660+
if (!isa<TagDecl>(D)) {
5661+
S.Diag(D->getBeginLoc(), diag::err_nserrordomain_invalid_decl)
5662+
<< 0;
56665663
return;
56675664
}
5665+
IdentifierLoc *identLoc =
5666+
Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
5667+
if (!identLoc || !identLoc->Ident) {
5668+
// Try to locate the argument directly
5669+
SourceLocation loc = Attr.getLoc();
5670+
if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0))
5671+
loc = Attr.getArgAsExpr(0)->getBeginLoc();
56685672

5669-
auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
5670-
if (!VD) {
5671-
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 1 << DRE->getDecl();
5673+
S.Diag(loc, diag::err_nserrordomain_invalid_decl) << 0;
56725674
return;
56735675
}
56745676

5675-
if (!isNSStringType(VD->getType(), S.Context) &&
5676-
!isCFStringType(VD->getType(), S.Context)) {
5677-
S.Diag(Loc, diag::err_nserrordomain_wrong_type) << VD;
5677+
// Verify that the identifier is a valid decl in the C decl namespace
5678+
LookupResult lookupResult(S, DeclarationName(identLoc->Ident),
5679+
SourceLocation(),
5680+
Sema::LookupNameKind::LookupOrdinaryName);
5681+
if (!S.LookupName(lookupResult, S.TUScope) ||
5682+
!lookupResult.getAsSingle<VarDecl>()) {
5683+
S.Diag(identLoc->Loc, diag::err_nserrordomain_invalid_decl)
5684+
<< 1 << identLoc->Ident;
56785685
return;
56795686
}
56805687

5681-
D->addAttr(::new (S.Context) NSErrorDomainAttr(S.Context, AL, VD));
5688+
D->addAttr(::new (S.Context)
5689+
NSErrorDomainAttr(S.Context, Attr, identLoc->Ident));
56825690
}
56835691

56845692
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)