1818import cpp
1919import codingstandards.cpp.misra
2020import codingstandards.cpp.types.Pointers
21+ import codingstandards.cpp.Call
2122import codingstandards.cpp.SideEffect
2223import codingstandards.cpp.alertreporting.HoldsForAllCopies
2324
@@ -94,8 +95,7 @@ class PointerLikeParam extends Parameter {
9495 pointerLikeType = this .getType ( ) and
9596 not pointerLikeType .pointsToConst ( ) and
9697 // Exclude pointers to non-object types
97- not pointerLikeType .getInnerType ( ) instanceof RoutineType and
98- not pointerLikeType .getInnerType ( ) instanceof VoidType
98+ not pointerLikeType .getInnerType ( ) instanceof RoutineType
9999 }
100100
101101 /**
@@ -134,6 +134,24 @@ class PointerLikeParam extends Parameter {
134134 }
135135}
136136
137+ predicate test ( Assignment a , Field f , Parameter p , string n ) {
138+ a .getLValue ( ) = f .getAnAccess ( ) and
139+ a .getRValue ( ) = p .getAnAccess ( ) and
140+ f .getName ( ) = "planes" and
141+ n = p .getFunction ( ) .getName ( ) and
142+ a .getFile ( ) .getBaseName ( ) = "matrix_iterator.cpp"
143+ }
144+
145+ predicate test2 ( Parameter p ) {
146+ p .getName ( ) = [ "planes" , "_planes" ] and
147+ p .getFunction ( ) .getName ( ) = "init"
148+ }
149+
150+ predicate test3 ( Function f , int num ) {
151+ f .getName ( ) = "init" and
152+ num = strictcount ( Parameter p | p .getFunction ( ) = f and p .getName ( ) = [ "planes" , "_planes" ] )
153+ }
154+
137155/**
138156 * A `VariableEffect` whose target variable is a `PointerLikeParam`.
139157 *
@@ -176,6 +194,7 @@ class PointerLikeEffect extends VariableEffect {
176194class NonConstParam extends PointerLikeParam {
177195 NonConstParam ( ) {
178196 not pointerLikeType .pointsToConst ( ) and
197+ not pointerLikeType .getInnerType ( ) instanceof VoidType and
179198 // Ignore parameters in functions without bodies
180199 exists ( this .getFunction ( ) .getBlock ( ) ) and
181200 // Ignore unnamed parameters
@@ -199,14 +218,14 @@ class NonConstParam extends PointerLikeParam {
199218 effect .getTarget ( ) = this and
200219 effect .affectsInnerType ( )
201220 ) and
202- // Exclude parameters passed as arguments to functions taking non-const pointer/ref params
203- not exists ( FunctionCall fc , int i |
204- fc . getArgument ( i ) = this .getAPointerLikeAccess ( ) and
205- fc . getTarget ( ) . getParameter ( i ) . getType ( ) .( PointerLikeType ) .pointsToNonConst ( )
221+ // Exclude parameters passed as arguments to non-const pointer/ref params
222+ not exists ( CallArgumentExpr arg |
223+ arg = this .getAPointerLikeAccess ( ) and
224+ arg . getParamType ( ) .( PointerLikeType ) .pointsToNonConst ( )
206225 ) and
207226 // Exclude parameters used as qualifier for a non-const member function
208227 not exists ( FunctionCall fc |
209- fc .getQualifier ( ) = this .getAnAccess ( ) and
228+ fc .getQualifier ( ) = [ this .getAnAccess ( ) , this . getAPointerLikeAccess ( ) ] and
210229 not fc .getTarget ( ) .hasSpecifier ( "const" ) and
211230 not fc .getTarget ( ) .isStatic ( )
212231 ) and
@@ -228,14 +247,25 @@ class NonConstParam extends PointerLikeParam {
228247 // exclude pointer to pointer and reference to pointer cases.
229248 addrOf .getOperand ( ) = this .getAPointerLikeAccess ( ) and
230249 addrOf .getType ( ) .( PointerLikeType ) .getInnerType ( ) instanceof PointerLikeType
250+ ) and
251+ not exists ( PointerArithmeticOperation pointerManip |
252+ pointerManip .getAnOperand ( ) = this .getAPointerLikeAccess ( ) and
253+ pointerManip .getType ( ) .( PointerLikeType ) .pointsToNonConst ( )
231254 )
232255 }
233256}
234257
235258from NonConstParam param , Type innerType
236259where
237260 not isExcluded ( param , Declarations3Package:: pointerOrRefParamNotConstQuery ( ) ) and
238- innerType = param .getPointerLikeType ( ) .getInnerType ( )
261+ innerType = param .getPointerLikeType ( ) .getInnerType ( ) and
262+ not param .isAffectedByMacro ( ) and
263+ // Exclude functions with copied parameters which leads to wrong results.
264+ count ( Parameter p |
265+ p .getFunction ( ) = param .getFunction ( ) and
266+ p .getIndex ( ) = param .getIndex ( )
267+ ) = 1
239268select param ,
240269 "Parameter '" + param .getName ( ) + "' points/refers to a non-const type '" + innerType .toString ( ) +
241- "' but does not modify the target object."
270+ "' but does not modify the target object in the $@." , param .getFunction ( ) .getDefinition ( ) ,
271+ "function definition"
0 commit comments