@@ -99,26 +99,30 @@ Function: verilog_preprocessort::include
99
99
\*******************************************************************/
100
100
101
101
std::string verilog_preprocessort::find_include_file (
102
- const std::string &filename,
102
+ const std::string &including_file,
103
+ const std::string &given_filename,
103
104
bool include_paths_only)
104
105
{
105
106
if (!include_paths_only)
106
107
{
107
- // first try filename as is
108
- if (std::filesystem::directory_entry (filename).exists ())
109
- return filename; // done
108
+ // First try given filename relative to the path of the
109
+ // including file.
110
+ auto path = std::filesystem::path (including_file);
111
+ path.replace_filename (given_filename);
112
+ if (std::filesystem::directory_entry (path).exists ())
113
+ return path; // done
110
114
}
111
115
112
- // try include paths in given order
116
+ // Then try include paths in given order.
113
117
for (const auto &path : config.verilog .include_paths )
114
118
{
115
- auto full_name = std::filesystem::path (path).append (filename );
119
+ auto full_name = std::filesystem::path (path).append (given_filename );
116
120
if (std::filesystem::directory_entry (full_name).exists ())
117
121
return full_name; // done
118
122
}
119
123
120
124
throw verilog_preprocessor_errort ()
121
- << " include file `" << filename << " ' not found" ;
125
+ << " include file `" << given_filename << " ' not found" ;
122
126
}
123
127
124
128
/* ******************************************************************\
@@ -507,7 +511,7 @@ void verilog_preprocessort::directive()
507
511
// We expect one of:
508
512
// <filename> -- include paths only
509
513
// "filename" -- relative path, then include paths.
510
- std::string filename ;
514
+ std::string given_filename ;
511
515
bool include_paths_only;
512
516
513
517
if (tokenizer ().peek ().is_string_literal ())
@@ -516,7 +520,7 @@ void verilog_preprocessort::directive()
516
520
const auto file_token = tokenizer ().next_token ();
517
521
CHECK_RETURN (file_token.is_string_literal ());
518
522
// strip quotes off string literal, escaping, etc.
519
- filename = file_token.string_literal_value ();
523
+ given_filename = file_token.string_literal_value ();
520
524
}
521
525
else if (tokenizer ().peek () == ' <' )
522
526
{
@@ -528,7 +532,7 @@ void verilog_preprocessort::directive()
528
532
if (tokenizer ().peek ().is_eof ())
529
533
throw verilog_preprocessor_errort () << " eof in include directive" ;
530
534
531
- filename += tokenizer ().next_token ().text ;
535
+ given_filename += tokenizer ().next_token ().text ;
532
536
}
533
537
534
538
tokenizer ().next_token (); // >
@@ -539,7 +543,8 @@ void verilog_preprocessort::directive()
539
543
<< " expecting either \" or < after `include" ;
540
544
}
541
545
542
- auto full_path = find_include_file (filename, include_paths_only);
546
+ auto full_path =
547
+ find_include_file (context ().filename , given_filename, include_paths_only);
543
548
544
549
#ifdef _MSC_VER
545
550
auto in = new std::ifstream (widen (full_path));
@@ -553,7 +558,7 @@ void verilog_preprocessort::directive()
553
558
tokenizer ().skip_until_eol ();
554
559
tokenizer ().next_token (); // eat the \n
555
560
556
- context_stack.emplace_back (true , in, filename );
561
+ context_stack.emplace_back (true , in, full_path );
557
562
emit_line_directive (1 ); // 'enter'
558
563
// we now continue in the new context
559
564
}
0 commit comments