annotate_ifdef_directives 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/python
  2. # Copyright (c) 2017-2019, The Tor Project, Inc.
  3. # See LICENSE for licensing information
  4. # This script iterates over a list of C files. For each file, it looks at the
  5. # #if/#else C macros, and annotates them with comments explaining what they
  6. # match.
  7. #
  8. # For example, it replaces this:
  9. #
  10. # #ifdef HAVE_OCELOT
  11. # // 500 lines of ocelot code
  12. # #endif
  13. #
  14. # with this:
  15. #
  16. # #ifdef HAVE_OCELOT
  17. # // 500 lines of ocelot code
  18. # #endif /* defined(HAVE_OCELOT) */
  19. #
  20. # Note that only #else and #endif lines are annotated. Existing comments
  21. # on those lines are removed.
  22. import re
  23. # Any block with fewer than this many lines does not need annotations.
  24. LINE_OBVIOUSNESS_LIMIT = 4
  25. class Problem(Exception):
  26. pass
  27. def uncomment(s):
  28. """
  29. Remove existing trailing comments from an #else or #endif line.
  30. """
  31. s = re.sub(r'//.*','',s)
  32. s = re.sub(r'/\*.*','',s)
  33. return s.strip()
  34. def translate(f_in, f_out):
  35. """
  36. Read a file from f_in, and write its annotated version to f_out.
  37. """
  38. # A stack listing our current if/else state. Each member of the stack
  39. # is a list of directives. Each directive is a 3-tuple of
  40. # (command, rest, lineno)
  41. # where "command" is one of if/ifdef/ifndef/else/elif, and where
  42. # "rest" is an expression in a format suitable for use with #if, and where
  43. # lineno is the line number where the directive occurred.
  44. stack = []
  45. # the stack element corresponding to the top level of the file.
  46. whole_file = []
  47. cur_level = whole_file
  48. lineno = 0
  49. for line in f_in:
  50. lineno += 1
  51. m = re.match(r'\s*#\s*(if|ifdef|ifndef|else|endif|elif)\b\s*(.*)',
  52. line)
  53. if not m:
  54. # no directive, so we can just write it out.
  55. f_out.write(line)
  56. continue
  57. command,rest = m.groups()
  58. if command in ("if", "ifdef", "ifndef"):
  59. # The #if directive pushes us one level lower on the stack.
  60. if command == 'ifdef':
  61. rest = "defined(%s)"%uncomment(rest)
  62. elif command == 'ifndef':
  63. rest = "!defined(%s)"%uncomment(rest)
  64. elif rest.endswith("\\"):
  65. rest = rest[:-1]+"..."
  66. rest = uncomment(rest)
  67. new_level = [ (command, rest, lineno) ]
  68. stack.append(cur_level)
  69. cur_level = new_level
  70. f_out.write(line)
  71. elif command in ("else", "elif"):
  72. # We stay at the same level on the stack. If we have an #else,
  73. # we comment it.
  74. if len(cur_level) == 0 or cur_level[-1][0] == 'else':
  75. raise Problem("Unexpected #%s on %d"% (command,lineno))
  76. if (len(cur_level) == 1 and command == 'else' and
  77. lineno > cur_level[0][2] + LINE_OBVIOUSNESS_LIMIT):
  78. f_out.write("#else /* !(%s) */\n"%cur_level[0][1])
  79. else:
  80. f_out.write(line)
  81. cur_level.append((command, rest, lineno))
  82. else:
  83. # We pop one element on the stack, and comment an endif.
  84. assert command == 'endif'
  85. if len(stack) == 0:
  86. raise Problem("Unmatched #%s on %s"% (command,lineno))
  87. if lineno <= cur_level[0][2] + LINE_OBVIOUSNESS_LIMIT:
  88. f_out.write(line)
  89. elif len(cur_level) == 1 or (
  90. len(cur_level) == 2 and cur_level[1][0] == 'else'):
  91. f_out.write("#endif /* %s */\n"%cur_level[0][1])
  92. else:
  93. f_out.write("#endif /* %s || ... */\n"%cur_level[0][1])
  94. cur_level = stack.pop()
  95. if len(stack) or cur_level != whole_file:
  96. raise Problem("Missing #endif")
  97. import sys,os
  98. for fn in sys.argv[1:]:
  99. with open(fn+"_OUT", 'w') as output_file:
  100. translate(open(fn, 'r'), output_file)
  101. os.rename(fn+"_OUT", fn)