@@ -137,9 +137,6 @@ __get_cxx_version ()
137137 {
138138 nl::json kernel_res;
139139
140-
141- auto input_guard = input_redirection (config.allow_stdin );
142-
143140 // Check for magics
144141 for (auto & pre : preamble_manager.preamble )
145142 {
@@ -151,7 +148,11 @@ __get_cxx_version ()
151148 }
152149 }
153150
151+ // Split code from includes
152+ auto blocks = split_from_includes (code.c_str ());
153+
154154 auto errorlevel = 0 ;
155+
155156 std::string ename;
156157 std::string evalue;
157158 // bool compilation_result = false;
@@ -173,30 +174,36 @@ __get_cxx_version ()
173174
174175 std::string err;
175176
176- // Attempt normal evaluation
177- try
178- {
179- StreamRedirectRAII R (err);
180- output_value = Cpp::Evaluate (code.c_str (), &hadError);
181- }
182- catch (std::exception& e)
183- {
184- errorlevel = 1 ;
185- ename = " Standard Exception: " ;
186- evalue = e.what ();
187- }
188- catch (...)
189- {
190- errorlevel = 1 ;
191- ename = " Error: " ;
192- }
177+ // Scope guard performing the temporary redirection of input requests.
178+ auto input_guard = input_redirection (config.allow_stdin );
193179
194- if (hadError )
180+ for ( const auto & block : blocks )
195181 {
196- errorlevel = 1 ;
197- ename = " Error: " ;
198- evalue = " Compilation error! " + err;
199- std::cerr << err;
182+ // Attempt normal evaluation
183+ try
184+ {
185+ StreamRedirectRAII R (err);
186+ output_value = Cpp::Evaluate (block.c_str (), &hadError);
187+ }
188+ catch (std::exception& e)
189+ {
190+ errorlevel = 1 ;
191+ ename = " Standard Exception: " ;
192+ evalue = e.what ();
193+ }
194+ catch (...)
195+ {
196+ errorlevel = 1 ;
197+ ename = " Error: " ;
198+ }
199+
200+ if (hadError)
201+ {
202+ errorlevel = 1 ;
203+ ename = " Error: " ;
204+ evalue = " Compilation error! " + err;
205+ std::cerr << err;
206+ }
200207 }
201208
202209 // Flush streams
@@ -238,7 +245,7 @@ __get_cxx_version ()
238245 {
239246 // Publish a mime bundle for the last return value if
240247 // the semicolon was omitted.
241- if (!config.silent && !hadError && trim (code ).back () != ' ;' ) {
248+ if (!config.silent && !hadError && trim (blocks. back () ).back () != ' ;' ) {
242249 nl::json pub_data = mime_repr (output_value);
243250 publish_execution_result (execution_counter, std::move (pub_data), nl::json::object ());
244251 }
0 commit comments