clang  8.0.0svn
ObjCRetainCount.h
Go to the documentation of this file.
1 //==-- ObjCRetainCount.h - Retain count summaries for Cocoa -------*- C++ -*--//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the core data structures for retain count "summaries"
11 // for Objective-C and Core Foundation APIs. These summaries are used
12 // by the static analyzer to summarize the retain/release effects of
13 // function and method calls. This drives a path-sensitive typestate
14 // analysis in the static analyzer, but can also potentially be used by
15 // other clients.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_CLANG_STATICANALYZER_CHECKERS_OBJCRETAINCOUNT_H
20 #define LLVM_CLANG_STATICANALYZER_CHECKERS_OBJCRETAINCOUNT_H
21 
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/SmallVector.h"
25 
26 namespace clang {
27 class FunctionDecl;
28 class ObjCMethodDecl;
29 
30 namespace ento { namespace objc_retain {
31 
32 /// An ArgEffect summarizes the retain count behavior on an argument or receiver
33 /// to a function or method.
34 enum ArgEffect {
35  /// There is no effect.
37 
38  /// The argument is treated as if an -autorelease message had been sent to
39  /// the referenced object.
41 
42  /// The argument is treated as if an -dealloc message had been sent to
43  /// the referenced object.
45 
46  /// The argument has its reference count decreased by 1. This is as
47  /// if CFRelease has been called on the argument.
49 
50  /// The argument has its reference count decreased by 1. This is as
51  /// if a -release message has been sent to the argument. This differs
52  /// in behavior from DecRef when GC is enabled.
54 
55  /// The argument has its reference count decreased by 1 to model
56  /// a transferred bridge cast under ARC.
58 
59  /// The argument has its reference count increased by 1. This is as
60  /// if a -retain message has been sent to the argument. This differs
61  /// in behavior from IncRef when GC is enabled.
63 
64  /// The argument has its reference count increased by 1. This is as
65  /// if CFRetain has been called on the argument.
67 
68  /// Used to mark an argument as collectible in GC mode, currently a noop.
70 
71  /// The argument is a pointer to a retain-counted object; on exit, the new
72  /// value of the pointer is a +0 value or NULL.
74 
75  /// The argument is a pointer to a retain-counted object; on exit, the new
76  /// value of the pointer is a +1 value or NULL.
78 
79  /// The argument is treated as potentially escaping, meaning that
80  /// even when its reference count hits 0 it should be treated as still
81  /// possibly being alive as someone else *may* be holding onto the object.
83 
84  /// All typestate tracking of the object ceases. This is usually employed
85  /// when the effect of the call is completely unknown.
87 
88  /// All typestate tracking of the object ceases. Unlike StopTracking,
89  /// this is also enforced when the method body is inlined.
90  ///
91  /// In some cases, we obtain a better summary for this checker
92  /// by looking at the call site than by inlining the function.
93  /// Signifies that we should stop tracking the symbol even if
94  /// the function is inlined.
96 
97  /// Performs the combined functionality of DecRef and StopTrackingHard.
98  ///
99  /// The models the effect that the called function decrements the reference
100  /// count of the argument and all typestate tracking on that argument
101  /// should cease.
103 
104  /// Performs the combined functionality of DecRefMsg and StopTrackingHard.
105  ///
106  /// The models the effect that the called function decrements the reference
107  /// count of the argument and all typestate tracking on that argument
108  /// should cease.
110 };
111 
112 /// RetEffect summarizes a call's retain/release behavior with respect
113 /// to its return value.
114 class RetEffect {
115 public:
116  enum Kind {
117  /// Indicates that no retain count information is tracked for
118  /// the return value.
120  /// Indicates that the returned value is an owned (+1) symbol.
122  /// Indicates that the returned value is an object with retain count
123  /// semantics but that it is not owned (+0). This is the default
124  /// for getters, etc.
126  /// Indicates that the object is not owned and controlled by the
127  /// Garbage collector.
129  /// Indicates that the return value is an owned object when the
130  /// receiver is also a tracked object.
132  // Treat this function as returning a non-tracked symbol even if
133  // the function has been inlined. This is used where the call
134  // site summary is more presise than the summary indirectly produced
135  // by inlining the function
137  };
138 
139  /// Determines the object kind of a tracked object.
140  enum ObjKind {
141  /// Indicates that the tracked object is a CF object. This is
142  /// important between GC and non-GC code.
143  CF,
144  /// Indicates that the tracked object is an Objective-C object.
146  /// Indicates that the tracked object could be a CF or Objective-C object.
148  /// Indicates that the tracked object is a generalized object.
150  };
151 
152 private:
153  Kind K;
154  ObjKind O;
155 
156  RetEffect(Kind k, ObjKind o = AnyObj) : K(k), O(o) {}
157 
158 public:
159  Kind getKind() const { return K; }
160 
161  ObjKind getObjKind() const { return O; }
162 
163  bool isOwned() const {
164  return K == OwnedSymbol || K == OwnedWhenTrackedReceiver;
165  }
166 
167  bool notOwned() const {
168  return K == NotOwnedSymbol;
169  }
170 
171  bool operator==(const RetEffect &Other) const {
172  return K == Other.K && O == Other.O;
173  }
174 
177  }
178 
180  return RetEffect(OwnedSymbol, o);
181  }
183  return RetEffect(NotOwnedSymbol, o);
184  }
187  }
188  static RetEffect MakeNoRet() {
189  return RetEffect(NoRet);
190  }
192  return RetEffect(NoRetHard);
193  }
194 };
195 
196 /// Encapsulates the retain count semantics on the arguments, return value,
197 /// and receiver (if any) of a function/method call.
198 ///
199 /// Note that construction of these objects is not highly efficient. That
200 /// is okay for clients where creating these objects isn't really a bottleneck.
201 /// The purpose of the API is to provide something simple. The actual
202 /// static analyzer checker that implements retain/release typestate
203 /// tracking uses something more efficient.
204 class CallEffects {
206  RetEffect Ret;
207  ArgEffect Receiver;
208 
209  CallEffects(const RetEffect &R) : Ret(R) {}
210 
211 public:
212  /// Returns the argument effects for a call.
213  ArrayRef<ArgEffect> getArgs() const { return Args; }
214 
215  /// Returns the effects on the receiver.
216  ArgEffect getReceiver() const { return Receiver; }
217 
218  /// Returns the effect on the return value.
219  RetEffect getReturnValue() const { return Ret; }
220 
221  /// Return the CallEfect for a given Objective-C method.
222  static CallEffects getEffect(const ObjCMethodDecl *MD);
223 
224  /// Return the CallEfect for a given C/C++ function.
225  static CallEffects getEffect(const FunctionDecl *FD);
226 };
227 
228 }}}
229 
230 #endif
231 
RetEffect getReturnValue() const
Returns the effect on the return value.
Represents a function declaration or definition.
Definition: Decl.h:1722
static RetEffect MakeOwnedWhenTrackedReceiver()
Used to mark an argument as collectible in GC mode, currently a noop.
The argument has its reference count decreased by 1.
Indicates that the tracked object is an Objective-C object.
Indicates that the tracked object is a generalized object.
The argument is a pointer to a retain-counted object; on exit, the new value of the pointer is a +1 v...
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
ArrayRef< ArgEffect > getArgs() const
Returns the argument effects for a call.
The argument is treated as if an -autorelease message had been sent to the referenced object...
The argument has its reference count increased by 1.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
ArgEffect
An ArgEffect summarizes the retain count behavior on an argument or receiver to a function or method...
Indicates that the returned value is an owned (+1) symbol.
Indicates that the return value is an owned object when the receiver is also a tracked object...
The argument is treated as potentially escaping, meaning that even when its reference count hits 0 it...
Indicates that the tracked object is a CF object.
ArgEffect getReceiver() const
Returns the effects on the receiver.
Indicates that the object is not owned and controlled by the Garbage collector.
ObjKind
Determines the object kind of a tracked object.
bool operator==(const RetEffect &Other) const
static RetEffect MakeNotOwned(ObjKind o)
The argument has its reference count increased by 1.
The argument has its reference count decreased by 1 to model a transferred bridge cast under ARC...
All typestate tracking of the object ceases.
Dataflow Directional Tag Classes.
The argument is treated as if an -dealloc message had been sent to the referenced object...
Performs the combined functionality of DecRef and StopTrackingHard.
RetEffect summarizes a call&#39;s retain/release behavior with respect to its return value.
Performs the combined functionality of DecRefMsg and StopTrackingHard.
The argument is a pointer to a retain-counted object; on exit, the new value of the pointer is a +0 v...
Indicates that no retain count information is tracked for the return value.
static RetEffect MakeOwned(ObjKind o)
All typestate tracking of the object ceases.
The argument has its reference count decreased by 1.
Encapsulates the retain count semantics on the arguments, return value, and receiver (if any) of a fu...
Indicates that the returned value is an object with retain count semantics but that it is not owned (...
Indicates that the tracked object could be a CF or Objective-C object.