-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathdecls_json.py
executable file
·83 lines (75 loc) · 3.36 KB
/
decls_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import sys
import json
import clang.cindex
from typing import List
clang.cindex.Config.set_library_file('/usr/lib/llvm-16/lib/libclang-16.so.1')
def has_annotation(node, annotation):
for child in node.get_children():
if child.kind == clang.cindex.CursorKind.ANNOTATE_ATTR and annotation in child.spelling:
return True
return False
def print_diagnostic(diagnostic, file=sys.stdout):
# color codes for printing
BLUE = '\033[94m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
color_map = {
clang.cindex.Diagnostic.Warning: YELLOW,
clang.cindex.Diagnostic.Error: RED,
clang.cindex.Diagnostic.Fatal: RED,
}
color = color_map.get(diagnostic.severity, BLUE)
print(color + str(diagnostic) + ENDC, file=file)
def process_files(files: List[str]) -> List[dict]:
result = []
idx = clang.cindex.Index.create()
for path in files:
tu = idx.parse(path, args=[
'-isystem', '/usr/include/c++/10',
'-isystem', '/usr/include/x86_64-linux-gnu/c++/10',
'-isystem', '/usr/include/c++/10/backward',
'-isystem', '/usr/lib/llvm-15/lib/clang/15.0.7/include',
'-isystem', '/usr/local/include',
'-isystem', '/usr/include/x86_64-linux-gnu',
'-isystem', '/usr/include',
"-I./cpp/src",
'-std=gnu++20', '-Wall', '-Wextra'])
for diag in tu.diagnostics:
print_diagnostic(diag, file=sys.stderr)
for node in tu.cursor.walk_preorder():
try:
if node.kind == clang.cindex.CursorKind.FUNCTION_DECL:
# if node.spelling != "env_test_threads":
# continue
# Only interested in function declarations with WASM_EXPORT token.
if not has_annotation(node, 'wasm_export'):
continue
if node.result_type.spelling != "void":
raise ValueError(f"Error: Function '{node.spelling}' must have a 'void' return type")
func = {
'functionName': node.spelling,
'inArgs': [
{
'name': arg.spelling,
'type': arg.type.spelling,
} for arg in node.get_arguments() if arg.type.get_canonical().get_pointee().is_const_qualified() or arg.type.get_canonical().is_const_qualified()
],
'outArgs': [
{
'name': arg.spelling,
'type': arg.type.spelling,
} for arg in node.get_arguments() if not (arg.type.get_canonical().get_pointee().is_const_qualified() or arg.type.get_canonical().is_const_qualified())
],
'isAsync': has_annotation(node, 'async_wasm_export')
}
result.append(func)
except ValueError as e:
if not str(e).startswith("Unknown template argument kind"):
raise
return result
if __name__ == '__main__':
file_list = [line.strip() for line in sys.stdin]
processed_data = process_files(file_list)
print(json.dumps(processed_data, indent=2))