Here's my somewhat raw solution for building a call tree. It looks for proc names which appear at the beginning of a line (ignoring leading spaces and tabs) or immediately after an opening square bracket. It has several other limitations, e.g. it doesn't consider multiple tcl commands on the same line (separated by ;
), and it also matches proc names if they appear after square brackets inside curly brackets.
The result are two arrays called
and isCalledBy
which can be used for further analysis.
# https://stackoverflow.com/a/15043787/3852630
proc reEscape {str} {
regsub -all {\W} $str {\\&}
}
proc buildCallTree {} {
set procNameList [info procs]
foreach caller $procNameList {
puts $caller
foreach callee $procNameList {
set escCallee [reEscape $callee]
set calleeRE [string cat {(^|[^\\]\[)} $escCallee {($|[\s\]])}]
set callerBodyLines [split [info body $caller] \n]
foreach callerBodyLine $callerBodyLines {
set callerBodyLine [string trim $callerBodyLine]
if {[string index $callerBodyLine 0] != "#"} {
if {[regexp $calleeRE $callerBodyLine]} {
lappend ::calls($caller) $callee
lappend ::isCalledBy($callee) $caller
break
}
}
}
}
}
}
Any comments or suggestions for improvements are welcome.
Edit: Code now ignores proc names after escaped opening square brackets.