]> granicus.if.org Git - clang/commitdiff
Patch to support transparent_union arguments
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 27 Sep 2010 22:42:37 +0000 (22:42 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 27 Sep 2010 22:42:37 +0000 (22:42 +0000)
passed to nonnull attributed functions. Implements radar
6857843.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114917 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/Expr.cpp
lib/Sema/SemaDeclAttr.cpp
test/SemaObjC/nonnull.m

index 9536b9bf2454618c7d84eb5d07e1bbd0a387f274..708512ce461938f6e404ad3a9aa898f1229d2efb 100644 (file)
@@ -1862,6 +1862,13 @@ bool Expr::isNullPointerConstant(ASTContext &Ctx,
   if (getType()->isNullPtrType())
     return true;
 
+  if (const RecordType *UT = getType()->getAsUnionType())
+    if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
+      if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
+        const Expr *InitExpr = CLE->getInitializer();
+        if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
+          return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
+      }
   // This expression must be an integer type.
   if (!getType()->isIntegerType() || 
       (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
index 09feb50bd3018f31828a5f4231ea7960e3eca1ec..682a430ee40638c217d24039338db0a6e608f769 100644 (file)
@@ -371,6 +371,19 @@ static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
       QualType T = getFunctionOrMethodArgType(d, I);
       if (T->isAnyPointerType() || T->isBlockPointerType())
         NonNullArgs.push_back(I);
+      else if (const RecordType *UT = T->getAsUnionType()) {
+        if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
+          RecordDecl *UD = UT->getDecl();
+          for (RecordDecl::field_iterator it = UD->field_begin(),
+               itend = UD->field_end(); it != itend; ++it) {
+            T = it->getType();
+            if (T->isAnyPointerType() || T->isBlockPointerType()) {
+              NonNullArgs.push_back(I);
+              break;
+            }
+          }
+        }
+      }
     }
 
     // No pointer arguments?  The attribute in this case is
index cbafc37e8ec0b45dac7d67c9748a8d9398290be6..6c45d97b06601037f62c339461a3d195a7401e72 100644 (file)
@@ -48,3 +48,22 @@ foo (int i1, int i2, int i3, void (^cp1)(), void (^cp2)(), void (^cp3)())
 }
 
 void func5(int) NONNULL_ATTR; //  no warning
+
+// rdar://6857843
+struct dispatch_object_s {
+    int x;
+};
+
+typedef union {
+    long first;
+    struct dispatch_object_s *_do;
+} dispatch_object_t __attribute__((transparent_union));
+
+__attribute__((nonnull))
+void _dispatch_queue_push_list(dispatch_object_t _head); // no warning
+
+void func6(dispatch_object_t _head) {
+  _dispatch_queue_push_list(0); // expected-warning {{null passed to a callee which requires a non-null argument}}
+  _dispatch_queue_push_list(_head._do);  // no warning
+}
+