blob: 3e485abdfd18056f370b0ba5f825570fb3bf6e74 [file] [log] [blame]
Chris Lattner213a9f92009-08-23 03:56:06 +00001//===- LeaksContext.h - LeadDetector Implementation ------------*- C++ -*--===//
Owen Anderson6d549d62009-08-19 17:07:46 +00002//
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 various helper methods and classes used by
11// LLVMContextImpl for leaks detectors.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_IR_LEAKSCONTEXT_H
16#define LLVM_LIB_IR_LEAKSCONTEXT_H
Richard Smith8d039e42014-04-26 00:53:26 +000017
Owen Anderson6d549d62009-08-19 17:07:46 +000018#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Value.h"
Richard Smith8d039e42014-04-26 00:53:26 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanb29cda92010-04-15 17:08:50 +000021
22namespace llvm {
Owen Anderson6d549d62009-08-19 17:07:46 +000023
24template <class T>
25struct PrinterTrait {
Chris Lattner213a9f92009-08-23 03:56:06 +000026 static void print(const T* P) { errs() << P; }
Owen Anderson6d549d62009-08-19 17:07:46 +000027};
28
29template<>
30struct PrinterTrait<Value> {
Chris Lattner213a9f92009-08-23 03:56:06 +000031 static void print(const Value* P) { errs() << *P; }
Owen Anderson6d549d62009-08-19 17:07:46 +000032};
33
34template <typename T>
Benjamin Kramer079b96e2013-09-11 18:05:11 +000035struct LeakDetectorImpl {
Owen Anderson6d549d62009-08-19 17:07:46 +000036 explicit LeakDetectorImpl(const char* const name = "") :
Craig Toppere73658d2014-04-28 04:05:08 +000037 Cache(nullptr), Name(name) { }
Owen Anderson6d549d62009-08-19 17:07:46 +000038
39 void clear() {
Craig Toppere73658d2014-04-28 04:05:08 +000040 Cache = nullptr;
Owen Anderson6d549d62009-08-19 17:07:46 +000041 Ts.clear();
42 }
43
44 void setName(const char* n) {
45 Name = n;
46 }
47
48 // Because the most common usage pattern, by far, is to add a
49 // garbage object, then remove it immediately, we optimize this
50 // case. When an object is added, it is not added to the set
51 // immediately, it is added to the CachedValue Value. If it is
52 // immediately removed, no set search need be performed.
53 void addGarbage(const T* o) {
Rafael Espindola2c5792a2009-12-18 20:35:38 +000054 assert(Ts.count(o) == 0 && "Object already in set!");
Owen Anderson6d549d62009-08-19 17:07:46 +000055 if (Cache) {
Rafael Espindola2c5792a2009-12-18 20:35:38 +000056 assert(Cache != o && "Object already in set!");
Owen Anderson6d549d62009-08-19 17:07:46 +000057 Ts.insert(Cache);
58 }
59 Cache = o;
60 }
61
62 void removeGarbage(const T* o) {
63 if (o == Cache)
Craig Toppere73658d2014-04-28 04:05:08 +000064 Cache = nullptr; // Cache hit
Owen Anderson6d549d62009-08-19 17:07:46 +000065 else
66 Ts.erase(o);
67 }
68
69 bool hasGarbage(const std::string& Message) {
Craig Toppere73658d2014-04-28 04:05:08 +000070 addGarbage(nullptr); // Flush the Cache
Owen Anderson6d549d62009-08-19 17:07:46 +000071
Craig Toppere73658d2014-04-28 04:05:08 +000072 assert(!Cache && "No value should be cached anymore!");
Owen Anderson6d549d62009-08-19 17:07:46 +000073
74 if (!Ts.empty()) {
Chris Lattner213a9f92009-08-23 03:56:06 +000075 errs() << "Leaked " << Name << " objects found: " << Message << ":\n";
Owen Anderson6d549d62009-08-19 17:07:46 +000076 for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(),
77 E = Ts.end(); I != E; ++I) {
Chris Lattner213a9f92009-08-23 03:56:06 +000078 errs() << '\t';
Owen Anderson6d549d62009-08-19 17:07:46 +000079 PrinterTrait<T>::print(*I);
Chris Lattner213a9f92009-08-23 03:56:06 +000080 errs() << '\n';
Owen Anderson6d549d62009-08-19 17:07:46 +000081 }
Chris Lattner213a9f92009-08-23 03:56:06 +000082 errs() << '\n';
Owen Anderson6d549d62009-08-19 17:07:46 +000083
84 return true;
85 }
86
87 return false;
88 }
89
90private:
91 SmallPtrSet<const T*, 8> Ts;
92 const T* Cache;
93 const char* Name;
94};
Dan Gohmanb29cda92010-04-15 17:08:50 +000095
96}
Richard Smith8d039e42014-04-26 00:53:26 +000097
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000098#endif