Skip to content

Commit

Permalink
Verilog: fix includes of files in same subdirectory
Browse files Browse the repository at this point in the history
This fixes the case where a Verilog file is in a subdirectory and a local
include is in the same subdirectory.
  • Loading branch information
kroening committed Apr 22, 2024
1 parent 7766725 commit 85fa857
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions regression/verilog/preprocessor/in_subdir.desc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
KNOWNBUG
CORE
subdir/in_subdir.v
--preprocess
`line 1 "subdir/include_file\.vh" 1
^EXIT=0$
^SIGNAL=0$
--
include file in subdirectory not found
2 changes: 1 addition & 1 deletion regression/verilog/preprocessor/include2.desc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include2.v
// Enable multi-line checking
activate-multi-line-match
`line 1 "include2\.v" 0
`line 1 "include_file\.vh" 1
`line 1 "subdir/include_file\.vh" 1

`line 2 "include2\.v" 2
^EXIT=0$
Expand Down
29 changes: 17 additions & 12 deletions src/verilog/verilog_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,30 @@ Function: verilog_preprocessort::include
\*******************************************************************/

std::string verilog_preprocessort::find_include_file(
const std::string &filename,
const std::string &including_file,
const std::string &given_filename,
bool include_paths_only)
{
if(!include_paths_only)
{
// first try filename as is
if(std::filesystem::directory_entry(filename).exists())
return filename; // done
// First try given filename relative to the path of the
// including file.
auto path = std::filesystem::path(including_file);
path.replace_filename(given_filename);
if(std::filesystem::directory_entry(path).exists())
return path; // done
}

// try include paths in given order
// Then try include paths in given order.
for(const auto &path : config.verilog.include_paths)
{
auto full_name = std::filesystem::path(path).append(filename);
auto full_name = std::filesystem::path(path).append(given_filename);
if(std::filesystem::directory_entry(full_name).exists())
return full_name; // done
}

throw verilog_preprocessor_errort()
<< "include file `" << filename << "' not found";
<< "include file `" << given_filename << "' not found";
}

/*******************************************************************\
Expand Down Expand Up @@ -507,7 +511,7 @@ void verilog_preprocessort::directive()
// We expect one of:
// <filename> -- include paths only
// "filename" -- relative path, then include paths.
std::string filename;
std::string given_filename;
bool include_paths_only;

if(tokenizer().peek().is_string_literal())
Expand All @@ -516,7 +520,7 @@ void verilog_preprocessort::directive()
const auto file_token = tokenizer().next_token();
CHECK_RETURN(file_token.is_string_literal());
// strip quotes off string literal, escaping, etc.
filename = file_token.string_literal_value();
given_filename = file_token.string_literal_value();
}
else if(tokenizer().peek() == '<')
{
Expand All @@ -528,7 +532,7 @@ void verilog_preprocessort::directive()
if(tokenizer().peek().is_eof())
throw verilog_preprocessor_errort() << "eof in include directive";

filename += tokenizer().next_token().text;
given_filename += tokenizer().next_token().text;
}

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

auto full_path = find_include_file(filename, include_paths_only);
auto full_path =
find_include_file(context().filename, given_filename, include_paths_only);

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

context_stack.emplace_back(true, in, filename);
context_stack.emplace_back(true, in, full_path);
emit_line_directive(1); // 'enter'
// we now continue in the new context
}
Expand Down
6 changes: 4 additions & 2 deletions src/verilog/verilog_preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ class verilog_preprocessort:public preprocessort
definest defines;

void directive();
std::string
find_include_file(const std::string &filename, bool include_paths_only);
std::string find_include_file(
const std::string &including_file,
const std::string &given_filename,
bool include_paths_only);
definet::parameterst parse_define_parameters();

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

0 comments on commit 85fa857

Please sign in to comment.