-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
191 lines (168 loc) · 6.46 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#%%#
import cppyy
import os
import pathlib
import time
editorRuntimeDir = pathlib.Path(__file__).parent.resolve()
llvmDir = "/opt/homebrew/opt/llvm@14"
# import jupyter display
# from IPython.display import display, Javascript, HTML
# from html import escape
import shlex
import ctypes
cppyy.add_include_path(os.path.join(llvmDir, "include"))
cppyy.gbl.gInterpreter.Load(os.path.join(llvmDir, "lib/libLLVM.dylib"))
cppyy.gbl.gInterpreter.Load(os.path.join(llvmDir, "lib/libclang-cpp.dylib"))
cppyy.gbl.gInterpreter.Load(os.path.join(llvmDir, "lib/libclang.dylib"))
cppyy.cppdef("""
#include "clang-c/Index.h"
#include "clang-c/Rewrite.h"
""")
globalNsCount = 1
def defineInNewNs(contents):
global globalNsCount
globalNsCount += 1
nsName = f"sfGlobalNs{globalNsCount}"
cppyy.cppdef(f"""
namespace {nsName} {{
{contents}
}}
""")
return (nsName, getattr(cppyy.gbl, nsName))
def includeFile(dir, path, runtimeNsName, transform=False):
clangNs = cppyy.gbl
fullPath = os.path.join(dir, path)
fileContents = open(fullPath, "r").read()
# Basically want output of cppyy.gbl.gInterpreter.ProcessLine(".I")
cppyy.gbl.gInterpreter.GetIncludePath()
clangArgs = shlex.split(cppyy.gbl.gInterpreter.GetIncludePath()) + ["-resource-dir", cppyy.gbl.CppyyLegacy.GetROOT().GetEtcDir().Data() + "cling/lib/clang/9.0.1"]
index2 = clangNs.clang_createIndex(0, 0)
tu2 = clangNs.clang_parseTranslationUnit(index2, fullPath.encode("utf-8"), clangArgs, len(clangArgs), cppyy.nullptr, 0, 0)
def CXtoCStr(cxstr):
string = clangNs.clang_getCString(cxstr)
clangNs.clang_disposeString(cxstr)
return string
for i in range(clangNs.clang_getNumDiagnostics(tu2)):
diag = clangNs.clang_getDiagnostic(tu2, i)
diagStr = CXtoCStr(clangNs.clang_formatDiagnostic(diag, clangNs.clang_defaultDiagnosticDisplayOptions()))
diagSeverity = clangNs.clang_getDiagnosticSeverity(diag)
# print("clang diagnostic: " + diagStr)
if (diagSeverity > clangNs.CXDiagnostic_Warning):
print("clang diagnostic: " + diagStr)
raise Exception("Clang error: ")
clangNs.clang_disposeDiagnostic(diag)
_, tmp = defineInNewNs("""
CXCursor* clang_makeCursorCopy(CXCursor* cursor) {
CXCursor* newCursor = new CXCursor();
*newCursor = *cursor;
return newCursor;
}
""")
def getCursorChildren(cursor):
children = []
def visit(c, p, l):
# clang_visitChildren reuses same CXCursor
cCopy = tmp.clang_makeCursorCopy(c)
children.append(cCopy)
return clangNs.CXChildVisit_Continue
clangNs.clang_visitChildren(cursor, visit, cppyy.nullptr)
return children
getCursorChildren(clangNs.clang_getTranslationUnitCursor(tu2))
cxrewriter = clangNs.clang_CXRewriter_create(tu2)
def visitor(cursor, depth):
location = clangNs.clang_getCursorLocation(cursor)
isFromMainFile = clangNs.clang_Location_isFromMainFile(location)
if not isFromMainFile:
return
clangNs.clang_Location_isFromMainFile
kind = clangNs.clang_getCursorKind(cursor)
spelling = CXtoCStr(clangNs.clang_getCursorSpelling(cursor))
kind = clangNs.clang_getCursorKind(cursor)
kindReadable = CXtoCStr(clangNs.clang_getCursorKindSpelling(kind))
line = ctypes.c_uint32()
column = ctypes.c_uint32()
clangNs.clang_getExpansionLocation(location, ctypes.c_void_p(), line, column, cppyy.nullptr)
extent = clangNs.clang_getCursorExtent(cursor)
# check if it's a printf
if kind == clangNs.CXCursor_CallExpr:
if spelling == "printf":
argStrings = []
# e.g. printf("hello %d", 5) -> ["printf", "\"hello %d\"", "5"]
for argCursor in getCursorChildren(cursor):
argExtent = clangNs.clang_getCursorExtent(argCursor)
start = clangNs.clang_getRangeStart(argExtent)
end = clangNs.clang_getRangeEnd(argExtent)
startIdx = ctypes.c_uint32()
endIdx = ctypes.c_uint32()
_file = ctypes.c_void_p()
clangNs.clang_getExpansionLocation(start, _file, cppyy.nullptr, cppyy.nullptr, startIdx)
clangNs.clang_getExpansionLocation(end, _file, cppyy.nullptr, cppyy.nullptr, endIdx)
extentStr = fileContents[startIdx.value:endIdx.value]
argStrings.append(extentStr)
# print("extentStr: " + extentStr)
vaArgs = ", ".join(argStrings[1:])
truncPath = "/".join(fullPath.split("/")[-3:])
newCode = f"""
{{
int sf_bufferSize = snprintf(NULL, 0, {vaArgs});
char *sf_buffer = (char*)malloc(sf_bufferSize + 1);
snprintf(sf_buffer, sf_bufferSize + 1, {vaArgs});
{runtimeNsName}::msgserver_inthread_sendlog("{truncPath}", {line.value-1}, {column.value-1}, sf_buffer);
free(sf_buffer);
}}
"""
clangNs.clang_CXRewriter_replaceText(cxrewriter, extent, newCode)
for child in getCursorChildren(cursor):
visitor(child, depth+1)
if transform:
for child in getCursorChildren(clangNs.clang_getTranslationUnitCursor(tu2)):
visitor(child, 0)
cppyy.cppdef("""
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Rewrite/Core/Rewriter.h"
""")
_, tmp = defineInNewNs("""
std::string getRewriteBuffer(CXRewriter rew) {
clang::Rewriter &rewriter = *(clang::Rewriter*)rew;
// get the rewritten buffer
std::string buffer;
llvm::raw_string_ostream os(buffer);
rewriter.getEditBuffer(rewriter.getSourceMgr().getMainFileID()).write(os);
return os.str();
}
""")
transformedFileContents = tmp.getRewriteBuffer(cxrewriter)
# create jupyter scrollable output
# display(HTML(f"""
# <style>
# .sf-code-output {{
# max-height: 200px;
# overflow-y: scroll;
# }}
# </style>
# <div class="sf-code-output">
# <pre>{escape(transformedFileContents)}</pre>
# </div>
# """))
# print(transformedFileContents)
return transformedFileContents
cppyy.load_library("libzmq.so")
cppyy.cppdef("""
#include <zmq.h>
#include <math.h>
""")
editorRuntimeNsName, editorRuntime = defineInNewNs(f"""
{includeFile(editorRuntimeDir, "SunflowerEditorRuntime.cpp", runtimeNsName=None, transform=False)}
""")
editorRuntime.msgserver_init()
#%%#
_, runtimeTest = defineInNewNs(f"""
{includeFile(editorRuntimeDir, "SunflowerEditorTesting.cpp", runtimeNsName=editorRuntimeNsName, transform=True)}
""")
runtimeTest.doStuff()
for i in range(100):
runtimeTest.doStuff2()
time.sleep(0.05)
#%%#
editorRuntime.msgserver_close()