gcc-order-headers 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #! /usr/bin/python2
  2. import os
  3. import sys
  4. import shlex
  5. import re
  6. from headerutils import *
  7. import Queue
  8. file_list = list ()
  9. usage = False
  10. ignore_conditional = False
  11. order = [
  12. "system.h",
  13. "coretypes.h",
  14. "backend.h",
  15. "target.h",
  16. "rtl.h",
  17. "c-family/c-target.h",
  18. "c-family/c-target-def.h",
  19. "tree.h",
  20. "cp/cp-tree.h",
  21. "c-family/c-common.h", # these must come before diagnostic.h
  22. "c/c-tree.h",
  23. "fortran/gfortran.h",
  24. "gimple.h",
  25. "cfghooks.h",
  26. "df.h",
  27. "tm_p.h",
  28. "gimple-iterators.h",
  29. "ssa.h",
  30. "expmed.h",
  31. "optabs.h",
  32. "regs.h",
  33. "ira.h",
  34. "ira-int.h",
  35. "gimple-streamer.h"
  36. ]
  37. exclude_special = [ "bversion.h", "obstack.h", "insn-codes.h", "hooks.h" ]
  38. # includes is a dictionary indexed by a header files basename.
  39. # it consists of a 2 element tuple:
  40. # [0] - Name of header file which included this header.
  41. # [1] - vector of header file names included by this file.
  42. includes = { }
  43. # when a header is included multiple times, indexing this dictionary will
  44. # return a vector of all the headers which included it.
  45. dups = { }
  46. # When creating the master list, do not descend into these files for what
  47. # they include. Simply put the file itself in the list. This is primarily
  48. # required because the front end files inlcude orders tend to be at odds with
  49. # the order of middle end files, and its impossible to synchronize them.\
  50. # They are ordered such that everything resolves properly.
  51. exclude_processing = [ "tree-vectorizer.h" , "c-target.h", "c-target-def.h", "cp-tree.h", "c-common.h", "c-tree.h", "gfortran.h" ]
  52. master_list = list ()
  53. # where include file comes from in src
  54. h_from = { }
  55. # create the master ordering list... this is the desired order of headers
  56. def create_master_list (fn, verbose):
  57. if fn not in exclude_processing:
  58. for x in includes[fn][1]:
  59. create_master_list (x, verbose)
  60. if not fn in master_list:
  61. # Don't put diagnostic*.h into the ordering list. It is special since
  62. # various front ends have to set GCC_DIAG_STYLE before including it.
  63. # for each file, we'll tailor where it belongs by looking at the include
  64. # list and determine its position appropriately.
  65. if fn != "diagnostic.h" and fn != "diagnostic-core.h":
  66. master_list.append (fn)
  67. if (verbose):
  68. print fn + " included by: " + includes[fn][0]
  69. def print_dups ():
  70. if dups:
  71. print "\nduplicated includes"
  72. for i in dups:
  73. string = "dup : " + i + " : "
  74. string += includes[i][0]
  75. for i2 in dups[i]:
  76. string += ", "+i2
  77. print string
  78. def process_known_dups ():
  79. # rtl.h gets tagged as a duplicate includer for all of coretypes.h, but that
  80. # is really for only generator files
  81. rtl_remove = includes["coretypes.h"][1] + ["statistics.h", "vec.h"]
  82. if dups:
  83. for i in rtl_remove:
  84. if dups[i] and "rtl.h" in dups[i]:
  85. dups[i].remove("rtl.h")
  86. if not dups[i]:
  87. dups.pop (i, None)
  88. # make sure diagnostic.h is the owner of diagnostic-core.h
  89. if includes["diagnostic-core.h"][0] != "diagnostic.h":
  90. dups["diagnostic-core.h"].append (includes["diagnostic-core.h"][0])
  91. includes["diagnostic-core.h"] = ("diagnostic.h", includes["diagnostic-core.h"][1])
  92. # This function scans back thorugh the list of headers which included other
  93. # headers to determine what file in HEADER_LIST brought 'HEADER' in.
  94. def indirectly_included (header, header_list):
  95. nm = os.path.basename (header)
  96. while nm and includes.get(nm):
  97. if includes[nm][0] in header_list:
  98. return includes[nm][0]
  99. nm = includes[nm][0]
  100. # diagnostic.h and diagnostic-core.h may not show up because we removed them
  101. # from the header list to manually position in an appropriate place. They have
  102. # specific requirements that they need to occur after certain FE files which
  103. # may overide the definition of GCC_DIAG_STYLE.
  104. # Check the dup list for whete they may have been included from and return
  105. # that header.
  106. if header == "diagnostic-core.h":
  107. if dups.get("diagnostic-core.h"):
  108. for f in dups["diagnostic-core.h"]:
  109. if f in header_list:
  110. return f
  111. else:
  112. if header in header_list:
  113. return header
  114. # Now check if diagnostics is included indirectly anywhere
  115. header = "diagnostic.h"
  116. if header == "diagnostic.h":
  117. if dups.get("diagnostic.h"):
  118. for f in dups["diagnostic.h"]:
  119. if f in header_list:
  120. return f
  121. else:
  122. if header in header_list:
  123. return header
  124. return ""
  125. # This function will take a list of headers from a source file and return
  126. # the desired new new order of the canonical headers in DESIRED_ORDER.
  127. def get_new_order (src_h, desired_order):
  128. new_order = list ()
  129. for h in desired_order:
  130. if h in master_list:
  131. # Create the list of nested headers which included this file.
  132. iclist = list ()
  133. ib = includes[h][0]
  134. while ib:
  135. iclist.insert(0, ib)
  136. ib = includes[ib][0]
  137. if iclist:
  138. for x in iclist:
  139. # If header is in the source code, and we are allowed to look inside
  140. if x in src_h and x not in exclude_processing:
  141. if x not in new_order and x[:10] != "diagnostic" and h not in exclude_special:
  142. new_order.append (x)
  143. break;
  144. else:
  145. if h not in new_order:
  146. new_order.append (h)
  147. f = ""
  148. if "diagnostic.h" in src_h:
  149. f = "diagnostic.h"
  150. elif "diagnostic-core.h" in src_h:
  151. f = "diagnostic-core.h"
  152. # If either diagnostic header was directly included in the main file, check to
  153. # see if its already included indirectly, or whether we need to add it to the
  154. # end of the canonically orders headers.
  155. if f:
  156. ii = indirectly_included (f, src_h)
  157. if not ii or ii == f:
  158. new_order.append (f)
  159. return new_order
  160. # stack of files to process
  161. process_stack = list ()
  162. def process_one (info):
  163. i = info[0]
  164. owner = info[1]
  165. name = os.path.basename(i)
  166. if os.path.exists (i):
  167. if includes.get(name) == None:
  168. l = find_unique_include_list (i)
  169. # create a list which has just basenames in it
  170. new_list = list ()
  171. for x in l:
  172. new_list.append (os.path.basename (x))
  173. process_stack.append((x, name))
  174. includes[name] = (owner, new_list)
  175. elif owner:
  176. if dups.get(name) == None:
  177. dups[name] = [ owner ]
  178. else:
  179. dups[name].append (owner)
  180. else:
  181. # seed tm.h with options.h since it is a build file and won't be seen.
  182. if not includes.get(name):
  183. if name == "tm.h":
  184. includes[name] = (owner, [ "options.h" ])
  185. includes["options.h"] = ("tm.h", list ())
  186. else:
  187. includes[name] = (owner, list ())
  188. show_master = False
  189. for arg in sys.argv[1:]:
  190. if arg[0:1] == "-":
  191. if arg[0:2] == "-h":
  192. usage = True
  193. elif arg[0:2] == "-i":
  194. ignore_conditional = True
  195. elif arg[0:2] == "-v":
  196. show_master = True
  197. else:
  198. print "Error: unrecognized option " + arg
  199. elif os.path.exists(arg):
  200. file_list.append (arg)
  201. else:
  202. print "Error: file " + arg + " Does not exist."
  203. usage = True
  204. if not file_list and not show_master:
  205. usage = True
  206. if not usage and not os.path.exists ("coretypes.h"):
  207. usage = True
  208. print "Error: Must run command in main gcc source directory containing coretypes.h\n"
  209. # process diagnostic.h first.. it's special since GCC_DIAG_STYLE can be
  210. # overridden by languages, but must be done so by a file included BEFORE it.
  211. # so make sure it isn't seen as included by one of those files by making it
  212. # appear to be included by the src file.
  213. process_stack.insert (0, ("diagnostic.h", ""))
  214. # Add the list of files in reverse order since it is processed as a stack later
  215. for i in order:
  216. process_stack.insert (0, (i, "") )
  217. # build up the library of what header files include what other files.
  218. while process_stack:
  219. info = process_stack.pop ()
  220. process_one (info)
  221. # Now create the master ordering list
  222. for i in order:
  223. create_master_list (os.path.basename (i), show_master)
  224. # handle warts in the duplicate list
  225. process_known_dups ()
  226. desired_order = master_list
  227. if show_master:
  228. print " Canonical order of gcc include files: "
  229. for x in master_list:
  230. print x
  231. print " "
  232. if usage:
  233. print "gcc-order-headers [-i] [-v] file1 [filen]"
  234. print " Ensures gcc's headers files are included in a normalized form with"
  235. print " redundant headers removed. The original files are saved in filename.bak"
  236. print " Outputs a list of files which changed."
  237. print " -i ignore conditional compilation."
  238. print " Use after examining the file to be sure includes within #ifs are safe"
  239. print " Any headers within conditional sections will be ignored."
  240. print " -v Show the canonical order of known headers"
  241. sys.exit(0)
  242. didnt_do = list ()
  243. for fn in file_list:
  244. nest = 0
  245. src_h = list ()
  246. src_line = { }
  247. master_list = list ()
  248. includes = { }
  249. dups = { }
  250. iinfo = process_ii_src (fn)
  251. src = ii_src (iinfo)
  252. include_list = ii_include_list (iinfo)
  253. if ii_include_list_cond (iinfo):
  254. if not ignore_conditional:
  255. print fn + ": Cannot process due to conditional compilation of includes"
  256. didnt_do.append (fn)
  257. src = list ()
  258. if not src:
  259. continue
  260. process_stack = list ()
  261. # prime the stack with headers in the main ordering list so we get them in
  262. # this order.
  263. for d in order:
  264. if d in include_list:
  265. process_stack.insert (0, (d, ""))
  266. for d in include_list:
  267. nm = os.path.basename(d)
  268. src_h.append (nm)
  269. iname = d
  270. iname2 = os.path.dirname (fn) + "/" + d
  271. if not os.path.exists (d) and os.path.exists (iname2):
  272. iname = iname2
  273. if iname not in process_stack:
  274. process_stack.insert (0, (iname, ""))
  275. src_line[nm] = ii_src_line(iinfo)[d]
  276. if src_line[nm].find("/*") != -1 and src_line[nm].find("*/") == -1:
  277. # this means we have a multi line comment, abort!'
  278. print fn + ": Cannot process due to a multi-line comment :"
  279. print " " + src_line[nm]
  280. if fn not in didnt_do:
  281. didnt_do.append (fn)
  282. src = list ()
  283. if not src:
  284. continue
  285. # Now create the list of includes as seen by the source file.
  286. while process_stack:
  287. info = process_stack.pop ()
  288. process_one (info)
  289. for i in include_list:
  290. create_master_list (os.path.basename (i), False)
  291. new_src = list ()
  292. header_added = list ()
  293. new_order = list ()
  294. for line in src:
  295. d = find_pound_include (line, True, True)
  296. if not d or d[-2:] != ".h":
  297. new_src.append (line)
  298. else:
  299. if d == order[0] and not new_order:
  300. new_order = get_new_order (src_h, desired_order)
  301. for i in new_order:
  302. new_src.append (src_line[i])
  303. # if not seen, add it.
  304. if i not in header_added:
  305. header_added.append (i)
  306. else:
  307. nm = os.path.basename(d)
  308. if nm not in header_added:
  309. iby = indirectly_included (nm, src_h)
  310. if not iby:
  311. new_src.append (line)
  312. header_added.append (nm)
  313. if src != new_src:
  314. os.rename (fn, fn + ".bak")
  315. fl = open(fn,"w")
  316. for line in new_src:
  317. fl.write (line)
  318. fl.close ()
  319. print fn
  320. if didnt_do:
  321. print "\n\n Did not process the following files due to conditional dependencies:"
  322. str = ""
  323. for x in didnt_do:
  324. str += x + " "
  325. print str
  326. print "\n"
  327. print "Please examine to see if they are safe to process, and re-try with -i. "
  328. print "Safeness is determined by checking whether any of the reordered headers are"
  329. print "within a conditional and could be hauled out of the conditional, thus changing"
  330. print "what the compiler will see."
  331. print "Multi-line comments after a #include can also cause failuer, they must be turned"
  332. print "into single line comments or removed."