+}
+
+/// This method inserts names for any unnamed structure types that are used by
+/// the program, and removes names from structure types that are not used by the
+/// program.
+///
+bool CBackendNameAllUsedStructs::runOnModule(Module &M) {
+ // Get a set of types that are used by the program...
+ std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
+
+ // Loop over the module symbol table, removing types from UT that are
+ // already named, and removing names for structure types that are not used.
+ //
+ SymbolTable &MST = M.getSymbolTable();
+ for (SymbolTable::type_iterator TI = MST.type_begin(), TE = MST.type_end();
+ TI != TE; ) {
+ SymbolTable::type_iterator I = TI++;
+ if (const StructType *STy = dyn_cast<StructType>(I->second)) {
+ // If this is not used, remove it from the symbol table.
+ std::set<const Type *>::iterator UTI = UT.find(STy);
+ if (UTI == UT.end())
+ MST.remove(I);
+ else
+ UT.erase(UTI);
+ }
+ }
+
+ // UT now contains types that are not named. Loop over it, naming
+ // structure types.
+ //
+ bool Changed = false;
+ unsigned RenameCounter = 0;
+ for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
+ I != E; ++I)
+ if (const StructType *ST = dyn_cast<StructType>(*I)) {
+ while (M.addTypeName("unnamed"+utostr(RenameCounter), ST))
+ ++RenameCounter;
+ Changed = true;
+ }
+ return Changed;
+}
+