r182948 - [analyzer] Don't crash if a block's signature just has the return type.

Jordan Rose jordan_rose at apple.com
Thu May 30 11:14:27 PDT 2013


Author: jrose
Date: Thu May 30 13:14:27 2013
New Revision: 182948

URL: http://llvm.org/viewvc/llvm-project?rev=182948&view=rev
Log:
[analyzer] Don't crash if a block's signature just has the return type.

It is okay to declare a block without an argument list: ^ {} or ^void {}.
In these cases, the BlockDecl's signature-as-written will just contain
the return type, rather than the entire function type. It is unclear if
this is intentional, but the analyzer shouldn't crash because of it.

<rdar://problem/14018351>

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp
    cfe/trunk/test/Analysis/blocks.m

Modified: cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp?rev=182948&r1=182947&r2=182948&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp Thu May 30 13:14:27 2013
@@ -245,12 +245,17 @@ QualType CallEvent::getDeclaredResultTyp
     // Blocks are difficult because the return type may not be stored in the
     // BlockDecl itself. The AST should probably be enhanced, but for now we
     // just do what we can.
+    // If the block is declared without an explicit argument list, the
+    // signature-as-written just includes the return type, not the entire
+    // function type.
     // FIXME: All blocks should have signatures-as-written, even if the return
-    // type is inferred. (That's signified is with a dependent result type.)
+    // type is inferred. (That's signified with a dependent result type.)
     if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten()) {
-      const FunctionType *FT = TSI->getType()->castAs<FunctionType>();
-      if (!FT->getResultType()->isDependentType())
-        return FT->getResultType();
+      QualType Ty = TSI->getType();
+      if (const FunctionType *FT = Ty->getAs<FunctionType>())
+        Ty = FT->getResultType();
+      if (!Ty->isDependentType())
+        return Ty;
     }
 
     return QualType();

Modified: cfe/trunk/test/Analysis/blocks.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/blocks.m?rev=182948&r1=182947&r2=182948&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/blocks.m (original)
+++ cfe/trunk/test/Analysis/blocks.m Thu May 30 13:14:27 2013
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify -x objective-c++ %s
 
 //===----------------------------------------------------------------------===//
 // The following code is reduced using delta-debugging from Mac OS X headers:
@@ -13,6 +14,10 @@ void dispatch_async(dispatch_queue_t que
 __attribute__((visibility("default"))) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__nothrow__)) dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
 typedef long dispatch_once_t;
 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
+dispatch_queue_t
+dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
+
+
 typedef signed char BOOL;
 typedef unsigned long NSUInteger;
 typedef struct _NSZone NSZone;
@@ -56,8 +61,8 @@ void test1(NSString *format, ...) {
   do {
     if (__builtin_expect(*(&pred), ~0l) != ~0l)
       dispatch_once(&pred, ^{
-        logQueue = dispatch_queue_create("com.mycompany.myproduct.asl", ((void*)0));
-        client = asl_open(((void*)0), "com.mycompany.myproduct", 0);
+        logQueue = dispatch_queue_create("com.mycompany.myproduct.asl", 0);
+        client = asl_open(((char*)0), "com.mycompany.myproduct", 0);
       });
   } while (0);
 
@@ -65,7 +70,7 @@ void test1(NSString *format, ...) {
   __builtin_va_start(args, format);
 
   NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
-  dispatch_async(logQueue, ^{ asl_log(client, ((void*)0), 4, "%s", [str UTF8String]); });
+  dispatch_async(logQueue, ^{ asl_log(client, ((aslmsg)0), 4, "%s", [str UTF8String]); });
   [str release];
 
   __builtin_va_end(args);
@@ -123,3 +128,21 @@ void testMessaging() {
   });
 }
 @end
+
+void testReturnVariousSignatures() {
+  (void)^int(){
+    return 42;
+  }();
+
+  (void)^int{
+    return 42;
+  }();
+
+  (void)^(){
+    return 42;
+  }();
+
+  (void)^{
+    return 42;
+  }();
+}





More information about the cfe-commits mailing list