IR: Store MDNodes in a separate LeakDetector container
[oota-llvm.git] / include / llvm / IR / LeakDetector.h
1 //===- LeakDetector.h - Provide leak detection ------------------*- 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 a class that can be used to provide very simple memory leak
11 // checks for an API.  Basically LLVM uses this to make sure that Instructions,
12 // for example, are deleted when they are supposed to be, and not leaked away.
13 //
14 // When compiling with NDEBUG (Release build), this class does nothing, thus
15 // adding no checking overhead to release builds.  Note that this class is
16 // implemented in a very simple way, requiring completely manual manipulation
17 // and checking for garbage, but this is intentional: users should not be using
18 // this API, only other APIs should.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_IR_LEAKDETECTOR_H
23 #define LLVM_IR_LEAKDETECTOR_H
24
25 #include <string>
26
27 namespace llvm {
28
29 class LLVMContext;
30 class Value;
31 class MDNode;
32
33 struct LeakDetector {
34   /// addGarbageObject - Add a pointer to the internal set of "garbage" object
35   /// pointers.  This should be called when objects are created, or if they are
36   /// taken out of an owning collection.
37   ///
38   template <class T> static void addGarbageObject(T *Object) {
39 #ifndef NDEBUG
40     addGarbageObjectImpl(Object);
41 #endif
42   }
43
44   /// removeGarbageObject - Remove a pointer from our internal representation of
45   /// our "garbage" objects.  This should be called when an object is added to
46   /// an "owning" collection.
47   ///
48   template <class T> static void removeGarbageObject(T *Object) {
49 #ifndef NDEBUG
50     removeGarbageObjectImpl(Object);
51 #endif
52   }
53
54   /// checkForGarbage - Traverse the internal representation of garbage
55   /// pointers.  If there are any pointers that have been add'ed, but not
56   /// remove'd, big obnoxious warnings about memory leaks are issued.
57   ///
58   /// The specified message will be printed indicating when the check was
59   /// performed.
60   ///
61   static void checkForGarbage(LLVMContext &C, const std::string &Message) {
62 #ifndef NDEBUG
63     checkForGarbageImpl(C, Message);
64 #endif
65   }
66
67 private:
68   /// Overload the normal methods to work better with Value* because they are
69   /// by far the most common in LLVM.
70   ///
71   /// Besides making the warning messages nicer, this hides errors by storing
72   /// Value* in a different leak-detection container than other classes.
73   static void addGarbageObjectImpl(const Value *Object);
74   static void removeGarbageObjectImpl(const Value *Object);
75
76   /// Overload the normal methods to work better with MDNode* to improve error
77   /// messages.
78   ///
79   /// For better or worse, this hides errors when other types are added as
80   /// garbage, deleted without being removed, and an MDNode is allocated in the
81   /// same spot.
82   ///
83   /// \note Only handle \a MDNode for now, since we can't always get access to
84   /// an \a LLVMContext for other \a Metadata types.
85   static void addGarbageObjectImpl(const MDNode *Object);
86   static void removeGarbageObjectImpl(const MDNode *Object);
87
88   static void addGarbageObjectImpl(void *Object);
89   static void removeGarbageObjectImpl(void *Object);
90   static void checkForGarbageImpl(LLVMContext &C, const std::string &Message);
91 };
92
93 } // End llvm namespace
94
95 #endif