blob: 17500227a349b14bfdae7e4fbeb2d387245826c0 [file] [log] [blame]
Daniel Dunbaref7f7982010-02-13 18:33:18 +00001#!/usr/bin/env python
2
3#===- cindex-includes.py - cindex/Python Inclusion Graph -----*- python -*--===#
4#
5# The LLVM Compiler Infrastructure
6#
7# This file is distributed under the University of Illinois Open Source
8# License. See LICENSE.TXT for details.
9#
10#===------------------------------------------------------------------------===#
11
12"""
13A simple command line tool for dumping a Graphviz description (dot) that
14describes include dependencies.
15"""
16
17def main():
18 import sys
19 from clang.cindex import Index
20
Daniel Dunbaref7f7982010-02-13 18:33:18 +000021 from optparse import OptionParser, OptionGroup
22
23 parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
24 parser.disable_interspersed_args()
25 (opts, args) = parser.parse_args()
26 if len(args) == 0:
27 parser.error('invalid number arguments')
28
29 # FIXME: Add an output file option
30 out = sys.stdout
31
Daniel Dunbaref7f7982010-02-13 18:33:18 +000032 index = Index.create()
Daniel Dunbar8bb44d52010-02-13 18:33:28 +000033 tu = index.parse(None, args)
Daniel Dunbaref7f7982010-02-13 18:33:18 +000034 if not tu:
35 parser.error("unable to load input")
36
37 # A helper function for generating the node name.
38 def name(f):
Daniel Dunbar8bb44d52010-02-13 18:33:28 +000039 if f:
40 return "\"" + f.name + "\""
Daniel Dunbaref7f7982010-02-13 18:33:18 +000041
42 # Generate the include graph
43 out.write("digraph G {\n")
44 for i in tu.get_includes():
45 line = " ";
46 if i.is_input_file:
47 # Always write the input file as a node just in case it doesn't
48 # actually include anything. This would generate a 1 node graph.
49 line += name(i.include)
50 else:
Daniel Dunbar8bb44d52010-02-13 18:33:28 +000051 line += '%s->%s' % (name(i.source), name(i.include))
Daniel Dunbaref7f7982010-02-13 18:33:18 +000052 line += "\n";
53 out.write(line)
54 out.write("}\n")
55
56if __name__ == '__main__':
57 main()
58