+
+
+%*********************************************************
+%* *
+\subsection{Unused names}
+%* *
+%*********************************************************
+
+\begin{code}
+reportUnusedNames gbl_env avail_env (ExportEnv export_avails _) mentioned_names
+ | not (opt_WarnUnusedBinds || opt_WarnUnusedImports)
+ = returnRn ()
+
+ | otherwise
+ = let
+ used_names = mentioned_names `unionNameSets` availsToNameSet export_avails
+
+ -- Now, a use of C implies a use of T,
+ -- if C was brought into scope by T(..) or T(C)
+ really_used_names = used_names `unionNameSets`
+ mkNameSet [ availName avail
+ | sub_name <- nameSetToList used_names,
+ let avail = case lookupNameEnv avail_env sub_name of
+ Just avail -> avail
+ Nothing -> pprTrace "r.u.n" (ppr sub_name) $
+ Avail sub_name
+ ]
+
+ defined_names = mkNameSet (concat (rdrEnvElts gbl_env))
+ defined_but_not_used =
+ nameSetToList (defined_names `minusNameSet` really_used_names)
+
+ -- Filter out the ones only defined implicitly
+ bad_guys = filter reportableUnusedName defined_but_not_used
+ in
+ warnUnusedTopNames bad_guys `thenRn_`
+ returnRn ()
+
+reportableUnusedName :: Name -> Bool
+reportableUnusedName name
+ = explicitlyImported (getNameProvenance name) &&
+ not (startsWithUnderscore (occNameUserString (nameOccName name)))
+ where
+ explicitlyImported (LocalDef _ _) = True
+ -- Report unused defns of local vars
+ explicitlyImported (NonLocalDef (UserImport _ _ expl) _) = expl
+ -- Report unused explicit imports
+ explicitlyImported other = False
+ -- Don't report others
+
+ -- Haskell 98 encourages compilers to suppress warnings about
+ -- unused names in a pattern if they start with "_".
+ startsWithUnderscore ('_' : _) = True
+ -- Suppress warnings for names starting with an underscore
+ startsWithUnderscore other = False
+
+rnStats :: [RenamedHsDecl] -> RnMG ()
+rnStats imp_decls
+ | opt_D_dump_rn_trace ||
+ opt_D_dump_rn_stats ||
+ opt_D_dump_rn
+ = getRnStats imp_decls `thenRn` \ msg ->
+ ioToRnM (printErrs msg) `thenRn_`
+ returnRn ()
+
+ | otherwise = returnRn ()
+\end{code}
+
+
+
+%*********************************************************
+%* *
+\subsection{Statistics}
+%* *
+%*********************************************************
+
+\begin{code}
+getRnStats :: [RenamedHsDecl] -> RnMG SDoc
+getRnStats imported_decls
+ = getIfacesRn `thenRn` \ ifaces ->
+ let
+ n_mods = length [() | (_, _, Just _) <- eltsFM (iImpModInfo ifaces)]
+
+ decls_read = [decl | (_, avail, True, (_,decl)) <- nameEnvElts (iDecls ifaces),
+ -- Data, newtype, and class decls are in the decls_fm
+ -- under multiple names; the tycon/class, and each
+ -- constructor/class op too.
+ -- The 'True' selects just the 'main' decl
+ not (isLocallyDefined (availName avail))
+ ]
+
+ (cd_rd, dd_rd, nd_rd, sd_rd, vd_rd, _) = count_decls decls_read
+ (cd_sp, dd_sp, nd_sp, sd_sp, vd_sp, id_sp) = count_decls imported_decls
+
+ unslurped_insts = iInsts ifaces
+ inst_decls_unslurped = length (bagToList unslurped_insts)
+ inst_decls_read = id_sp + inst_decls_unslurped
+
+ stats = vcat
+ [int n_mods <+> text "interfaces read",
+ hsep [ int cd_sp, text "class decls imported, out of",
+ int cd_rd, text "read"],
+ hsep [ int dd_sp, text "data decls imported, out of",
+ int dd_rd, text "read"],
+ hsep [ int nd_sp, text "newtype decls imported, out of",
+ int nd_rd, text "read"],
+ hsep [int sd_sp, text "type synonym decls imported, out of",
+ int sd_rd, text "read"],
+ hsep [int vd_sp, text "value signatures imported, out of",
+ int vd_rd, text "read"],
+ hsep [int id_sp, text "instance decls imported, out of",
+ int inst_decls_read, text "read"],
+ text "cls dcls slurp" <+> fsep (map (ppr . tyClDeclName)
+ [d | TyClD d <- imported_decls, isClassDecl d]),
+ text "cls dcls read" <+> fsep (map (ppr . tyClDeclName)
+ [d | TyClD d <- decls_read, isClassDecl d])]
+ in
+ returnRn (hcat [text "Renamer stats: ", stats])
+
+count_decls decls
+ = (class_decls,
+ data_decls,
+ newtype_decls,
+ syn_decls,
+ val_decls,
+ inst_decls)
+ where
+ tycl_decls = [d | TyClD d <- decls]
+ (class_decls, data_decls, newtype_decls, syn_decls) = countTyClDecls tycl_decls
+
+ val_decls = length [() | SigD _ <- decls]
+ inst_decls = length [() | InstD _ <- decls]
+\end{code}
+