+static void AddModuleTypesToPrinter(TypePrinting &TP, const Module *M) {
+ if (M == 0) return;
+
+ // If the module has a symbol table, take all global types and stuff their
+ // names into the TypeNames map.
+ const TypeSymbolTable &ST = M->getTypeSymbolTable();
+ for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
+ TI != E; ++TI) {
+ const Type *Ty = cast<Type>(TI->second);
+
+ // As a heuristic, don't insert pointer to primitive types, because
+ // they are used too often to have a single useful name.
+ if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
+ const Type *PETy = PTy->getElementType();
+ if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
+ !isa<OpaqueType>(PETy))
+ continue;
+ }
+
+ // Likewise don't insert primitives either.
+ if (Ty->isInteger() || Ty->isPrimitiveType())
+ continue;
+
+ // Get the name as a string and insert it into TypeNames.
+ std::string NameStr;
+ raw_string_ostream NameOS(NameStr);
+ PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix);
+ TP.addTypeName(Ty, NameOS.str());
+ }
+}
+