[cfe-commits] r150896 - in /cfe/trunk: lib/StaticAnalyzer/Core/MemRegion.cpp test/Analysis/stack-addr-ps.c

Ted Kremenek kremenek at apple.com
Sat Feb 18 14:41:01 PST 2012


Author: kremenek
Date: Sat Feb 18 16:41:01 2012
New Revision: 150896

URL: http://llvm.org/viewvc/llvm-project?rev=150896&view=rev
Log:
Teach analyzer that blocks with no captures are globals.  Fixes <rdar://problem/10348049>.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp
    cfe/trunk/test/Analysis/stack-addr-ps.c

Modified: cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp?rev=150896&r1=150895&r2=150896&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp Sat Feb 18 16:41:01 2012
@@ -690,18 +690,24 @@
 MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
                                      const LocationContext *LC) {
   const MemRegion *sReg = 0;
-
-  if (LC) {
-    // FIXME: Once we implement scope handling, we want the parent region
-    // to be the scope.
-    const StackFrameContext *STC = LC->getCurrentStackFrame();
-    assert(STC);
-    sReg = getStackLocalsRegion(STC);
+  const BlockDecl *BD = BC->getDecl();
+  if (!BD->hasCaptures()) {
+    // This handles 'static' blocks.
+    sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
   }
   else {
-    // We allow 'LC' to be NULL for cases where want BlockDataRegions
-    // without context-sensitivity.
-    sReg = getUnknownRegion();
+    if (LC) {
+      // FIXME: Once we implement scope handling, we want the parent region
+      // to be the scope.
+      const StackFrameContext *STC = LC->getCurrentStackFrame();
+      assert(STC);
+      sReg = getStackLocalsRegion(STC);
+    }
+    else {
+      // We allow 'LC' to be NULL for cases where want BlockDataRegions
+      // without context-sensitivity.
+      sReg = getUnknownRegion();
+    }
   }
 
   return getSubRegion<BlockDataRegion>(BC, LC, sReg);

Modified: cfe/trunk/test/Analysis/stack-addr-ps.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/stack-addr-ps.c?rev=150896&r1=150895&r2=150896&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/stack-addr-ps.c (original)
+++ cfe/trunk/test/Analysis/stack-addr-ps.c Sat Feb 18 16:41:01 2012
@@ -57,8 +57,15 @@
 
 typedef int (^ComparatorBlock)(int a, int b);
 ComparatorBlock test_return_block(void) {
+  // This block is a global since it has no captures.
   ComparatorBlock b = ^int(int a, int b){ return a > b; };
-  return b; // expected-warning{{Address of stack-allocated block declared on line 60 returned to caller}}
+  return b; // no-warning
+}
+
+ComparatorBlock test_return_block_with_capture(int x) {
+  // This block is stack allocated because it has captures.
+  ComparatorBlock b = ^int(int a, int b){ return a > b + x; };
+  return b; // expected-warning{{Address of stack-allocated block}}
 }
 
 ComparatorBlock test_return_block_neg_aux(void);
@@ -73,4 +80,13 @@
   return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}}
 };
 
+// Handle blocks that have no captures or are otherwise declared 'static'.
+// <rdar://problem/10348049>
+typedef int (^RDar10348049)(int value);
+RDar10348049 test_rdar10348049(void) {
+  static RDar10348049 b = ^int(int x) {
+    return x + 2;
+  };
+  return b; // no-warning
+}
 





More information about the cfe-commits mailing list