Skip to content

Commit 85fa857

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 7766725 commit 85fa857

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed
Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,30 @@ Function: verilog_preprocessort::include
9999
\*******************************************************************/
100100

101101
std::string verilog_preprocessort::find_include_file(
102-
const std::string &filename,
102+
const std::string &including_file,
103+
const std::string &given_filename,
103104
bool include_paths_only)
104105
{
105106
if(!include_paths_only)
106107
{
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
110114
}
111115

112-
// try include paths in given order
116+
// Then try include paths in given order.
113117
for(const auto &path : config.verilog.include_paths)
114118
{
115-
auto full_name = std::filesystem::path(path).append(filename);
119+
auto full_name = std::filesystem::path(path).append(given_filename);
116120
if(std::filesystem::directory_entry(full_name).exists())
117121
return full_name; // done
118122
}
119123

120124
throw verilog_preprocessor_errort()
121-
<< "include file `" << filename << "' not found";
125+
<< "include file `" << given_filename << "' not found";
122126
}
123127

124128
/*******************************************************************\
@@ -507,7 +511,7 @@ void verilog_preprocessort::directive()
507511
// We expect one of:
508512
// <filename> -- include paths only
509513
// "filename" -- relative path, then include paths.
510-
std::string filename;
514+
std::string given_filename;
511515
bool include_paths_only;
512516

513517
if(tokenizer().peek().is_string_literal())
@@ -516,7 +520,7 @@ void verilog_preprocessort::directive()
516520
const auto file_token = tokenizer().next_token();
517521
CHECK_RETURN(file_token.is_string_literal());
518522
// strip quotes off string literal, escaping, etc.
519-
filename = file_token.string_literal_value();
523+
given_filename = file_token.string_literal_value();
520524
}
521525
else if(tokenizer().peek() == '<')
522526
{
@@ -528,7 +532,7 @@ void verilog_preprocessort::directive()
528532
if(tokenizer().peek().is_eof())
529533
throw verilog_preprocessor_errort() << "eof in include directive";
530534

531-
filename += tokenizer().next_token().text;
535+
given_filename += tokenizer().next_token().text;
532536
}
533537

534538
tokenizer().next_token(); // >
@@ -539,7 +543,8 @@ void verilog_preprocessort::directive()
539543
<< "expecting either \" or < after `include";
540544
}
541545

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);
543548

544549
#ifdef _MSC_VER
545550
auto in = new std::ifstream(widen(full_path));
@@ -553,7 +558,7 @@ void verilog_preprocessort::directive()
553558
tokenizer().skip_until_eol();
554559
tokenizer().next_token(); // eat the \n
555560

556-
context_stack.emplace_back(true, in, filename);
561+
context_stack.emplace_back(true, in, full_path);
557562
emit_line_directive(1); // 'enter'
558563
// we now continue in the new context
559564
}

src/verilog/verilog_preprocessor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ class verilog_preprocessort:public preprocessort
4444
definest defines;
4545

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

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

0 commit comments

Comments
 (0)