26
26
27
27
using Args = std::vector<const char *>;
28
28
29
- void * createInterpreter (const Args &ExtraArgs = {}) {
30
- Args ClangArgs = {/* "-xc++"*/ " -v" }; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"};
29
+ void * createInterpreter (const Args& ExtraArgs = {})
30
+ {
31
+ Args ClangArgs = {/* "-xc++"*/ " -v" }; // ? {"-Xclang", "-emit-llvm-only", "-Xclang",
32
+ // "-diagnostic-log-file", "-Xclang", "-", "-xc++"};
31
33
#ifdef EMSCRIPTEN
32
- ClangArgs.push_back (" -std=c++20" );
34
+ ClangArgs.push_back (" -std=c++20" );
33
35
#else
34
- if (std::find_if (ExtraArgs.begin (), ExtraArgs.end (), [](const std::string& s) {
35
- return s == " -resource-dir" ;}) == ExtraArgs.end ()) {
36
- std::string resource_dir = Cpp::DetectResourceDir ();
37
- if (resource_dir.empty ())
38
- std::cerr << " Failed to detect the resource-dir\n " ;
39
- ClangArgs.push_back (" -resource-dir" );
40
- ClangArgs.push_back (resource_dir.c_str ());
41
- }
42
- std::vector<std::string> CxxSystemIncludes;
43
- Cpp::DetectSystemCompilerIncludePaths (CxxSystemIncludes);
44
- for (const std::string& CxxInclude : CxxSystemIncludes) {
45
- ClangArgs.push_back (" -isystem" );
46
- ClangArgs.push_back (CxxInclude.c_str ());
47
- }
36
+ if (std::find_if (
37
+ ExtraArgs.begin (),
38
+ ExtraArgs.end (),
39
+ [](const std::string& s)
40
+ {
41
+ return s == " -resource-dir" ;
42
+ }
43
+ )
44
+ == ExtraArgs.end ())
45
+ {
46
+ std::string resource_dir = Cpp::DetectResourceDir ();
47
+ if (resource_dir.empty ())
48
+ {
49
+ std::cerr << " Failed to detect the resource-dir\n " ;
50
+ }
51
+ ClangArgs.push_back (" -resource-dir" );
52
+ ClangArgs.push_back (resource_dir.c_str ());
53
+ }
54
+ std::vector<std::string> CxxSystemIncludes;
55
+ Cpp::DetectSystemCompilerIncludePaths (CxxSystemIncludes);
56
+ for (const std::string& CxxInclude : CxxSystemIncludes)
57
+ {
58
+ ClangArgs.push_back (" -isystem" );
59
+ ClangArgs.push_back (CxxInclude.c_str ());
60
+ }
48
61
#endif
49
- ClangArgs.insert (ClangArgs.end (), ExtraArgs.begin (), ExtraArgs.end ());
50
- // FIXME: We should process the kernel input options and conditionally pass
51
- // the gpu args here.
52
- return Cpp::CreateInterpreter (ClangArgs/* , {"-cuda"}*/ );
62
+ ClangArgs.insert (ClangArgs.end (), ExtraArgs.begin (), ExtraArgs.end ());
63
+ // FIXME: We should process the kernel input options and conditionally pass
64
+ // the gpu args here.
65
+ return Cpp::CreateInterpreter (ClangArgs /* , {"-cuda"}*/ );
53
66
}
54
67
55
68
using namespace std ::placeholders;
56
69
57
70
namespace xcpp
58
71
{
59
- struct StreamRedirectRAII {
60
- std::string &err;
61
- StreamRedirectRAII (std::string &e) : err(e) {
62
- Cpp::BeginStdStreamCapture (Cpp::kStdErr );
63
- Cpp::BeginStdStreamCapture (Cpp::kStdOut );
64
- }
65
- ~StreamRedirectRAII () {
66
- std::string out = Cpp::EndStdStreamCapture ();
67
- err = Cpp::EndStdStreamCapture ();
68
- std::cout << out;
69
- }
72
+ struct StreamRedirectRAII
73
+ {
74
+ std::string& err;
75
+
76
+ StreamRedirectRAII (std::string& e)
77
+ : err(e)
78
+ {
79
+ Cpp::BeginStdStreamCapture (Cpp::kStdErr );
80
+ Cpp::BeginStdStreamCapture (Cpp::kStdOut );
81
+ }
82
+
83
+ ~StreamRedirectRAII ()
84
+ {
85
+ std::string out = Cpp::EndStdStreamCapture ();
86
+ err = Cpp::EndStdStreamCapture ();
87
+ std::cout << out;
88
+ }
70
89
};
71
90
72
91
void interpreter::configure_impl ()
@@ -101,14 +120,14 @@ __get_cxx_version ()
101
120
return std::to_string (cxx_version);
102
121
}
103
122
104
- interpreter::interpreter (int argc, const char * const * argv) :
105
- xmagics ()
123
+ interpreter::interpreter (int argc, const char * const * argv)
124
+ : xmagics()
106
125
, p_cout_strbuf(nullptr )
107
126
, p_cerr_strbuf(nullptr )
108
127
, m_cout_buffer(std::bind(&interpreter::publish_stdout, this , _1))
109
128
, m_cerr_buffer(std::bind(&interpreter::publish_stderr, this , _1))
110
129
{
111
- // NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
130
+ // NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
112
131
createInterpreter (Args (argv ? argv + 1 : argv, argv + argc));
113
132
m_version = get_stdopt ();
114
133
redirect_output ();
@@ -174,6 +193,9 @@ __get_cxx_version ()
174
193
StreamRedirectRAII R (err);
175
194
if (code.rfind (" %undo" , 0 ) == 0 )
176
195
{
196
+ #ifdef EMSCRIPTEN
197
+ throw std::logic_error (" Undo is not supported in xeus-cpp-lite" );
198
+ #else
177
199
int n = 1 ; // Default value
178
200
if (code.length () > 5 )
179
201
{
@@ -184,21 +206,28 @@ __get_cxx_version ()
184
206
catch (const std::invalid_argument&)
185
207
{
186
208
throw std::runtime_error (
187
- " Invalid format for %undo. Expected '%undo n' where n is an integer. "
209
+ " Invalid format for %undo. Expected '%undo n' where n is an integer"
188
210
);
189
211
}
190
212
catch (const std::out_of_range&)
191
213
{
192
- throw std::runtime_error (" Number out of range for %undo. " );
214
+ throw std::runtime_error (" Number out of range for %undo" );
193
215
}
194
216
}
195
- compilation_result = Cpp::Undo (n) ? true : false ;
217
+ compilation_result = static_cast <bool >(Cpp::Undo (n));
218
+ #endif
196
219
}
197
220
else
198
221
{
199
222
compilation_result = Cpp::Process (code.c_str ());
200
223
}
201
224
}
225
+ catch (std::logic_error& e)
226
+ {
227
+ errorlevel = 1 ;
228
+ ename = " Logic Error: " ;
229
+ evalue = e.what ();
230
+ }
202
231
catch (std::exception& e)
203
232
{
204
233
errorlevel = 1 ;
@@ -239,10 +268,11 @@ __get_cxx_version ()
239
268
//
240
269
// JupyterLab displays the "{ename}: {evalue}" if the traceback is
241
270
// empty.
242
- if (evalue.size () < 4 ) {
271
+ if (evalue.size () < 4 )
272
+ {
243
273
ename = " " ;
244
274
}
245
- std::vector<std::string> traceback ({ename + evalue});
275
+ std::vector<std::string> traceback ({ename + evalue});
246
276
if (!config.silent )
247
277
{
248
278
publish_execution_error (ename, evalue, traceback);
@@ -285,7 +315,8 @@ __get_cxx_version ()
285
315
286
316
Cpp::CodeComplete (results, code.c_str (), 1 , _cursor_pos + 1 );
287
317
288
- return xeus::create_complete_reply (results /* matches*/ ,
318
+ return xeus::create_complete_reply (
319
+ results /* matches*/ ,
289
320
cursor_pos - to_complete.length () /* cursor_start*/ ,
290
321
cursor_pos /* cursor_end*/
291
322
);
@@ -306,13 +337,17 @@ __get_cxx_version ()
306
337
307
338
nl::json interpreter::is_complete_request_impl (const std::string& code)
308
339
{
309
- if (!code.empty () && code[code.size () - 1 ] == ' \\ ' ) {
340
+ if (!code.empty () && code[code.size () - 1 ] == ' \\ ' )
341
+ {
310
342
auto found = code.rfind (' \n ' );
311
343
if (found == std::string::npos)
344
+ {
312
345
found = -1 ;
346
+ }
313
347
auto found1 = found++;
314
- while (isspace (code[++found1])) ;
315
- return xeus::create_is_complete_reply (" incomplete" , code.substr (found, found1-found));
348
+ while (isspace (code[++found1]))
349
+ ;
350
+ return xeus::create_is_complete_reply (" incomplete" , code.substr (found, found1 - found));
316
351
}
317
352
318
353
return xeus::create_is_complete_reply (" complete" );
@@ -386,11 +421,11 @@ __get_cxx_version ()
386
421
387
422
void interpreter::init_preamble ()
388
423
{
389
- // NOLINTBEGIN(cppcoreguidelines-owning-memory)
424
+ // NOLINTBEGIN(cppcoreguidelines-owning-memory)
390
425
preamble_manager.register_preamble (" introspection" , std::make_unique<xintrospection>());
391
426
preamble_manager.register_preamble (" magics" , std::make_unique<xmagics_manager>());
392
427
preamble_manager.register_preamble (" shell" , std::make_unique<xsystem>());
393
- // NOLINTEND(cppcoreguidelines-owning-memory)
428
+ // NOLINTEND(cppcoreguidelines-owning-memory)
394
429
}
395
430
396
431
void interpreter::init_magic ()
0 commit comments