Skip to content

Instantly share code, notes, and snippets.

@theodoregoetz
Created January 1, 2016 00:23
Show Gist options
  • Save theodoregoetz/f6ded398c8207bef0ce7 to your computer and use it in GitHub Desktop.
Save theodoregoetz/f6ded398c8207bef0ce7 to your computer and use it in GitHub Desktop.
C++ header include graph
#!/usr/bin/env python3
import sys
import os
import re
from os import path
from glob import glob
from graph_tool.all import *
ignore_dirs = '''
_build
.svn
'''.split()
include_pattern = re.compile('^\s*#\s*include\s*(<(.*)>|"(.*)")')
def included_headers(filename):
ret = []
with open(filename,'r') as fin:
for m in include_pattern.findall(fin.read().decode(),re.MULTILINE):
ret.append(m.group(2) or m.group(3))
return ret
g = Graph()
header_names = g.new_vertex_property('string')
header_types = g.new_vertex_property('string')
vertices = {}
for root,dirs,files in os.walk(sys.argv[1]):
for d in ignore_dirs:
if d in dirs:
dirs.remove(d)
for f in files:
if path.splitext(f)[-1] == '.h':
print(f)
v = g.add_vertex()
vertices[f] = v
header_names[v] = f
header_types[v] = 'internal'
for root,dirs,files in os.walk(sys.argv[1]):
for d in ignore_dirs:
if d in dirs:
dirs.remove(d)
for f in files:
if path.splitext(f)[-1] in ['.h','.cpp']:
if f in header_names:
headers = included_headers(path.join(root,f))
for h in headers:
if h in header_names:
print(f,'-->',h)
g.add_edge(vertices[f],vertices[h])
else:
print(h)
v = g.add_vertex()
vertices[h] = v
header_names[v] = h
header_types[v] = 'external'
g.vertex_properties['header_name'] = header_name
g.vertex_properties['header_type'] = header_type
g.save('header_graph.xml.gz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment