-
Notifications
You must be signed in to change notification settings - Fork 66
Implement RULE-2-8, project should not contain unused object definitions #784
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
Merged
lcartey
merged 11 commits into
main
from
michaelrfairhurst/implement-deadcode-2-unused-object-definitions
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
11 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 a75dc0a
Address feedback
MichaelRFairhurst d23c035
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 333cc77
Code format
MichaelRFairhurst a8784e1
Simplify DeduplicateMacroResults::Report::ReportResult::toString() fo…
MichaelRFairhurst 3566e0f
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
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 hidden or 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 |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
- exclude: | ||
tags contain: | ||
- external/misra/audit | ||
- external/misra/strict | ||
- external/misra/default-disabled |
This file contains hidden or 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 @@ | ||
- description: MISRA C 2012 (Strict) | ||
- qlpack: codeql/misra-c-coding-standards | ||
- include: | ||
kind: | ||
- problem | ||
- path-problem | ||
tags contain: | ||
- external/misra/strict |
This file contains hidden or 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 ReportDeadObject report | ||
where | ||
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionQuery()) and | ||
not 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 hidden or 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 ReportDeadObject report | ||
where | ||
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionStrictQuery()) and | ||
report.hasAttrUnused() | ||
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(), | ||
report.getOptionalPlaceholderMessage() |
12 changes: 12 additions & 0 deletions
12
c/misra/test/rules/RULE-2-8/UnusedObjectDefinition.expected
This file contains hidden or 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,12 @@ | ||
| test.c:6:5:6:6 | definition of g2 | Unused object 'g2'. | test.c:6:5:6:6 | test.c:6:5:6:6 | (ignored) | | ||
| test.c:9:5:9:6 | definition of g3 | Unused object 'g3'. | test.c:9:5:9:6 | test.c:9:5:9:6 | (ignored) | | ||
| test.c:20:7:20:8 | definition of l2 | Unused object 'l2'. | test.c:20:7:20:8 | test.c:20:7:20:8 | (ignored) | | ||
| test.c:27:7:27:8 | definition of l5 | Unused object 'l5'. | test.c:27:7:27:8 | test.c:27:7:27:8 | (ignored) | | ||
| test.c:37:10:37:11 | definition of g5 | Unused object 'g5'. | test.c:37:10:37:11 | test.c:37:10:37:11 | (ignored) | | ||
| test.c:45:9:45:10 | definition of g6 | Unused object 'g6'. | test.c:45:9:45:10 | test.c:45:9:45:10 | (ignored) | | ||
| test.c:51:5:51:6 | definition of g7 | Unused object 'g7'. | test.c:51:5:51:6 | test.c:51:5:51:6 | (ignored) | | ||
| test.c:64:3:64:18 | ONLY_DEF_VAR(x) | Invocation of macro '$@' defines unused object 'l2'. | test.c:60:1:60:34 | test.c:60:1:60:34 | ONLY_DEF_VAR | | ||
| test.c:68:1:71:5 | #define ALSO_DEF_VAR(x) int x = 0; while (1) ; | Macro 'ALSO_DEF_VAR' defines unused object with an invocation-dependent name, 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) | | ||
| test.c:119:11:119:13 | definition of g10 | Unused object 'g10'. | test.c:119:11:119:13 | test.c:119:11:119:13 | (ignored) | | ||
| test.c:124:13:124:14 | definition of l2 | Unused object 'l2'. | test.c:124:13:124:14 | test.c:124:13:124:14 | (ignored) | |
This file contains hidden or 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 |
4 changes: 4 additions & 0 deletions
4
c/misra/test/rules/RULE-2-8/UnusedObjectDefinitionStrict.expected
This file contains hidden or 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,4 @@ | ||
| test.c:87:29:87:30 | definition of g8 | Unused object 'g8'. | test.c:87:29:87:30 | test.c:87:29:87:30 | (ignored) | | ||
| test.c:92:3:92:30 | ONLY_DEF_ATTR_UNUSED_VAR(x) | Invocation of macro '$@' defines unused object 'l2'. | test.c:88:1:88:70 | test.c:88:1:88:70 | ONLY_DEF_ATTR_UNUSED_VAR | | ||
| test.c:96:1:99:5 | #define ALSO_DEF_ATTR_UNUSED_VAR(x) __attribute__((unused)) int x = 0; while (1) ; | Macro 'ALSO_DEF_ATTR_UNUSED_VAR' defines unused object with an invocation-dependent name, for example, '$@'. | test.c:101:28:101:29 | test.c:101:28:101:29 | l1 | | ||
| test.c:106:1:111: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:106:1:111:3 | test.c:106:1:111:3 | (ignored) | |
This file contains hidden or 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 hidden or 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,133 @@ | ||
// Not a definition, only a declaration: | ||
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(l1); // COMPLIANT | ||
l1; | ||
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 | ||
} | ||
|
||
// Const variable tests: | ||
const int g9 = 1; // COMPLIANT | ||
const int g10 = 1; // NON-COMPLIANT | ||
|
||
void f10() { | ||
g9; | ||
const int l1 = 1; // COMPLIANT | ||
const int l2 = 1; // NON-COMPLIANT | ||
l1; | ||
} | ||
|
||
// Side effects should not disable this rule: | ||
void f11() { | ||
int l1 = 1; // COMPLIANT | ||
int l2 = l1++; // COMPLIANT | ||
l2; | ||
} |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.