Skip to content

Commit

Permalink
Fix erroneous warning on unused stock variables.
Browse files Browse the repository at this point in the history
Bug: issue #976
Test: new test cases
Signed-off-by: David Anderson <[email protected]>
  • Loading branch information
c0rp3n authored and dvander committed Nov 29, 2024
1 parent c07a41c commit 3cdb429
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 3 deletions.
20 changes: 17 additions & 3 deletions compiler/semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,15 @@ bool Semantics::CheckExprStmt(ExprStmt* stmt) {
return true;
}

bool Semantics::IsIncluded(Decl* sym) {
const auto fileno = cc_.sources()->GetSourceFileIndex(sym->pos());
return !cc_.sources()->opened_files()[fileno]->is_main_file();
}

bool Semantics::IsIncludedStock(VarDeclBase* sym) {
return sym->vclass() == sGLOBAL && sym->is_stock() && IsIncluded(sym);
}

/* testsymbols - test for unused local or global variables
*
* "Public" functions are excluded from the check, since these
Expand Down Expand Up @@ -2359,9 +2368,14 @@ bool Semantics::TestSymbol(Decl* sym, bool testconst) {
default: {
auto var = sym->as<VarDeclBase>();
/* a variable */
if (!var->is_used() && !var->is_public()) {
report(sym, 203) << sym->name(); /* symbol isn't used (and not public) */
} else if (!var->is_public() && !var->is_read()) {

// We ignore variables that are marked as public or stock that was included.
if (var->is_public() || IsIncludedStock(var))
break;

if (!var->is_used()) {
report(sym, 203) << sym->name(); /* symbol isn't used (and not public/stock) */
} else if (!var->is_read()) {
report(sym, 204) << sym->name(); /* value assigned to symbol is never used */
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ class Semantics final
bool CheckScalarType(Expr* expr);
bool IsThisAtom(sp::Atom* atom);

bool IsIncluded(Decl* expr);
bool IsIncludedStock(VarDeclBase* expr);

private:
CompileContext& cc_;
TypeManager* types_ = nullptr;
Expand Down
9 changes: 9 additions & 0 deletions tests/compile-only/fail-stock-array-unused.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// warnings_are_errors: true

stock const char messages[2][] =
{
"first",
"second"
};

public void main() {}
1 change: 1 addition & 0 deletions tests/compile-only/fail-stock-array-unused.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(3) : error 203: symbol is never used: "messages"
5 changes: 5 additions & 0 deletions tests/compile-only/fail-stock-var-unused.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// warnings_are_errors: true

stock const int X = 1;

public void main() {}
1 change: 1 addition & 0 deletions tests/compile-only/fail-stock-var-unused.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(3) : error 204: symbol is assigned a value that is never used: "X"
5 changes: 5 additions & 0 deletions tests/compile-only/ok-included-stock-array-unused.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stock const char messages[2][] =
{
"first",
"second"
};
5 changes: 5 additions & 0 deletions tests/compile-only/ok-included-stock-array-unused.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// warnings_are_errors: true

#include "ok-included-stock-array-unused.inc"

public void main() {}
1 change: 1 addition & 0 deletions tests/compile-only/ok-included-stock-var-unused.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stock const int X = 1;
5 changes: 5 additions & 0 deletions tests/compile-only/ok-included-stock-var-unused.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// warnings_are_errors: true

#include "ok-included-stock-var-unused.inc"

public void main() {}

0 comments on commit 3cdb429

Please sign in to comment.