-
Notifications
You must be signed in to change notification settings - Fork 60
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
Implement RULE-2-8, project should not contain unused object definitions #784
Open
MichaelRFairhurst
wants to merge
6
commits into
main
Choose a base branch
from
michaelrfairhurst/implement-deadcode-2-unused-object-definitions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1340cdb
Implement RULE-2-8, project should not contain unused object definiti…
MichaelRFairhurst ff562f9
Fix strict misra tag in rules schema
MichaelRFairhurst 125650c
Address review feedback
MichaelRFairhurst daa68d3
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 9492933
Format
MichaelRFairhurst 8c31c8c
format c
MichaelRFairhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @id c/misra/unused-object-definition | ||
* @name RULE-2-8: A project should not contain unused object definitions | ||
* @description Object definitions which are unused should be removed. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity recommendation | ||
* @tags external/misra/id/rule-2-8 | ||
* maintainability | ||
* performance | ||
* external/misra/c/2012/amendment4 | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.cpp.deadcode.UnusedObjects | ||
|
||
from ReportDeadObjectAtDefinition report | ||
where | ||
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionQuery()) and | ||
not report.hasAttrUnused() | ||
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(), | ||
report.getOptionalPlaceholderMessage() |
24 changes: 24 additions & 0 deletions
24
c/misra/src/rules/RULE-2-8/UnusedObjectDefinitionInMacro.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @id c/misra/unused-object-definition-in-macro | ||
* @name RULE-2-8: Project macros should not include unused object definitions | ||
* @description Macros should not have unused object definitions. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity recommendation | ||
* @tags external/misra/id/rule-2-8 | ||
* maintainability | ||
* performance | ||
* external/misra/c/2012/amendment4 | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.cpp.deadcode.UnusedObjects | ||
|
||
from ReportDeadObjectInMacro report | ||
where | ||
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionInMacroQuery()) and | ||
not report.hasAttrUnused() | ||
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(), | ||
report.getOptionalPlaceholderMessage() |
27 changes: 27 additions & 0 deletions
27
c/misra/src/rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @id c/misra/unused-object-definition-in-macro-strict | ||
* @name RULE-2-8: Project macros should not include '__attribute__((unused))' object definitions | ||
* @description A strict query which reports all unused object definitions in macros with | ||
* '__attribute__((unused))'. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity recommendation | ||
* @tags external/misra/id/rule-2-8 | ||
* maintainability | ||
* performance | ||
* external/misra/c/2012/amendment4 | ||
* external/misra/c/strict | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.cpp.deadcode.UnusedObjects | ||
|
||
from ReportDeadObjectInMacro report | ||
where | ||
not isExcluded(report.getPrimaryElement(), | ||
DeadCode2Package::unusedObjectDefinitionInMacroStrictQuery()) and | ||
report.hasAttrUnused() | ||
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(), | ||
report.getOptionalPlaceholderMessage() |
26 changes: 26 additions & 0 deletions
26
c/misra/src/rules/RULE-2-8/UnusedObjectDefinitionStrict.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @id c/misra/unused-object-definition-strict | ||
* @name RULE-2-8: A project should not contain '__attribute__((unused))' object definitions | ||
* @description A strict query which reports all unused object definitions with | ||
* '__attribute__((unused))'. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity recommendation | ||
* @tags external/misra/id/rule-2-8 | ||
* maintainability | ||
* performance | ||
* external/misra/c/2012/amendment4 | ||
* external/misra/c/strict | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.cpp.deadcode.UnusedObjects | ||
|
||
from ReportDeadObjectAtDefinition report | ||
where | ||
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionStrictQuery()) and | ||
report.hasAttrUnused() | ||
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(), | ||
report.getOptionalPlaceholderMessage() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| test.c:6:5:6:6 | definition of g2 | Unused object definition 'g2'. | test.c:6:5:6:6 | test.c:6:5:6:6 | | | ||
| test.c:9:5:9:6 | definition of g3 | Unused object definition 'g3'. | test.c:9:5:9:6 | test.c:9:5:9:6 | | | ||
| test.c:20:7:20:8 | definition of l2 | Unused object definition 'l2'. | test.c:20:7:20:8 | test.c:20:7:20:8 | | | ||
| test.c:27:7:27:8 | definition of l5 | Unused object definition 'l5'. | test.c:27:7:27:8 | test.c:27:7:27:8 | | | ||
| test.c:37:10:37:11 | definition of g5 | Unused object definition 'g5'. | test.c:37:10:37:11 | test.c:37:10:37:11 | | | ||
| test.c:45:9:45:10 | definition of g6 | Unused object definition 'g6'. | test.c:45:9:45:10 | test.c:45:9:45:10 | | | ||
| test.c:51:5:51:6 | definition of g7 | Unused object definition 'g7'. | test.c:51:5:51:6 | test.c:51:5:51:6 | | | ||
| test.c:64:3:64:18 | ONLY_DEF_VAR(x) | Unused object definition 'l2' from macro '$@'. | test.c:60:1:60:34 | test.c:60:1:60:34 | ONLY_DEF_VAR | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-2-8/UnusedObjectDefinition.ql |
2 changes: 2 additions & 0 deletions
2
c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacro.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.c:68:1:71:5 | #define ALSO_DEF_VAR(x) int x = 0; while (1) ; | Macro 'ALSO_DEF_VAR' defines unused object of varied names, for example, '$@'. | test.c:73:16:73:17 | test.c:73:16:73:17 | l1 | | ||
| test.c:77:1:82:3 | #define DEF_UNUSED_INNER_VAR() { int _v = 0; while (1) ; } | Macro 'DEF_UNUSED_INNER_VAR' defines unused object '_v'. | test.c:77:1:82:3 | test.c:77:1:82:3 | (ignored) | |
1 change: 1 addition & 0 deletions
1
c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacro.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-2-8/UnusedObjectDefinitionInMacro.ql |
2 changes: 2 additions & 0 deletions
2
c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.c:94:1:97:5 | #define ALSO_DEF_ATTR_UNUSED_VAR(x) __attribute__((unused)) int x = 0; while (1) ; | Macro 'ALSO_DEF_ATTR_UNUSED_VAR' defines unused object of varied names, for example, '$@'. | test.c:99:28:99:29 | test.c:99:28:99:29 | l1 | | ||
| test.c:104:1:109:3 | #define DEF_ATTR_UNUSED_INNER_VAR() { __attribute__((unused)) int _v = 0; while (1) ; } | Macro 'DEF_ATTR_UNUSED_INNER_VAR' defines unused object '_v'. | test.c:104:1:109:3 | test.c:104:1:109:3 | (ignored) | |
1 change: 1 addition & 0 deletions
1
c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.ql |
2 changes: 2 additions & 0 deletions
2
c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionStrict.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.c:87:29:87:30 | definition of g8 | Unused object definition 'g8'. | test.c:87:29:87:30 | test.c:87:29:87:30 | | | ||
| test.c:90:3:90:30 | ONLY_DEF_ATTR_UNUSED_VAR(x) | Unused object definition 'l2' from macro '$@'. | test.c:88:1:88:70 | test.c:88:1:88:70 | ONLY_DEF_ATTR_UNUSED_VAR | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-2-8/UnusedObjectDefinitionStrict.ql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Not a definition, only a declaration: | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extern int g1; // COMPLIANT | ||
|
||
// Both declared + defined: | ||
extern int g2; // COMPLIANT | ||
int g2 = 1; // NON_COMPLIANT | ||
|
||
// Definition is only declaration: | ||
int g3 = 1; // NON_COMPLIANT | ||
|
||
// Definition, but value is required for program to compile: | ||
int g4 = 1; // COMPLIANT | ||
void f1() { g4; } | ||
|
||
// Local variables: | ||
void f2() { | ||
int l1; // COMPLIANT | ||
l1; | ||
|
||
int l2; // NON-COMPLIANT | ||
|
||
// Value is required for the program to compile: | ||
int l3; // COMPLIANT | ||
sizeof(l3); | ||
|
||
int l4, // COMPLIANT | ||
l5; // NON-COMPLIANT | ||
l4; | ||
} | ||
|
||
// Struct fields are not objects: | ||
struct s { | ||
int x; // COMPLIANT | ||
}; | ||
|
||
// Declaration of type struct is an object: | ||
struct s g5; // NON-COMPLIANT | ||
|
||
// Struct fields are not objects: | ||
union u { | ||
int x; // COMPLIANT | ||
}; | ||
|
||
// Declaration of type union is an object: | ||
union u g6; // NON-COMPLIANT | ||
|
||
// Typedefs are not objects: | ||
typedef int td1; // COMPLIANT | ||
|
||
// Declaration of typedef type object: | ||
td1 g7; // NON-COMPLIANT | ||
|
||
// Function parameters are not objects: | ||
void f3(int p) {} // COMPLIANT | ||
|
||
// Function type parameters are not objects: | ||
typedef int td2(int x); // COMPLIANT | ||
|
||
// Macros that define unused vars tests: | ||
#define ONLY_DEF_VAR(x) int x = 0; | ||
void f4() { | ||
ONLY_DEF_VAR(l1); // COMPLIANT | ||
l1; | ||
ONLY_DEF_VAR(l2); // NON-COMPLIANT | ||
} | ||
|
||
// NON-COMPLIANT | ||
#define ALSO_DEF_VAR(x) \ | ||
int x = 0; \ | ||
while (1) \ | ||
; | ||
void f5() { | ||
ALSO_DEF_VAR(l1); // COMPLIANT | ||
ALSO_DEF_VAR(l2); // COMPLIANT | ||
} | ||
|
||
#define DEF_UNUSED_INNER_VAR() \ | ||
{ \ | ||
int _v = 0; \ | ||
while (1) \ | ||
; \ | ||
} // NON-COMPLIANT | ||
void f6() { | ||
DEF_UNUSED_INNER_VAR(); // COMPLIANT | ||
} | ||
|
||
__attribute__((unused)) int g8 = 1; // NON-COMPLIANT | ||
#define ONLY_DEF_ATTR_UNUSED_VAR(x) __attribute__((unused)) int x = 0; | ||
void f7() { | ||
ONLY_DEF_ATTR_UNUSED_VAR(l2); // NON-COMPLIANT | ||
} | ||
|
||
// NON-COMPLIANT | ||
#define ALSO_DEF_ATTR_UNUSED_VAR(x) \ | ||
__attribute__((unused)) int x = 0; \ | ||
while (1) \ | ||
; | ||
void f8() { | ||
ALSO_DEF_ATTR_UNUSED_VAR(l1); // COMPLIANT | ||
ALSO_DEF_ATTR_UNUSED_VAR(l2); // COMPLIANT | ||
} | ||
|
||
// NON-COMPLIANT | ||
#define DEF_ATTR_UNUSED_INNER_VAR() \ | ||
{ \ | ||
__attribute__((unused)) int _v = 0; \ | ||
while (1) \ | ||
; \ | ||
} | ||
|
||
void f9() { | ||
DEF_ATTR_UNUSED_INNER_VAR(); // COMPLIANT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it makes sense to use the tag "strict" here to cover these cases. Although we've added strict tags before (to AUTOSAR queries), we've never integrated them into our query suites, or provided user facing recommendations around them.
There are a few ways we could consider doing so, but I think my preference would be to:
user_manual.md
to discuss the option of "strict" mode.development_handbook.md
to discuss the criteria for a query to be considered for "strict" mode.We should also, in this case, put into the "implementation_scope" why we consider this to be a "strict" query - i.e. why it is safe to default disable it.
The alternative to a "strict mode tag", that I think is worth mentioning here, is to provide a default "deviations" configuration that disables such queries that we would consider to be "strict".
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.
Offered a strict definition of what should be considered a strict query, hopefully it LGTY