Skip to content

Commit

Permalink
Add Wunused-import and Wunused-wildcard-import
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed May 19, 2024
1 parent 7d97140 commit e342f51
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion scripts/diagnostics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ warning unused-type-parameter UnusedTypeParameter "unused type parameter '{}'"
warning unused-typedef UnusedTypedef "unused typedef '{}'"
warning unused-genvar UnusedGenvar "unused genvar '{}'"
warning unused-assertion-decl UnusedAssertionDecl "unused {} '{}'"
warning unused-import UnusedImport "unused import '{}'"
warning unused-wildcard-import UnusedWildcardImport "unused wildcard import"
warning missing-top NoTopModules "no top-level modules found in design"
warning duplicate-defparam DuplicateDefparam "parameter already has a defparam override applied; ignoring this one"

Expand Down Expand Up @@ -1149,4 +1151,4 @@ group conversion = { width-trunc width-expand port-width-trunc port-width-expand
group unused = { unused-def unused-net unused-implicit-net unused-variable undriven-net unassigned-variable
unused-but-set-net unused-but-set-variable unused-port undriven-port unused-argument
unused-parameter unused-type-parameter unused-typedef unused-genvar unused-assertion-decl
unused-but-set-port unused-config-cell unused-config-instance }
unused-but-set-port unused-config-cell unused-config-instance unused-import unused-wildcard-import }
24 changes: 24 additions & 0 deletions scripts/warning_docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,30 @@ module m;
endmodule
```

-Wunused-import
A package import directive is never used.
```
package p;
int a;
endpackage

module m;
import p::a;
endmodule
```

-Wunused-wildcard-import
A wildcard package import directive is never used.
```
package p;
int a;
endpackage

module m;
import p::*;
endmodule
```

-Wbad-procedural-force
According to the Verilog/SystemVerilog standard, it is illegal to procedurally force a
bit-select or range select of a variable (you can do that with a net, or with a plain
Expand Down
14 changes: 14 additions & 0 deletions source/ast/ElabVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,20 @@ struct PostElabVisitor : public ASTVisitor<PostElabVisitor, false, false> {
void handle(const LetDeclSymbol& symbol) { checkAssertionDeclUnused(symbol, "let"sv); }
void handle(const CheckerSymbol& symbol) { checkAssertionDeclUnused(symbol, "checker"sv); }

void handle(const ExplicitImportSymbol& symbol) { checkUnused(symbol, diag::UnusedImport); }

void handle(const WildcardImportSymbol& symbol) {
auto syntax = symbol.getSyntax();
if (!syntax)
return;

auto [used, _] = compilation.isReferenced(*syntax);
if (!used) {
if (shouldWarn(symbol))
symbol.getParentScope()->addDiag(diag::UnusedWildcardImport, symbol.location);
}
}

private:
void checkValueUnused(const ValueSymbol& symbol, DiagCode unusedCode,
std::optional<DiagCode> unsetCode, std::optional<DiagCode> unreadCode) {
Expand Down
3 changes: 3 additions & 0 deletions source/ast/Lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,7 @@ void Lookup::unqualifiedImpl(const Scope& scope, std::string_view name, LookupLo
case SymbolKind::ExplicitImport:
result.found = symbol->as<ExplicitImportSymbol>().importedSymbol();
result.flags |= LookupResultFlags::WasImported;
scope.getCompilation().noteReference(*symbol);
break;
case SymbolKind::ForwardingTypedef:
// If we find a forwarding typedef, the actual typedef was never defined.
Expand Down Expand Up @@ -1886,6 +1887,8 @@ void Lookup::unqualifiedImpl(const Scope& scope, std::string_view name, LookupLo

result.flags |= LookupResultFlags::WasImported;
result.found = imports[0].imported;
scope.getCompilation().noteReference(*imports[0].import);

wildcardImportData->importedSymbols.try_emplace(result.found->name, result.found);
return;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/ast/WarningTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,30 @@ endmodule
CHECK(diags[2].code == diag::UnusedAssertionDecl);
}

TEST_CASE("Unused imports") {
auto tree = SyntaxTree::fromText(R"(
package p;
int a;
endpackage
module m;
import p::a;
import p::*;
endmodule
)");

CompilationOptions coptions;
coptions.flags = CompilationFlags::None;

Compilation compilation(coptions);
compilation.addSyntaxTree(tree);

auto& diags = compilation.getAllDiagnostics();
REQUIRE(diags.size() == 2);
CHECK(diags[0].code == diag::UnusedImport);
CHECK(diags[1].code == diag::UnusedWildcardImport);
}

TEST_CASE("Implicit conversions with constants") {
auto tree = SyntaxTree::fromText(R"(
module m;
Expand Down

0 comments on commit e342f51

Please sign in to comment.