show-headers 3.8 KB

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