Skip to content

Commit bc99947

Browse files
authored
Merge pull request github#6515 from MathiasVP/clarify-initialization-vs-assignment-in-docs
C++: Clarify difference between 'Initializer' and 'Assignment'.
2 parents 57d44b8 + 207dcb0 commit bc99947

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

cpp/ql/lib/semmle/code/cpp/Initializer.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import semmle.code.cpp.controlflow.ControlFlowGraph
1818
* ...
1919
* }
2020
* ```
21+
* But _not_ `4` in the following code:
22+
* ```
23+
* int myUninitializedVariable;
24+
* myUninitializedVariable = 4;
25+
* ```
26+
* Instead, this is an `Assignment`.
2127
*/
2228
class Initializer extends ControlFlowNode, @initialiser {
2329
override Location getLocation() { initialisers(underlyingElement(this), _, _, result) }

cpp/ql/lib/semmle/code/cpp/Variable.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ class Variable extends Declaration, @variable {
136136
/**
137137
* Gets an assignment expression that assigns to this variable.
138138
* For example: `x=...` or `x+=...`.
139+
*
140+
* This does _not_ include the initialization of the variable. Use
141+
* `Variable.getInitializer()` to get the variable's initializer,
142+
* or use `Variable.getAnAssignedValue()` to get an expression that
143+
* is the right-hand side of an assignment or an initialization of
144+
* the varible.
139145
*/
140146
Assignment getAnAssignment() { result.getLValue() = this.getAnAccess() }
141147

cpp/ql/lib/semmle/code/cpp/exprs/Assignment.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import semmle.code.cpp.exprs.BitwiseOperation
55
/**
66
* A non-overloaded binary assignment operation, including `=`, `+=`, `&=`,
77
* etc. A C++ overloaded assignment operation looks syntactically identical but is instead
8-
* a `FunctionCall`.
8+
* a `FunctionCall`. This class does _not_ include variable initializers. To get a variable
9+
* initializer, use `Initializer` instead.
910
*
1011
* This is a QL base class for all (non-overloaded) assignments.
1112
*/
@@ -34,6 +35,8 @@ class Assignment extends Operation, @assign_expr {
3435
* ```
3536
* a = b;
3637
* ```
38+
* Note that `int a = b;` is _not_ an `AssignExpr`. It is a `Variable`,
39+
* and `b` can be obtained using `Variable.getInitializer()`.
3740
*/
3841
class AssignExpr extends Assignment, @assignexpr {
3942
override string getOperator() { result = "=" }
@@ -46,6 +49,9 @@ class AssignExpr extends Assignment, @assignexpr {
4649

4750
/**
4851
* A non-overloaded binary assignment operation other than `=`.
52+
*
53+
* This class does _not_ include variable initializers. To get a variable
54+
* initializer, use `Initializer` instead.
4955
*/
5056
class AssignOperation extends Assignment, @assign_op_expr {
5157
override string toString() { result = "... " + this.getOperator() + " ..." }

0 commit comments

Comments
 (0)