Home | History | Annotate | Line # | Download | only in header-tools
      1 #! /usr/bin/python2
      2 import os.path
      3 import sys
      4 import shlex
      5 import re
      6 
      7 from headerutils import *
      8 
      9 
     10 tabstop = 2
     11 padding = "                                                                  "
     12 seen = { }
     13 output = list()
     14 summary = list()
     15 sawcore = False
     16 
     17 # list of headers to emphasize
     18 highlight = list ()
     19 
     20 bld_dir = ""
     21 # search path for headers
     22 incl_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.
     26 def 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
     36 rtl_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 
     38 def 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 
     54 def 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 
     78 extradir = list()
     79 usage = False
     80 src = list()
     81 
     82 for 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 
     93 if len(src) != 1:
     94   usage = True
     95 elif not os.path.exists (src[0]):
     96   print src[0] + ": Requested source file does not exist.\n"
     97   usage = True
     98 
     99 if 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 
    111 if extradir:
    112   incl_dirs = extradir + incl_dirs;
    113 
    114 blddir = find_gcc_bld_dir ("../..")
    115 
    116 if blddir:
    117   print "Using build directory: " + blddir
    118   incl_dirs.insert (0, blddir)
    119 else:
    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
    123 incl_dirs.insert (0, ".")
    124 
    125 # if source is in a subdirectory, prepend the subdirectory to the search list
    126 x = src[0]
    127 srcpath = os.path.dirname(x)
    128 if srcpath:
    129   incl_dirs.insert (0, srcpath)
    130 
    131 output = list()
    132 sawcore = False
    133 
    134 data = open (x).read().splitlines()
    135 for line in data:
    136   d = find_pound_include (line, True, True)
    137   if d and d[-2:] == ".h":
    138     process_include (d, 1)
    139 
    140 print "\n" + x
    141 for line in output:
    142   print line
    143 
    144 if 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