make dependency analyser free of global variables
This commit is contained in:
parent
73799903ee
commit
006ee79a91
2 changed files with 17 additions and 10 deletions
|
|
@ -16,35 +16,39 @@ from modulegraphs import ModuleGraph
|
||||||
|
|
||||||
type
|
type
|
||||||
TGen = object of TPassContext
|
TGen = object of TPassContext
|
||||||
module*: PSym
|
module: PSym
|
||||||
config: ConfigRef
|
config: ConfigRef
|
||||||
|
graph: ModuleGraph
|
||||||
PGen = ref TGen
|
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) =
|
proc addDependencyAux(b: Backend; importing, imported: string) =
|
||||||
addf(gDotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
|
addf(b.dotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
|
||||||
# s1 -> s2_4[label="[0-9]"];
|
# s1 -> s2_4[label="[0-9]"];
|
||||||
|
|
||||||
proc addDotDependency(c: PPassContext, n: PNode): PNode =
|
proc addDotDependency(c: PPassContext, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
var g = PGen(c)
|
let g = PGen(c)
|
||||||
|
let b = Backend(g.graph.backend)
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkImportStmt:
|
of nkImportStmt:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var imported = getModuleName(g.config, n.sons[i])
|
var imported = getModuleName(g.config, n.sons[i])
|
||||||
addDependencyAux(g.module.name.s, imported)
|
addDependencyAux(b, g.module.name.s, imported)
|
||||||
of nkFromStmt, nkImportExceptStmt:
|
of nkFromStmt, nkImportExceptStmt:
|
||||||
var imported = getModuleName(g.config, n.sons[0])
|
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:
|
of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
|
||||||
for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
|
for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
|
||||||
else:
|
else:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc generateDot*(project: string) =
|
proc generateDot*(graph: ModuleGraph; project: string) =
|
||||||
|
let b = Backend(graph.backend)
|
||||||
discard writeRope("digraph $1 {$n$2}$n" % [
|
discard writeRope("digraph $1 {$n$2}$n" % [
|
||||||
rope(changeFileExt(extractFilename(project), "")), gDotGraph],
|
rope(changeFileExt(extractFilename(project), "")), b.dotGraph],
|
||||||
changeFileExt(project, "dot"))
|
changeFileExt(project, "dot"))
|
||||||
|
|
||||||
proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
|
proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
|
||||||
|
|
@ -52,6 +56,9 @@ proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
|
||||||
new(g)
|
new(g)
|
||||||
g.module = module
|
g.module = module
|
||||||
g.config = graph.config
|
g.config = graph.config
|
||||||
|
g.graph = graph
|
||||||
|
if graph.backend == nil:
|
||||||
|
graph.backend = Backend(dotGraph: nil)
|
||||||
result = g
|
result = g
|
||||||
|
|
||||||
const gendependPass* = makePass(open = myOpen, process = addDotDependency)
|
const gendependPass* = makePass(open = myOpen, process = addDotDependency)
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ proc commandGenDepend(graph: ModuleGraph; cache: IdentCache) =
|
||||||
compileProject(graph, cache)
|
compileProject(graph, cache)
|
||||||
let project = graph.config.projectFull
|
let project = graph.config.projectFull
|
||||||
writeDepsFile(graph, project)
|
writeDepsFile(graph, project)
|
||||||
generateDot(project)
|
generateDot(graph, project)
|
||||||
execExternalProgram(graph.config, "dot -Tpng -o" & changeFileExt(project, "png") &
|
execExternalProgram(graph.config, "dot -Tpng -o" & changeFileExt(project, "png") &
|
||||||
' ' & changeFileExt(project, "dot"))
|
' ' & changeFileExt(project, "dot"))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue