clang: include/clang/Analysis/RetainSummaryManager.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef LLVM_CLANG_ANALYSIS_RETAINSUMMARYMANAGER_H
16#define LLVM_CLANG_ANALYSIS_RETAINSUMMARYMANAGER_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/ImmutableMap.h"
27#include "llvm/ADT/STLExtras.h"
28#include
29
30using namespace clang;
31
33namespace ento {
34
35
37
39
40
42
43
45
46
48
49
50
52};
53
55
57
58
59
61
62
64
65
67
68
69
71
72
74
75
76
78
79
80
82
83
84
86
87
88
90
91
92
93
95
96
97
99
100
101
102
103
104
105
106
108
109
110
111
112
113
115};
116
117
118
122public:
124 : K(K), O(O) {}
125
128
131 }
132
135 }
136};
137
138
139
141public:
143
144
146
147
149
150
151
152
154
155
156
158
159
160
161
162
165
166private:
169
171
172public:
174
176
179 }
180
183 }
184
187 }
188
191 }
192
195 }
198 }
201 }
204 }
205};
206
207
211public:
213 : II(ii), S(s) {}
214
217
219 : II(nullptr), S(s) {}
220
223};
224
225}
226}
227
228using namespace ento;
229
230namespace llvm {
231
232
233
234
237 ID.AddInteger((unsigned) X.getKind());
238 ID.AddInteger((unsigned) X.getObjKind());
239}
240};
243 ID.AddInteger((unsigned) X.getKind());
244 ID.AddInteger((unsigned) X.getObjKind());
245}
246};
247
250 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
251 DenseMapInfo::getEmptyKey());
252 }
253
255 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
256 DenseMapInfo::getTombstoneKey());
257 }
258
260 typedef std::pair<IdentifierInfo*, Selector> PairTy;
261 return DenseMapInfo::getHashValue(PairTy(V.getIdentifier(),
262 V.getSelector()));
263 }
264
268 }
269
270};
271
272}
273
274
276namespace ento {
277
278
279
280typedef llvm::ImmutableMap<unsigned, ArgEffect> ArgEffects;
281
282
284
285
286
288
289
290
292
293
294
296
297
299
300
301
303
304public:
310 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff),
311 This(ThisEff), Ret(R) {}
312
313
314
316 if (const ArgEffect *AE = Args.lookup(idx))
317 return *AE;
318
319 return DefaultArgEffect;
320 }
321
323 Args = af.add(Args, idx, e);
324 }
325
326
328 DefaultArgEffect = E;
329 }
330
331
333
334
336
337
338
340
341
342
344
345
346
348
350
351
353
357 && Args.isEmpty();
358 }
359
360
361
362
364 return Args == Other.Args && DefaultArgEffect == Other.DefaultArgEffect &&
365 Receiver == Other.Receiver && This == Other.This && Ret == Other.Ret;
366 }
367
368
369 void Profile(llvm::FoldingSetNodeID& ID) const {
370 ID.Add(Args);
371 ID.Add(DefaultArgEffect);
372 ID.Add(Receiver);
373 ID.Add(This);
374 ID.Add(Ret);
375 }
376
377
379 return Args.isEmpty();
380 }
381
383
384private:
385 ArgEffect getDefaultArgEffect() const { return DefaultArgEffect; }
386
388};
389
391 typedef llvm::DenseMap<ObjCSummaryKey, const RetainSummary *> MapTy;
392 MapTy M;
393public:
395
397
398
400 MapTy::iterator I = M.find(K);
401
402 if (I != M.end())
403 return I->second;
404 if ()
405 return nullptr;
406
407
408
409
410
411
412
415 break;
416
417 if ()
418 return nullptr;
419 }
420
421
422
424 M[K] = Summ;
425 return Summ;
426 }
427
429
430
432
433 if (I == M.end())
435
436 return I == M.end() ? nullptr : I->second;
437 }
438
440 return M[K];
441 }
442
445 }
446};
447
449
451 typedef llvm::DenseMap<const FunctionDecl*, const RetainSummary *>
452 FuncSummariesTy;
453
455
456 typedef llvm::FoldingSetNodeWrapper CachedSummaryNode;
457
458
460
461
462 const bool ARCEnabled;
463
464
465 const bool TrackObjCAndCFObjects;
466
467
468 const bool TrackOSObjects;
469
470
471 FuncSummariesTy FuncSummaries;
472
473
474
476
477
479
480
481
482 llvm::BumpPtrAllocator BPAlloc;
483
484
485 ArgEffects::Factory AF;
486
487
488
490
491
492
494
495
496
497 llvm::FoldingSet SimpleSummaries;
498
499
501
502
504
505
507
508
510
511
513
516
520
522
528 RetainSummary Summ(ScratchArgs, RetEff, DefaultEff, ReceiverEff, ThisEff);
529 return getPersistentSummary(Summ);
530 }
531
536 }
537
542 }
543
544 const RetainSummary *getPersistentStopSummary() {
545 return getPersistentSummary(
548 }
549
550 void InitializeClassMethodSummaries();
551 void InitializeMethodSummaries();
552
554 ObjCClassMethodSummaries[S] = Summ;
555 }
556
558 ObjCMethodSummaries[S] = Summ;
559 }
560
561 void addClassMethSummary(const char* Cls, const char* name,
562 const RetainSummary *Summ, bool isNullary = true) {
566 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
567 }
568
569 void addInstMethSummary(const char* Cls, const char* nullaryName,
573 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
574 }
575
576 template <typename... Keywords>
581 }
582
583 template <typename... Keywords>
584 void addInstMethSummary(const char *Cls, const RetainSummary *Summ,
585 Keywords *... Kws) {
586 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, Kws...);
587 }
588
589 template <typename... Keywords>
590 void addClsMethSummary(const char *Cls, const RetainSummary *Summ,
591 Keywords *... Kws) {
592 addMethodSummary(&Ctx.Idents.get(Cls), ObjCClassMethodSummaries, Summ,
593 Kws...);
594 }
595
596 template <typename... Keywords>
598 Keywords *... Kws) {
599 addMethodSummary(II, ObjCClassMethodSummaries, Summ, Kws...);
600 }
601
603 bool &AllowAnnotations);
604
605
607 StringRef FName, QualType RetTy);
608
609
610 const RetainSummary *getSummaryForObjCOrCFObject(
612 StringRef FName,
615 bool &AllowAnnotations);
616
617
618
619
620 bool applyParamAnnotationEffect(const ParmVarDecl *pd, unsigned parm_idx,
623
624public:
626 bool trackOSObjects)
627 : Ctx(ctx), ARCEnabled((bool)Ctx.getLangOpts().ObjCAutoRefCount),
628 TrackObjCAndCFObjects(trackObjCAndCFObjects),
629 TrackOSObjects(trackOSObjects), AF(BPAlloc),
633 : RetEffect::MakeOwnedWhenTrackedReceiver()) {
634 InitializeClassMethodSummaries();
635 InitializeMethodSummaries();
636 }
637
639
641
642
644
645
647
648
650 };
651
652 std::optional
654 bool &hasTrustedImplementationAnnotation);
655
656
657
659
661
663 bool HasNonZeroCallbackArg=false,
664 bool IsReceiverUnconsumedSelf=false,
666
668
669private:
670
671
672
674
676
680 ObjCMethodSummariesTy &CachedSummaries);
681
684
686
689
690
691 std::optional getRetEffectFromAnnotations(QualType RetTy,
693
694 void updateSummaryFromAnnotations(const RetainSummary *&Summ,
696
697 void updateSummaryFromAnnotations(const RetainSummary *&Summ,
699
702
703
704
705
706
707
708
709
710
711
712
713
714
715 void updateSummaryForReceiverUnconsumedSelf(const RetainSummary *&S);
716
717
719
720
721
722
723
724
725
726
727 template
728 std::optional hasAnyEnabledAttrOf(const Decl *D, QualType QT);
729
730 template <class T1, class T2, class... Others>
731 std::optional hasAnyEnabledAttrOf(const Decl *D, QualType QT);
732
734};
735
736
737
738
739
740
745 bool Accessed;
746public:
748 : Manager(mgr), RealSummary(real), ScratchSummary(*real), Accessed(false) {}
749
751 if (Accessed)
752 RealSummary = Manager.getPersistentSummary(ScratchSummary);
753 }
754
756 Accessed = true;
757 return ScratchSummary;
758 }
759
761 Accessed = true;
762 return &ScratchSummary;
763 }
764};
765
766}
767}
768
769#endif
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
__device__ __2f16 float __ockl_bool s
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
An instance of this class corresponds to a call.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Decl - This represents one declaration (or definition), e.g.
Represents a function declaration or definition.
FunctionType - C99 6.7.5.3 - Function Declarators.
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
This represents a decl that may have a name.
Represents an ObjC class declaration.
An expression that sends a message to the given Objective-C object or class.
ObjCMethodDecl - Represents an instance or class method declaration.
Represents a parameter to a function.
A (possibly-)qualified type.
Smart pointer class that efficiently represents Objective-C method names.
An ArgEffect summarizes the retain count behavior on an argument or receiver to a function or method.
ArgEffect withKind(ArgEffectKind NewK)
ObjKind getObjKind() const
bool operator==(const ArgEffect &Other) const
ArgEffect(ArgEffectKind K=DoNothing, ObjKind O=ObjKind::AnyObj)
ArgEffectKind getKind() const
const RetainSummary * find(const ObjCInterfaceDecl *D, Selector S)
const RetainSummary *& operator[](Selector S)
const RetainSummary * find(IdentifierInfo *II, Selector S)
const RetainSummary *& operator[](ObjCSummaryKey K)
A key identifying a summary.
ObjCSummaryKey(const ObjCInterfaceDecl *d, Selector s)
IdentifierInfo * getIdentifier() const
Selector getSelector() const
ObjCSummaryKey(IdentifierInfo *ii, Selector s)
ObjCSummaryKey(Selector s)
RetEffect summarizes a call's retain/release behavior with respect to its return value.
static RetEffect MakeNotOwned(ObjKind o)
ObjKind getObjKind() const
static RetEffect MakeOwned(ObjKind o)
@ OwnedSymbol
Indicates that the returned value is an owned (+1) symbol.
@ OwnedWhenTrackedReceiver
Indicates that the return value is an owned object when the receiver is also a tracked object.
@ NoRet
Indicates that no retain count information is tracked for the return value.
@ NotOwnedSymbol
Indicates that the returned value is an object with retain count semantics but that it is not owned (...
static RetEffect MakeNoRet()
static RetEffect MakeOwnedWhenTrackedReceiver()
static RetEffect MakeNoRetHard()
bool operator==(const RetEffect &Other) const
bool isTrustedReferenceCountImplementation(const Decl *FD)
std::optional< BehaviorSummary > canEval(const CallExpr *CE, const FunctionDecl *FD, bool &hasTrustedImplementationAnnotation)
static bool isKnownSmartPointer(QualType QT)
const RetainSummary * getSummary(AnyCall C, bool HasNonZeroCallbackArg=false, bool IsReceiverUnconsumedSelf=false, QualType ReceiverType={})
RetEffect getObjAllocRetEffect() const
RetainSummaryManager(ASTContext &ctx, bool trackObjCAndCFObjects, bool trackOSObjects)
RetainSummaryTemplate(const RetainSummary *&real, RetainSummaryManager &mgr)
RetainSummary * operator->()
RetainSummary & operator*()
Summary for a function with respect to ownership changes.
void setRetEffect(RetEffect E)
setRetEffect - Set the effect of the return value of the call.
ArgEffect getThisEffect() const
void setDefaultArgEffect(ArgEffect E)
setDefaultArgEffect - Set the default argument effect.
RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff, ArgEffect ReceiverEff, ArgEffect ThisEff)
void addArg(ArgEffects::Factory &af, unsigned idx, ArgEffect e)
ArgEffect getReceiverEffect() const
getReceiverEffect - Returns the effect on the receiver of the call.
RetEffect getRetEffect() const
getRetEffect - Returns the effect on the return value of the call.
bool isSimple() const
A retain summary is simple if it has no ArgEffects other than the default.
ArgEffects getArgEffects() const
ArgEffect getDefaultEffect() const
void setThisEffect(ArgEffect e)
Set the effect of the method on "this".
ArgEffect getArg(unsigned idx) const
getArg - Return the argument effect on the argument specified by idx (starting from 0).
void setReceiverEffect(ArgEffect e)
Sets the effect on the receiver of the message.
bool operator==(const RetainSummary &Other) const
Test if two retain summaries are identical.
void Profile(llvm::FoldingSetNodeID &ID) const
Profile this summary for inclusion in a FoldingSet.
llvm::ImmutableMap< unsigned, ArgEffect > ArgEffects
ArgEffects summarizes the effects of a function/method call on all of its arguments.
ObjKind
Determines the object kind of a tracked object.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
@ Generalized
Indicates that the tracked object is a generalized object.
@ CF
Indicates that the tracked object is a CF object.
@ AnyObj
Indicates that the tracked object could be a CF or Objective-C object.
@ ObjC
Indicates that the tracked object is an Objective-C object.
@ IncRef
The argument has its reference count increased by 1.
@ UnretainedOutParameter
The argument is a pointer to a retain-counted object; on exit, the new value of the pointer is a +0 v...
@ DoNothing
There is no effect.
@ RetainedOutParameter
The argument is a pointer to a retain-counted object; on exit, the new value of the pointer is a +1 v...
@ RetainedOutParameterOnZero
The argument is a pointer to a retain-counted object; on exit, the new value of the pointer is a +1 v...
@ MayEscape
The argument is treated as potentially escaping, meaning that even when its reference count hits 0 it...
@ StopTracking
All typestate tracking of the object ceases.
@ Dealloc
The argument is treated as if the referenced object was deallocated.
@ Autorelease
The argument is treated as if an -autorelease message had been sent to the referenced object.
@ RetainedOutParameterOnNonZero
The argument is a pointer to a retain-counted object; on exit, the new value of the pointer is a +1 v...
@ DecRef
The argument has its reference count decreased by 1.
@ StopTrackingHard
All typestate tracking of the object ceases.
@ DecRefAndStopTrackingHard
Performs the combined functionality of DecRef and StopTrackingHard.
@ DecRefBridgedTransferred
The argument has its reference count decreased by 1 to model a transferred bridge cast under ARC.
The JSON file list parser is used to communicate input to InstallAPI.
static Selector getKeywordSelector(ASTContext &Ctx, const IdentifierInfos *...IIs)
Selector GetUnarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing an unary selector.
Selector GetNullarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing a nullary selector.
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
static unsigned getHashValue(const ObjCSummaryKey &V)
static ObjCSummaryKey getEmptyKey()
static bool isEqual(const ObjCSummaryKey &LHS, const ObjCSummaryKey &RHS)
static ObjCSummaryKey getTombstoneKey()
static void Profile(const ArgEffect X, FoldingSetNodeID &ID)
static void Profile(const RetEffect &X, FoldingSetNodeID &ID)