Skip to content

Commit

Permalink
Add another example program.
Browse files Browse the repository at this point in the history
  • Loading branch information
nominolo committed Oct 20, 2008
1 parent 9fc53b0 commit c3b30af
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
53 changes: 53 additions & 0 deletions examples/CallGraph0.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Main where

import WriteDotGraph

import GHC
import Scion
import Scion.Inspect
import Bag ( bagToList )
import Outputable
--import TcRnTypes
import HscTypes
import SrcLoc

import GHC.Uniplate.Instances

import Control.Monad
import System.Exit

main = runScion $ do
setWorkingDir "../ghc/compiler" -- "../mtl-1.1.0.2" --"../ghc/compiler"
openCabalProject "./dist-stage2" --"dist" --"./dist-stage2"
setDynFlagsFromCabal Library
setTargetsFromCabal Library
addCmdLineFlags ["-DSTAGE=2"]
handleSourceError print_error_and_exit $ do
mss <- modulesInDepOrder
io $ print (length mss)
deps <- forM mss $ \ms -> do
--mod <- loadModule =<< typecheckModule =<< parseModule ms
--clearWarnings
return ( moduleNameString (ms_mod_name ms)
++ hscSourceString (ms_hsc_src ms),
map (moduleNameString . unLoc) (ms_srcimps ms)
++ map (moduleNameString . unLoc) (ms_imps ms) )
io $ writeFile "deps.dot" $
writeDotGraph [ (mod, imp) | (mod, imps) <- deps
, imp <- imps ]

{-
let binds = bagToList (typecheckedSource mod)
let Just rn@(grp, _, _, _, _) = renamedSource mod
io $ putStrLn $ moduleNameString (ms_mod_name ms)
-- io $ putStrLn $ showSDoc $ nest 4 $ ppr $ typeDecls mod
-- io $ putStrLn $ showSDoc $ nest 4 $ ppr $ classDecls rn
pp $ ppr $ tcg_dus (fst (tm_internals mod))
return ()
-}
print_error_and_exit err = do
printExceptionAndWarnings err
io $ exitWith (ExitFailure 1)

pp :: SDoc -> ScionM ()
pp = io . putStrLn . showSDoc
27 changes: 27 additions & 0 deletions examples/WriteDotGraph.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

module WriteDotGraph (writeDotGraph) where

import Data.List (nub)

writeDotGraph :: (Show node, Eq node) => [(node, node)] -> String
writeDotGraph edges =
unlines (
[header
,graphDefaultAtribs
,nodeDefaultAtribs
,edgeDefaultAtribs]
++ map makeNode nodes
++ map makeEdge edges
++ [footer]
)
where nodes = nub $ concat [ [a,b] | (a,b) <- edges ]
makeNode name = "\t" ++ show (show name) ++ " [];"
makeEdge (node1, node2) =
"\t" ++ show (show node1) ++ " -> " ++ show (show node2) ++ "[];"

header = "digraph g {"
footer = "}"

graphDefaultAtribs = "\tgraph [fontsize=14, fontcolor=black, color=black];"
nodeDefaultAtribs = "\tnode [label=\"\\N\", width=\"0.75\", shape=ellipse];"
edgeDefaultAtribs = "\tedge [fontsize=10];"
10 changes: 9 additions & 1 deletion scion.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ executable test_get_imports
build-depends: ghc-paths >= 0.1 && < 0.2,
ghc >= 6.10 && < 6.12,
ghc-syb >= 0.1 && < 0.2
other-modules: Scion
other-modules: Scion

executable test_call_graph
main-is: CallGraph0.hs
hs-source-dirs: examples
src
build-depends: ghc >= 6.10 && < 6.12,
ghc-uniplate == 0.1.*
other-modules: Scion

0 comments on commit c3b30af

Please sign in to comment.