diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 222bad3dd29..444a148fc16 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -36,7 +36,6 @@ namespace { const Module *TheModule; map TypeNames; std::set MangledGlobals; - std::set StructPrinted; public: CWriter(ostream &o) : Out(o) {} @@ -74,9 +73,9 @@ namespace { private : bool nameAllUsedStructureTypes(Module &M); - void parseStruct(const Type *Ty); void printModule(Module *M); void printSymbolTable(const SymbolTable &ST); + void printContainedStructs(const Type *Ty, std::set &); void printGlobal(const GlobalVariable *GV); void printFunctionSignature(const Function *F, bool Prototype); @@ -594,6 +593,9 @@ void CWriter::printSymbolTable(const SymbolTable &ST) { Out << "\n"; + // Keep track of which structures have been printed so far... + std::set StructPrinted; + // Loop over all structures then push them into the stack so they are // printed in the correct order. for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { @@ -602,37 +604,38 @@ void CWriter::printSymbolTable(const SymbolTable &ST) { for (; I != End; ++I) if (const StructType *STy = dyn_cast(I->second)) - parseStruct(STy); + printContainedStructs(STy, StructPrinted); } } // Push the struct onto the stack and recursively push all structs // this one depends on. -void CWriter::parseStruct(const Type *Ty) { +void CWriter::printContainedStructs(const Type *Ty, + std::set &StructPrinted){ if (const StructType *STy = dyn_cast(Ty)){ //Check to see if we have already printed this struct - if (StructPrinted.find(STy) == StructPrinted.end()){ + if (StructPrinted.count(STy) == 0) { + // Print all contained types first... for (StructType::ElementTypes::const_iterator I = STy->getElementTypes().begin(), E = STy->getElementTypes().end(); I != E; ++I) { - const Type *Ty1 = dyn_cast(I->get()); + const Type *Ty1 = I->get(); if (isa(Ty1) || isa(Ty1)) - parseStruct(Ty1); + printContainedStructs(Ty1, StructPrinted); } - //Print struct + //Print structure type out.. StructPrinted.insert(STy); string Name = TypeNames[STy]; printType(STy, Name, true); Out << ";\n"; } - } - // If it is an array, check it's type and continue - else if (const ArrayType *ATy = dyn_cast(Ty)){ + // If it is an array, check contained types and continue + } else if (const ArrayType *ATy = dyn_cast(Ty)){ const Type *Ty1 = ATy->getElementType(); if (isa(Ty1) || isa(Ty1)) - parseStruct(Ty1); + printContainedStructs(Ty1, StructPrinted); } } diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index 222bad3dd29..444a148fc16 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -36,7 +36,6 @@ namespace { const Module *TheModule; map TypeNames; std::set MangledGlobals; - std::set StructPrinted; public: CWriter(ostream &o) : Out(o) {} @@ -74,9 +73,9 @@ namespace { private : bool nameAllUsedStructureTypes(Module &M); - void parseStruct(const Type *Ty); void printModule(Module *M); void printSymbolTable(const SymbolTable &ST); + void printContainedStructs(const Type *Ty, std::set &); void printGlobal(const GlobalVariable *GV); void printFunctionSignature(const Function *F, bool Prototype); @@ -594,6 +593,9 @@ void CWriter::printSymbolTable(const SymbolTable &ST) { Out << "\n"; + // Keep track of which structures have been printed so far... + std::set StructPrinted; + // Loop over all structures then push them into the stack so they are // printed in the correct order. for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { @@ -602,37 +604,38 @@ void CWriter::printSymbolTable(const SymbolTable &ST) { for (; I != End; ++I) if (const StructType *STy = dyn_cast(I->second)) - parseStruct(STy); + printContainedStructs(STy, StructPrinted); } } // Push the struct onto the stack and recursively push all structs // this one depends on. -void CWriter::parseStruct(const Type *Ty) { +void CWriter::printContainedStructs(const Type *Ty, + std::set &StructPrinted){ if (const StructType *STy = dyn_cast(Ty)){ //Check to see if we have already printed this struct - if (StructPrinted.find(STy) == StructPrinted.end()){ + if (StructPrinted.count(STy) == 0) { + // Print all contained types first... for (StructType::ElementTypes::const_iterator I = STy->getElementTypes().begin(), E = STy->getElementTypes().end(); I != E; ++I) { - const Type *Ty1 = dyn_cast(I->get()); + const Type *Ty1 = I->get(); if (isa(Ty1) || isa(Ty1)) - parseStruct(Ty1); + printContainedStructs(Ty1, StructPrinted); } - //Print struct + //Print structure type out.. StructPrinted.insert(STy); string Name = TypeNames[STy]; printType(STy, Name, true); Out << ";\n"; } - } - // If it is an array, check it's type and continue - else if (const ArrayType *ATy = dyn_cast(Ty)){ + // If it is an array, check contained types and continue + } else if (const ArrayType *ATy = dyn_cast(Ty)){ const Type *Ty1 = ATy->getElementType(); if (isa(Ty1) || isa(Ty1)) - parseStruct(Ty1); + printContainedStructs(Ty1, StructPrinted); } }