From f7399bf929f401d4e1aa40f4f7a2265e2cdedc39 Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Sun, 7 Mar 2010 06:55:35 +0000 Subject: [PATCH] Avoid leaking CompileUnits and DbgScopes from DwarfDebug. Leaks found by Valgrind! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97906 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/STLExtras.h | 22 ++++++++++++++++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 37 +++++++++++++++------------ lib/CodeGen/AsmPrinter/DwarfDebug.h | 9 +++---- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 32cf4590e99..8dbf79031c3 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -279,6 +279,28 @@ static inline void array_pod_sort(IteratorTy Start, IteratorTy End, qsort(&*Start, End-Start, sizeof(*Start), Compare); } +//===----------------------------------------------------------------------===// +// Extra additions to +//===----------------------------------------------------------------------===// + +/// For a container of pointers, deletes the pointers and then clears the +/// container. +template +void DeleteContainerPointers(Container &C) { + for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I) + delete *I; + C.clear(); +} + +/// In a container of pairs (usually a map) whose second element is a pointer, +/// deletes the second elements and then clears the container. +template +void DeleteContainerSeconds(Container &C) { + for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I) + delete I->second; + C.clear(); +} + } // End llvm namespace #endif diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index 5ad1e5ea050..d83cbf21d24 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -23,6 +23,7 @@ #include "llvm/Target/TargetFrameInfo.h" #include "llvm/Target/TargetLoweringObjectFile.h" #include "llvm/Target/TargetRegisterInfo.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" @@ -174,8 +175,10 @@ class DbgScope { unsigned EndLabelID; // Label ID of the end of scope. const MachineInstr *LastInsn; // Last instruction of this scope. const MachineInstr *FirstInsn; // First instruction of this scope. - SmallVector Scopes; // Scopes defined in scope. - SmallVector Variables;// Variables declared in scope. + // Scopes defined in scope. Contents not owned. + SmallVector Scopes; + // Variables declared in scope. Contents owned. + SmallVector Variables; // Private state for dump() mutable unsigned IndentLevel; @@ -196,8 +199,8 @@ public: MDNode *getScopeNode() const { return Desc.getNode(); } unsigned getStartLabelID() const { return StartLabelID; } unsigned getEndLabelID() const { return EndLabelID; } - SmallVector &getScopes() { return Scopes; } - SmallVector &getVariables() { return Variables; } + const SmallVector &getScopes() { return Scopes; } + const SmallVector &getVariables() { return Variables; } void setStartLabelID(unsigned S) { StartLabelID = S; } void setEndLabelID(unsigned E) { EndLabelID = E; } void setLastInsn(const MachineInstr *MI) { LastInsn = MI; } @@ -220,14 +223,14 @@ public: assert (getFirstInsn() && "First instruction is missing!"); // Use the end of last child scope as end of this scope. - SmallVector &Scopes = getScopes(); + const SmallVector &Scopes = getScopes(); const MachineInstr *LastInsn = getFirstInsn(); unsigned LIndex = 0; if (Scopes.empty()) { assert (getLastInsn() && "Inner most scope does not have last insn!"); return; } - for (SmallVector::iterator SI = Scopes.begin(), + for (SmallVector::const_iterator SI = Scopes.begin(), SE = Scopes.end(); SI != SE; ++SI) { DbgScope *DS = *SI; DS->fixInstructionMarkers(MIIndexMap); @@ -279,8 +282,6 @@ void DbgScope::dump() const { #endif DbgScope::~DbgScope() { - for (unsigned i = 0, N = Scopes.size(); i < N; ++i) - delete Scopes[i]; for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j]; } @@ -1585,7 +1586,7 @@ DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) { } // Add variables to scope. - SmallVector &Variables = Scope->getVariables(); + const SmallVector &Variables = Scope->getVariables(); for (unsigned i = 0, N = Variables.size(); i < N; ++i) { DIE *VariableDIE = constructVariableDIE(Variables[i], Scope); if (VariableDIE) @@ -1593,7 +1594,7 @@ DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) { } // Add nested scopes. - SmallVector &Scopes = Scope->getScopes(); + const SmallVector &Scopes = Scope->getScopes(); for (unsigned j = 0, M = Scopes.size(); j < M; ++j) { // Define the Scope debug information entry. DIE *NestedDIE = constructScopeDIE(Scopes[j]); @@ -1696,7 +1697,6 @@ CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) { } CompileUnitMap[DIUnit.getNode()] = Unit; - CompileUnits.push_back(Unit); return Unit; } @@ -1802,7 +1802,7 @@ void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) { E = DbgFinder.compile_unit_end(); I != E; ++I) constructCompileUnit(*I); - if (CompileUnits.empty()) { + if (CompileUnitMap.empty()) { if (TimePassesIsEnabled) DebugTimer->stopTimer(); @@ -1812,7 +1812,7 @@ void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) { // If main compile unit for this module is not seen than randomly // select first compile unit. if (!ModuleCU) - ModuleCU = CompileUnits[0]; + ModuleCU = CompileUnitMap.begin()->second; // Create DIEs for each subprogram. for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(), @@ -1944,6 +1944,10 @@ void DwarfDebug::endModule() { // Emit inline info. emitDebugInlineInfo(); + // Clear debug info in preparation for the next Module. + ModuleCU = NULL; + DeleteContainerSeconds(CompileUnitMap); + if (TimePassesIsEnabled) DebugTimer->stopTimer(); } @@ -2114,9 +2118,9 @@ bool DwarfDebug::extractScopeInformation() { while (!WorkList.empty()) { DbgScope *S = WorkList.back(); WorkList.pop_back(); - SmallVector &Children = S->getScopes(); + const SmallVector &Children = S->getScopes(); if (!Children.empty()) - for (SmallVector::iterator SI = Children.begin(), + for (SmallVector::const_iterator SI = Children.begin(), SE = Children.end(); SI != SE; ++SI) WorkList.push_back(*SI); @@ -2221,10 +2225,11 @@ void DwarfDebug::endFunction(const MachineFunction *MF) { // Clear debug info CurrentFnDbgScope = NULL; - DbgScopeMap.clear(); + DeleteContainerSeconds(DbgScopeMap); DbgScopeBeginMap.clear(); DbgScopeEndMap.clear(); ConcreteScopes.clear(); + DeleteContainerSeconds(AbstractScopes); AbstractScopesList.clear(); Lines.clear(); diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.h b/lib/CodeGen/AsmPrinter/DwarfDebug.h index 55baa921006..8d75b1ff937 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -66,10 +66,6 @@ class DwarfDebug : public DwarfPrinter { /// compile units. DenseMap CompileUnitMap; - /// CompileUnits - All the compile units in this module. - /// - SmallVector CompileUnits; - /// ModuleCU - All DIEs are inserted in ModuleCU. CompileUnit *ModuleCU; @@ -134,7 +130,8 @@ class DwarfDebug : public DwarfPrinter { // DbgScope *CurrentFnDbgScope; - /// DbgScopeMap - Tracks the scopes in the current function. + /// DbgScopeMap - Tracks the scopes in the current function. Owns the + /// contained DbgScope*s. /// DenseMap DbgScopeMap; @@ -143,7 +140,7 @@ class DwarfDebug : public DwarfPrinter { DenseMap ConcreteScopes; /// AbstractScopes - Tracks the abstract scopes a module. These scopes are - /// not included DbgScopeMap. + /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s. DenseMap AbstractScopes; SmallVectorAbstractScopesList;