Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verilog: `__FILE__ and `__LINE__ #941

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions regression/verilog/preprocessor/file1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
file1.v
--preprocess
^ "file1\.v", 4\);$
^EXIT=0$
^SIGNAL=0$
--
^PREPROCESSING FAILED$
6 changes: 6 additions & 0 deletions regression/verilog/preprocessor/file1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module main;

initial $display("Internal error: null handle at %s, line %d.",
`__FILE__, `__LINE__);

endmodule
99 changes: 54 additions & 45 deletions src/verilog/expr2verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,59 @@ Author: Daniel Kroening, [email protected]
#include <iomanip>
#include <sstream>

std::string verilog_string_literal(const std::string &src)
{
std::string dest;

dest = '"';

for(auto &ch : src)
{
// Follows Table Table 5-1 in 1800-2017.
switch(ch)
{
case '\n':
dest += "\\n";
break;
case '\t':
dest += "\\t";
break;
case '\\':
dest += "\\\\";
break;
case '"':
dest += "\\\"";
break;
case '\v':
dest += "\\v";
break;
case '\f':
dest += "\\f";
break;
case '\a':
dest += "\\a";
break;
default:
if(
(unsigned(ch) >= ' ' && unsigned(ch) <= 126) ||
(unsigned(ch) >= 128 && unsigned(ch) <= 254))
{
dest += ch;
}
else
{
std::ostringstream oss;
oss << "\\x" << std::setw(2) << std::setfill('0') << std::hex << ch;
dest += oss.str();
}
}
}

dest += '"';

return dest;
}

/*******************************************************************\

Function: expr2verilogt::convert_if
Expand Down Expand Up @@ -1214,51 +1267,7 @@ expr2verilogt::resultt expr2verilogt::convert_constant(
}
else if(type.id() == ID_string)
{
dest = '"';

for(auto &ch : id2string(src.get_value()))
{
// Follows Table Table 5-1 in 1800-2017.
switch(ch)
{
case '\n':
dest += "\\n";
break;
case '\t':
dest += "\\t";
break;
case '\\':
dest += "\\\\";
break;
case '"':
dest += "\\\"";
break;
case '\v':
dest += "\\v";
break;
case '\f':
dest += "\\f";
break;
case '\a':
dest += "\\a";
break;
default:
if(
(unsigned(ch) >= ' ' && unsigned(ch) <= 126) ||
(unsigned(ch) >= 128 && unsigned(ch) <= 254))
{
dest += ch;
}
else
{
std::ostringstream oss;
oss << "\\x" << std::setw(2) << std::setfill('0') << std::hex << ch;
dest += oss.str();
}
}
}

dest += '"';
dest = verilog_string_literal(id2string(src.get_value()));
}
else if(type.id() == ID_verilog_chandle || type.id() == ID_verilog_event)
{
Expand Down
2 changes: 2 additions & 0 deletions src/verilog/expr2verilog.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ Author: Daniel Kroening, [email protected]

std::string expr2verilog(const exprt &, const namespacet &);
std::string type2verilog(const typet &, const namespacet &);

std::string verilog_string_literal(const std::string &);
13 changes: 13 additions & 0 deletions src/verilog/verilog_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
#include <util/config.h>
#include <util/unicode.h>

#include "expr2verilog.h"
#include "verilog_preprocessor_error.h"

#include <filesystem>
Expand Down Expand Up @@ -672,6 +673,18 @@ void verilog_preprocessort::directive()
break;
}
}
else if(text == "__FILE__")
{
// 1800 2017 22.13
// String literal
out << verilog_string_literal(context().filename_as_string());
}
else if(text == "__LINE__")
{
// 1800 2017 22.13
// decimal number
out << tokenizer().line_no();
}
else
{
// check defines
Expand Down
Loading