Skip to content

Commit 088f9c9

Browse files
committedMar 14, 2017
Fix verilog pre-processor for multi-level relative includes
1 parent c855353 commit 088f9c9

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed
 

‎frontends/verilog/preproc.cc

+26-4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ std::string frontend_verilog_preproc(std::istream &f, std::string filename, cons
215215
{
216216
std::set<std::string> defines_with_args;
217217
std::map<std::string, std::string> defines_map(pre_defines_map);
218+
std::vector<std::string> filename_stack;
218219
int ifdef_fail_level = 0;
219220
bool in_elseif = false;
220221

@@ -305,26 +306,47 @@ std::string frontend_verilog_preproc(std::istream &f, std::string filename, cons
305306
}
306307
std::ifstream ff;
307308
ff.clear();
308-
ff.open(fn.c_str());
309+
std::string fixed_fn = fn;
310+
ff.open(fixed_fn.c_str());
309311
if (ff.fail() && fn.size() > 0 && fn[0] != '/' && filename.find('/') != std::string::npos) {
310312
// if the include file was not found, it is not given with an absolute path, and the
311313
// currently read file is given with a path, then try again relative to its directory
312314
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);
314317
}
315318
if (ff.fail() && fn.size() > 0 && fn[0] != '/') {
316319
// if the include file was not found and it is not given with an absolute path, then
317320
// search it in the include path
318321
for (auto incdir : include_dirs) {
319322
ff.clear();
320-
ff.open(incdir + '/' + fn);
323+
fixed_fn = incdir + '/' + fn;
324+
ff.open(fixed_fn);
321325
if (!ff.fail()) break;
322326
}
323327
}
324328
if (ff.fail())
325329
output_code.push_back("`file_notfound " + fn);
326330
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();
328350
continue;
329351
}
330352

0 commit comments

Comments
 (0)
Please sign in to comment.