2626
2727using Args = std::vector<const char *>;
2828
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++"};
3133#ifdef EMSCRIPTEN
32- ClangArgs.push_back (" -std=c++20" );
34+ ClangArgs.push_back (" -std=c++20" );
3335#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+ }
4861#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"}*/ );
5366}
5467
5568using namespace std ::placeholders;
5669
5770namespace xcpp
5871{
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+ }
7089 };
7190
7291 void interpreter::configure_impl ()
@@ -101,14 +120,14 @@ __get_cxx_version ()
101120 return std::to_string (cxx_version);
102121 }
103122
104- interpreter::interpreter (int argc, const char * const * argv) :
105- xmagics ()
123+ interpreter::interpreter (int argc, const char * const * argv)
124+ : xmagics()
106125 , p_cout_strbuf(nullptr )
107126 , p_cerr_strbuf(nullptr )
108127 , m_cout_buffer(std::bind(&interpreter::publish_stdout, this , _1))
109128 , m_cerr_buffer(std::bind(&interpreter::publish_stderr, this , _1))
110129 {
111- // NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
130+ // NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
112131 createInterpreter (Args (argv ? argv + 1 : argv, argv + argc));
113132 m_version = get_stdopt ();
114133 redirect_output ();
@@ -174,6 +193,9 @@ __get_cxx_version ()
174193 StreamRedirectRAII R (err);
175194 if (code.rfind (" %undo" , 0 ) == 0 )
176195 {
196+ #ifdef EMSCRIPTEN
197+ throw std::logic_error (" Undo is not supported in xeus-cpp-lite" );
198+ #else
177199 int n = 1 ; // Default value
178200 if (code.length () > 5 )
179201 {
@@ -184,21 +206,28 @@ __get_cxx_version ()
184206 catch (const std::invalid_argument&)
185207 {
186208 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"
188210 );
189211 }
190212 catch (const std::out_of_range&)
191213 {
192- throw std::runtime_error (" Number out of range for %undo. " );
214+ throw std::runtime_error (" Number out of range for %undo" );
193215 }
194216 }
195- compilation_result = Cpp::Undo (n) ? true : false ;
217+ compilation_result = static_cast <bool >(Cpp::Undo (n));
218+ #endif
196219 }
197220 else
198221 {
199222 compilation_result = Cpp::Process (code.c_str ());
200223 }
201224 }
225+ catch (std::logic_error& e)
226+ {
227+ errorlevel = 1 ;
228+ ename = " Logic Error: " ;
229+ evalue = e.what ();
230+ }
202231 catch (std::exception& e)
203232 {
204233 errorlevel = 1 ;
@@ -239,10 +268,11 @@ __get_cxx_version ()
239268 //
240269 // JupyterLab displays the "{ename}: {evalue}" if the traceback is
241270 // empty.
242- if (evalue.size () < 4 ) {
271+ if (evalue.size () < 4 )
272+ {
243273 ename = " " ;
244274 }
245- std::vector<std::string> traceback ({ename + evalue});
275+ std::vector<std::string> traceback ({ename + evalue});
246276 if (!config.silent )
247277 {
248278 publish_execution_error (ename, evalue, traceback);
@@ -285,7 +315,8 @@ __get_cxx_version ()
285315
286316 Cpp::CodeComplete (results, code.c_str (), 1 , _cursor_pos + 1 );
287317
288- return xeus::create_complete_reply (results /* matches*/ ,
318+ return xeus::create_complete_reply (
319+ results /* matches*/ ,
289320 cursor_pos - to_complete.length () /* cursor_start*/ ,
290321 cursor_pos /* cursor_end*/
291322 );
@@ -306,13 +337,17 @@ __get_cxx_version ()
306337
307338 nl::json interpreter::is_complete_request_impl (const std::string& code)
308339 {
309- if (!code.empty () && code[code.size () - 1 ] == ' \\ ' ) {
340+ if (!code.empty () && code[code.size () - 1 ] == ' \\ ' )
341+ {
310342 auto found = code.rfind (' \n ' );
311343 if (found == std::string::npos)
344+ {
312345 found = -1 ;
346+ }
313347 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));
316351 }
317352
318353 return xeus::create_is_complete_reply (" complete" );
@@ -386,11 +421,11 @@ __get_cxx_version ()
386421
387422 void interpreter::init_preamble ()
388423 {
389- // NOLINTBEGIN(cppcoreguidelines-owning-memory)
424+ // NOLINTBEGIN(cppcoreguidelines-owning-memory)
390425 preamble_manager.register_preamble (" introspection" , std::make_unique<xintrospection>());
391426 preamble_manager.register_preamble (" magics" , std::make_unique<xmagics_manager>());
392427 preamble_manager.register_preamble (" shell" , std::make_unique<xsystem>());
393- // NOLINTEND(cppcoreguidelines-owning-memory)
428+ // NOLINTEND(cppcoreguidelines-owning-memory)
394429 }
395430
396431 void interpreter::init_magic ()
0 commit comments