-
Notifications
You must be signed in to change notification settings - Fork 69
Fix for #976 to Ignore Unused Stock Variables #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2307,6 +2307,18 @@ bool Semantics::CheckExprStmt(ExprStmt* stmt) { | |
return true; | ||
} | ||
|
||
bool Semantics::IsIncluded(Decl* sym) { | ||
const auto fileno = cc().sources()->GetSourceFileIndex(sym->pos()); | ||
if (fileno >= cc().sources()->opened_files().size()) { return false; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: no single-line conditionals. should be:
Though, I think this condition is unnecessary. We should never get a source index for a file not in the open list. |
||
|
||
return ! cc().sources()->opened_files()[fileno]->is_main_file(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "!cc_" (no space) |
||
} | ||
|
||
template <typename DeclType> | ||
bool Semantics::IsIncludedStock(DeclType* sym) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be VarDeclBase, no template needed. |
||
return sym->is_stock() && IsIncluded(sym); | ||
} | ||
|
||
/* testsymbols - test for unused local or global variables | ||
* | ||
* "Public" functions are excluded from the check, since these | ||
|
@@ -2355,9 +2367,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 */ | ||
} | ||
} | ||
|
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() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(3) : error 203: symbol is never used: "messages" |
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() {} |
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
stock const char messages[2][] = | ||
{ | ||
"first", | ||
"second" | ||
}; |
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() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stock const int X = 1; |
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() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc() should be cc_, here and elsewhere.