-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathUnusedObjects.qll
More file actions
69 lines (60 loc) · 2.75 KB
/
UnusedObjects.qll
File metadata and controls
69 lines (60 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import cpp
import codingstandards.cpp.deadcode.UnusedVariables
import codingstandards.cpp.alertreporting.HoldsForAllCopies
import codingstandards.cpp.alertreporting.DeduplicateMacroResults
/**
* An unused object definition is an object, meaning a place in memory, whose definition could be
* removed and the program would still compile.
*
* Technically, parameters may be considered objects, but they are covered by their own rule.
* Similarly, members of structs are an addressable place in memory, and may be considered objects.
* However, the member declaration is nothing but a layout offset, which is not an object.
*
* This therefore only reports variables (local or top level) which have a definition, and are
* unused.
*/
class UnusedObjectDefinition extends VariableDeclarationEntry {
UnusedObjectDefinition() {
(
getVariable() instanceof FirstPass::UnusedLocalVariable
or
getVariable() instanceof FirstPass::UnusedGlobalOrNamespaceVariable
) and
getVariable().getDefinition() = this and
not exists(getVariable().getAnAccess())
}
/* Dead objects with these attributes are reported in the "strict" queries. */
predicate hasAttrUnused() { hasAttrUnused(getVariable()) }
}
/* Configuration to use the `DedupMacroResults` module to reduce alert noise */
module UnusedObjectDefinitionDedupeConfig implements
DeduplicateMacroConfigSig<UnusedObjectDefinition>
{
string describe(UnusedObjectDefinition def) { result = def.getName() }
}
import DeduplicateMacroResults<UnusedObjectDefinition, UnusedObjectDefinitionDedupeConfig> as DeduplicateUnusedMacroObjects
/* Module config to use the `DeduplicateUnusedMacroObjects::Report` module */
module ReportDeadObjectConfig implements MacroReportConfigSig<UnusedObjectDefinition> {
bindingset[description]
string getMessageSameResultInAllExpansions(Macro m, string description) {
result = "Macro '" + m.getName() + "' defines unused object '" + description + "'."
}
string getMessageVariedResultInAllExpansions(Macro m) {
result =
"Macro '" + m.getName() +
"' defines unused object with an invocation-dependent name, for example, '$@'."
}
string getMessageResultInIsolatedExpansion(UnusedObjectDefinition unused) {
result = "Invocation of macro '$@' defines unused object '" + unused.getName() + "'."
}
string getMessageNotInMacro(UnusedObjectDefinition unused, Locatable optLoc1, string optStr1) {
result = "Unused object '" + unused.getName() + "'." and
optLoc1 = unused and
optStr1 = "(ignored)"
}
}
/* The object to report in queries of dead objects used in macros */
class ReportDeadObject extends DeduplicateUnusedMacroObjects::Report<ReportDeadObjectConfig>::ReportResult
{
predicate hasAttrUnused() { getAResultElement().hasAttrUnused() }
}