From ad312155a6a357e3ed73232f8f945f2bd75886b4 Mon Sep 17 00:00:00 2001 From: Alon Mishne Date: Tue, 18 Mar 2014 09:41:07 +0000 Subject: [PATCH] [C++11] Change DebugInfoFinder to use range-based loops Also changes the iterators to return actual DI type over MDNode. llvm-svn: 204130 --- llvm/include/llvm/IR/DebugInfo.h | 47 ++++++++++++------- llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp | 20 ++++---- llvm/lib/IR/Verifier.cpp | 31 ++++++------ llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp | 10 +--- llvm/lib/Transforms/IPO/StripSymbols.cpp | 5 +- .../Transforms/Instrumentation/DebugIR.cpp | 13 ++--- llvm/lib/Transforms/Utils/CloneFunction.cpp | 10 +--- llvm/unittests/Transforms/Utils/Cloning.cpp | 4 +- 8 files changed, 66 insertions(+), 74 deletions(-) diff --git a/llvm/include/llvm/IR/DebugInfo.h b/llvm/include/llvm/IR/DebugInfo.h index a8fb24565e65..85a86f40f14e 100644 --- a/llvm/include/llvm/IR/DebugInfo.h +++ b/llvm/include/llvm/IR/DebugInfo.h @@ -18,6 +18,7 @@ #define LLVM_IR_DEBUGINFO_H #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" @@ -876,17 +877,31 @@ private: bool addScope(DIScope Scope); public: - typedef SmallVectorImpl::const_iterator iterator; - iterator compile_unit_begin() const { return CUs.begin(); } - iterator compile_unit_end() const { return CUs.end(); } - iterator subprogram_begin() const { return SPs.begin(); } - iterator subprogram_end() const { return SPs.end(); } - iterator global_variable_begin() const { return GVs.begin(); } - iterator global_variable_end() const { return GVs.end(); } - iterator type_begin() const { return TYs.begin(); } - iterator type_end() const { return TYs.end(); } - iterator scope_begin() const { return Scopes.begin(); } - iterator scope_end() const { return Scopes.end(); } + typedef SmallVectorImpl::const_iterator compile_unit_iterator; + typedef SmallVectorImpl::const_iterator subprogram_iterator; + typedef SmallVectorImpl::const_iterator global_variable_iterator; + typedef SmallVectorImpl::const_iterator type_iterator; + typedef SmallVectorImpl::const_iterator scope_iterator; + + iterator_range compile_units() const { + return iterator_range(CUs.begin(), CUs.end()); + } + + iterator_range subprograms() const { + return iterator_range(SPs.begin(), SPs.end()); + } + + iterator_range global_variables() const { + return iterator_range(GVs.begin(), GVs.end()); + } + + iterator_range types() const { + return iterator_range(TYs.begin(), TYs.end()); + } + + iterator_range scopes() const { + return iterator_range(Scopes.begin(), Scopes.end()); + } unsigned compile_unit_count() const { return CUs.size(); } unsigned global_variable_count() const { return GVs.size(); } @@ -895,11 +910,11 @@ public: unsigned scope_count() const { return Scopes.size(); } private: - SmallVector CUs; // Compile Units - SmallVector SPs; // Subprograms - SmallVector GVs; // Global Variables; - SmallVector TYs; // Types - SmallVector Scopes; // Scopes + SmallVector CUs; // Compile Units + SmallVector SPs; // Subprograms + SmallVector GVs; // Global Variables; + SmallVector TYs; // Types + SmallVector Scopes; // Scopes SmallPtrSet NodesSeen; DITypeIdentifierMap TypeIdentifierMap; /// Specify if TypeIdentifierMap is initialized. diff --git a/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp b/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp index 33ad8c5e9459..f645558bd814 100644 --- a/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp +++ b/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp @@ -56,31 +56,27 @@ bool ModuleDebugInfoPrinter::runOnModule(Module &M) { } void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const { - for (DebugInfoFinder::iterator I = Finder.compile_unit_begin(), - E = Finder.compile_unit_end(); I != E; ++I) { + for (DICompileUnit CU : Finder.compile_units()) { O << "Compile Unit: "; - DICompileUnit(*I).print(O); + CU.print(O); O << '\n'; } - for (DebugInfoFinder::iterator I = Finder.subprogram_begin(), - E = Finder.subprogram_end(); I != E; ++I) { + for (DISubprogram S : Finder.subprograms()) { O << "Subprogram: "; - DISubprogram(*I).print(O); + S.print(O); O << '\n'; } - for (DebugInfoFinder::iterator I = Finder.global_variable_begin(), - E = Finder.global_variable_end(); I != E; ++I) { + for (DIGlobalVariable GV : Finder.global_variables()) { O << "GlobalVariable: "; - DIGlobalVariable(*I).print(O); + GV.print(O); O << '\n'; } - for (DebugInfoFinder::iterator I = Finder.type_begin(), - E = Finder.type_end(); I != E; ++I) { + for (DIType T : Finder.types()) { O << "Type: "; - DIType(*I).print(O); + T.print(O); O << '\n'; } } diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 2fc77cc2d727..50c6ae204e93 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -2355,22 +2355,21 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { void Verifier::verifyDebugInfo() { // Verify Debug Info. if (!DisableDebugInfoVerifier) { - for (DebugInfoFinder::iterator I = Finder.compile_unit_begin(), - E = Finder.compile_unit_end(); I != E; ++I) - Assert1(DICompileUnit(*I).Verify(), "DICompileUnit does not Verify!", *I); - for (DebugInfoFinder::iterator I = Finder.subprogram_begin(), - E = Finder.subprogram_end(); I != E; ++I) - Assert1(DISubprogram(*I).Verify(), "DISubprogram does not Verify!", *I); - for (DebugInfoFinder::iterator I = Finder.global_variable_begin(), - E = Finder.global_variable_end(); I != E; ++I) - Assert1(DIGlobalVariable(*I).Verify(), - "DIGlobalVariable does not Verify!", *I); - for (DebugInfoFinder::iterator I = Finder.type_begin(), - E = Finder.type_end(); I != E; ++I) - Assert1(DIType(*I).Verify(), "DIType does not Verify!", *I); - for (DebugInfoFinder::iterator I = Finder.scope_begin(), - E = Finder.scope_end(); I != E; ++I) - Assert1(DIScope(*I).Verify(), "DIScope does not Verify!", *I); + for (DICompileUnit CU : Finder.compile_units()) { + Assert1(CU.Verify(), "DICompileUnit does not Verify!", CU); + } + for (DISubprogram S : Finder.subprograms()) { + Assert1(S.Verify(), "DISubprogram does not Verify!", S); + } + for (DIGlobalVariable GV : Finder.global_variables()) { + Assert1(GV.Verify(), "DIGlobalVariable does not Verify!", GV); + } + for (DIType T : Finder.types()) { + Assert1(T.Verify(), "DIType does not Verify!", T); + } + for (DIScope S : Finder.scopes()) { + Assert1(S.Verify(), "DIScope does not Verify!", S); + } } } diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp index 0cbdcc49aa9a..bc725865c2ec 100644 --- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -838,10 +838,7 @@ void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) { DbgFinder.processModule(M); unsigned i = 1; - for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), - E = DbgFinder.compile_unit_end(); - I != E; ++I) { - DICompileUnit DIUnit(*I); + for (DICompileUnit DIUnit : DbgFinder.compile_units()) { StringRef Filename(DIUnit.getFilename()); StringRef Dirname(DIUnit.getDirectory()); SmallString<128> FullPathName = Dirname; @@ -856,10 +853,7 @@ void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) { ++i; } - for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(), - E = DbgFinder.subprogram_end(); - I != E; ++I) { - DISubprogram SP(*I); + for (DISubprogram SP : DbgFinder.subprograms()) { StringRef Filename(SP.getFilename()); StringRef Dirname(SP.getDirectory()); SmallString<128> FullPathName = Dirname; diff --git a/llvm/lib/Transforms/IPO/StripSymbols.cpp b/llvm/lib/Transforms/IPO/StripSymbols.cpp index 33c834058c3e..6d0be8fac2dd 100644 --- a/llvm/lib/Transforms/IPO/StripSymbols.cpp +++ b/llvm/lib/Transforms/IPO/StripSymbols.cpp @@ -306,10 +306,7 @@ bool StripDeadDebugInfo::runOnModule(Module &M) { SmallVector LiveSubprograms; DenseSet VisitedSet; - for (DebugInfoFinder::iterator CI = F.compile_unit_begin(), - CE = F.compile_unit_end(); CI != CE; ++CI) { - // Create our compile unit. - DICompileUnit DIC(*CI); + for (DICompileUnit DIC : F.compile_units()) { assert(DIC.Verify() && "DIC must verify as a DICompileUnit."); // Create our live subprogram list. diff --git a/llvm/lib/Transforms/Instrumentation/DebugIR.cpp b/llvm/lib/Transforms/Instrumentation/DebugIR.cpp index 45e2c11c152a..069886e0719a 100644 --- a/llvm/lib/Transforms/Instrumentation/DebugIR.cpp +++ b/llvm/lib/Transforms/Instrumentation/DebugIR.cpp @@ -183,8 +183,8 @@ public: if (Finder.compile_unit_count() > 1) report_fatal_error("DebugIR pass supports only a signle compile unit per " "Module."); - createCompileUnit( - Finder.compile_unit_count() == 1 ? *Finder.compile_unit_begin() : 0); + createCompileUnit(Finder.compile_unit_count() == 1 ? + (MDNode*)*Finder.compile_units().begin() : 0); } void visitFunction(Function &F) { @@ -325,14 +325,11 @@ private: << " subprogram nodes" << "\n"); - for (DebugInfoFinder::iterator i = Finder.subprogram_begin(), - e = Finder.subprogram_end(); - i != e; ++i) { - DISubprogram S(*i); + for (DISubprogram S : Finder.subprograms()) { if (S.getFunction() == F) { - DEBUG(dbgs() << "Found DISubprogram " << *i << " for function " + DEBUG(dbgs() << "Found DISubprogram " << S << " for function " << S.getFunction() << "\n"); - return *i; + return S; } } DEBUG(dbgs() << "unable to find DISubprogram node for function " diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp index 22222112f177..8df3ca0f15a5 100644 --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -154,10 +154,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, // Find the MDNode which corresponds to the DISubprogram data that described F. static MDNode* FindSubprogram(const Function *F, DebugInfoFinder &Finder) { - for (DebugInfoFinder::iterator I = Finder.subprogram_begin(), - E = Finder.subprogram_end(); - I != E; ++I) { - DISubprogram Subprogram(*I); + for (DISubprogram Subprogram : Finder.subprograms()) { if (Subprogram.describes(F)) return Subprogram; } return NULL; @@ -190,10 +187,7 @@ static void CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc, VMap[OldFunc] = NewFunc; DISubprogram NewSubprogram(MapValue(OldSubprogramMDNode, VMap)); - for (DebugInfoFinder::iterator CUIter = Finder.compile_unit_begin(), - CUEnd = Finder.compile_unit_end(); CUIter != CUEnd; ++CUIter) { - DICompileUnit CU(*CUIter); - + for (DICompileUnit CU : Finder.compile_units()) { DIArray Subprograms(CU.getSubprograms()); // If the compile unit's function list contains the old function, it should diff --git a/llvm/unittests/Transforms/Utils/Cloning.cpp b/llvm/unittests/Transforms/Utils/Cloning.cpp index 63585b82ae73..6dec807b0c72 100644 --- a/llvm/unittests/Transforms/Utils/Cloning.cpp +++ b/llvm/unittests/Transforms/Utils/Cloning.cpp @@ -276,7 +276,7 @@ TEST_F(CloneFunc, Subprogram) { unsigned SubprogramCount = Finder->subprogram_count(); EXPECT_EQ(2U, SubprogramCount); - DebugInfoFinder::iterator Iter = Finder->subprogram_begin(); + auto Iter = Finder->subprograms().begin(); DISubprogram Sub1(*Iter); EXPECT_TRUE(Sub1.Verify()); Iter++; @@ -292,7 +292,7 @@ TEST_F(CloneFunc, Subprogram) { TEST_F(CloneFunc, SubprogramInRightCU) { EXPECT_EQ(2U, Finder->compile_unit_count()); - DebugInfoFinder::iterator Iter = Finder->compile_unit_begin(); + auto Iter = Finder->compile_units().begin(); DICompileUnit CU1(*Iter); EXPECT_TRUE(CU1.Verify()); Iter++;