Skip to content

Commit 198503d

Browse files
authored
Merge pull request #2635 from apple/eng/revert-alexis
Revert Alexis' ns_error_domain changes from swift/main
2 parents 5f17b63 + c4a4358 commit 198503d

File tree

5 files changed

+29
-35
lines changed

5 files changed

+29
-35
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ def ObjCBridgeRelated : InheritableAttr {
19441944
def NSErrorDomain : InheritableAttr {
19451945
let Spellings = [GNU<"ns_error_domain">];
19461946
let Subjects = SubjectList<[Enum], ErrorDiag>;
1947-
let Args = [DeclArgument<Var, "ErrorDomain">];
1947+
let Args = [IdentifierArgument<"ErrorDomain">];
19481948
let Documentation = [NSErrorDomainDocs];
19491949
}
19501950

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9762,8 +9762,6 @@ def err_nsreturns_retained_attribute_mismatch : Error<
97629762
" attributes">;
97639763
def err_nserrordomain_invalid_decl : Error<
97649764
"domain argument %select{|%1 }0does not refer to global constant">;
9765-
def warn_nserrordomain_invalid_decl : Warning<
9766-
"domain argument %select{|%1 }0does not refer to global constant">;
97679765
def err_nserrordomain_wrong_type : Error<
97689766
"domain argument %0 does not point to an NSString or CFString constant">;
97699767

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::warn_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
@@ -5545,29 +5545,37 @@ static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
55455545
D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
55465546
}
55475547

5548-
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &AL) {
5549-
auto *E = AL.getArgAsExpr(0);
5550-
auto Loc = E ? E->getBeginLoc() : AL.getLoc();
5551-
5552-
auto *DRE = dyn_cast<DeclRefExpr>(AL.getArgAsExpr(0));
5553-
if (!DRE) {
5554-
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 0;
5548+
static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &Attr) {
5549+
if (!isa<TagDecl>(D)) {
5550+
S.Diag(D->getBeginLoc(), diag::err_nserrordomain_invalid_decl)
5551+
<< 0;
55555552
return;
55565553
}
5554+
IdentifierLoc *identLoc =
5555+
Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
5556+
if (!identLoc || !identLoc->Ident) {
5557+
// Try to locate the argument directly
5558+
SourceLocation loc = Attr.getLoc();
5559+
if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0))
5560+
loc = Attr.getArgAsExpr(0)->getBeginLoc();
55575561

5558-
auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
5559-
if (!VD) {
5560-
S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 1 << DRE->getDecl();
5562+
S.Diag(loc, diag::err_nserrordomain_invalid_decl) << 0;
55615563
return;
55625564
}
55635565

5564-
if (!isNSStringType(VD->getType(), S.Context) &&
5565-
!isCFStringType(VD->getType(), S.Context)) {
5566-
S.Diag(Loc, diag::err_nserrordomain_wrong_type) << VD;
5566+
// Verify that the identifier is a valid decl in the C decl namespace
5567+
LookupResult lookupResult(S, DeclarationName(identLoc->Ident),
5568+
SourceLocation(),
5569+
Sema::LookupNameKind::LookupOrdinaryName);
5570+
if (!S.LookupName(lookupResult, S.TUScope) ||
5571+
!lookupResult.getAsSingle<VarDecl>()) {
5572+
S.Diag(identLoc->Loc, diag::err_nserrordomain_invalid_decl)
5573+
<< 1 << identLoc->Ident;
55675574
return;
55685575
}
55695576

5570-
D->addAttr(::new (S.Context) NSErrorDomainAttr(S.Context, AL, VD));
5577+
D->addAttr(::new (S.Context)
5578+
NSErrorDomainAttr(S.Context, Attr, identLoc->Ident));
55715579
}
55725580

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