[cfe-commits] r56309 - in /cfe/trunk: include/clang/AST/Attr.h include/clang/Parse/AttributeList.h lib/Parse/AttributeList.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/block-byref-args.c test/Sema/block-storageclass.c

Steve Naroff snaroff at apple.com
Thu Sep 18 09:44:58 PDT 2008


Author: snaroff
Date: Thu Sep 18 11:44:58 2008
New Revision: 56309

URL: http://llvm.org/viewvc/llvm-project?rev=56309&view=rev
Log:
Finish pushing blocks attribute through the clang attribute machinery.
Also added a couple simple tests from the "gcc.apple" test suite.

Added:
    cfe/trunk/test/Sema/block-byref-args.c
    cfe/trunk/test/Sema/block-storageclass.c
Modified:
    cfe/trunk/include/clang/AST/Attr.h
    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=56309&r1=56308&r2=56309&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Thu Sep 18 11:44:58 2008
@@ -46,7 +46,8 @@
     TransparentUnion,
     Unused,    
     Visibility,
-    Weak
+    Weak,
+    Blocks
   };
     
 private:
@@ -363,6 +364,24 @@
   static bool classof(const ObjCGCAttr *A) { return true; }
 };
 
+class BlocksAttr : public Attr {
+public:
+  enum BlocksAttrTypes {
+    ByRef = 0
+  };
+private:
+  BlocksAttrTypes BlocksAttrType;
+public:
+  BlocksAttr(BlocksAttrTypes t) : Attr(Blocks), BlocksAttrType(t) {}
+
+  BlocksAttrTypes getType() const { return BlocksAttrType; }
+
+  // Implement isa/cast/dyncast/etc.
+
+  static bool classof(const Attr *A) { return A->getKind() == Blocks; }
+  static bool classof(const ObjCGCAttr *A) { return true; }
+};
+
 }  // end namespace clang
 
 #endif

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

==============================================================================
--- cfe/trunk/include/clang/Parse/AttributeList.h (original)
+++ cfe/trunk/include/clang/Parse/AttributeList.h Thu Sep 18 11:44:58 2008
@@ -71,6 +71,7 @@
     AT_warn_unused_result,
     AT_weak,
     AT_objc_gc,
+    AT_blocks,
     UnknownAttribute
   };
   

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

==============================================================================
--- cfe/trunk/lib/Parse/AttributeList.cpp (original)
+++ cfe/trunk/lib/Parse/AttributeList.cpp Thu Sep 18 11:44:58 2008
@@ -65,6 +65,7 @@
     if (!memcmp(Str, "malloc", 6)) return AT_malloc;
     if (!memcmp(Str, "format", 6)) return AT_format;
     if (!memcmp(Str, "unused", 6)) return AT_unused;
+    if (!memcmp(Str, "blocks", 6)) return AT_blocks;
     break;
   case 7:
     if (!memcmp(Str, "aligned", 7)) return AT_aligned;

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Thu Sep 18 11:44:58 2008
@@ -509,6 +509,34 @@
   d->addAttr(new ObjCGCAttr(type));
 }
 
+static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
+  if (!Attr.getParameterName()) {    
+    S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string,
+           "blocks", std::string("1"));
+    return;
+  }
+  
+  if (Attr.getNumArgs() != 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
+           std::string("1"));
+    return;
+  }
+  const char *TypeStr = Attr.getParameterName()->getName();
+  unsigned TypeLen = Attr.getParameterName()->getLength();
+  
+  BlocksAttr::BlocksAttrTypes type;
+  
+  if (TypeLen == 5 && !memcmp(TypeStr, "byref", 5))
+    type = BlocksAttr::ByRef;
+  else {
+    S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported,
+           "blocks", TypeStr);
+    return;
+  }
+  
+  d->addAttr(new BlocksAttr(type));
+}
+
 static void HandleWeakAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   // check the attribute arguments.
   if (Attr.getNumArgs() != 0) {
@@ -968,6 +996,7 @@
     HandleTransparentUnionAttr(D, Attr, S);
     break;
   case AttributeList::AT_objc_gc:     HandleObjCGCAttr    (D, Attr, S); break;
+  case AttributeList::AT_blocks:      HandleBlocksAttr    (D, Attr, S); break;
   default:
 #if 0
     // TODO: when we have the full set of attributes, warn about unknown ones.

Added: cfe/trunk/test/Sema/block-byref-args.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-byref-args.c?rev=56309&view=auto

==============================================================================
--- cfe/trunk/test/Sema/block-byref-args.c (added)
+++ cfe/trunk/test/Sema/block-byref-args.c Thu Sep 18 11:44:58 2008
@@ -0,0 +1,18 @@
+// RUN: clang %s -fsyntax-only -verify
+
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  __block void(*bobTheFunction)(void);
+  __block void(^bobTheBlock)(void);
+
+  bobTheBlock = ^{;};
+
+  __block int JJJJ;
+  __attribute__((__blocks__(byref))) int III;
+
+  int (^XXX)(void) = ^{ return III+JJJJ; };
+
+  return 0;
+}
+

Added: cfe/trunk/test/Sema/block-storageclass.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-storageclass.c?rev=56309&view=auto

==============================================================================
--- cfe/trunk/test/Sema/block-storageclass.c (added)
+++ cfe/trunk/test/Sema/block-storageclass.c Thu Sep 18 11:44:58 2008
@@ -0,0 +1,18 @@
+// RUN: clang %s -fsyntax-only -verify
+
+#include <stdio.h>
+void _Block_byref_release(void*src){}
+
+int main() {
+   __block  int X = 1234;
+   __block  const char * message = "HELLO";
+
+   X = X - 1234;
+
+   X += 1;
+
+   printf ("%s(%d)\n", message, X);
+   X -= 1;
+
+   return X;
+}





More information about the cfe-commits mailing list