xref: /netbsd-src/external/gpl3/gcc/dist/contrib/header-tools/show-headers (revision f9a78e0e885f664fa1b5fd1637673b39c1aa53b3)
1#! /usr/bin/python2
2import os.path
3import sys
4import shlex
5import re
6
7from headerutils import *
8
9
10tabstop = 2
11padding = "                                                                  "
12seen = { }
13output = list()
14summary = list()
15sawcore = False
16
17# list of headers to emphasize
18highlight = list ()
19
20bld_dir = ""
21# search path for headers
22incl_dirs = ["../include", "../libcpp/include", "common", "c-family", "c", "cp", "config" ]
23# extra search paths to look in *after* the directory the source file is in.
24
25# append (1) to the end of the first line which includes INC in list INC.
26def append_1 (output, inc):
27  for n,t in enumerate (output):
28    idx = t.find(inc)
29    if idx != -1:
30      eos = idx + len (inc)
31      t = t[:eos] + "  (1)" + t[eos+1:]
32      output[n] = t
33      return
34
35# These headers show up as duplicates in rtl.h due to conditional code arund the includes
36rtl_core = [ "machmode.h" , "signop.h" , "wide-int.h" , "double-int.h" , "real.h" , "fixed-value.h" , "statistics.h" , "vec.h" , "hash-table.h" , "hash-set.h" , "input.h" , "is-a.h" ]
37
38def find_include_data (inc):
39  global sawcore
40  for x in incl_dirs:
41    nm = x+"/"+inc
42    if os.path.exists (nm):
43      info = find_unique_include_list (nm)
44      # rtl.h mimics coretypes for GENERATOR FILES, remove if coretypes.h seen.
45      if inc == "coretypes.h":
46        sawcore = True
47      elif inc  == "rtl.h" and sawcore:
48        for i in rtl_core:
49          if i in info:
50            info.remove (i)
51      return info
52  return list()
53
54def process_include (inc, indent):
55  if inc[-2:] != ".h":
56    return
57  bname  = os.path.basename (inc)
58  if bname in highlight:
59    arrow = "                <<-------"
60    if bname not in summary:
61      summary.append (bname)
62  else:
63    arrow = ""
64  if seen.get(inc) == None:
65    seen[inc] = 1
66    output.append (padding[:indent*tabstop] + bname + arrow)
67    info = find_include_data (inc)
68    for y in info:
69      process_include (y, indent+1)
70  else:
71    seen[inc] += 1
72    if (seen[inc] == 2):
73      append_1(output, inc)
74    output.append (padding[:indent*tabstop] + bname + "  ("+str(seen[inc])+")" + arrow)
75
76
77
78extradir = list()
79usage = False
80src = list()
81
82for x in sys.argv[1:]:
83  if x[0:2] == "-i":
84    bld = x[2:]
85    extradir.append (bld)
86  elif x[0:2] == "-s":
87    highlight.append (os.path.basename (x[2:]))
88  elif x[0:2] == "-h":
89    usage = True
90  else:
91    src.append (x)
92
93if len(src) != 1:
94  usage = True
95elif not os.path.exists (src[0]):
96  print src[0] + ": Requested source file does not exist.\n"
97  usage = True
98
99if usage:
100  print "show-headers [-idir] [-sfilen] file1 "
101  print " "
102  print " Show a hierarchical visual format how many times each header file"
103  print " is included in a source file.  Should be run from the source directory"
104  print " files from find-include-depends"
105  print "      -s : search for a header, and point it out."
106  print "      -i : Specifies additonal directories to search for includes."
107  sys.exit(0)
108
109
110
111if extradir:
112  incl_dirs = extradir + incl_dirs;
113
114blddir = find_gcc_bld_dir ("../..")
115
116if blddir:
117  print "Using build directory: " + blddir
118  incl_dirs.insert (0, blddir)
119else:
120  print "Could not find a build directory, better results if you specify one with -i"
121
122# search path is now ".", blddir, extradirs_from_-i, built_in_incl_dirs
123incl_dirs.insert (0, ".")
124
125# if source is in a subdirectory, prepend the subdirectory to the search list
126x = src[0]
127srcpath = os.path.dirname(x)
128if srcpath:
129  incl_dirs.insert (0, srcpath)
130
131output = list()
132sawcore = False
133
134data = open (x).read().splitlines()
135for line in data:
136  d = find_pound_include (line, True, True)
137  if d and d[-2:] == ".h":
138    process_include (d, 1)
139
140print "\n" + x
141for line in output:
142  print line
143
144if highlight:
145  print " "
146  for h in summary:
147    print h + " is included by source file."
148  for h in highlight:
149    if h not in summary:
150      print h + " is not included by source file."
151
152