Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Source code include visualiser for c++
import sys
import os.path
import re
# Use this like this:
# cppfind . -l autogen | grep -v test | python | neato -otest3.pdf -Tpdf -Goverlap=scale -Gstart=rand
# you can replace neato with dot or any of the other graphvis commands
# What the example does is finds all files using autogen in them anywhere, and graphs who includes what (directly)
# This can be handy to find groups of includes that could be combined into one mega include file
# Or to find odd dependencies.
class FileInfo(object):
def key(self):
return self.m_key
def full_path(self):
return self.m_full_path
def includes(self):
return self.m_includes
def as_key( fileinfos, id ):
#Slice off any relative path mumbojumbo
idx = id.rfind('../')
if idx!=-1:
id = id[(idx+3):]
potentials = [ f for f in fileinfos if f.full_path().endswith(id) ]
if len(potentials)==0:
return id
return potentials[0].key()
def write_dot_file( fileinfos ):
print "digraph {"
for f in fileinfos:
print '"'+f.key() + '" [];'
for i in f.includes():
print '"'+f.key() +'" -> "'+as_key( fileinfos, i )+'";'
print "}"
def parse_file( filename ):
retval = FileInfo()
retval.m_key = os.path.basename(filename)
retval.m_full_path = filename
retval.m_includes = []
f = open(filename,"r")
for line in f:
m = re.match(r'#include\s+"(.*)"',line)
if m:
retval.m_includes.append( )
m = re.match(r'#include\s+<(.*)>',line)
if m:
retval.m_includes.append( )
return retval
fileinfos = []
for fn in sys.stdin.readlines():
fi = parse_file(fn.strip())
#fi = parse_file("dev/tmp/hello.h")
#fi = parse_file("lib/src/johnny.h")
write_dot_file( fileinfos )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment