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