]> granicus.if.org Git - clang/commitdiff
[analyzer] Rely on os_consumes_this attribute to signify that the method call consume...
authorGeorge Karpenkov <ekarpenkov@apple.com>
Thu, 6 Dec 2018 22:07:12 +0000 (22:07 +0000)
committerGeorge Karpenkov <ekarpenkov@apple.com>
Thu, 6 Dec 2018 22:07:12 +0000 (22:07 +0000)
Differential Revision: https://reviews.llvm.org/D55158

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

include/clang/StaticAnalyzer/Core/RetainSummaryManager.h
lib/StaticAnalyzer/Core/RetainSummaryManager.cpp
test/Analysis/osobject-retain-release.cpp

index dfcbd0c85c342c7bafdaac1fbb80c1cfc413417d..c2e9eaa6220c8d5747243410957c8f9f423c7ef4 100644 (file)
@@ -369,8 +369,12 @@ public:
   ///  This is only meaningful if the summary applies to an ObjCMessageExpr*.
   ArgEffect getReceiverEffect() const { return Receiver; }
 
+  /// \return the effect on the "this" receiver of the method call.
   ArgEffect getThisEffect() const { return This; }
 
+  /// Set the effect of the method on "this".
+  void setThisEffect(ArgEffect e) { This = e; }
+
   bool isNoop() const {
     return Ret == RetEffect::MakeNoRet() && Receiver == DoNothing
       && DefaultArgEffect == MayEscape && This == DoNothing
index 0e82d9ab400bf94cd89bfccf96bfa5aec63203aa..67efa542d824eb841523ca2844f9f493f274884c 100644 (file)
@@ -759,6 +759,9 @@ RetainSummaryManager::updateSummaryFromAnnotations(const RetainSummary *&Summ,
   QualType RetTy = FD->getReturnType();
   if (Optional<RetEffect> RetE = getRetEffectFromAnnotations(RetTy, FD))
     Template->setRetEffect(*RetE);
+
+  if (FD->hasAttr<OSConsumesThisAttr>())
+    Template->setThisEffect(DecRef);
 }
 
 void
index a596d0d1b639324d3cae26a85cd74e0827754a46..cf92e2d6788a4de8483111fc5a378fd4cbcd633e 100644 (file)
@@ -5,6 +5,7 @@ struct OSMetaClass;
 #define OS_CONSUME __attribute__((os_consumed))
 #define OS_RETURNS_RETAINED __attribute__((os_returns_retained))
 #define OS_RETURNS_NOT_RETAINED __attribute__((os_returns_not_retained))
+#define OS_CONSUMES_THIS __attribute__((os_consumes_this))
 
 #define OSTypeID(type)   (type::metaClass)
 
@@ -49,6 +50,11 @@ struct OSArray : public OSObject {
 
   virtual void consumeReference(OS_CONSUME OSArray *other);
 
+  void putIntoArray(OSArray *array) OS_CONSUMES_THIS;
+
+  template <typename T>
+  void putIntoT(T *owner) OS_CONSUMES_THIS;
+
   static OSArray *generateArrayHasCode() {
     return new OSArray;
   }
@@ -112,6 +118,16 @@ unsigned int check_attribute_indirect_propagation(MyArray *arr) {
   return 0;
 }
 
+void check_consumes_this(OSArray *owner) {
+  OSArray *arr = new OSArray;
+  arr->putIntoArray(owner);
+}
+
+void check_consumes_this_with_template(OSArray *owner) {
+  OSArray *arr = new OSArray;
+  arr->putIntoT(owner);
+}
+
 void check_free_no_error() {
   OSArray *arr = OSArray::withCapacity(10);
   arr->retain();