cindex-includes.py 1.6 KB

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