parser.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import sys
  2. import os
  3. from threading import Thread
  4. import clang.cindex
  5. import ccsyspath
  6. import itertools
  7. from mako.template import Template
  8. def get_annotations(node):
  9. return [c.displayname for c in node.get_children()
  10. if c.kind == clang.cindex.CursorKind.ANNOTATE_ATTR]
  11. class Function(object):
  12. def __init__(self, cursor):
  13. self.name = cursor.spelling
  14. self.annotations = get_annotations(cursor)
  15. self.access = cursor.access_specifier
  16. # template_pars = [c.extent for c in cursor.get_children() if c.kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER]
  17. parameter_dec = [c for c in cursor.get_children() if c.kind == clang.cindex.CursorKind.PARM_DECL]
  18. parameters = []
  19. for p in parameter_dec:
  20. children = []
  21. for c in p.get_children():
  22. # print(c.spelling)
  23. children.append(c.spelling)
  24. parameters.append((p.spelling, p.type.spelling, children))
  25. self.parameters = parameters
  26. self.documentation = cursor.raw_comment
  27. class Enum(object):
  28. def __init__(self, cursor):
  29. self.name = cursor.spelling
  30. self.constants = [c.spelling for c in cursor.get_children() if c.kind ==
  31. clang.cindex.CursorKind.ENUM_CONSTANT_DECL]
  32. self.documentation = cursor.raw_comment
  33. class Class(object):
  34. def __init__(self, cursor):
  35. self.name = cursor.spelling
  36. # self.functions = []
  37. self.annotations = get_annotations(cursor)
  38. # for c in cursor.get_children():
  39. # if (c.kind == clang.cindex.CursorKind.CXX_METHOD and
  40. # c.access_specifier == clang.cindex.AccessSpecifier.PUBLIC):
  41. # f = Function(c)
  42. # self.functions.append(f)
  43. def traverse(c, path, objects):
  44. if c.location.file and not c.location.file.name.endswith(path):
  45. return
  46. if c.spelling == "PARULA_COLOR_MAP": # Fix to prevent python stack overflow from infinite recursion
  47. return
  48. # print(c.kind, c.spelling)
  49. if c.kind == clang.cindex.CursorKind.TRANSLATION_UNIT or c.kind == clang.cindex.CursorKind.UNEXPOSED_DECL:
  50. # Ignore other cursor kinds
  51. pass
  52. elif c.kind == clang.cindex.CursorKind.NAMESPACE:
  53. objects["namespaces"].append(c.spelling)
  54. # print("Namespace", c.spelling, c.get_children())
  55. pass
  56. elif c.kind == clang.cindex.CursorKind.FUNCTION_TEMPLATE:
  57. # print("Function Template", c.spelling, c.raw_comment)
  58. objects["functions"].append(Function(c))
  59. return
  60. elif c.kind == clang.cindex.CursorKind.FUNCTION_DECL:
  61. # print("FUNCTION_DECL", c.spelling, c.raw_comment)
  62. objects["functions"].append(Function(c))
  63. return
  64. elif c.kind == clang.cindex.CursorKind.ENUM_DECL:
  65. # print("ENUM_DECL", c.spelling, c.raw_comment)
  66. objects["enums"].append(Enum(c))
  67. return
  68. elif c.kind == clang.cindex.CursorKind.CLASS_DECL:
  69. objects["classes"].append(Class(c))
  70. return
  71. elif c.kind == clang.cindex.CursorKind.CLASS_TEMPLATE:
  72. objects["classes"].append(Class(c))
  73. return
  74. elif c.kind == clang.cindex.CursorKind.STRUCT_DECL:
  75. objects["structs"].append(Class(c))
  76. return
  77. else:
  78. # print("Unknown", c.kind, c.spelling)
  79. pass
  80. for child_node in c.get_children():
  81. traverse(child_node, path, objects)
  82. def parse(path):
  83. index = clang.cindex.Index.create()
  84. # Clang can't parse files with missing definitions, add static library definition or not?
  85. args = ['-x', 'c++', '-std=c++11', '-fparse-all-comments', '-DIGL_STATIC_LIBRARY']
  86. args.append('-I/usr/include/eigen3/') # TODO Properly add all needed includes
  87. syspath = ccsyspath.system_include_paths('clang++-3.7') # Add the system libraries
  88. incargs = [(b'-I' + inc).decode("utf-8") for inc in syspath]
  89. args.extend(incargs)
  90. tu = index.parse(path, args)
  91. objects = {"functions": [], "enums": [], "namespaces": [], "classes": [], "structs": []}
  92. traverse(tu.cursor, path, objects)
  93. return objects
  94. if __name__ == '__main__':
  95. if len(sys.argv) != 2:
  96. print("Usage: python3 parser.py <headerfile_path>")
  97. exit(-1)
  98. parse(sys.argv[1])