Skip to content

Commit 1340cdb

Browse files
Implement RULE-2-8, project should not contain unused object definitions.
Also add a new AlertReporting shared query library for deduplicating results across macro definitions/invocations/etc. Split __attribute__((unused)) variables (and similar) to a Strict pair of queries.
1 parent 2a805c0 commit 1340cdb

22 files changed

+1050
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/unused-object-definition
3+
* @name RULE-2-8: A project should not contain unused object definitions
4+
* @description Object definitions which are unused should be removed.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/rule-2-8
9+
* maintainability
10+
* performance
11+
* external/misra/c/2012/amendment4
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.deadcode.UnusedObjects
18+
19+
from ReportDeadObjectAtDefinition report
20+
where
21+
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionQuery()) and
22+
not report.hasAttrUnused()
23+
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(),
24+
report.getOptionalPlaceholderMessage()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/unused-object-definition-in-macro
3+
* @name RULE-2-8: Project macros should not include unused object definitions
4+
* @description Macros should not have unused object definitions.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/rule-2-8
9+
* maintainability
10+
* performance
11+
* external/misra/c/2012/amendment4
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.deadcode.UnusedObjects
18+
19+
from ReportDeadObjectInMacro report
20+
where
21+
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionInMacroQuery()) and
22+
not report.hasAttrUnused()
23+
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(),
24+
report.getOptionalPlaceholderMessage()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @id c/misra/unused-object-definition-in-macro-strict
3+
* @name RULE-2-8: Project macros should not include '__attribute__((unused))' object definitions
4+
* @description A strict query which reports all unused object definitions in macros with
5+
* '__attribute__((unused))'.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-2-8
10+
* maintainability
11+
* performance
12+
* external/misra/c/2012/amendment4
13+
* external/misra/c/strict
14+
* external/misra/obligation/advisory
15+
*/
16+
17+
import cpp
18+
import codingstandards.c.misra
19+
import codingstandards.cpp.deadcode.UnusedObjects
20+
21+
from ReportDeadObjectInMacro report
22+
where
23+
not isExcluded(report.getPrimaryElement(),
24+
DeadCode2Package::unusedObjectDefinitionInMacroStrictQuery()) and
25+
report.hasAttrUnused()
26+
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(),
27+
report.getOptionalPlaceholderMessage()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/unused-object-definition-strict
3+
* @name RULE-2-8: A project should not contain '__attribute__((unused))' object definitions
4+
* @description A strict query which reports all unused object definitions with
5+
* '__attribute__((unused))'.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-2-8
10+
* maintainability
11+
* performance
12+
* external/misra/c/2012/amendment4
13+
* external/misra/c/strict
14+
* external/misra/obligation/advisory
15+
*/
16+
17+
import cpp
18+
import codingstandards.c.misra
19+
import codingstandards.cpp.deadcode.UnusedObjects
20+
21+
from ReportDeadObjectAtDefinition report
22+
where
23+
not isExcluded(report.getPrimaryElement(), DeadCode2Package::unusedObjectDefinitionStrictQuery()) and
24+
report.hasAttrUnused()
25+
select report.getPrimaryElement(), report.getMessage(), report.getOptionalPlaceholderLocation(),
26+
report.getOptionalPlaceholderMessage()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| 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 | |
2+
| 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 | |
3+
| 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 | |
4+
| 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 | |
5+
| 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 | |
6+
| 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 | |
7+
| 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 | |
8+
| 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 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-2-8/UnusedObjectDefinition.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| 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 |
2+
| 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) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-2-8/UnusedObjectDefinitionInMacro.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| 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 |
2+
| 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) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-2-8/UnusedObjectDefinitionInMacroStrict.ql

0 commit comments

Comments
 (0)