Skip to content

Revert back to previous ns_Error_enum behavior #3101

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 1 commit into from
Jul 16, 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
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,7 @@ def ObjCBridgeRelated : InheritableAttr {
def NSErrorDomain : InheritableAttr {
let Spellings = [GNU<"ns_error_domain">];
let Subjects = SubjectList<[Enum], ErrorDiag>;
let Args = [DeclArgument<Var, "ErrorDomain">];
let Args = [IdentifierArgument<"ErrorDomain">];
let Documentation = [NSErrorDomainDocs];
}

Expand Down
21 changes: 5 additions & 16 deletions clang/lib/Sema/SemaAPINotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,11 @@ static void ProcessAPINotes(Sema &S, Decl *D,

// ns_error_domain
if (auto nsErrorDomain = info.getNSErrorDomain()) {
handleAPINotedAttribute<NSErrorDomainAttr>(
S, D, !nsErrorDomain->empty(), metadata, [&]() -> NSErrorDomainAttr * {
LookupResult lookupResult(
S, DeclarationName(&S.Context.Idents.get(*nsErrorDomain)),
SourceLocation(), Sema::LookupNameKind::LookupOrdinaryName);
S.LookupName(lookupResult, S.TUScope);
auto *VD = lookupResult.getAsSingle<VarDecl>();

if (!VD) {
S.Diag(D->getLocation(), diag::err_nserrordomain_invalid_decl) << 0;
return nullptr;
}

return new (S.Context)
NSErrorDomainAttr(S.Context, getDummyAttrInfo(), VD);
});
handleAPINotedAttribute<NSErrorDomainAttr>(S, D, !nsErrorDomain->empty(),
metadata, [&] {
return new (S.Context) NSErrorDomainAttr(
S.Context, getDummyAttrInfo(), &S.Context.Idents.get(*nsErrorDomain));
});
}

ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(info),
Expand Down
36 changes: 22 additions & 14 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5509,29 +5509,37 @@ static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
}

static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &AL) {
auto *E = AL.getArgAsExpr(0);
auto Loc = E ? E->getBeginLoc() : AL.getLoc();

auto *DRE = dyn_cast<DeclRefExpr>(AL.getArgAsExpr(0));
if (!DRE) {
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 0;
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &Attr) {
if (!isa<TagDecl>(D)) {
S.Diag(D->getBeginLoc(), diag::err_nserrordomain_invalid_decl)
<< 0;
return;
}
IdentifierLoc *identLoc =
Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
if (!identLoc || !identLoc->Ident) {
// Try to locate the argument directly
SourceLocation loc = Attr.getLoc();
if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0))
loc = Attr.getArgAsExpr(0)->getBeginLoc();

auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
if (!VD) {
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 1 << DRE->getDecl();
S.Diag(loc, diag::err_nserrordomain_invalid_decl) << 0;
return;
}

if (!isNSStringType(VD->getType(), S.Context) &&
!isCFStringType(VD->getType(), S.Context)) {
S.Diag(Loc, diag::err_nserrordomain_wrong_type) << VD;
// Verify that the identifier is a valid decl in the C decl namespace
LookupResult lookupResult(S, DeclarationName(identLoc->Ident),
SourceLocation(),
Sema::LookupNameKind::LookupOrdinaryName);
if (!S.LookupName(lookupResult, S.TUScope) ||
!lookupResult.getAsSingle<VarDecl>()) {
S.Diag(identLoc->Loc, diag::err_nserrordomain_invalid_decl)
<< 1 << identLoc->Ident;
return;
}

D->addAttr(::new (S.Context) NSErrorDomainAttr(S.Context, AL, VD));
D->addAttr(::new (S.Context)
NSErrorDomainAttr(S.Context, Attr, identLoc->Ident));
}

static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Expand Down
3 changes: 1 addition & 2 deletions clang/test/Sema/ns_error_enum.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ typedef NS_ERROR_ENUM(unsigned char, MyCFTypedefErrorEnum, MyCFTypedefErrorDomai

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

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

typedef NS_ERROR_ENUM(unsigned char, MyErrorEnumInvalid, InvalidDomain) {
// expected-error@-1{{use of undeclared identifier 'InvalidDomain'}}
// expected-error@-1{{domain argument 'InvalidDomain' does not refer to global constant}}
MyErrFirstInvalid,
MyErrSecondInvalid,
};
Expand Down