blob: a6d96df7ba1f18d4b723759d9e4ccdde9b95bafc [file] [log] [blame]
Chris Lattnerde69a4c2002-09-08 18:51:16 +00001//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerde69a4c2002-09-08 18:51:16 +00009//
10// This file implements the LeakDetector class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Support/LeakDetector.h"
15#include "llvm/Value.h"
16#include <set>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000017using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000018
Alkis Evlogimenosb663d762004-02-14 23:33:39 +000019namespace {
Chris Lattnerde69a4c2002-09-08 18:51:16 +000020
Alkis Evlogimenosb663d762004-02-14 23:33:39 +000021 template <typename T>
22 struct LeakDetectorImpl {
23 LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { }
Chris Lattner3f6962e2002-09-19 19:22:11 +000024
Alkis Evlogimenosb663d762004-02-14 23:33:39 +000025 // Because the most common usage pattern, by far, is to add a
26 // garbage object, then remove it immediately, we optimize this
27 // case. When an object is added, it is not added to the set
28 // immediately, it is added to the CachedValue Value. If it is
29 // immediately removed, no set search need be performed.
30 void addGarbage(const T* o) {
31 if (Cache) {
32 assert(Ts.count(Cache) == 0 && "Object already in set!");
33 Ts.insert(Cache);
34 }
35 Cache = o;
36 }
37
38 void removeGarbage(const T* o) {
39 if (o == Cache)
40 Cache = 0; // Cache hit
41 else
42 Ts.erase(o);
43 }
44
45 bool hasGarbage(const std::string& Message) {
46 addGarbage(0); // Flush the Cache
47
48 assert(Cache == 0 && "No value should be cached anymore!");
49
50 if (!Ts.empty()) {
51 std::cerr
52 << "Leaked " << Name << " objects found: " << Message << ":\n\t";
53 std::copy(Ts.begin(), Ts.end(),
54 std::ostream_iterator<const T*>(std::cerr, " "));
55 std::cerr << '\n';
56
57 // Clear out results so we don't get duplicate warnings on
58 // next call...
59 Ts.clear();
60 return true;
61 }
62 return false;
63 }
64
65 private:
66 std::set<const T*> Ts;
67 const T* Cache;
68 const char* const Name;
69 };
70
71 typedef LeakDetectorImpl<void> Objects;
72 typedef LeakDetectorImpl<Value> LLVMObjects;
73
74 Objects& getObjects() {
75 static Objects o("GENERIC");
76 return o;
77 }
78
79 LLVMObjects& getLLVMObjects() {
80 static LLVMObjects o("LLVM");
81 return o;
82 }
Chris Lattnerde69a4c2002-09-08 18:51:16 +000083}
84
Alkis Evlogimenosb663d762004-02-14 23:33:39 +000085void LeakDetector::addGarbageObjectImpl(void *Object) {
86 getObjects().addGarbage(Object);
Chris Lattnerde69a4c2002-09-08 18:51:16 +000087}
88
89void LeakDetector::addGarbageObjectImpl(const Value *Object) {
Alkis Evlogimenosb663d762004-02-14 23:33:39 +000090 getLLVMObjects().addGarbage(Object);
91}
92
93void LeakDetector::removeGarbageObjectImpl(void *Object) {
94 getObjects().removeGarbage(Object);
Chris Lattnerde69a4c2002-09-08 18:51:16 +000095}
96
97void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
Alkis Evlogimenosb663d762004-02-14 23:33:39 +000098 getLLVMObjects().removeGarbage(Object);
Chris Lattnerde69a4c2002-09-08 18:51:16 +000099}
100
101void LeakDetector::checkForGarbageImpl(const std::string &Message) {
Alkis Evlogimenosb663d762004-02-14 23:33:39 +0000102 // use non-short-circuit version so that both checks are performed
103 if (getObjects().hasGarbage(Message) |
104 getLLVMObjects().hasGarbage(Message))
105 std::cerr << "\nThis is probably because you removed an object, but didn't "
106 "delete it. Please check your code for memory leaks.\n";
Chris Lattnerde69a4c2002-09-08 18:51:16 +0000107}