]> granicus.if.org Git - clang/commitdiff
static analyzer: Also invalidate instance variables of a receiver in a message expres...
authorTed Kremenek <kremenek@apple.com>
Sat, 12 Feb 2011 01:01:31 +0000 (01:01 +0000)
committerTed Kremenek <kremenek@apple.com>
Sat, 12 Feb 2011 01:01:31 +0000 (01:01 +0000)
Fixes <rdar://problem/8725041>.

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

lib/StaticAnalyzer/Core/CFRefCount.cpp
test/Analysis/idempotent-operations.m [new file with mode: 0644]

index 09e69239d64109c84438e163c99192f963552cfd..92bd4116a7412c4f78f17c391f5750c5efc5ad4d 100644 (file)
@@ -2514,6 +2514,18 @@ void CFRefCount::evalSummary(ExplodedNodeSet& Dst,
   //  done an invalidation pass.
   llvm::DenseSet<SymbolRef> WhitelistedSymbols;
 
+  // Invalidate all instance variables of the receiver of a message.
+  // FIXME: We should be able to do better with inter-procedural analysis.
+  if (Receiver) {
+    SVal V = Receiver.getSValAsScalarOrLoc(state);
+    if (SymbolRef Sym = V.getAsLocSymbol()) {
+      if (state->get<RefBindings>(Sym))
+        WhitelistedSymbols.insert(Sym);
+    }
+    if (const MemRegion *region = V.getAsRegion())
+      RegionsToInvalidate.push_back(region);
+  }
+  
   for (unsigned idx = 0, e = callOrMsg.getNumArgs(); idx != e; ++idx) {
     SVal V = callOrMsg.getArgSValAsScalarOrLoc(idx);
     SymbolRef Sym = V.getAsLocSymbol();
diff --git a/test/Analysis/idempotent-operations.m b/test/Analysis/idempotent-operations.m
new file mode 100644 (file)
index 0000000..3f68206
--- /dev/null
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -analyzer-check-idempotent-operations -verify %s
+
+typedef signed char BOOL;
+typedef unsigned long NSUInteger;
+typedef struct _NSZone NSZone;
+@protocol NSObject  - (BOOL)isEqual:(id)object;
+@end    @interface NSObject <NSObject> {
+}
+@end
+
+
+// <rdar://problem/8725041> - Don't flag idempotent operation warnings when
+// a method may invalidate an instance variable.
+@interface Rdar8725041 : NSObject {
+  id _attribute;
+}
+  - (void) method2;
+@end
+
+@implementation Rdar8725041
+- (BOOL) method1 {
+  BOOL needsUpdate = (BOOL)0;
+  id oldAttribute = _attribute;
+  [self method2];
+  needsUpdate |= (_attribute != oldAttribute); // no-warning
+  return needsUpdate;
+}
+
+- (void) method2
+{
+  _attribute = ((void*)0);
+}
+@end
+