@@ -98,23 +98,29 @@ Function: verilog_preprocessort::include
9898
9999\*******************************************************************/
100100
101- std::string
102- verilog_preprocessort::find_include_file (const std::string &filename)
101+ std::string verilog_preprocessort::find_include_file (
102+ const std::string &including_file,
103+ const std::string &given_filename)
103104{
104- // first try filename as is
105- if (std::filesystem::directory_entry (filename).exists ())
106- return filename; // done
105+ {
106+ // First try given filename relative to the path of the
107+ // including file.
108+ auto path = std::filesystem::path (including_file);
109+ path.replace_filename (given_filename);
110+ if (std::filesystem::directory_entry (path).exists ())
111+ return path; // done
112+ }
107113
108114 // try include paths in given order
109115 for (const auto &path : config.verilog .include_paths )
110116 {
111- auto full_name = std::filesystem::path (path).append (filename );
117+ auto full_name = std::filesystem::path (path).append (given_filename );
112118 if (std::filesystem::directory_entry (full_name).exists ())
113119 return full_name; // done
114120 }
115121
116122 throw verilog_preprocessor_errort ()
117- << " include file `" << filename << " ' not found" ;
123+ << " include file `" << given_filename << " ' not found" ;
118124}
119125
120126/* ******************************************************************\
@@ -505,8 +511,8 @@ void verilog_preprocessort::directive()
505511 << " expecting a string literal after `include" ;
506512
507513 // strip quotes off string literal, escaping, etc.
508- auto filename = file_token.string_literal_value ();
509- auto full_path = find_include_file (filename);
514+ auto given_filename = file_token.string_literal_value ();
515+ auto full_path = find_include_file (context (). filename , given_filename );
510516
511517#ifdef _MSC_VER
512518 auto in = new std::ifstream (widen (full_path));
@@ -520,7 +526,7 @@ void verilog_preprocessort::directive()
520526 tokenizer ().skip_until_eol ();
521527 tokenizer ().next_token (); // eat the \n
522528
523- context_stack.emplace_back (true , in, filename );
529+ context_stack.emplace_back (true , in, full_path );
524530 emit_line_directive (1 ); // 'enter'
525531 // we now continue in the new context
526532 }
0 commit comments