]> granicus.if.org Git - clang/commitdiff
Hook up attribute ns_consumes_self in the ObjC retain/release checker in the static...
authorTed Kremenek <kremenek@apple.com>
Thu, 27 Jan 2011 06:54:14 +0000 (06:54 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 27 Jan 2011 06:54:14 +0000 (06:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124360 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/PPMacroExpansion.cpp
lib/StaticAnalyzer/CFRefCount.cpp
test/Analysis/retain-release.m
www/analyzer/annotations.html

index 7727eafd73feef713bba6b10b690c14a64a7cf89..c7233344f44a9804e9badd7214c1cafafb1ec2ee 100644 (file)
@@ -536,6 +536,7 @@ static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
            .Case("attribute_ext_vector_type", true)
            .Case("attribute_ns_returns_not_retained", true)
            .Case("attribute_ns_returns_retained", true)
+           .Case("attribute_ns_consumes_self", true)
            .Case("attribute_objc_ivar_unused", true)
            .Case("attribute_overloadable", true)
            .Case("attribute_unavailable_with_message", true)
index 473b7313bb7ab871bf234e8328c8d0fbba07ed20..546687a7a2c48686458b01f9a794d82191f5379f 100644 (file)
@@ -467,6 +467,10 @@ public:
   ///  terminate the path.
   bool isEndPath() const { return EndPath; }
 
+  
+  /// Sets the effect on the receiver of the message.
+  void setReceiverEffect(ArgEffect e) { Receiver = e; }
+  
   /// getReceiverEffect - Returns the effect on the receiver of the call.
   ///  This is only meaningful if the summary applies to an ObjCMessageExpr*.
   ArgEffect getReceiverEffect() const { return Receiver; }
@@ -1219,6 +1223,11 @@ RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
 
   bool isTrackedLoc = false;
 
+  // Effects on the receiver.
+  if (MD->getAttr<NSConsumesSelfAttr>()) {
+    Summ.setReceiverEffect(DecRefMsg);      
+  }
+  
   // Determine if there is a special return effect for this method.
   if (cocoa::isCocoaObjectRef(MD->getResultType())) {
     if (MD->getAttr<NSReturnsRetainedAttr>()) {
index eefd132282ff6cef8c9373ee92dda719fea330cc..e68e4d7e7590997726cc17b8b4d79b59f4c0ca03 100644 (file)
@@ -13,6 +13,9 @@
 #if __has_feature(attribute_cf_returns_not_retained)
 #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
 #endif
+#if __has_feature(attribute_ns_consumes_self)
+#define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
+#endif
 
 //===----------------------------------------------------------------------===//
 // The following code is reduced using delta-debugging from Mac OS X headers:
@@ -1211,6 +1214,7 @@ typedef NSString* MyStringTy;
 - (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning
 - (NSString*) newStringNoAttr;
 - (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to methods that return an Objective-C object}}
+- (id) pseudoInit NS_CONSUMES_SELF NS_RETURNS_RETAINED;
 @end
 
 static int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to functions and methods}}
@@ -1228,6 +1232,19 @@ void test_attr1c(TestOwnershipAttr *X) {
   NSString *str2 = [X newStringNoAttr]; // expected-warning{{leak}}
 }
 
+void testattr2_a() {
+  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // expected-warning{{leak}}
+}
+
+void testattr2_b() {
+  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit];  // expected-warning{{leak}}
+}
+
+void testattr2_c() {
+  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit]; // no-warning
+  [x release];
+}
+
 @interface MyClassTestCFAttr : NSObject {}
 - (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
 - (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
@@ -1401,7 +1418,7 @@ static void rdar_8724287(CFErrorRef error)
     while (error_to_dump != ((void*)0)) {
         CFDictionaryRef info;
 
-        info = CFErrorCopyUserInfo(error_to_dump); // expected-warning{{Potential leak of an object allocated on line 1404 and stored into 'info'}}
+        info = CFErrorCopyUserInfo(error_to_dump); // expected-warning{{Potential leak of an object allocated on line 1421 and stored into 'info'}}
 
         if (info != ((void*)0)) {
         }
index f43e361cca56f653c2ec06f574cbd9e5f0817605..b2182eb9341339c2c0a4d8a1711a198dbcf8ade3 100644 (file)
@@ -56,6 +56,7 @@ recognized by GCC. Their use can be conditioned using preprocessor macros
       <li><a href="#attr_ns_returns_not_retained">Attribute 'ns_returns_not_retained'</a></li>
       <li><a href="#attr_cf_returns_retained">Attribute 'cf_returns_retained'</a></li>
       <li><a href="#attr_cf_returns_not_retained">Attribute 'cf_returns_not_retained'</a></li>
+      <li><a href="#attr_ns_consumes_self">Attribute 'ns_consumes_self'</a></li>
     </ul>
     </li>
   </ul>
@@ -350,6 +351,38 @@ its availability, as it is not available in earlier versions of the analyzer:</p
 #endif
 </pre>
 
+<h4 id="attr_ns_consumes_self">Attribute 'ns_consumes_self'
+(Clang-specific)</h4>
+
+<p>The 'ns_consumes_self' attribute can be placed only on an Objective-C method declaration.
+  It indicates that the receiver of the message is &quot;consumed&quot; (a single reference count decremented)
+  after the message is sent.  This matches the semantics of all &quot;init&quot; methods.
+</p>
+
+<p>One use of this attribute is declare your own init-like methods that do not follow the
+  standard Cocoa naming conventions.  For example:</p>
+  
+<pre class="code_example">
+#ifndef __has_feature
+#define __has_Feature(x) 0 // Compatibility with non-clang compilers.
+#endif
+
+#ifndef NS_CONSUMES_SELF
+#if __has_feature((attribute_ns_consumes_self))
+#else
+#define NS_CONSUMES_SELF
+#endif
+#endif
+
+@interface MyClass : NSObject
+- initWith:(MyClass *)x;
+- nonstandardInitWith:(MyClass *)x NS_CONSUMES_SELF NS_RETURNS_RETAINED;
+@end
+</pre>
+
+<p>In this example, <tt>nonstandardInitWith:</tt> has the same ownership semantics as the init method <tt>initWith:</tt>.
+  The static analyzer will observe that the method consumes the receiver, and then returns an object with a +1 retain count.</p>
+
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <h2 id="custom_assertions">Custom Assertion Handlers</h2>
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->