cindex-includes.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. #===- cindex-includes.py - cindex/Python Inclusion Graph -----*- python -*--===#
  3. #
  4. # Copyright (C) Microsoft Corporation. All rights reserved.
  5. # This file is distributed under the University of Illinois Open Source License. See LICENSE.TXT for details.
  6. #
  7. #===------------------------------------------------------------------------===#
  8. """
  9. A simple command line tool for dumping a Graphviz description (dot) that
  10. describes include dependencies.
  11. """
  12. def main():
  13. import sys
  14. from clang.cindex import Index
  15. from optparse import OptionParser, OptionGroup
  16. parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
  17. parser.disable_interspersed_args()
  18. (opts, args) = parser.parse_args()
  19. if len(args) == 0:
  20. parser.error('invalid number arguments')
  21. # FIXME: Add an output file option
  22. out = sys.stdout
  23. index = Index.create()
  24. tu = index.parse(None, args)
  25. if not tu:
  26. parser.error("unable to load input")
  27. # A helper function for generating the node name.
  28. def name(f):
  29. if f:
  30. return "\"" + f.name + "\""
  31. # Generate the include graph
  32. out.write("digraph G {\n")
  33. for i in tu.get_includes():
  34. line = " ";
  35. if i.is_input_file:
  36. # Always write the input file as a node just in case it doesn't
  37. # actually include anything. This would generate a 1 node graph.
  38. line += name(i.include)
  39. else:
  40. line += '%s->%s' % (name(i.source), name(i.include))
  41. line += "\n";
  42. out.write(line)
  43. out.write("}\n")
  44. if __name__ == '__main__':
  45. main()