Skip to content

Commit 0f12688

Browse files
Address feedback
1 parent 97c997f commit 0f12688

File tree

4 files changed

+17
-48
lines changed

4 files changed

+17
-48
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- All queries related to side effects:
2+
- Compound assignments of pointer parameters (e.g. `p += 1`) are no longer treated as a modification of the pointed-to object. This was previously only handled for simple assignments (e.g. `p = ...`).

cpp/common/src/codingstandards/cpp/SideEffect.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class AliasParameter extends Parameter {
295295
this.getUnspecifiedType() instanceof ReferenceType
296296
or
297297
// Exclude effects that change the who we point to, since we care about changes to the referenced object.
298-
not result.(AssignExpr).getLValue() = va and
298+
not result.(Assignment).getLValue() = va and
299299
not result.(CrementOperation).getOperand() = va
300300
)
301301
)

cpp/misra/src/rules/RULE-10-1-1/PointerOrRefParamNotConst.ql

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import codingstandards.cpp.Call
2323
import codingstandards.cpp.SideEffect
2424

2525
/**
26-
* Holds if the function is in a template scope and should be excluded.
26+
* Holds if `f` is a `Function` in a template scope and should be excluded.
2727
*/
2828
predicate isInTemplateScope(Function f) {
2929
f.isFromTemplateInstantiation(_)
@@ -39,9 +39,7 @@ class PointerLikeParam extends Parameter {
3939

4040
PointerLikeParam() {
4141
pointerLikeType = this.getType() and
42-
not pointerLikeType.pointsToConst() and
43-
// Exclude pointers to non-object types
44-
not pointerLikeType.getInnerType() instanceof RoutineType
42+
not pointerLikeType.pointsToConst()
4543
}
4644

4745
/**
@@ -69,42 +67,6 @@ class PointerLikeParam extends Parameter {
6967
Expr getAPointerLikeAccess() { result = getAPointerLikeAccessOf(this.getAnAccess()) }
7068
}
7169

72-
/**
73-
* A `VariableEffect` whose target variable is a `PointerLikeParam`.
74-
*
75-
* Examples of pointer-like effects on a pointer-like parameter `p` would include `p = ...`, `++p`,
76-
* `*p = ...`, and `++*p`, etc.
77-
*/
78-
class PointerLikeEffect extends VariableEffect {
79-
PointerLikeParam param;
80-
81-
PointerLikeEffect() { param = this.getTarget() }
82-
83-
/**
84-
* Holds if this effect modifies the pointed-to or referred-to object.
85-
*
86-
* For example, `*p = 0` modifies the inner type if `p` is a pointer, and `p = 0` affects the
87-
* inner type if `p` is a reference.
88-
*/
89-
predicate affectsInnerType() {
90-
if param.getPointerLikeType() instanceof ReferenceType
91-
then affectsOuterType()
92-
else not affectsOuterType()
93-
}
94-
95-
/**
96-
* Holds if this effect modifies the pointer or reference itself.
97-
*
98-
* For example, `p = ...` and `++p` modify the outer type, whether that type is a pointer or
99-
* reference, while `*p = 0` does not modify the outer type.
100-
*/
101-
predicate affectsOuterType() {
102-
this.(Assignment).getLValue() = param.getAnAccess()
103-
or
104-
this.(CrementOperation).getOperand() = param.getAnAccess()
105-
}
106-
}
107-
10870
/**
10971
* A candidate parameter that could have its target type const-qualified.
11072
*/
@@ -119,8 +81,8 @@ class NonConstParam extends PointerLikeParam {
11981
not exists(AsmStmt a | a.getEnclosingFunction() = this.getFunction()) and
12082
// Must have a pointer, array, or lvalue reference type with non-const target
12183
// Exclude pointers to non-object types
122-
not pointerLikeType.getInnerType() instanceof RoutineType and
123-
not pointerLikeType.getInnerType() instanceof VoidType and
84+
not pointerLikeType.getInnerType+().getUnderlyingType() instanceof RoutineType and
85+
not pointerLikeType.getInnerType+().getUnderlyingType() instanceof VoidType and
12486
// Exclude virtual functions
12587
not this.getFunction().isVirtual() and
12688
// Exclude functions in template scope
@@ -130,10 +92,7 @@ class NonConstParam extends PointerLikeParam {
13092
// Exclude deleted functions
13193
not this.getFunction().isDeleted() and
13294
// Exclude any parameter whose underlying data is modified
133-
not exists(PointerLikeEffect effect |
134-
effect.getTarget() = this and
135-
effect.affectsInnerType()
136-
) and
95+
not exists(AliasParameter alias | alias = this | alias.isModified()) and
13796
// Exclude parameters passed as arguments to non-const pointer/ref params
13897
not exists(CallArgumentExpr arg |
13998
arg = this.getAPointerLikeAccess() and

cpp/misra/test/rules/RULE-10-1-1/test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,12 @@ template <typename T> struct A {
233233
}
234234
};
235235

236-
template <typename T> void f11(T &l1) {} // COMPLIANT - function template
236+
template <typename T> void f11(T &l1) {} // COMPLIANT - function template
237+
238+
using func = int(int);
239+
void f12(void **p1, // COMPLIANT
240+
void *const *p2, // COMPLIANT
241+
func *p3, // COMPLIANT
242+
func **p4, // COMPLIANT
243+
func *const *p5 // COMPLIANT
244+
) {}

0 commit comments

Comments
 (0)