Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98eca47

Browse files
zlaski-semmlegeoffw0
authored andcommitted
[CPP-418] Calls, casts, assignments and other goodness.
1 parent e647dc3 commit 98eca47

6 files changed

Lines changed: 346 additions & 130 deletions

File tree

cpp/ql/src/semmle/code/cpp/UserType.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class UserType extends Type, Declaration, NameQualifyingElement, AccessHolder, @
9797
* ```
9898
* class C;
9999
* typedef int ti;
100-
* extern void foo(int);
101100
* ```
102101
*/
103102
class TypeDeclarationEntry extends DeclarationEntry, @type_decl {

cpp/ql/src/semmle/code/cpp/exprs/ArithmeticOperation.qll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class UnaryPlusExpr extends UnaryArithmeticOperation, @unaryplusexpr {
3737

3838
/**
3939
* A C/C++ GNU conjugation expression. It operates on `_Complex` or
40-
* `__complex_`numbers, and is similar to the C99 `conj`, `conjf` and `conjl`
40+
* `__complex__ `numbers, and is similar to the C99 `conj`, `conjf` and `conjl`
4141
* functions.
4242
* ```
4343
* _Complex double a = ( 1.0, 2.0 );
@@ -55,7 +55,7 @@ class ConjugationExpr extends UnaryArithmeticOperation, @conjugation {
5555
*
5656
* This is the abstract base QL class for increment and decrement operations.
5757
*
58-
* Note that this doesn't include calls to _user-defined_ `operator++`
58+
* Note that this does not include calls to _user-defined_ `operator++`
5959
* or `operator--`.
6060
*/
6161
abstract class CrementOperation extends UnaryArithmeticOperation {
@@ -74,35 +74,35 @@ abstract class CrementOperation extends UnaryArithmeticOperation {
7474
/**
7575
* A C/C++ `++` expression (either prefix or postfix).
7676
*
77-
* Note that this doesn't include calls to user-defined `operator++`.
77+
* Note that this does not include calls to _user-defined_ `operator++`.
7878
*/
7979
abstract class IncrementOperation extends CrementOperation { }
8080

8181
/**
8282
* A C/C++ `--` expression (either prefix or postfix).
8383
*
84-
* Note that this doesn't include calls to user-defined `operator--`.
84+
* Note that this does not include calls to _user-defined_ `operator--`.
8585
*/
8686
abstract class DecrementOperation extends CrementOperation { }
8787

8888
/**
8989
* A C/C++ `++` or `--` prefix expression.
9090
*
91-
* Note that this doesn't include calls to user-defined operators.
91+
* Note that this does not include calls to _user-defined_ operators.
9292
*/
9393
abstract class PrefixCrementOperation extends CrementOperation { }
9494

9595
/**
9696
* A C/C++ `++` or `--` postfix expression.
9797
*
98-
* Note that this doesn't include calls to user-defined operators.
98+
* Note that this does not include calls to _user-defined_ operators.
9999
*/
100100
abstract class PostfixCrementOperation extends CrementOperation { }
101101

102102
/**
103103
* A C/C++ prefix increment expression, as in `++x`.
104104
*
105-
* Note that this doesn't include calls to _user-defined_ `operator++`.
105+
* Note that this does not include calls to _user-defined_ `operator++`.
106106
* ```
107107
* b = ++a;
108108
* ```
@@ -118,7 +118,7 @@ class PrefixIncrExpr extends IncrementOperation, PrefixCrementOperation, @preinc
118118
/**
119119
* A C/C++ prefix decrement expression, as in `--x`.
120120
*
121-
* Note that this doesn't include calls to _user-defined_ `operator--`.
121+
* Note that this does not include calls to _user-defined_ `operator--`.
122122
* ```
123123
* b = --a;
124124
* ```
@@ -134,7 +134,7 @@ class PrefixDecrExpr extends DecrementOperation, PrefixCrementOperation, @predec
134134
/**
135135
* A C/C++ postfix increment expression, as in `x++`.
136136
*
137-
* Note that this doesn't include calls to _user-defined_ `operator++`.
137+
* Note that this does not include calls to _user-defined_ `operator++`.
138138
* ```
139139
* b = a++;
140140
* ```
@@ -152,7 +152,7 @@ class PostfixIncrExpr extends IncrementOperation, PostfixCrementOperation, @post
152152
/**
153153
* A C/C++ postfix decrement expression, as in `x--`.
154154
*
155-
* Note that this doesn't include calls to _user-defined_ `operator--`.
155+
* Note that this does not include calls to _user-defined_ `operator--`.
156156
* ```
157157
* b = a--;
158158
* ```

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import semmle.code.cpp.exprs.BitwiseOperation
44

55
/**
66
* A non-overloaded binary assignment operation, including `=`, `+=`, `&=`,
7-
* etc. A C++ overloaded operation looks syntactically identical but is instead
7+
* etc. A C++ overloaded `operator` looks syntactically identical but is instead
88
* a `FunctionCall`.
9+
*
10+
* This is an abstract root QL class for all (non-overloaded) assignments.
911
*/
1012
abstract class Assignment extends Operation {
11-
/** Gets the lvalue of this assignment. */
12-
Expr getLValue() { this.hasChild(result, 0) }
13+
/** Gets the *lvalue* of this assignment. */
14+
Expr get*lvalue*() { this.hasChild(result, 0) }
1315

1416
/** Gets the rvalue of this assignment. */
1517
Expr getRValue() { this.hasChild(result, 1) }
@@ -20,7 +22,7 @@ abstract class Assignment extends Operation {
2022
this.getRValue().mayBeGloballyImpure()
2123
or
2224
not exists(VariableAccess va, LocalScopeVariable v |
23-
va = this.getLValue() and
25+
va = this.get*lvalue*() and
2426
v = va.getTarget() and
2527
not va.getConversion+() instanceof ReferenceDereferenceExpr and
2628
not v.isStatic()
@@ -51,13 +53,13 @@ abstract class AssignOperation extends Assignment {
5153
}
5254

5355
/**
54-
* A non-overloaded arithmetic assignment operation on a non-pointer lvalue:
56+
* A non-overloaded arithmetic assignment operation on a non-pointer *lvalue*:
5557
* `+=`, `-=`, `*=`, `/=` and `%=`.
5658
*/
5759
abstract class AssignArithmeticOperation extends AssignOperation { }
5860

5961
/**
60-
* A non-overloaded `+=` assignment expression on a non-pointer lvalue.
62+
* A non-overloaded `+=` assignment expression on a non-pointer *lvalue*.
6163
* ```
6264
* a += b;
6365
* ```
@@ -69,7 +71,7 @@ class AssignAddExpr extends AssignArithmeticOperation, @assignaddexpr {
6971
}
7072

7173
/**
72-
* A non-overloaded `-=` assignment expression on a non-pointer lvalue.
74+
* A non-overloaded `-=` assignment expression on a non-pointer *lvalue*.
7375
* ```
7476
* a -= b;
7577
* ```
@@ -207,11 +209,16 @@ class AssignPointerSubExpr extends AssignOperation, @assignpsubexpr {
207209
}
208210

209211
/**
210-
* A C++ variable declaration in an expression where a condition is expected.
211-
* For example, on the `ConditionDeclExpr` in `if (bool c = x < y)`,
212-
* `getVariableAccess()` is an access to `c` (with possible casts),
213-
* `getVariable()` is the variable `c` (which has an initializer `x < y`), and
214-
* `getInitializingExpr()` is `x < y`.
212+
* A C++ variable declaration inside the conditional expression of a `while` or `if`
213+
* compound statement. Declaring a variable this way narrows its lifetime and scope
214+
* to be strictly the compound statement itself. For example:
215+
* ```
216+
* extern int x, y;
217+
* if (bool c = x < y) { do_something_with(c); }
218+
* // c is no longer in scope
219+
* while (int d = x - y) { do_something_else_with(d); }
220+
* // d is no longer is scope
221+
* ```
215222
*/
216223
class ConditionDeclExpr extends Expr, @condition_decl {
217224
/**

0 commit comments

Comments
 (0)