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

Skip to content

Commit 113481d

Browse files
zlaski-semmlegeoffw0
authored andcommitted
[CPP-418] Add concrete syntax for arithmetic operation, EXCEPT for assorted complex number operations, where surface syntax could not be determined.
1 parent e328e78 commit 113481d

1 file changed

Lines changed: 96 additions & 9 deletions

File tree

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

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import semmle.code.cpp.exprs.Expr
22

33
/**
4-
* A C/C++ arithmetic operation.
4+
* A C/C++ unary arithmetic operation.
5+
*
6+
* This is an abstract base QL class.
57
*/
68
abstract class UnaryArithmeticOperation extends UnaryOperation { }
79

810
/**
911
* A C/C++ unary minus expression.
12+
* ```
13+
* b = - a;
14+
* ```
1015
*/
1116
class UnaryMinusExpr extends UnaryArithmeticOperation, @arithnegexpr {
1217
override string getOperator() { result = "-" }
@@ -18,6 +23,9 @@ class UnaryMinusExpr extends UnaryArithmeticOperation, @arithnegexpr {
1823

1924
/**
2025
* A C/C++ unary plus expression.
26+
* ```
27+
* b = + a;
28+
* ```
2129
*/
2230
class UnaryPlusExpr extends UnaryArithmeticOperation, @unaryplusexpr {
2331
override string getOperator() { result = "+" }
@@ -28,7 +36,13 @@ class UnaryPlusExpr extends UnaryArithmeticOperation, @unaryplusexpr {
2836
}
2937

3038
/**
31-
* A C/C++ GNU conjugation expression.
39+
* A C/C++ GNU conjugation expression. It operates on `_Complex` or
40+
* `__complex_`numbers, and is similar to the C99 `conj`, `conjf` and `conjl`
41+
* functions.
42+
* ```
43+
* _Complex double a = ( 1.0, 2.0 );
44+
* b = ~ a; // ( 1,0, - 2.0 )
45+
* ```
3246
*/
3347
class ConjugationExpr extends UnaryArithmeticOperation, @conjugation {
3448
override string getOperator() { result = "~" }
@@ -39,7 +53,9 @@ class ConjugationExpr extends UnaryArithmeticOperation, @conjugation {
3953
/**
4054
* A C/C++ `++` or `--` expression (either prefix or postfix).
4155
*
42-
* Note that this doesn't include calls to user-defined `operator++`
56+
* This is the abstract base QL class for increment and decrement operations.
57+
*
58+
* Note that this doesn't include calls to _user-defined_ `operator++`
4359
* or `operator--`.
4460
*/
4561
abstract class CrementOperation extends UnaryArithmeticOperation {
@@ -86,7 +102,10 @@ abstract class PostfixCrementOperation extends CrementOperation { }
86102
/**
87103
* A C/C++ prefix increment expression, as in `++x`.
88104
*
89-
* Note that this doesn't include calls to user-defined `operator++`.
105+
* Note that this doesn't include calls to _user-defined_ `operator++`.
106+
* ```
107+
* b = ++a;
108+
* ```
90109
*/
91110
class PrefixIncrExpr extends IncrementOperation, PrefixCrementOperation, @preincrexpr {
92111
override string getOperator() { result = "++" }
@@ -99,7 +118,10 @@ class PrefixIncrExpr extends IncrementOperation, PrefixCrementOperation, @preinc
99118
/**
100119
* A C/C++ prefix decrement expression, as in `--x`.
101120
*
102-
* Note that this doesn't include calls to user-defined `operator--`.
121+
* Note that this doesn't include calls to _user-defined_ `operator--`.
122+
* ```
123+
* b = --a;
124+
* ```
103125
*/
104126
class PrefixDecrExpr extends DecrementOperation, PrefixCrementOperation, @predecrexpr {
105127
override string getOperator() { result = "--" }
@@ -112,7 +134,10 @@ class PrefixDecrExpr extends DecrementOperation, PrefixCrementOperation, @predec
112134
/**
113135
* A C/C++ postfix increment expression, as in `x++`.
114136
*
115-
* Note that this doesn't include calls to user-defined `operator++`.
137+
* Note that this doesn't include calls to _user-defined_ `operator++`.
138+
* ```
139+
* b = a++;
140+
* ```
116141
*/
117142
class PostfixIncrExpr extends IncrementOperation, PostfixCrementOperation, @postincrexpr {
118143
override string getOperator() { result = "++" }
@@ -127,7 +152,10 @@ class PostfixIncrExpr extends IncrementOperation, PostfixCrementOperation, @post
127152
/**
128153
* A C/C++ postfix decrement expression, as in `x--`.
129154
*
130-
* Note that this doesn't include calls to user-defined `operator--`.
155+
* Note that this doesn't include calls to _user-defined_ `operator--`.
156+
* ```
157+
* b = a--;
158+
* ```
131159
*/
132160
class PostfixDecrExpr extends DecrementOperation, PostfixCrementOperation, @postdecrexpr {
133161
override string getOperator() { result = "--" }
@@ -140,7 +168,13 @@ class PostfixDecrExpr extends DecrementOperation, PostfixCrementOperation, @post
140168
}
141169

142170
/**
143-
* A C/C++ GNU real part expression.
171+
* A C/C++ GNU real part expression. It operates on `_Complex` or
172+
* `__complex__` numbers.
173+
* ```
174+
* #include <complex.h>
175+
* _Complex double f = CMPLX( 2.0, 3.0 );
176+
* double d = __real(f); // 2.0
177+
* ```
144178
*/
145179
class RealPartExpr extends UnaryArithmeticOperation, @realpartexpr {
146180
override string getOperator() { result = "__real" }
@@ -149,7 +183,13 @@ class RealPartExpr extends UnaryArithmeticOperation, @realpartexpr {
149183
}
150184

151185
/**
152-
* A C/C++ GNU imaginary part expression.
186+
* A C/C++ GNU imaginary part expression. It operates on `_Complex` or
187+
* `__complex__` numbers.
188+
* ```
189+
* #include <complex.h>
190+
* _Complex double f = CMPLX( 2.0, 3.0 );
191+
* double d = __imag(f); // 3.0
192+
* ```
153193
*/
154194
class ImaginaryPartExpr extends UnaryArithmeticOperation, @imagpartexpr {
155195
override string getOperator() { result = "__imag" }
@@ -159,11 +199,16 @@ class ImaginaryPartExpr extends UnaryArithmeticOperation, @imagpartexpr {
159199

160200
/**
161201
* A C/C++ binary arithmetic operation.
202+
*
203+
* This is an abstract base QL class for all binary arithmetic operations.
162204
*/
163205
abstract class BinaryArithmeticOperation extends BinaryOperation { }
164206

165207
/**
166208
* A C/C++ add expression.
209+
* ```
210+
* c = a + b;
211+
* ```
167212
*/
168213
class AddExpr extends BinaryArithmeticOperation, @addexpr {
169214
override string getOperator() { result = "+" }
@@ -175,6 +220,9 @@ class AddExpr extends BinaryArithmeticOperation, @addexpr {
175220

176221
/**
177222
* A C/C++ subtract expression.
223+
* ```
224+
* c = a - b;
225+
* ```
178226
*/
179227
class SubExpr extends BinaryArithmeticOperation, @subexpr {
180228
override string getOperator() { result = "-" }
@@ -186,6 +234,9 @@ class SubExpr extends BinaryArithmeticOperation, @subexpr {
186234

187235
/**
188236
* A C/C++ multiply expression.
237+
* ```
238+
* c = a * b;
239+
* ```
189240
*/
190241
class MulExpr extends BinaryArithmeticOperation, @mulexpr {
191242
override string getOperator() { result = "*" }
@@ -197,6 +248,9 @@ class MulExpr extends BinaryArithmeticOperation, @mulexpr {
197248

198249
/**
199250
* A C/C++ divide expression.
251+
* ```
252+
* c = a / b;
253+
* ```
200254
*/
201255
class DivExpr extends BinaryArithmeticOperation, @divexpr {
202256
override string getOperator() { result = "/" }
@@ -208,6 +262,9 @@ class DivExpr extends BinaryArithmeticOperation, @divexpr {
208262

209263
/**
210264
* A C/C++ remainder expression.
265+
* ```
266+
* c = a % b;
267+
* ```
211268
*/
212269
class RemExpr extends BinaryArithmeticOperation, @remexpr {
213270
override string getOperator() { result = "%" }
@@ -226,6 +283,8 @@ class ImaginaryMulExpr extends BinaryArithmeticOperation, @jmulexpr {
226283
override string getCanonicalQLClass() { result = "ImaginaryMulExpr" }
227284

228285
override int getPrecedence() { result = 13 }
286+
287+
override string getCanonicalQLClass() { result = "ImaginaryMulExpr" }
229288
}
230289

231290
/**
@@ -237,6 +296,8 @@ class ImaginaryDivExpr extends BinaryArithmeticOperation, @jdivexpr {
237296
override string getCanonicalQLClass() { result = "ImaginaryDivExpr" }
238297

239298
override int getPrecedence() { result = 13 }
299+
300+
override string getCanonicalQLClass() { result = "ImaginaryDivExpr" }
240301
}
241302

242303
/**
@@ -248,6 +309,8 @@ class RealImaginaryAddExpr extends BinaryArithmeticOperation, @fjaddexpr {
248309
override string getCanonicalQLClass() { result = "RealImaginaryAddExpr" }
249310

250311
override int getPrecedence() { result = 12 }
312+
313+
override string getCanonicalQLClass() { result = "RealImaginaryAddExpr" }
251314
}
252315

253316
/**
@@ -259,6 +322,8 @@ class ImaginaryRealAddExpr extends BinaryArithmeticOperation, @jfaddexpr {
259322
override string getCanonicalQLClass() { result = "ImaginaryRealAddExpr" }
260323

261324
override int getPrecedence() { result = 12 }
325+
326+
override string getCanonicalQLClass() { result = "ImaginaryRealAddExpr" }
262327
}
263328

264329
/**
@@ -270,6 +335,8 @@ class RealImaginarySubExpr extends BinaryArithmeticOperation, @fjsubexpr {
270335
override string getCanonicalQLClass() { result = "RealImaginarySubExpr" }
271336

272337
override int getPrecedence() { result = 12 }
338+
339+
override string getCanonicalQLClass() { result = "RealImaginarySubExpr" }
273340
}
274341

275342
/**
@@ -281,10 +348,15 @@ class ImaginaryRealSubExpr extends BinaryArithmeticOperation, @jfsubexpr {
281348
override string getCanonicalQLClass() { result = "ImaginaryRealSubExpr" }
282349

283350
override int getPrecedence() { result = 12 }
351+
352+
override string getCanonicalQLClass() { result = "ImaginaryRealSubExpr" }
284353
}
285354

286355
/**
287356
* A C/C++ GNU min expression.
357+
* ```
358+
* c = a <? b;
359+
* ```
288360
*/
289361
class MinExpr extends BinaryArithmeticOperation, @minexpr {
290362
override string getOperator() { result = "<?" }
@@ -294,6 +366,9 @@ class MinExpr extends BinaryArithmeticOperation, @minexpr {
294366

295367
/**
296368
* A C/C++ GNU max expression.
369+
* ```
370+
* c = a >? b;
371+
* ```
297372
*/
298373
class MaxExpr extends BinaryArithmeticOperation, @maxexpr {
299374
override string getOperator() { result = ">?" }
@@ -308,6 +383,10 @@ abstract class PointerArithmeticOperation extends BinaryArithmeticOperation { }
308383

309384
/**
310385
* A C/C++ pointer add expression.
386+
* ```
387+
* foo *ptr = &f[0];
388+
* ptr = ptr + 2;
389+
* ```
311390
*/
312391
class PointerAddExpr extends PointerArithmeticOperation, @paddexpr {
313392
override string getOperator() { result = "+" }
@@ -319,6 +398,10 @@ class PointerAddExpr extends PointerArithmeticOperation, @paddexpr {
319398

320399
/**
321400
* A C/C++ pointer subtract expression.
401+
* ```
402+
* foo *ptr = &f[3];
403+
* ptr = ptr - 2;
404+
* ```
322405
*/
323406
class PointerSubExpr extends PointerArithmeticOperation, @psubexpr {
324407
override string getOperator() { result = "-" }
@@ -330,6 +413,10 @@ class PointerSubExpr extends PointerArithmeticOperation, @psubexpr {
330413

331414
/**
332415
* A C/C++ pointer difference expression.
416+
* ```
417+
* foo *start = &f[0], *end = &f[4];
418+
* int size = end - size;
419+
* ```
333420
*/
334421
class PointerDiffExpr extends PointerArithmeticOperation, @pdiffexpr {
335422
override string getOperator() { result = "-" }

0 commit comments

Comments
 (0)