make dependency analyser free of global variables

This commit is contained in:
Andreas Rumpf
2018-05-28 00:04:55 +02:00
parent 73799903ee
commit 006ee79a91
2 changed files with 17 additions and 10 deletions

View File

@@ -16,35 +16,39 @@ from modulegraphs import ModuleGraph
type
TGen = object of TPassContext
module*: PSym
module: PSym
config: ConfigRef
graph: ModuleGraph
PGen = ref TGen
var gDotGraph: Rope # the generated DOT file; we need a global variable
Backend = ref object of RootRef
dotGraph: Rope
proc addDependencyAux(importing, imported: string) =
addf(gDotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
proc addDependencyAux(b: Backend; importing, imported: string) =
addf(b.dotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
# s1 -> s2_4[label="[0-9]"];
proc addDotDependency(c: PPassContext, n: PNode): PNode =
result = n
var g = PGen(c)
let g = PGen(c)
let b = Backend(g.graph.backend)
case n.kind
of nkImportStmt:
for i in countup(0, sonsLen(n) - 1):
var imported = getModuleName(g.config, n.sons[i])
addDependencyAux(g.module.name.s, imported)
addDependencyAux(b, g.module.name.s, imported)
of nkFromStmt, nkImportExceptStmt:
var imported = getModuleName(g.config, n.sons[0])
addDependencyAux(g.module.name.s, imported)
addDependencyAux(b, g.module.name.s, imported)
of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
else:
discard
proc generateDot*(project: string) =
proc generateDot*(graph: ModuleGraph; project: string) =
let b = Backend(graph.backend)
discard writeRope("digraph $1 {$n$2}$n" % [
rope(changeFileExt(extractFilename(project), "")), gDotGraph],
rope(changeFileExt(extractFilename(project), "")), b.dotGraph],
changeFileExt(project, "dot"))
proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
@@ -52,6 +56,9 @@ proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
new(g)
g.module = module
g.config = graph.config
g.graph = graph
if graph.backend == nil:
graph.backend = Backend(dotGraph: nil)
result = g
const gendependPass* = makePass(open = myOpen, process = addDotDependency)

View File

@@ -48,7 +48,7 @@ proc commandGenDepend(graph: ModuleGraph; cache: IdentCache) =
compileProject(graph, cache)
let project = graph.config.projectFull
writeDepsFile(graph, project)
generateDot(project)
generateDot(graph, project)
execExternalProgram(graph.config, "dot -Tpng -o" & changeFileExt(project, "png") &
' ' & changeFileExt(project, "dot"))