@@ -215,6 +215,7 @@ std::string frontend_verilog_preproc(std::istream &f, std::string filename, cons
215
215
{
216
216
std::set<std::string> defines_with_args;
217
217
std::map<std::string, std::string> defines_map (pre_defines_map);
218
+ std::vector<std::string> filename_stack;
218
219
int ifdef_fail_level = 0 ;
219
220
bool in_elseif = false ;
220
221
@@ -305,26 +306,47 @@ std::string frontend_verilog_preproc(std::istream &f, std::string filename, cons
305
306
}
306
307
std::ifstream ff;
307
308
ff.clear ();
308
- ff.open (fn.c_str ());
309
+ std::string fixed_fn = fn;
310
+ ff.open (fixed_fn.c_str ());
309
311
if (ff.fail () && fn.size () > 0 && fn[0 ] != ' /' && filename.find (' /' ) != std::string::npos) {
310
312
// if the include file was not found, it is not given with an absolute path, and the
311
313
// currently read file is given with a path, then try again relative to its directory
312
314
ff.clear ();
313
- ff.open (filename.substr (0 , filename.rfind (' /' )+1 ) + fn);
315
+ fixed_fn = filename.substr (0 , filename.rfind (' /' )+1 ) + fn;
316
+ ff.open (fixed_fn);
314
317
}
315
318
if (ff.fail () && fn.size () > 0 && fn[0 ] != ' /' ) {
316
319
// if the include file was not found and it is not given with an absolute path, then
317
320
// search it in the include path
318
321
for (auto incdir : include_dirs) {
319
322
ff.clear ();
320
- ff.open (incdir + ' /' + fn);
323
+ fixed_fn = incdir + ' /' + fn;
324
+ ff.open (fixed_fn);
321
325
if (!ff.fail ()) break ;
322
326
}
323
327
}
324
328
if (ff.fail ())
325
329
output_code.push_back (" `file_notfound " + fn);
326
330
else
327
- input_file (ff, fn);
331
+ input_file (ff, fixed_fn);
332
+ continue ;
333
+ }
334
+
335
+ if (tok == " `file_push" ) {
336
+ skip_spaces ();
337
+ std::string fn = next_token (true );
338
+ if (!fn.empty () && fn.front () == ' "' && fn.back () == ' "' )
339
+ fn = fn.substr (1 , fn.size ()-2 );
340
+ output_code.push_back (tok + " \" " + fn + " \" " );
341
+ filename_stack.push_back (filename);
342
+ filename = fn;
343
+ continue ;
344
+ }
345
+
346
+ if (tok == " `file_pop" ) {
347
+ output_code.push_back (tok);
348
+ filename = filename_stack.back ();
349
+ filename_stack.pop_back ();
328
350
continue ;
329
351
}
330
352
0 commit comments