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