llvm.org GIT mirror llvm / f7399bf
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 Jeffrey Yasskin 10 years ago
3 changed file(s) with 46 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
278278 qsort(&*Start, End-Start, sizeof(*Start), Compare);
279279 }
280280
281 //===----------------------------------------------------------------------===//
282 // Extra additions to
283 //===----------------------------------------------------------------------===//
284
285 /// For a container of pointers, deletes the pointers and then clears the
286 /// container.
287 template
288 void DeleteContainerPointers(Container &C) {
289 for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
290 delete *I;
291 C.clear();
292 }
293
294 /// In a container of pairs (usually a map) whose second element is a pointer,
295 /// deletes the second elements and then clears the container.
296 template
297 void DeleteContainerSeconds(Container &C) {
298 for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
299 delete I->second;
300 C.clear();
301 }
302
281303 } // End llvm namespace
282304
283305 #endif
2222 #include "llvm/Target/TargetFrameInfo.h"
2323 #include "llvm/Target/TargetLoweringObjectFile.h"
2424 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/ADT/STLExtras.h"
2526 #include "llvm/ADT/StringExtras.h"
2627 #include "llvm/Support/Debug.h"
2728 #include "llvm/Support/ErrorHandling.h"
173174 unsigned EndLabelID; // Label ID of the end of scope.
174175 const MachineInstr *LastInsn; // Last instruction of this scope.
175176 const MachineInstr *FirstInsn; // First instruction of this scope.
176 SmallVector Scopes; // Scopes defined in scope.
177 SmallVector Variables;// Variables declared in scope.
177 // Scopes defined in scope. Contents not owned.
178 SmallVector Scopes;
179 // Variables declared in scope. Contents owned.
180 SmallVector Variables;
178181
179182 // Private state for dump()
180183 mutable unsigned IndentLevel;
195198 MDNode *getScopeNode() const { return Desc.getNode(); }
196199 unsigned getStartLabelID() const { return StartLabelID; }
197200 unsigned getEndLabelID() const { return EndLabelID; }
198 SmallVector &getScopes() { return Scopes; }
199 SmallVector &getVariables() { return Variables; }
201 const SmallVector &getScopes() { return Scopes; }
202 const SmallVector &getVariables() { return Variables; }
200203 void setStartLabelID(unsigned S) { StartLabelID = S; }
201204 void setEndLabelID(unsigned E) { EndLabelID = E; }
202205 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
219222 assert (getFirstInsn() && "First instruction is missing!");
220223
221224 // Use the end of last child scope as end of this scope.
222 SmallVector &Scopes = getScopes();
225 const SmallVector &Scopes = getScopes();
223226 const MachineInstr *LastInsn = getFirstInsn();
224227 unsigned LIndex = 0;
225228 if (Scopes.empty()) {
226229 assert (getLastInsn() && "Inner most scope does not have last insn!");
227230 return;
228231 }
229 for (SmallVector::iterator SI = Scopes.begin(),
232 for (SmallVector::const_iterator SI = Scopes.begin(),
230233 SE = Scopes.end(); SI != SE; ++SI) {
231234 DbgScope *DS = *SI;
232235 DS->fixInstructionMarkers(MIIndexMap);
278281 #endif
279282
280283 DbgScope::~DbgScope() {
281 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
282 delete Scopes[i];
283284 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
284285 delete Variables[j];
285286 }
15841585 }
15851586
15861587 // Add variables to scope.
1587 SmallVector &Variables = Scope->getVariables();
1588 const SmallVector &Variables = Scope->getVariables();
15881589 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
15891590 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
15901591 if (VariableDIE)
15921593 }
15931594
15941595 // Add nested scopes.
1595 SmallVector &Scopes = Scope->getScopes();
1596 const SmallVector &Scopes = Scope->getScopes();
15961597 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
15971598 // Define the Scope debug information entry.
15981599 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
16951696 }
16961697
16971698 CompileUnitMap[DIUnit.getNode()] = Unit;
1698 CompileUnits.push_back(Unit);
16991699 return Unit;
17001700 }
17011701
18011801 E = DbgFinder.compile_unit_end(); I != E; ++I)
18021802 constructCompileUnit(*I);
18031803
1804 if (CompileUnits.empty()) {
1804 if (CompileUnitMap.empty()) {
18051805 if (TimePassesIsEnabled)
18061806 DebugTimer->stopTimer();
18071807
18111811 // If main compile unit for this module is not seen than randomly
18121812 // select first compile unit.
18131813 if (!ModuleCU)
1814 ModuleCU = CompileUnits[0];
1814 ModuleCU = CompileUnitMap.begin()->second;
18151815
18161816 // Create DIEs for each subprogram.
18171817 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
19421942
19431943 // Emit inline info.
19441944 emitDebugInlineInfo();
1945
1946 // Clear debug info in preparation for the next Module.
1947 ModuleCU = NULL;
1948 DeleteContainerSeconds(CompileUnitMap);
19451949
19461950 if (TimePassesIsEnabled)
19471951 DebugTimer->stopTimer();
21132117 while (!WorkList.empty()) {
21142118 DbgScope *S = WorkList.back(); WorkList.pop_back();
21152119
2116 SmallVector &Children = S->getScopes();
2120 const SmallVector &Children = S->getScopes();
21172121 if (!Children.empty())
2118 for (SmallVector::iterator SI = Children.begin(),
2122 for (SmallVector::const_iterator SI = Children.begin(),
21192123 SE = Children.end(); SI != SE; ++SI)
21202124 WorkList.push_back(*SI);
21212125
22202224
22212225 // Clear debug info
22222226 CurrentFnDbgScope = NULL;
2223 DbgScopeMap.clear();
2227 DeleteContainerSeconds(DbgScopeMap);
22242228 DbgScopeBeginMap.clear();
22252229 DbgScopeEndMap.clear();
22262230 ConcreteScopes.clear();
2231 DeleteContainerSeconds(AbstractScopes);
22272232 AbstractScopesList.clear();
22282233 Lines.clear();
22292234
6565 /// compile units.
6666 DenseMap CompileUnitMap;
6767
68 /// CompileUnits - All the compile units in this module.
69 ///
70 SmallVector CompileUnits;
71
7268 /// ModuleCU - All DIEs are inserted in ModuleCU.
7369 CompileUnit *ModuleCU;
7470
133129 //
134130 DbgScope *CurrentFnDbgScope;
135131
136 /// DbgScopeMap - Tracks the scopes in the current function.
132 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
133 /// contained DbgScope*s.
137134 ///
138135 DenseMap DbgScopeMap;
139136
142139 DenseMap ConcreteScopes;
143140
144141 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
145 /// not included DbgScopeMap.
142 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
146143 DenseMap AbstractScopes;
147144 SmallVectorAbstractScopesList;
148145