Enable "garbage detection" of LLVM objects. Now users should be obnoxious
authorChris Lattner <sabre@nondot.org>
Sun, 8 Sep 2002 18:59:35 +0000 (18:59 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 8 Sep 2002 18:59:35 +0000 (18:59 +0000)
warnings.  If they accidentally leak LLVM Value's.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3620 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/BasicBlock.cpp
lib/VMCore/Function.cpp
lib/VMCore/Instruction.cpp
lib/VMCore/Module.cpp
lib/VMCore/Value.cpp

index 4271f32e7acddb245a3d08edd46f685a1dbb90ef..bf7191c659328fa5435731abd9cf756c277f0e4a 100644 (file)
@@ -1,4 +1,4 @@
-//===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
+//===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
 //
 // This file implements the BasicBlock class for the VMCore library.
 //
@@ -11,6 +11,7 @@
 #include "llvm/Constant.h"
 #include "llvm/iPHINode.h"
 #include "llvm/SymbolTable.h"
+#include "Support/LeakDetector.h"
 #include "SymbolTableListTraitsImpl.h"
 #include <algorithm>
 
 // instruction list.  This is not a real instruction.
 //
 struct DummyInst : public Instruction {
-  DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {}
+  DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {
+    // This should not be garbage monitored.
+    LeakDetector::removeGarbageObject(this);
+  }
 
   virtual Instruction *clone() const {
     assert(0 && "Cannot clone EOL");abort();
@@ -56,6 +60,9 @@ BasicBlock::BasicBlock(const std::string &name, Function *Parent)
   // Initialize the instlist...
   InstList.setItemParent(this);
 
+  // Make sure that we get added to a function
+  LeakDetector::addGarbageObject(this);
+
   if (Parent)
     Parent->getBasicBlockList().push_back(this);
 }
@@ -66,7 +73,13 @@ BasicBlock::~BasicBlock() {
 }
 
 void BasicBlock::setParent(Function *parent) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
+
   InstList.setParent(parent);
+
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
 }
 
 // Specialize setName to take care of symbol table majik
index 59017178f68abbd2b4ce920509d315d471cd6a88..fb940bf804af356722d275b38995530ee42d8369 100644 (file)
@@ -8,10 +8,14 @@
 #include "llvm/Module.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iOther.h"
+#include "Support/LeakDetector.h"
 #include "SymbolTableListTraitsImpl.h"
 
 BasicBlock *ilist_traits<BasicBlock>::createNode() {
-  return new BasicBlock();
+  BasicBlock *Ret = new BasicBlock();
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 
 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
@@ -19,7 +23,10 @@ iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
 }
 
 Argument *ilist_traits<Argument>::createNode() {
-  return new Argument(Type::IntTy);
+  Argument *Ret = new Argument(Type::IntTy);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 
 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
@@ -38,6 +45,10 @@ template SymbolTableListTraits<BasicBlock, Function, Function>;
 Argument::Argument(const Type *Ty, const std::string &Name = "", Function *Par) 
   : Value(Ty, Value::ArgumentVal, Name) {
   Parent = 0;
+
+  // Make sure that we get added to a function
+  LeakDetector::addGarbageObject(this);
+
   if (Par)
     Par->getArgumentList().push_back(this);
 }
@@ -54,7 +65,11 @@ void Argument::setName(const std::string &name, SymbolTable *ST) {
 }
 
 void Argument::setParent(Function *parent) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
   Parent = parent;
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
 }
 
 
@@ -71,6 +86,9 @@ Function::Function(const FunctionType *Ty, bool isInternal,
   ArgumentList.setParent(this);
   ParentSymTab = SymTab = 0;
 
+  // Make sure that we get added to a function
+  LeakDetector::addGarbageObject(this);
+
   if (ParentModule)
     ParentModule->getFunctionList().push_back(this);
 }
@@ -97,7 +115,11 @@ void Function::setName(const std::string &name, SymbolTable *ST) {
 }
 
 void Function::setParent(Module *parent) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
   Parent = parent;
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
 
   // Relink symbol tables together...
   ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
@@ -157,12 +179,18 @@ GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
     isConstantGlobal(constant) {
   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
 
+  LeakDetector::addGarbageObject(this);
+
   if (ParentModule)
     ParentModule->getGlobalList().push_back(this);
 }
 
 void GlobalVariable::setParent(Module *parent) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
   Parent = parent;
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
 }
 
 // Specialize setName to take care of symbol table majik
index 2c7f6eae91fa881939dc59edc3bf96bc1941a061..4368b7e109235c92f6165b10189eb4fd49a5ebc0 100644 (file)
@@ -7,15 +7,25 @@
 #include "llvm/Function.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/Type.h"
+#include "Support/LeakDetector.h"
 
 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name) 
   : User(ty, Value::InstructionVal, Name) {
   Parent = 0;
   iType = it;
+
+  // Make sure that we get added to a basicblock
+  LeakDetector::addGarbageObject(this);
 }
 
 void Instruction::setParent(BasicBlock *P) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
+
   Parent = P;
+
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
 }
 
 // Specialize setName to take care of symbol table majik
index a895929280d9c280bf3799be02fddb735863e2a6..4bd3a884b308674aecc7237dc3c4a6bb1b710652 100644 (file)
@@ -9,16 +9,24 @@
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "Support/STLExtras.h"
+#include "Support/LeakDetector.h"
 #include "SymbolTableListTraitsImpl.h"
 #include <algorithm>
 #include <map>
 
 Function *ilist_traits<Function>::createNode() {
-  return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
-                                        false), false);
+  FunctionType *FTy =
+    FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
+  Function *Ret = new Function(FTy, false);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
-  return new GlobalVariable(Type::IntTy, false, false);
+  GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 
 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
index ad2b18066843a36c766047df8010a44ca3e866b9..402171be36ab10a421c801f018ef7e14b492a955 100644 (file)
@@ -7,6 +7,7 @@
 #include "llvm/InstrTypes.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/DerivedTypes.h"
+#include "Support/LeakDetector.h"
 #include <algorithm>
 
 //===----------------------------------------------------------------------===//
@@ -39,6 +40,9 @@ Value::~Value() {
   }
 #endif
   assert(Uses.begin() == Uses.end());
+
+  // There should be no uses of this object anymore, remove it.
+  LeakDetector::removeGarbageObject(this);
 }
 
 void Value::replaceAllUsesWith(Value *D) {