Skip to content

Commit

Permalink
PR: Make command-line defines stronger than `define's inside Verilog …
Browse files Browse the repository at this point in the history
…files (#929)
  • Loading branch information
udif authored Apr 2, 2024
1 parent 3ec5fe0 commit 9c9b487
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/slang/parsing/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class SLANG_EXPORT Preprocessor {
const syntax::DefineDirectiveSyntax* syntax = nullptr;
MacroIntrinsic intrinsic = MacroIntrinsic::None;
bool builtIn = false;
bool commandLine = false;

MacroDef() = default;
MacroDef(const syntax::DefineDirectiveSyntax* syntax) : syntax(syntax) {}
Expand Down
8 changes: 6 additions & 2 deletions source/parsing/Preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ void Preprocessor::predefine(const std::string& definition, std::string_view nam
// Look for the macro in the temporary preprocessor's macro map.
// Any macros found that are not the built-in intrinsic macros should
// be copied over to our own map.
for (const auto& pair : pp.macros) {
if (!pair.second.isIntrinsic())
for (auto& pair : pp.macros) {
if (!pair.second.isIntrinsic()) {
pair.second.commandLine = true;
macros.insert(pair);
}
}
}

Expand Down Expand Up @@ -640,6 +642,8 @@ Trivia Preprocessor::handleDefineDirective(Token directive) {
addDiag(diag::InvalidMacroName, name.range());
bad = true;
}
else if (it->second.commandLine)
bad = true; // not really bad, but commandLine args has precedence so we skip this
else if (!bad && it->second.valid() && !isSameMacro(*result, *it->second.syntax)) {
auto& diag = addDiag(diag::RedefiningMacro, name.range());
diag << name.valueText();
Expand Down
35 changes: 35 additions & 0 deletions tests/unittests/parsing/PreprocessorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,41 @@ TEST_CASE("Preprocessor API") {
CHECK(pp.getDefinedMacros().size() == 19);
}

TEST_CASE("Command-line defines priority over `define") {
PreprocessorOptions ppOptions;
ppOptions.predefines.emplace_back("A=2");
ppOptions.predefines.emplace_back("B=2");
ppOptions.predefines.emplace_back("C=2");

Bag options;
options.set(ppOptions);

auto& text = R"(
`define A 1
`undef B
`undef C
`define C 1
)";
Preprocessor preprocessor(getSourceManager(), alloc, diagnostics, options);
preprocessor.pushSource(text);

while (true) {
Token token = preprocessor.next();
if (token.kind == TokenKind::EndOfFile)
break;
}

CHECK(!preprocessor.isDefined("B"));
CHECK(preprocessor.isDefined("A"));
CHECK(preprocessor.isDefined("C"));
for (auto macro : preprocessor.getDefinedMacros()) {
if (macro->name.toString() == "A")
CHECK(macro->body[0].toString() == "2");
if (macro->name.toString() == "C")
CHECK(macro->body[0].toString() == "1");
}
}

TEST_CASE("Undef builtin") {
auto& text = R"(
`undef __slang__
Expand Down

0 comments on commit 9c9b487

Please sign in to comment.