Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / bindings / python / examples / cindex / cindex-includes.py
blob4e8bae19005cdfd76564146dc75207ed634b09b0
1 #!/usr/bin/env python
3 # ===- cindex-includes.py - cindex/Python Inclusion Graph -----*- python -*--===#
5 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 # See https://llvm.org/LICENSE.txt for license information.
7 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 # ===------------------------------------------------------------------------===#
11 """
12 A simple command line tool for dumping a Graphviz description (dot) that
13 describes include dependencies.
14 """
17 def main():
18 import sys
19 from clang.cindex import Index
21 from optparse import OptionParser, OptionGroup
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")
29 # FIXME: Add an output file option
30 out = sys.stdout
32 index = Index.create()
33 tu = index.parse(None, args)
34 if not tu:
35 parser.error("unable to load input")
37 # A helper function for generating the node name.
38 def name(f):
39 if f:
40 return '"' + f.name + '"'
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:
51 line += "%s->%s" % (name(i.source), name(i.include))
52 line += "\n"
53 out.write(line)
54 out.write("}\n")
57 if __name__ == "__main__":
58 main()