Skip to content

Commit 7d5f08b

Browse files
committed
Count in typedefs and cv-qualifiers
We are interested if the underlying *data* can be mutated, not the pointer itself. Also, the surface type may be a typedef, so resolve that as well.
1 parent f0b53e2 commit 7d5f08b

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
8585
predicate loopVariableAssignedToNonConstPointerOrReferenceType(
8686
ForStmt forLoop, VariableAccess loopVariableAccessInCondition
8787
) {
88-
exists(Expr assignmentRhs, DerivedType targetType |
88+
exists(Expr assignmentRhs, Type targetType, DerivedType strippedType |
8989
isAssignment(assignmentRhs, targetType, _) and
90-
not targetType.getBaseType().isConst() and
90+
strippedType = targetType.stripTopLevelSpecifiers() and
91+
not strippedType.getBaseType().isConst() and
9192
(
9293
targetType instanceof PointerType or
9394
targetType instanceof ReferenceType
@@ -105,9 +106,14 @@ predicate loopVariableAssignedToNonConstPointerOrReferenceType(
105106
)
106107
}
107108

108-
/*
109-
* An adapted part of `BuiltinTypeRules::MisraCpp23BuiltInTypes::isPreConversionAssignment`
110-
* that is only relevant to an argument passed to a parameter, seen as an assignment.
109+
/**
110+
* Holds if the given variable access has another variable access with the same target
111+
* variable that is passed as reference to a non-const reference parameter of a function,
112+
* constituting a `T` -> `&T` conversion.
113+
*
114+
* This is an adapted part of
115+
* `BuiltinTypeRules::MisraCpp23BuiltInTypes::isPreConversionAssignment` that is only
116+
* relevant to an argument passed to a parameter, seen as an assignment.
111117
*
112118
* This predicate adds two constraints to the target type, as compared to the original
113119
* portion of the predicate:
@@ -117,20 +123,15 @@ predicate loopVariableAssignedToNonConstPointerOrReferenceType(
117123
*
118124
* Also, this predicate requires that the call is the body of the given for-loop.
119125
*/
120-
121-
/**
122-
* Holds if the given variable access has another variable access with the same target
123-
* variable that is passed as reference to a non-const reference parameter of a function,
124-
* constituting a `T` -> `&T` conversion.
125-
*/
126126
predicate loopVariablePassedAsArgumentToNonConstReferenceParameter(
127127
ForStmt forLoop, VariableAccess loopVariableAccessInCondition
128128
) {
129-
exists(ReferenceType targetType |
129+
exists(Type targetType, ReferenceType strippedReferenceType |
130130
exists(Call call, int i |
131131
call.getArgument(i) = loopVariableAccessInCondition.getTarget().getAnAccess() and
132132
call.getEnclosingStmt().getParent*() = forLoop.getStmt() and
133-
not targetType.getBaseType().isConst()
133+
strippedReferenceType = targetType.stripTopLevelSpecifiers() and
134+
not strippedReferenceType.getBaseType().isConst()
134135
|
135136
/* A regular function call */
136137
targetType = call.getTarget().getParameter(i).getType()

0 commit comments

Comments
 (0)