forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarationUnmodifiedObjectMissingConstSpecifier.ql
More file actions
43 lines (41 loc) · 1.7 KB
/
DeclarationUnmodifiedObjectMissingConstSpecifier.ql
File metadata and controls
43 lines (41 loc) · 1.7 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/autosar/declaration-unmodified-object-missing-const-specifier
* @name A7-1-1: Constexpr or const specifiers shall be used for immutable data declaration
* @description `Constexpr`/`const` specifiers prevent unintentional data modification for data
* intended as immutable.
* @kind problem
* @precision high
* @problem.severity warning
* @tags external/autosar/id/a7-1-1
* correctness
* maintainability
* readability
* external/autosar/allocated-target/implementation
* external/autosar/enforcement/automated
* external/autosar/obligation/required
*/
import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.ConstHelpers
from MutableVariable v, string cond
where
not isExcluded(v, ConstPackage::declarationUnmodifiedObjectMissingConstSpecifierQuery()) and
// We handle parameters in a separate query to allow for specific deviations.
not v instanceof Parameter and
(
isNotDirectlyModified(v) and
not v.getAnAccess().isAddressOfAccessNonConst() and
notPassedAsArgToNonConstParam(v) and
notAssignedToNonLocalNonConst(v) and
if v instanceof MutablePointerOrReferenceVariable
then
notUsedAsQualifierForNonConst(v) and
cond = " points to an object"
else cond = " is used for an object"
) and
not exists(LambdaExpression lc | lc.getACapture().getField() = v) and
not v.isFromUninstantiatedTemplate(_) and
not v.isCompilerGenerated() and
//if the instantiation is not constexpr but the template is, still exclude it as a candidate
not exists(TemplateVariable b | b.getAnInstantiation() = v and b.isConstexpr())
select v, "Non-constant variable " + v.getName() + cond + " and is not modified."