Skip to content

Commit

Permalink
[slang-tidy] Fix XilinxDoNotCareValues checker (#1225)
Browse files Browse the repository at this point in the history
Co-authored-by: Yan Churkin <[email protected]>
  • Loading branch information
likeamahoney and Yan Churkin authored Jan 28, 2025
1 parent f8042bf commit 2e39ffe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tools/tidy/src/synthesis/XilinxDoNotCareValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
#include "fmt/color.h"

#include "slang/syntax/AllSyntax.h"
#include "slang/syntax/SyntaxPrinter.h"

using namespace slang;
using namespace slang::ast;
using namespace slang::syntax;

namespace xilinx_do_not_care_values {
struct MainVisitor : public TidyVisitor, ASTVisitor<MainVisitor, true, true> {
explicit MainVisitor(Diagnostics& diagnostics) : TidyVisitor(diagnostics) {}

void handle(const IntegerLiteral& expr) {
if (auto syntax = expr.syntax; syntax) {
bool hasDoNotCare = syntax->toString().find('?') != std::string::npos;
bool hasDecimal = syntax->toString().find('d') != std::string::npos;
auto strSyntax = SyntaxPrinter().setIncludeTrivia(false).print(*syntax).str();
bool hasDoNotCare = strSyntax.find('?') != std::string::npos;
bool hasDecimal = strSyntax.find('d') != std::string::npos;

if (hasDoNotCare && hasDecimal)
diags.add(diag::XilinxDoNotCareValues, syntax->sourceRange());
Expand Down
21 changes: 21 additions & 0 deletions tools/tidy/tests/XilinxDoNotCareValuesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ endmodule
bool result = visitor->check(root);
CHECK(result);
}

TEST_CASE("XilinxDoNotCareValues: No do-not-care values but with comment") {
auto tree = SyntaxTree::fromText(R"(
module top;
logic [3:0] a =
/*'d?*/4'd10;
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);
compilation.getAllDiagnostics();
auto& root = compilation.getRoot();

TidyConfig config;
Registry::setConfig(config);
Registry::setSourceManager(compilation.getSourceManager());
auto visitor = Registry::create("XilinxDoNotCareValues");
bool result = visitor->check(root);
CHECK(result);
}

0 comments on commit 2e39ffe

Please sign in to comment.