Skip to content

Commit c8e53e3

Browse files
committed
Verilog: fix includes of files in same subdirectory
This fixes the case where a Verilog file is in a subdirectory and a local include is in the same subdirectory.
1 parent fff1078 commit c8e53e3

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
KNOWNBUG
1+
CORE
22
subdir/in_subdir.v
33
--preprocess
4+
`line 1 "subdir/include_file\.vh" 1
45
^EXIT=0$
56
^SIGNAL=0$
67
--
7-
include file in subdirectory not found

regression/verilog/preprocessor/include2.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include2.v
44
// Enable multi-line checking
55
activate-multi-line-match
66
`line 1 "include2\.v" 0
7-
`line 1 "include_file\.vh" 1
7+
`line 1 "subdir/include_file\.vh" 1
88

99
`line 2 "include2\.v" 2
1010
^EXIT=0$

src/verilog/verilog_preprocessor.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -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
}

src/verilog/verilog_preprocessor.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class verilog_preprocessort:public preprocessort
4444
definest defines;
4545

4646
void directive();
47-
std::string find_include_file(const std::string &filename);
47+
std::string find_include_file(
48+
const std::string &including_file,
49+
const std::string &given_filename);
4850
definet::parameterst parse_define_parameters();
4951

5052
using define_argumentst = std::map<std::string, std::vector<tokent>>;

0 commit comments

Comments
 (0)