Daniel Dunbar | 43813bf | 2010-02-13 18:33:18 +0000 | [diff] [blame^] | 1 | #!/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 | """ |
| 13 | A simple command line tool for dumping a Graphviz description (dot) that |
| 14 | describes include dependencies. |
| 15 | """ |
| 16 | |
| 17 | def main(): |
| 18 | import sys |
| 19 | from clang.cindex import Index |
| 20 | |
| 21 | # FIXME: Allow the user to pass command line options to clang so that |
| 22 | # we can use -D and -U. |
| 23 | from optparse import OptionParser, OptionGroup |
| 24 | |
| 25 | parser = OptionParser("usage: %prog [options] {filename} [clang-args*]") |
| 26 | parser.disable_interspersed_args() |
| 27 | (opts, args) = parser.parse_args() |
| 28 | if len(args) == 0: |
| 29 | parser.error('invalid number arguments') |
| 30 | |
| 31 | # FIXME: Add an output file option |
| 32 | out = sys.stdout |
| 33 | |
| 34 | input_path = args.pop(0) |
| 35 | |
| 36 | |
| 37 | index = Index.create() |
| 38 | tu = index.parse(input_path, args) |
| 39 | if not tu: |
| 40 | parser.error("unable to load input") |
| 41 | |
| 42 | # A helper function for generating the node name. |
| 43 | def name(f): |
| 44 | return "\"" + f.name + "\"" |
| 45 | |
| 46 | # Generate the include graph |
| 47 | out.write("digraph G {\n") |
| 48 | for i in tu.get_includes(): |
| 49 | line = " "; |
| 50 | if i.is_input_file: |
| 51 | # Always write the input file as a node just in case it doesn't |
| 52 | # actually include anything. This would generate a 1 node graph. |
| 53 | line += name(i.include) |
| 54 | else: |
| 55 | line += name(i.source) + "->" + name(i.include) |
| 56 | line += "\n"; |
| 57 | out.write(line) |
| 58 | out.write("}\n") |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | main() |
| 62 | |