Work around apparent Apple compiler bug which was making all mangled
[oota-llvm.git] / lib / VMCore / LeakDetector.cpp
1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LeakDetector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Support/LeakDetector.h"
15 #include "llvm/Value.h"
16 #include <iostream>
17 #include <set>
18 using namespace llvm;
19
20 namespace {
21   template <typename T>
22   struct LeakDetectorImpl {
23     LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { }
24
25     // 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 = 0;
76     if (o == 0)
77       o = new Objects("GENERIC");
78     return *o;
79   }
80
81   LLVMObjects& getLLVMObjects() {
82     static LLVMObjects *o = 0;
83     if (o == 0)
84       o = new LLVMObjects("LLVM");
85     return *o;
86   }
87 }
88
89 void LeakDetector::addGarbageObjectImpl(void *Object) {
90   getObjects().addGarbage(Object);
91 }
92
93 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
94   getLLVMObjects().addGarbage(Object);
95 }
96
97 void LeakDetector::removeGarbageObjectImpl(void *Object) {
98   getObjects().removeGarbage(Object);
99 }
100
101 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
102   getLLVMObjects().removeGarbage(Object);
103 }
104
105 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
106   // use non-short-circuit version so that both checks are performed
107   if (getObjects().hasGarbage(Message) |
108       getLLVMObjects().hasGarbage(Message))
109     std::cerr << "\nThis is probably because you removed an object, but didn't "
110                  "delete it.  Please check your code for memory leaks.\n";
111 }