[cfe-commits] r64546 - in /cfe/trunk: include/clang/AST/Attr.h include/clang/Basic/DiagnosticSemaKinds.def include/clang/Parse/AttributeList.h lib/Parse/AttributeList.cpp lib/Sema/SemaDeclAttr.cpp test/SemaObjC/attr-objc-exception.m

Chris Lattner sabre at nondot.org
Sat Feb 14 00:09:34 PST 2009


Author: lattner
Date: Sat Feb 14 02:09:34 2009
New Revision: 64546

URL: http://llvm.org/viewvc/llvm-project?rev=64546&view=rev
Log:
add parser and type checking support for attribute((objc_exception)).
We don't have "zero cost" exceptions for ObjC yet, so there is no codegen
support required.

Added:
    cfe/trunk/test/SemaObjC/attr-objc-exception.m
Modified:
    cfe/trunk/include/clang/AST/Attr.h
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
    cfe/trunk/include/clang/Parse/AttributeList.h
    cfe/trunk/lib/Parse/AttributeList.cpp
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp

Modified: cfe/trunk/include/clang/AST/Attr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Attr.h?rev=64546&r1=64545&r2=64546&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Sat Feb 14 02:09:34 2009
@@ -43,6 +43,7 @@
     NoThrow,
     ObjCGC,
     ObjCNSObject,
+    ObjCException,
     Overloadable, // Clang-specific
     Packed,
     Section,
@@ -290,7 +291,6 @@
   NoThrowAttr() : Attr(NoThrow) {}
 
   // Implement isa/cast/dyncast/etc.
-
   static bool classof(const Attr *A) { return A->getKind() == NoThrow; }
   static bool classof(const NoThrowAttr *A) { return true; }
 };
@@ -300,7 +300,6 @@
   ConstAttr() : Attr(Const) {}
 
   // Implement isa/cast/dyncast/etc.
-
   static bool classof(const Attr *A) { return A->getKind() == Const; }
   static bool classof(const ConstAttr *A) { return true; }
 };
@@ -310,7 +309,6 @@
   PureAttr() : Attr(Pure) {}
 
   // Implement isa/cast/dyncast/etc.
-
   static bool classof(const Attr *A) { return A->getKind() == Pure; }
   static bool classof(const PureAttr *A) { return true; }
 };
@@ -322,12 +320,11 @@
   NonNullAttr(unsigned* arg_nums = 0, unsigned size = 0) : Attr(NonNull),
     ArgNums(0), Size(0) {
   
-    if (size) {
-      assert (arg_nums);
-      ArgNums = new unsigned[size];
-      Size = size;
-      memcpy(ArgNums, arg_nums, sizeof(*ArgNums)*size);
-    }
+    if (size == 0) return;
+    assert(arg_nums);
+    ArgNums = new unsigned[size];
+    Size = size;
+    memcpy(ArgNums, arg_nums, sizeof(*ArgNums)*size);
   }
   
   virtual ~NonNullAttr() {
@@ -459,6 +456,17 @@
 static bool classof(const ObjCNSObjectAttr *A) { return true; }
 };
   
+  
+class ObjCExceptionAttr : public Attr {
+public:
+  ObjCExceptionAttr() : Attr(ObjCException) {}
+  
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Attr *A) { return A->getKind() == ObjCException; }
+  static bool classof(const ObjCExceptionAttr *A) { return true; }
+};
+  
+  
 class OverloadableAttr : public Attr {
 public:
   OverloadableAttr() : Attr(Overloadable) { }

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def?rev=64546&r1=64545&r2=64546&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Sat Feb 14 02:09:34 2009
@@ -314,6 +314,8 @@
      "'%0' attribute requires parameter %1 to be a string")
 DIAG(err_attribute_argument_out_of_bounds, ERROR,
      "'%0' attribute parameter %1 is out of bounds")
+DIAG(err_attribute_requires_objc_interface, ERROR,
+     "attribute may only be applied to an Objective-C interface")
 DIAG(err_nonnull_pointers_only, ERROR,
     "nonnull attribute only applies to pointer arguments")
 DIAG(err_format_strftime_third_parameter, ERROR,

Modified: cfe/trunk/include/clang/Parse/AttributeList.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/AttributeList.h?rev=64546&r1=64545&r2=64546&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/AttributeList.h (original)
+++ cfe/trunk/include/clang/Parse/AttributeList.h Sat Feb 14 02:09:34 2009
@@ -75,6 +75,7 @@
     AT_warn_unused_result,
     AT_weak,
     AT_objc_gc,
+    AT_objc_exception,
     AT_blocks,
     AT_sentinel,
     AT_const,

Modified: cfe/trunk/lib/Parse/AttributeList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/AttributeList.cpp?rev=64546&r1=64545&r2=64546&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/AttributeList.cpp (original)
+++ cfe/trunk/lib/Parse/AttributeList.cpp Sat Feb 14 02:09:34 2009
@@ -110,6 +110,9 @@
     if (!memcmp(Str, "address_space", 13)) return AT_address_space;
     if (!memcmp(Str, "always_inline", 13)) return AT_always_inline;
     break;
+  case 14:
+    if (!memcmp(Str, "objc_exception", 14)) return AT_objc_exception;
+    break;
   case 15:
     if (!memcmp(Str, "ext_vector_type", 15)) return AT_ext_vector_type;
     break;

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=64546&r1=64545&r2=64546&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Sat Feb 14 02:09:34 2009
@@ -568,7 +568,7 @@
   d->addAttr(new VisibilityAttr(type));
 }
 
-static void HandleObjCGCAttr(Decl *d, const AttributeList &Attr, Sema &S) {
+static void HandleObjCGCAttr(Decl *D, const AttributeList &Attr, Sema &S) {
   if (!Attr.getParameterName()) {    
     S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
       << "objc_gc" << 1;
@@ -579,11 +579,10 @@
     S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
     return;
   }
-  
-  
+
   ObjCGCAttr::GCAttrTypes type;
   if (Attr.getParameterName()->isStr("weak")) {
-    if (isa<FieldDecl>(d) && !isa<ObjCIvarDecl>(d))
+    if (isa<FieldDecl>(D) && !isa<ObjCIvarDecl>(D))
       S.Diag(Attr.getLoc(), diag::warn_attribute_weak_on_field);
     type = ObjCGCAttr::Weak;
   }
@@ -595,15 +594,31 @@
     return;
   }
   
-  d->addAttr(new ObjCGCAttr(type));
+  D->addAttr(new ObjCGCAttr(type));
+}
+
+static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
+                                    Sema &S) {
+  if (Attr.getNumArgs() != 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
+    return;
+  }
+  
+  ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
+  if (OCI == 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
+    return;
+  }
+  
+  D->addAttr(new ObjCExceptionAttr());
 }
 
-static void HandleObjCNSObject(Decl *d, const AttributeList &Attr, Sema &S) {
+static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
   if (Attr.getNumArgs() != 0) {
     S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
     return;
   }
-  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(d)) {
+  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
     QualType T = TD->getUnderlyingType();
     if (!T->isPointerType() ||
         !T->getAsPointerType()->getPointeeType()->isRecordType()) {
@@ -611,7 +626,7 @@
       return;
     }
   }
-  d->addAttr(new ObjCNSObjectAttr);
+  D->addAttr(new ObjCNSObjectAttr);
 }
 
 static void 
@@ -1406,6 +1421,9 @@
     HandleTransparentUnionAttr(D, Attr, S);
     break;
   case AttributeList::AT_objc_gc:     HandleObjCGCAttr    (D, Attr, S); break;
+  case AttributeList::AT_objc_exception:
+    HandleObjCExceptionAttr(D, Attr, S);
+    break;
   case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
   case AttributeList::AT_nsobject:    HandleObjCNSObject  (D, Attr, S); break;
   case AttributeList::AT_blocks:      HandleBlocksAttr    (D, Attr, S); break;

Added: cfe/trunk/test/SemaObjC/attr-objc-exception.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/attr-objc-exception.m?rev=64546&view=auto

==============================================================================
--- cfe/trunk/test/SemaObjC/attr-objc-exception.m (added)
+++ cfe/trunk/test/SemaObjC/attr-objc-exception.m Sat Feb 14 02:09:34 2009
@@ -0,0 +1,16 @@
+// RUN: clang %s -fsyntax-only -verify
+
+__attribute__((__objc_exception__))
+ at interface NSException {
+  int x;
+}
+
+ at end
+
+
+__attribute__((__objc_exception__)) // expected-error {{attribute may only be applied to an Objective-C interface}}
+int X;
+
+__attribute__((__objc_exception__)) // expected-error {{attribute may only be applied to an Objective-C interface}}
+void foo();
+





More information about the cfe-commits mailing list