Skip to content

Commit

Permalink
Verilog preprocessor: fix line number reporting when including
Browse files Browse the repository at this point in the history
This changes reporting of the line numbers when including to a more
idiomatic variant that avoids repeating the same line number for the file
that contains the include directive.
  • Loading branch information
kroening committed Sep 26, 2023
1 parent 5de0c61 commit a339806
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
3 changes: 1 addition & 2 deletions regression/verilog/preprocessor/include1.desc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ activate-multi-line-match
`line 1 "include1b.v" 1
line b 1
line b 2
`line 1 "include1.v" 2

`line 2 "include1.v" 2
line 2
line 3
^EXIT=0$
Expand Down
55 changes: 22 additions & 33 deletions src/verilog/verilog_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,45 +98,19 @@ Function: verilog_preprocessort::include
\*******************************************************************/

void verilog_preprocessort::include(const std::string &filename)
std::string
verilog_preprocessort::find_include_file(const std::string &filename)
{
// first try filename as is
{
#ifdef _MSC_VER
auto in = new std::ifstream(widen(filename));
#else
auto in = new std::ifstream(filename);
#endif

if(*in)
{
context_stack.emplace_back(true, in, filename);
context().print_line_directive(out, 1); // 'enter'
return; // done
}
else
delete in;
}
if(file_exists(filename))
return filename; // done

// try include paths in given order
for(const auto &path : config.verilog.include_paths)
{
auto full_name = concat_dir_file(path, filename);

#ifdef _MSC_VER
auto in = new std::ifstream(widen(full_name));
#else
auto in = new std::ifstream(full_name);
#endif

if(*in)
{
context_stack.emplace_back(true, in, filename);
context().print_line_directive(out, 1); // 'enter'
return; // done
}

delete in;
if(file_exists(full_name))
return full_name; // done
}

throw verilog_preprocessor_errort()
Expand Down Expand Up @@ -486,8 +460,23 @@ void verilog_preprocessort::directive()

// strip quotes off string literal, escaping, etc.
auto filename = file_token.string_literal_value();
auto full_path = find_include_file(filename);

#ifdef _MSC_VER
auto in = new std::ifstream(widen(full_path));
#else
auto in = new std::ifstream(full_path);
#endif

if(!*in)
throw verilog_preprocessor_errort() << "failed to open an include file";

tokenizer().skip_until_eol();
include(filename);
tokenizer().next_token(); // eat the \n

context_stack.emplace_back(true, in, filename);
context().print_line_directive(out, 1); // 'enter'
// we now continue in the new context
}
else if(text=="resetall")
{
Expand Down
2 changes: 1 addition & 1 deletion src/verilog/verilog_preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class verilog_preprocessort:public preprocessort
definest defines;

void directive();
void include(const std::string &filename);
std::string find_include_file(const std::string &filename);
definet::parameterst parse_define_parameters();

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

0 comments on commit a339806

Please sign in to comment.