stripdecls.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/python
  2. # vim: set fileencoding=utf-8:
  3. from __future__ import print_function, unicode_literals, division
  4. from clang.cindex import Index, CursorKind
  5. from collections import namedtuple, OrderedDict, defaultdict
  6. import sys
  7. import os
  8. DECL_KINDS = {
  9. CursorKind.FUNCTION_DECL,
  10. }
  11. Strip = namedtuple('Strip', 'start_line start_column end_line end_column')
  12. def main(progname, cfname, only_static, move_all):
  13. cfname = os.path.abspath(os.path.normpath(cfname))
  14. hfname1 = os.path.splitext(cfname)[0] + os.extsep + 'h'
  15. hfname2 = os.path.splitext(cfname)[0] + '_defs' + os.extsep + 'h'
  16. files_to_modify = (cfname, hfname1, hfname2)
  17. index = Index.create()
  18. src_dirname = os.path.join(os.path.dirname(__file__), '..', 'src')
  19. src_dirname = os.path.abspath(os.path.normpath(src_dirname))
  20. relname = os.path.join(src_dirname, 'nvim')
  21. unit = index.parse(cfname, args=('-I' + src_dirname,
  22. '-DUNIX',
  23. '-DEXITFREE',
  24. '-DFEAT_USR_CMDS',
  25. '-DFEAT_CMDL_COMPL',
  26. '-DFEAT_COMPL_FUNC',
  27. '-DPROTO',
  28. '-DUSE_MCH_ERRMSG'))
  29. cursor = unit.cursor
  30. tostrip = defaultdict(OrderedDict)
  31. definitions = set()
  32. for child in cursor.get_children():
  33. if not (child.location and child.location.file):
  34. continue
  35. fname = os.path.abspath(os.path.normpath(child.location.file.name))
  36. if fname not in files_to_modify:
  37. continue
  38. if child.kind not in DECL_KINDS:
  39. continue
  40. if only_static and next(child.get_tokens()).spelling == 'static':
  41. continue
  42. if child.is_definition() and fname == cfname:
  43. definitions.add(child.spelling)
  44. else:
  45. stripdict = tostrip[fname]
  46. assert(child.spelling not in stripdict)
  47. stripdict[child.spelling] = Strip(
  48. child.extent.start.line,
  49. child.extent.start.column,
  50. child.extent.end.line,
  51. child.extent.end.column,
  52. )
  53. for (fname, stripdict) in tostrip.items():
  54. if not move_all:
  55. for name in set(stripdict) - definitions:
  56. stripdict.pop(name)
  57. if not stripdict:
  58. continue
  59. if fname.endswith('.h'):
  60. is_h_file = True
  61. include_line = next(reversed(stripdict.values())).start_line + 1
  62. else:
  63. is_h_file = False
  64. include_line = next(iter(stripdict.values())).start_line
  65. lines = None
  66. generated_existed = os.path.exists(fname + '.generated.h')
  67. with open(fname, 'rb') as F:
  68. lines = list(F)
  69. stripped = []
  70. for name, position in reversed(stripdict.items()):
  71. sl = slice(position.start_line - 1, position.end_line)
  72. if is_h_file:
  73. include_line -= sl.stop - sl.start
  74. stripped += lines[sl]
  75. lines[sl] = ()
  76. if not generated_existed:
  77. lines[include_line:include_line] = [
  78. '#ifdef INCLUDE_GENERATED_DECLARATIONS\n',
  79. '# include "{0}.generated.h"\n'.format(
  80. os.path.relpath(fname, relname)),
  81. '#endif\n',
  82. ]
  83. with open(fname, 'wb') as F:
  84. F.writelines(lines)
  85. if __name__ == '__main__':
  86. progname = sys.argv[0]
  87. args = sys.argv[1:]
  88. if not args or '--help' in args:
  89. print('Usage:')
  90. print('')
  91. print(' {0} [--static [--all]] file.c...'.format(progname))
  92. print('')
  93. print('Stripts all declarations from file.c, file.h and file_defs.h.')
  94. print('If --static argument is given then only static declarations are')
  95. print('stripped. Declarations are stripped only if corresponding')
  96. print('definition is found unless --all argument was given.')
  97. print('')
  98. print('Note: it is assumed that static declarations starts with "static"')
  99. print(' keyword.')
  100. sys.exit(0 if args else 1)
  101. if args[0] == '--static':
  102. only_static = True
  103. args = args[1:]
  104. else:
  105. only_static = False
  106. if args[0] == '--all':
  107. move_all = True
  108. args = args[1:]
  109. else:
  110. move_all = False
  111. for cfname in args:
  112. print('Processing {0}'.format(cfname))
  113. main(progname, cfname, only_static, move_all)