@@ -116,8 +116,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
116116 for (const auto &typeDef : ir.typeDefs ) {
117117 if (ir.shouldOutput (typeDef)) {
118118 s << *typeDef;
119- } else if (isAliasForOpaqueType (typeDef.get ()) &&
120- ir.inMainFile (*typeDef)) {
119+ } else if (typeDef->hasLocation () &&
120+ isAliasForOpaqueType (typeDef.get ()) &&
121+ ir.locationManager .inMainFile (*typeDef->getLocation ())) {
121122 llvm::errs () << " Warning: type alias " + typeDef->getName ()
122123 << " is skipped because it is an unused alias for "
123124 " incomplete type."
@@ -271,7 +272,8 @@ void IR::replaceTypeInTypeDefs(std::shared_ptr<const Type> oldType,
271272
272273template <typename T>
273274bool IR::isTypeUsed (const std::vector<T> &declarations,
274- std::shared_ptr<Type> type, bool stopOnTypeDefs) const {
275+ std::shared_ptr<const Type> type,
276+ bool stopOnTypeDefs) const {
275277 for (const auto &decl : declarations) {
276278 if (decl->usesType (type, stopOnTypeDefs)) {
277279 return true ;
@@ -280,7 +282,8 @@ bool IR::isTypeUsed(const std::vector<T> &declarations,
280282 return false ;
281283}
282284
283- bool IR::typeIsUsedOnlyInTypeDefs (const std::shared_ptr<Type> &type) const {
285+ bool IR::typeIsUsedOnlyInTypeDefs (
286+ const std::shared_ptr<const Type> &type) const {
284287 /* varDefines are not checked here because they are simply
285288 * aliases for variables.*/
286289 return !(
@@ -289,7 +292,7 @@ bool IR::typeIsUsedOnlyInTypeDefs(const std::shared_ptr<Type> &type) const {
289292 isTypeUsed (literalDefines, type, true ));
290293}
291294
292- bool IR::isTypeUsed (const std::shared_ptr<Type> &type,
295+ bool IR::isTypeUsed (const std::shared_ptr<const Type> &type,
293296 bool checkRecursively) const {
294297 if (checkRecursively) {
295298 if (isTypeUsed (functions, type, true ) ||
@@ -437,26 +440,6 @@ IR::~IR() {
437440 varDefines.clear ();
438441}
439442
440- template <typename T> bool IR::inMainFile (const T &type) const {
441- std::shared_ptr<Location> location = type.getLocation ();
442- if (!location) {
443- /* generated TypeDef */
444- auto *typeDef = dynamic_cast <const TypeDef *>(&type);
445- assert (typeDef);
446- const Type *innerType = typeDef->getType ().get ();
447- if (isInstanceOf<Struct>(innerType)) {
448- return inMainFile (*dynamic_cast <const Struct *>(innerType));
449- }
450- if (isInstanceOf<Union>(innerType)) {
451- return inMainFile (*dynamic_cast <const Union *>(innerType));
452- }
453- if (isInstanceOf<Enum>(innerType)) {
454- return inMainFile (*dynamic_cast <const Enum *>(innerType));
455- }
456- }
457- return location && locationManager.inMainFile (*location);
458- }
459-
460443template <typename T>
461444bool IR::hasOutputtedDeclaration (
462445 const std::vector<std::shared_ptr<T>> &declarations) const {
@@ -468,20 +451,13 @@ bool IR::hasOutputtedDeclaration(
468451 return false ;
469452}
470453
471- template <typename T>
472- bool IR::shouldOutput (const std::shared_ptr<T> &type) const {
454+ bool IR::shouldOutput (const std::shared_ptr<const LocatableType> &type) const {
473455 if (isTypeUsed (type, true )) {
474456 return true ;
475457 }
476- if (!inMainFile (*type)) {
477- /* remove unused types from included files */
458+ if (isAliasForOpaqueType (type.get ())) {
478459 return false ;
479460 }
480- auto *typeDef = dynamic_cast <TypeDef *>(type.get ());
481- if (typeDef) {
482- /* unused typedefs from main file are printed only if they are not
483- * aliases for an opaque type. */
484- return !isAliasForOpaqueType (typeDef);
485- }
486- return true ;
461+ /* remove unused types from included files */
462+ return locationManager.inMainFile (*type->getLocation ());
487463}
0 commit comments