Skip to content

Commit 6d8a438

Browse files
author
Robert Marsh
authored
Merge pull request #1883 from jbj/partial-definitions-const
C++: Don't create partial defs for calls to const functions
2 parents 0524784 + d6fba0e commit 6d8a438

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

cpp/ql/src/semmle/code/cpp/dataflow/internal/FlowVar.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ private module PartialDefinitions {
113113
)
114114
)
115115
} or
116-
TExplicitCallQualifier(Expr qualifier, Call call) { qualifier = call.getQualifier() } or
116+
TExplicitCallQualifier(Expr qualifier) {
117+
exists(Call call |
118+
qualifier = call.getQualifier() and
119+
not call.getTarget().hasSpecifier("const")
120+
)
121+
} or
117122
TReferenceArgument(Expr arg, VariableAccess va) { referenceArgument(va, arg) }
118123

119124
private predicate isInstanceFieldWrite(FieldAccess fa, ControlFlowNode node) {
@@ -128,7 +133,7 @@ private module PartialDefinitions {
128133
PartialDefinition() {
129134
this = TExplicitFieldStoreQualifier(definedExpr, node)
130135
or
131-
this = TExplicitCallQualifier(definedExpr, _) and node = definedExpr
136+
this = TExplicitCallQualifier(definedExpr) and node = definedExpr
132137
or
133138
this = TReferenceArgument(definedExpr, node)
134139
}

cpp/ql/test/library-tests/dataflow/partialdefinitions/partialdefinitions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@ void test()
1414
s.x = 1;
1515
s.y.z = 1;
1616
}
17+
18+
struct Int {
19+
int data;
20+
Int(int value) : data(value) { } // Not a partial definition but a `PostUpdateNode`
21+
int getData() const { return data; }
22+
void setData(int value) { data = value; }
23+
};
24+
25+
int getSet() {
26+
Int myInt(1);
27+
myInt.setData(
28+
myInt.getData() + 1 // should not be a partial def
29+
);
30+
return myInt.getData(); // should not be a partial def
31+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| partialdefinitions.cpp:14:2:14:2 | partial def of s | partialdefinitions.cpp:14:2:14:2 | s | partialdefinitions.cpp:14:2:14:8 | ... = ... |
22
| partialdefinitions.cpp:15:2:15:2 | partial def of s | partialdefinitions.cpp:15:2:15:2 | s | partialdefinitions.cpp:15:2:15:10 | ... = ... |
33
| partialdefinitions.cpp:15:4:15:4 | partial def of y | partialdefinitions.cpp:15:4:15:4 | y | partialdefinitions.cpp:15:2:15:10 | ... = ... |
4+
| partialdefinitions.cpp:22:29:22:32 | partial def of this | file://:0:0:0:0 | this | partialdefinitions.cpp:22:29:22:40 | ... = ... |
5+
| partialdefinitions.cpp:27:3:27:7 | partial def of myInt | partialdefinitions.cpp:27:3:27:7 | myInt | partialdefinitions.cpp:27:3:27:7 | myInt |

0 commit comments

Comments
 (0)