[PATCH] D84005: Introduce ns_error_domain attribute.

Michael Forster via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 17 04:38:09 PDT 2020


MForster updated this revision to Diff 278724.
MForster marked 4 inline comments as done.
MForster added a comment.

- Address review comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D84005/new/

https://reviews.llvm.org/D84005

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/Sema/SemaDeclAttr.cpp


Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -5327,9 +5327,9 @@
   D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs));
 }
 
-static void handleNSErrorDomain(Sema &S, Decl *D, const AttributeList &Attr) {
+static void handleNSErrorDomain(Sema &S, Decl *D, const ParsedAttr &Attr) {
   if (!isa<TagDecl>(D)) {
-    S.Diag(D->getLocStart(), diag::err_nserrordomain_not_tagdecl)
+    S.Diag(D->getBeginLoc(), diag::err_nserrordomain_not_tagdecl)
         << S.getLangOpts().CPlusPlus;
     return;
   }
@@ -5339,7 +5339,7 @@
     // Try to locate the argument directly
     SourceLocation loc = Attr.getLoc();
     if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0))
-      loc = Attr.getArgAsExpr(0)->getLocStart();
+      loc = Attr.getArgAsExpr(0)->getBeginLoc();
 
     S.Diag(loc, diag::err_nserrordomain_requires_identifier);
     return;
@@ -5357,8 +5357,7 @@
   }
 
   D->addAttr(::new (S.Context)
-                 NSErrorDomainAttr(Attr.getRange(), S.Context, identLoc->Ident,
-                                   Attr.getAttributeSpellingListIndex()));
+                 NSErrorDomainAttr(S.Context, Attr, identLoc->Ident));
 }
 
 static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
@@ -7132,8 +7131,8 @@
   case ParsedAttr::AT_ObjCBoxable:
     handleObjCBoxable(S, D, AL);
     break;
-  case AttributeList::AT_NSErrorDomain:
-    handleNSErrorDomain(S, D, Attr);
+  case ParsedAttr::AT_NSErrorDomain:
+    handleNSErrorDomain(S, D, AL);
     break;
   case ParsedAttr::AT_CFAuditedTransfer:
     handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
Index: clang/include/clang/Basic/AttrDocs.td
===================================================================
--- clang/include/clang/Basic/AttrDocs.td
+++ clang/include/clang/Basic/AttrDocs.td
@@ -3314,9 +3314,31 @@
 }
 
 def NSErrorDomainDocs : Documentation {
-  let Category = DocCatFunction;
+  let Category = DocCatType;
   let Content = [{
-The ``ns_error_domain`` attribute indicates a global constant representing the error domain.
+In Cocoa frameworks in Objective-C, one can group related error codes in enums
+and categorize these enums with error domains.
+
+The ``ns_error_domain`` attribute indicates a global constant representing the
+error domain that error codes belong to. This attribute is attached to enums
+that describe error codes.
+
+This metadata is useful for documentation purposes, for static analysis, and for
+improving interoperability between Objective-C and Swift. It is not used for
+code generation in Objective-C.
+
+For example:
+
+  .. code-block:: objc
+
+    #define NS_ERROR_ENUM(_type, _name, _domain)  \
+      enum _name : _type _name; enum __attribute__((ns_error_domain(_domain))) _name : _type
+
+    const char *MyErrorDomain;
+    typedef NS_ERROR_ENUM(unsigned char, MyErrorEnum, MyErrorDomain) {
+      MyErrFirst,
+      MyErrSecond,
+    };
   }];
 }
 
Index: clang/include/clang/Basic/Attr.td
===================================================================
--- clang/include/clang/Basic/Attr.td
+++ clang/include/clang/Basic/Attr.td
@@ -1858,6 +1858,7 @@
 
 def NSErrorDomain : Attr {
   let Spellings = [GNU<"ns_error_domain">];
+  let Subjects = SubjectList<[Enum]>;
   let Args = [IdentifierArgument<"ErrorDomain">];
   let Documentation = [NSErrorDomainDocs];
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84005.278724.patch
Type: text/x-patch
Size: 3468 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200717/8589d66a/attachment.bin>


More information about the cfe-commits mailing list