Skip to content

Commit 125650c

Browse files
Address review feedback
1 parent ff562f9 commit 125650c

File tree

9 files changed

+82
-16
lines changed

9 files changed

+82
-16
lines changed

c/misra/src/codeql-suites/misra-c-default.qls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
- exclude:
88
tags contain:
99
- external/misra/audit
10+
- external/misra/strict
1011
- external/misra/default-disabled
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- description: MISRA C 2012 (Strict)
2+
- qlpack: codeql/misra-c-coding-standards
3+
- include:
4+
kind:
5+
- problem
6+
- path-problem
7+
tags contain:
8+
- external/misra/strict

c/misra/test/rules/RULE-2-8/UnusedObjectDefinition.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
| 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 | |
77
| 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 | |
88
| 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 |
9+
| test.c:117:11:117:13 | definition of g10 | Unused object definition 'g10'. | test.c:117:11:117:13 | test.c:117:11:117:13 | |
10+
| test.c:122:13:122:14 | definition of l2 | Unused object definition 'l2'. | test.c:122:13:122:14 | test.c:122:13:122:14 | |

c/misra/test/rules/RULE-2-8/test.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,21 @@ void f8() {
111111
void f9() {
112112
DEF_ATTR_UNUSED_INNER_VAR(); // COMPLIANT
113113
}
114+
115+
// Const variable tests:
116+
const int g9 = 1; // COMPLIANT
117+
const int g10 = 1; // NON-COMPLIANT
118+
119+
void f10() {
120+
g9;
121+
const int l1 = 1; // COMPLIANT
122+
const int l2 = 1; // NON-COMPLIANT
123+
l1;
124+
}
125+
126+
// Side effects should not disable this rule:
127+
void f11() {
128+
int l1 = 1; // COMPLIANT
129+
int l2 = l1++; // COMPLIANT
130+
l2;
131+
}

cpp/common/src/codingstandards/cpp/deadcode/UnusedObjects.qll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cpp
2+
import codingstandards.cpp.deadcode.UnusedVariables
23
import codingstandards.cpp.alertreporting.HoldsForAllCopies
34
import codingstandards.cpp.alertreporting.DeduplicateMacroResults
45

@@ -15,10 +16,13 @@ import codingstandards.cpp.alertreporting.DeduplicateMacroResults
1516
*/
1617
class UnusedObjectDefinition extends VariableDeclarationEntry {
1718
UnusedObjectDefinition() {
19+
(
20+
getVariable() instanceof BasePotentiallyUnusedLocalVariable
21+
or
22+
getVariable() instanceof BasePotentiallyUnusedGlobalOrNamespaceVariable
23+
) and
1824
not exists(VariableAccess access | access.getTarget() = getVariable()) and
19-
getVariable().getDefinition() = this and
20-
not this instanceof ParameterDeclarationEntry and
21-
not getVariable() instanceof MemberVariable
25+
getVariable().getDefinition() = this
2226
}
2327

2428
/* Dead objects with these attributes are reported in the "strict" queries. */

cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ predicate declarationHasSideEffects(Variable v) {
3636
v.getType() instanceof TemplateDependentType
3737
}
3838

39-
/** A `LocalVariable` which is a candidate for being unused. */
40-
class PotentiallyUnusedLocalVariable extends LocalVariable {
41-
PotentiallyUnusedLocalVariable() {
42-
// Ignore variables declared in macro expansions
43-
not exists(DeclStmt ds | ds.getADeclaration() = this and ds.isInMacroExpansion()) and
39+
/**
40+
* A `LocalVariable` which is a candidate for being unused, and may or may not be defined in a macro.
41+
*/
42+
class BasePotentiallyUnusedLocalVariable extends LocalVariable {
43+
BasePotentiallyUnusedLocalVariable() {
4444
// Ignore variables where initializing the variable has side effects
4545
not declarationHasSideEffects(this) and // TODO non POD types with initializers? Also, do something different with templates?
4646
exists(Function f | f = getFunction() |
@@ -56,6 +56,16 @@ class PotentiallyUnusedLocalVariable extends LocalVariable {
5656
}
5757
}
5858

59+
/**
60+
* A `LocalVariable` which is a candidate for being unused, and not defined in a macro.
61+
*/
62+
class PotentiallyUnusedLocalVariable extends BasePotentiallyUnusedLocalVariable {
63+
PotentiallyUnusedLocalVariable() {
64+
// Ignore variables declared in macro expansions
65+
not exists(DeclStmt ds | ds.getADeclaration() = this and ds.isInMacroExpansion())
66+
}
67+
}
68+
5969
/** Holds if `mf` is "defined" in this database. */
6070
predicate isDefined(MemberFunction mf) {
6171
exists(MemberFunction definedMemberFunction |
@@ -105,13 +115,11 @@ class PotentiallyUnusedMemberVariable extends MemberVariable {
105115
}
106116
}
107117

108-
/** A `GlobalOrNamespaceVariable` which is potentially unused. */
109-
class PotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariable {
110-
PotentiallyUnusedGlobalOrNamespaceVariable() {
118+
/** A `GlobalOrNamespaceVariable` which is potentially unused and may or may not be defined in a macro */
119+
class BasePotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariable {
120+
BasePotentiallyUnusedGlobalOrNamespaceVariable() {
111121
// A non-defined variable may never be used
112122
hasDefinition() and
113-
// Not declared in a macro expansion
114-
not isInMacroExpansion() and
115123
// No side-effects from declaration
116124
not declarationHasSideEffects(this) and
117125
// exclude uninstantiated template members
@@ -121,6 +129,17 @@ class PotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariab
121129
}
122130
}
123131

132+
/**
133+
* A `GlobalOrNamespaceVariable` which is potentially unused, and is not defined in a macro.
134+
*/
135+
class PotentiallyUnusedGlobalOrNamespaceVariable extends BasePotentiallyUnusedGlobalOrNamespaceVariable
136+
{
137+
PotentiallyUnusedGlobalOrNamespaceVariable() {
138+
// Not declared in a macro expansion
139+
not isInMacroExpansion()
140+
}
141+
}
142+
124143
predicate isUnused(Variable variable) {
125144
not exists(variable.getAnAccess()) and
126145
variable.getInitializer().fromSource()

docs/development_handbook.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Each coding standard consists of a list of "guidelines", however not all the gui
5151

5252
For some of the rules which are not amenable to static analysis, we may opt to provide a query which aids with "auditing" the rules. For example, AUTOSAR includes a rule (A10-0-1) "Public inheritance shall be used to implement 'is-a' relationship.". This is not directly amenable to static analysis, because it requires external context around the concept being modeled. However, we can provide an "audit" rule which reports all the public and private inheritance relationships in the program, so they can be manually verified.
5353

54+
For other rules, there may be means of indicating that a contravention is intentional, and where requiring a _devation report_ may be extra burdensome on developers and require double-entry. These results should be reported under a "strict" query. For instance, `RULE-2-8` "A project should not contain unused object definitions," where adding `__attribute__((unused))` may be preferable in order to suppress compiler warnings (which _deviation reports_ do not do) and are highly indicative of an intentional contravention by a developer.
55+
5456
For each rule which will be implemented with a query we have assigned a "rule package". Rule packages represent sets of rules, possibly across standards, that will be implemented together. Examples of rule packages include "Exceptions", "Naming", "Pointers" and so forth. By implementing queries for related rules together, we intend to maximize the amount of code shared between queries, and to ensure query developers can gain a deep understanding of that specific topic.
5557

5658
The canonical list of rules, with implementation categorization and assigned rule packages, are stored in this repository in the `rules.csv` file.

docs/user_manual.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ For each rule we therefore identify whether it is supportable or not. Furthermor
7171

7272
- **Automated** - the queries for the rule find contraventions directly.
7373
- **Audit only** - the queries for the rule does not find contraventions directly, but instead report a list of _candidates_ that can be used as input into a manual audit. For example, `A10-0-1` (_Public inheritance shall be used to implement 'is-a' relationship_) is not directly amenable to static analysis, but CodeQL can be used to produce a list of all the locations that use public inheritance so they can be manually reviewed.
74+
- **Strict only** - the queries for the rule find contraventions directly, but find results which are strongly indicated to be intentional, and where adding a _deviation report_ may be extra burden on developers. For example, in `RULE-2-8` (_A project should not contain unused object definitions_), declaring objects with `__attribute__((unused))` may be preferable to a _deviation report_, which will not suppress relevant compiler warnings, and therefore would otherwise require developer double-entry.
7475

7576
Each supported rule is implemented as one or more CodeQL queries, with each query covering an aspect of the rule. In many coding standards, the rules cover non-trivial semantic properties of the codebase under analysis.
7677

@@ -214,14 +215,22 @@ The following flags may be passed to the `database analyze` command to adjust th
214215

215216
The output of this command will be a [SARIF file](https://sarifweb.azurewebsites.net/) called `<name-of-results-file>.sarif`.
216217

217-
#### Running the analysis for audit level queries
218+
#### Running the analysis for strict and/or audit level queries
218219

219-
Optionally, you may want to run the "audit" level queries. These queries produce lists of results that do not directly highlight contraventions of the rule. Instead, they identify locations in the code that can be manually audited to verify the absence of problems for that particular rule.
220+
Optionally, you may want to run the "strict" or "audit" level queries.
221+
222+
Audit queries produce lists of results that do not directly highlight contraventions of the rule. Instead, they identify locations in the code that can be manually audited to verify the absence of problems for that particular rule.
220223

221224
```bash
222225
codeql database analyze --format=sarifv2.1.0 --output=<name-of-results-file>.sarif path/to/<output_database_name> path/to/codeql-coding-standards/cpp/<coding-standard>/src/codeql-suites/<coding-standard>-audit.qls...
223226
```
224227

228+
Strict queries identify contraventions in the code that strongly suggest they are deliberate, and where adding an explicit _deviation report_ may be extra burden on developers.
229+
230+
```bash
231+
codeql database analyze --format=sarifv2.1.0 --output=<name-of-results-file>.sarif path/to/<output_database_name> path/to/codeql-coding-standards/cpp/<coding-standard>/src/codeql-suites/<coding-standard>-strict.qls...
232+
```
233+
225234
For each Coding Standard you want to run, add a trailing entry in the following format: `path/to/codeql-coding-standards/cpp/<coding-standard>/src/codeql-suites/<coding-standard>-default.qls`.
226235

227236
#### Producing an analysis report

rule_packages/c/DeadCode2.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@
6060
]
6161
}
6262
],
63-
"title": "A project should not contain unused object definitions"
63+
"title": "A project should not contain unused object definitions",
64+
"implementation_scope": {
65+
"description": "Unused object definitions marked with `__attribute__((unused))` (and `used`, `maybe_used`, `cleanup`) are separately reported under the 'strict' query suite. This is because these attributes strongly indicate the contravention is intentional, and a deviation report alone will not suppress compiler warnings."
66+
}
6467
}
6568
}
6669
}

0 commit comments

Comments
 (0)