Skip to content

Commit

Permalink
Merge pull request #941 from diffblue/file1
Browse files Browse the repository at this point in the history
Verilog: ``__FILE__` and ``__LINE__`
  • Loading branch information
tautschnig authored Jan 17, 2025
2 parents 6e81cd1 + dce0b37 commit a7f30f8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 45 deletions.
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

0 comments on commit a7f30f8

Please sign in to comment.