-
Notifications
You must be signed in to change notification settings - Fork 291
/
xinspect.hpp
342 lines (294 loc) · 11.4 KB
/
xinspect.hpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***********************************************************************************
* Copyright (c) 2016, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* Copyright (c) 2016, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/
#ifndef XCPP_INSPECT_HPP
#define XCPP_INSPECT_HPP
#include <fstream>
#include <string>
#include <dirent.h>
#include "cling/Interpreter/Interpreter.h"
#include "cling/Interpreter/Value.h"
#include "cling/Utils/Output.h"
#include "pugixml.hpp"
#include "xeus-cling/xbuffer.hpp"
#include "xeus-cling/xpreamble.hpp"
#include "xdemangle.hpp"
#include "xparser.hpp"
namespace xcpp
{
struct node_predicate
{
std::string kind;
std::string child_value;
bool operator()(pugi::xml_node node) const
{
return static_cast<std::string>(node.attribute("kind").value()) == kind && static_cast<std::string>(node.child("name").child_value()) == child_value;
}
};
struct class_member_predicate
{
std::string class_name;
std::string kind;
std::string child_value;
std::string get_filename(pugi::xml_node node)
{
for (pugi::xml_node child : node.children())
{
if (static_cast<std::string>(child.attribute("kind").value()) == kind &&
static_cast<std::string>(child.child("name").child_value()) == child_value)
{
return child.child("anchorfile").child_value();
}
}
return "";
}
bool operator()(pugi::xml_node node)
{
auto parent = (static_cast<std::string>(node.attribute("kind").value()) == "class" ||
static_cast<std::string>(node.attribute("kind").value()) == "struct") &&
static_cast<std::string>(node.child("name").child_value()) == class_name;
auto found = false;
if (parent)
{
for (pugi::xml_node child : node.children())
{
if (static_cast<std::string>(child.attribute("kind").value()) == kind &&
static_cast<std::string>(child.child("name").child_value()) == child_value)
{
found = true;
break;
}
}
}
return found;
}
};
std::string find_type(const std::string& expression, cling::Interpreter& interpreter)
{
cling::Value result;
std::string typeString;
// add typeinfo in include files in order to use typeid
std::string code = "#include <typeinfo>";
auto compilation_result = interpreter.process(code.c_str(), &result);
// try to find the typename of the class
code = "typeid(" + expression + ").name();";
// Temporarily dismissing all std::cerr and std::cout resulting from `interpreter.process`
compilation_result = interpreter.process(code.c_str(), &result);
{
auto cout_strbuf = std::cout.rdbuf();
auto cerr_strbuf = std::cerr.rdbuf();
auto null = xnull();
std::cout.rdbuf(&null);
std::cerr.rdbuf(&null);
compilation_result = interpreter.process(code.c_str(), &result);
std::cout.rdbuf(cout_strbuf);
std::cerr.rdbuf(cerr_strbuf);
}
if (compilation_result == cling::Interpreter::kSuccess)
{
// we found the typeid
std::string valueString;
{
llvm::raw_string_ostream os(valueString);
result.print(os);
}
// search the typename in the output between ""
std::regex re_typename("\\\"(.*)\\\"");
std::smatch typename_;
std::regex_search(valueString, typename_, re_typename);
// set in valueString the typename given by typeid
valueString = typename_.str(1);
// we need demangling in order to have its string representation
valueString = demangle(valueString);
re_typename = "(\\w*(?:\\:{2}?\\w*)*)";
std::regex_search(valueString, typename_, re_typename);
if (!typename_.str(1).empty())
{
typeString = typename_[1];
}
}
return typeString;
}
static nl::json read_tagconfs(const char* path)
{
nl::json result = nl::json::array();
DIR* directory = opendir(path);
if (directory == nullptr)
{
return result;
}
dirent* item = readdir(directory);
while (item != nullptr)
{
std::string extension = "json";
if (item->d_type == DT_REG)
{
std::string fname = item->d_name;
if (fname.find(extension, (fname.length() - extension.length())) != std::string::npos)
{
std::ifstream i(path + ('/' + fname));
nl::json entry;
i >> entry;
result.emplace_back(std::move(entry));
}
}
item = readdir(directory);
}
closedir(directory);
return result;
}
void inspect(const std::string& code, nl::json& kernel_res, cling::Interpreter& interpreter)
{
std::string tagconf_dir = XCPP_TAGCONFS_DIR;
std::string tagfiles_dir = XCPP_TAGFILES_DIR;
nl::json tagconfs = read_tagconfs(tagconf_dir.c_str());
std::vector<std::string> check{"class", "struct", "function"};
std::string url, tagfile;
std::regex re_expression(R"((((?:\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)\.?)*))");
std::smatch inspect;
std::regex_search(code, inspect, re_expression);
std::string inspect_result;
std::smatch method;
std::string to_inspect = inspect[1];
// Method or variable of class found (xxxx.yyyy)
if (std::regex_search(to_inspect, method, std::regex(R"((.*)\.(\w*)$)")))
{
std::string typename_ = find_type(method[1], interpreter);
if (!typename_.empty())
{
for (nl::json::const_iterator it = tagconfs.cbegin(); it != tagconfs.cend(); ++it)
{
url = it->at("url");
tagfile = it->at("tagfile");
std::string filename = tagfiles_dir + "/" + tagfile;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
class_member_predicate predicate{typename_, "function", method[2]};
auto node = doc.find_node(predicate);
if (!node.empty())
{
inspect_result = url + predicate.get_filename(node);
}
}
}
}
else
{
std::string find_string;
// check if we try to find the documentation of a namespace
// if yes, don't try to find the type using typeid
std::regex is_namespace(R"(\w+(\:{2}\w+)+)");
std::smatch namespace_match;
if (std::regex_match(to_inspect, namespace_match, is_namespace))
{
find_string = to_inspect;
}
else
{
std::string typename_ = find_type(to_inspect, interpreter);
find_string = (typename_.empty()) ? to_inspect : typename_;
}
for (nl::json::const_iterator it = tagconfs.cbegin(); it != tagconfs.cend(); ++it)
{
url = it->at("url");
tagfile = it->at("tagfile");
std::string filename = tagfiles_dir + "/" + tagfile;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
for (auto c : check)
{
node_predicate predicate{c, find_string};
std::string node;
if (c == "class" || c == "struct")
{
node = doc.find_node(predicate).child("filename").child_value();
}
else
{
node = doc.find_node(predicate).child("anchorfile").child_value();
}
if (!node.empty())
{
inspect_result = url + node;
}
}
}
}
if (inspect_result.empty())
{
std::cerr << "No documentation found for " << code << "\n";
std::cout << std::flush;
kernel_res["found"] = false;
kernel_res["status"] = "error";
kernel_res["ename"] = "No documentation found";
kernel_res["evalue"] = "";
kernel_res["traceback"] = nl::json::array();
}
else
{
// Format html content.
std::string html_content = R"(<style>
#pager-container {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}
.xcpp-iframe-pager {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
border: none;
}
</style>
<iframe class="xcpp-iframe-pager" src=")" +
inspect_result + R"(?action=purge"></iframe>)";
// Note: Adding "?action=purge" suffix to force cppreference's
// Mediawiki to purge the HTTP cache.
kernel_res["payload"] = nl::json::array();
kernel_res["payload"][0] = nl::json::object({
{"data", {
{"text/plain", inspect_result},
{"text/html", html_content}}
},
{"source", "page"},
{"start", 0}
});
kernel_res["user_expressions"] = nl::json::object();
std::cout << std::flush;
kernel_res["found"] = true;
kernel_res["status"] = "ok";
}
}
class xintrospection : public xpreamble
{
public:
using xpreamble::pattern;
const std::string spattern = R"(^\?)";
xintrospection(cling::Interpreter& p)
: m_interpreter{p}
{
pattern = spattern;
}
void apply(const std::string& code, nl::json& kernel_res) override
{
std::regex re(spattern + R"((.*))");
std::smatch to_inspect;
std::regex_search(code, to_inspect, re);
inspect(to_inspect[1], kernel_res, m_interpreter);
}
virtual xpreamble* clone() const override
{
return new xintrospection(*this);
}
private:
cling::Interpreter& m_interpreter;
};
}
#endif