stripdecls.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. only_static = False
  14. cfname = os.path.abspath(os.path.normpath(cfname))
  15. hfname1 = os.path.splitext(cfname)[0] + os.extsep + 'h'
  16. hfname2 = os.path.splitext(cfname)[0] + '_defs' + os.extsep + 'h'
  17. files_to_modify = (cfname, hfname1, hfname2)
  18. index = Index.create()
  19. src_dirname = os.path.join(os.path.dirname(__file__), '..', 'src')
  20. src_dirname = os.path.abspath(os.path.normpath(src_dirname))
  21. relname = os.path.join(src_dirname, 'nvim')
  22. unit = index.parse(cfname, args=('-I' + src_dirname,
  23. '-DUNIX',
  24. '-DEXITFREE',
  25. '-DFEAT_USR_CMDS',
  26. '-DFEAT_CMDL_COMPL',
  27. '-DFEAT_COMPL_FUNC',
  28. '-DPROTO',
  29. '-DUSE_MCH_ERRMSG'))
  30. cursor = unit.cursor
  31. tostrip = defaultdict(OrderedDict)
  32. definitions = set()
  33. for child in cursor.get_children():
  34. if not (child.location and child.location.file):
  35. continue
  36. fname = os.path.abspath(os.path.normpath(child.location.file.name))
  37. if fname not in files_to_modify:
  38. continue
  39. if child.kind not in DECL_KINDS:
  40. continue
  41. if only_static and next(child.get_tokens()).spelling == 'static':
  42. continue
  43. if child.is_definition() and fname == cfname:
  44. definitions.add(child.spelling)
  45. else:
  46. stripdict = tostrip[fname]
  47. assert(child.spelling not in stripdict)
  48. stripdict[child.spelling] = Strip(
  49. child.extent.start.line,
  50. child.extent.start.column,
  51. child.extent.end.line,
  52. child.extent.end.column,
  53. )
  54. for (fname, stripdict) in tostrip.items():
  55. if not move_all:
  56. for name in set(stripdict) - definitions:
  57. stripdict.pop(name)
  58. if not stripdict:
  59. continue
  60. if fname.endswith('.h'):
  61. is_h_file = True
  62. include_line = next(reversed(stripdict.values())).start_line + 1
  63. else:
  64. is_h_file = False
  65. include_line = next(iter(stripdict.values())).start_line
  66. lines = None
  67. generated_existed = os.path.exists(fname + '.generated.h')
  68. with open(fname, 'rb') as F:
  69. lines = list(F)
  70. stripped = []
  71. for name, position in reversed(stripdict.items()):
  72. sl = slice(position.start_line - 1, position.end_line)
  73. if is_h_file:
  74. include_line -= sl.stop - sl.start
  75. stripped += lines[sl]
  76. lines[sl] = ()
  77. if not generated_existed:
  78. lines[include_line:include_line] = [
  79. '#ifdef INCLUDE_GENERATED_DECLARATIONS\n',
  80. '# include "{0}.generated.h"\n'.format(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)