-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathGlobalVariableUsed.ql
More file actions
43 lines (40 loc) · 1.41 KB
/
GlobalVariableUsed.ql
File metadata and controls
43 lines (40 loc) · 1.41 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
/**
* @id cpp/misra/global-variable-used
* @name RULE-6-7-2: Global variables shall not be used
* @description Global variables can be accessed and modified from distant and unclear points in the
* code, creating a risk of data races and unexpected behavior.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-6-7-2
* scope/single-translation-unit
* maintainability
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/
import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.Linkage
import codingstandards.cpp.orderofevaluation.Initialization
class GlobalVariable extends Variable {
GlobalVariable() {
hasNamespaceScope(this) or
this.(MemberVariable).isStatic()
}
}
from GlobalVariable var, string suffix, Element reasonElement, string reasonString
where
not isExcluded(var, Banned1Package::globalVariableUsedQuery()) and
not var.isConstexpr() and
(
not var.isConst() and
suffix = "as non-const" and
// Placeholder is not used, but they must be bound.
reasonString = "" and
reasonElement = var
or
var.isConst() and
isNotConstantInitialized(var, reasonString, reasonElement) and
suffix = "as const, but is not constant initialized because it $@"
)
select var, "Global variable " + var.getName() + " declared " + suffix, reasonElement, reasonString