Skip to content

Commit

Permalink
Add support for Verilog-A style legacy pragma protect comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Jun 22, 2024
1 parent 4d655ee commit d455173
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/slang/parsing/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class SLANG_EXPORT Lexer {
bool scanUTF8Char(bool alreadyErrored, uint32_t* code, int& computedLen);
void scanEncodedText(ProtectEncoding encoding, uint32_t expectedBytes, bool singleLine,
bool legacyProtectedMode);
void scanProtectComment();

template<typename... Args>
Token create(TokenKind kind, Args&&... args);
Expand Down
55 changes: 55 additions & 0 deletions source/parsing/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "slang/diagnostics/LexerDiags.h"
#include "slang/diagnostics/NumericDiags.h"
#include "slang/diagnostics/PreprocessorDiags.h"
#include "slang/syntax/SyntaxKind.h"
#include "slang/text/CharInfo.h"
#include "slang/text/SourceManager.h"
Expand All @@ -23,6 +24,9 @@ static_assert(std::numeric_limits<double>::is_iec559, "SystemVerilog requires IE

static const double BitsPerDecimal = log2(10.0);

static constexpr std::string_view PragmaBeginProtected = "pragma protect begin_protected"sv;
static constexpr std::string_view PragmaEndProtected = "pragma protect end_protected"sv;

namespace slang::parsing {

using namespace syntax;
Expand Down Expand Up @@ -1195,6 +1199,27 @@ void Lexer::scanWhitespace() {
}

void Lexer::scanLineComment() {
if (options.enableLegacyProtect) {
// See if we're looking at a pragma protect comment and skip
// over it if so.
while (peek() == ' ')
advance();

bool found = true;
for (char c : PragmaBeginProtected) {
if (!consume(c)) {
found = false;
break;
}
}

if (found) {
scanProtectComment();
addTrivia(TriviaKind::DisabledText);
return;
}
}

bool sawUTF8Error = false;
while (true) {
char c = peek();
Expand Down Expand Up @@ -1458,6 +1483,36 @@ void Lexer::scanEncodedText(ProtectEncoding encoding, uint32_t expectedBytes, bo
}
}

void Lexer::scanProtectComment() {
addDiag(diag::ProtectedEnvelope, currentOffset() - PragmaBeginProtected.size());

while (true) {
char c = peek();
if (c == '\0' && reallyAtEnd()) {
addDiag(diag::RawProtectEOF, currentOffset() - 1);
return;
}

advance();
if (c == '/' && peek() == '/') {
advance();
while (peek() == ' ')
advance();

bool found = true;
for (char d : PragmaEndProtected) {
if (!consume(d)) {
found = false;
break;
}
}

if (found)
return;
}
}
}

template<typename... Args>
Token Lexer::create(TokenKind kind, Args&&... args) {
SourceLocation location(bufferId, size_t(marker - originalBegin));
Expand Down
4 changes: 4 additions & 0 deletions source/syntax/SyntaxPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ SyntaxPrinter& SyntaxPrinter::print(Trivia trivia) {
print(t);
}
break;
case TriviaKind::DisabledText:
if (includeSkipped)
append(trivia.getRawText());
break;
case TriviaKind::LineComment:
case TriviaKind::BlockComment:
if (!includeComments)
Expand Down
43 changes: 43 additions & 0 deletions tests/unittests/parsing/PreprocessorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2639,3 +2639,46 @@ endmodule
CHECK(diagnostics[0].code == diag::ProtectedEnvelope);
CHECK(diagnostics[1].code == diag::ProtectedEnvelope);
}

TEST_CASE("Verilog-A style pragma protect comments") {
auto& text = R"(
module top_design (a, b, c);
bottom inst ();
// pragma protect begin_protected
//pragma protect key_keyowner=Cadence Design Systems.
//pragma protect key_keyname=CDS_KEY
//pragma protect key_method=RC5
//pragma protect key_block
hjQ2rsuJMpL9F3O43Xx7zf656dz2xxBxdnHC0GvJFJG3Y5HL0dSoPcLMN5Zy6Iq+
ySMMWcOGkowbtoHVjNn3UdcZFD6NFlWHJpb7KIc8Php8iT1uEZmtwTgDSy64yqLL
SCaqKffWXhnJ5n/936szbTSvc8vs2ILJYG4FnjIZeYARwKjbofvTgA==
//pragma protect end_key_block
//pragma protect digest_block
uilUH9+52Dwx1U6ajpWVBgZque4=
//pragma protect end_digest_block
//pragma protect data_block
jGZcQn3lBzXvF2kCXy+abmSjUdOfUzPOp7g7dfEzgN96O2ZRQP4aN7kqJOCA9shI
jcvO6pnBhjaTNlxUJBSbBA==
//pragma protect end_data_block
//pragma protect digest_block
tzEpxTPg7KWB9yMYYlqfoVE3lVk=
//pragma protect end_digest_block
// pragma protect end_protected
endmodule
)";

auto& expected = R"(
module top_design (a, b, c);
bottom inst ();
endmodule
)";

LexerOptions lo;
lo.enableLegacyProtect = true;

std::string result = preprocess(text, lo);
CHECK(result == expected);

REQUIRE(diagnostics.size() == 1);
CHECK(diagnostics[0].code == diag::ProtectedEnvelope);
}

0 comments on commit d455173

Please sign in to comment.