Skip to content

Commit a06228f

Browse files
committed
[CPP-348] Add concrete syntax for arithmetic operation, EXCEPT for assorted complex number operations, where surface syntax could not be determined.
1 parent dfe018f commit a06228f

File tree

1 file changed

+120
-23
lines changed

1 file changed

+120
-23
lines changed

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

+120-23
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,62 @@
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
/**
1012
* A C/C++ unary minus expression.
13+
* ```
14+
* b = - a;
15+
* ```
1116
*/
1217
class UnaryMinusExpr extends UnaryArithmeticOperation, @arithnegexpr {
1318
override string getOperator() { result = "-" }
1419

1520
override string getCanonicalQLClass() { result = "UnaryMinusExpr" }
16-
21+
1722
override int getPrecedence() { result = 15 }
1823
}
1924

2025
/**
2126
* A C/C++ unary plus expression.
27+
* ```
28+
* b = + a;
29+
* ```
2230
*/
2331
class UnaryPlusExpr extends UnaryArithmeticOperation, @unaryplusexpr {
2432
override string getOperator() { result = "+" }
2533

2634
override string getCanonicalQLClass() { result = "UnaryPlusExpr" }
27-
35+
2836
override int getPrecedence() { result = 15 }
2937
}
3038

3139
/**
32-
* A C/C++ GNU conjugation expression.
40+
* A C/C++ GNU conjugation expression. It operates on `_Complex` or
41+
* `__complex_`numbers, and is similar to the C99 `conj`, `conjf` and `conjl`
42+
* functions.
43+
* ```
44+
* _Complex double a = ( 1.0, 2.0 );
45+
* b = ~ a; // ( 1,0, - 2.0 )
46+
* ```
3347
*/
3448
class ConjugationExpr extends UnaryArithmeticOperation, @conjugation {
3549
override string getOperator() { result = "~" }
50+
51+
override string getCanonicalQLClass() { result = "ConjugationExpr" }
3652
}
3753

3854
/**
3955
* A C/C++ `++` or `--` expression (either prefix or postfix).
4056
*
41-
* Note that this doesn't include calls to user-defined `operator++`
57+
* This is the abstract base QL class for increment and decrement operations.
58+
*
59+
* Note that this doesn't include calls to _user-defined_ `operator++`
4260
* or `operator--`.
4361
*/
4462
abstract class CrementOperation extends UnaryArithmeticOperation {
@@ -89,39 +107,48 @@ abstract class PostfixCrementOperation extends CrementOperation {
89107
/**
90108
* A C/C++ prefix increment expression, as in `++x`.
91109
*
92-
* Note that this doesn't include calls to user-defined `operator++`.
110+
* Note that this doesn't include calls to _user-defined_ `operator++`.
111+
* ```
112+
* b = ++a;
113+
* ```
93114
*/
94115
class PrefixIncrExpr extends IncrementOperation, PrefixCrementOperation, @preincrexpr {
95116
override string getOperator() { result = "++" }
96117

97118
override string getCanonicalQLClass() { result = "PrefixIncrExpr" }
98-
119+
99120
override int getPrecedence() { result = 15 }
100121
}
101122

102123
/**
103124
* A C/C++ prefix decrement expression, as in `--x`.
104125
*
105-
* Note that this doesn't include calls to user-defined `operator--`.
126+
* Note that this doesn't include calls to _user-defined_ `operator--`.
127+
* ```
128+
* b = --a;
129+
* ```
106130
*/
107131
class PrefixDecrExpr extends DecrementOperation, PrefixCrementOperation, @predecrexpr {
108132
override string getOperator() { result = "--" }
109133

110134
override string getCanonicalQLClass() { result = "PrefixDecrExpr" }
111-
135+
112136
override int getPrecedence() { result = 15 }
113137
}
114138

115139
/**
116140
* A C/C++ postfix increment expression, as in `x++`.
117141
*
118-
* Note that this doesn't include calls to user-defined `operator++`.
142+
* Note that this doesn't include calls to _user-defined_ `operator++`.
143+
* ```
144+
* b = a++;
145+
* ```
119146
*/
120147
class PostfixIncrExpr extends IncrementOperation, PostfixCrementOperation, @postincrexpr {
121148
override string getOperator() { result = "++" }
122149

123150
override string getCanonicalQLClass() { result = "PostfixIncrExpr" }
124-
151+
125152
override int getPrecedence() { result = 16 }
126153

127154
override string toString() { result = "... " + getOperator() }
@@ -130,90 +157,126 @@ class PostfixIncrExpr extends IncrementOperation, PostfixCrementOperation, @post
130157
/**
131158
* A C/C++ postfix decrement expression, as in `x--`.
132159
*
133-
* Note that this doesn't include calls to user-defined `operator--`.
160+
* Note that this doesn't include calls to _user-defined_ `operator--`.
161+
* ```
162+
* b = a--;
163+
* ```
134164
*/
135165
class PostfixDecrExpr extends DecrementOperation, PostfixCrementOperation, @postdecrexpr {
136166
override string getOperator() { result = "--" }
137167

138168
override string getCanonicalQLClass() { result = "PostfixDecrExpr" }
139-
169+
140170
override int getPrecedence() { result = 16 }
141171

142172
override string toString() { result = "... " + getOperator() }
143173
}
144174

145175
/**
146-
* A C/C++ GNU real part expression.
176+
* A C/C++ GNU real part expression. It operates on `_Complex` or
177+
* `__complex__` numbers.
178+
* ```
179+
* #include <complex.h>
180+
* _Complex double f = CMPLX( 2.0, 3.0 );
181+
* double d = __real(f); // 2.0
182+
* ```
147183
*/
148184
class RealPartExpr extends UnaryArithmeticOperation, @realpartexpr {
149185
override string getOperator() { result = "__real" }
186+
187+
override string getCanonicalQLClass() { result = "RealPartExpr" }
150188
}
151189

152190
/**
153-
* A C/C++ GNU imaginary part expression.
191+
* A C/C++ GNU imaginary part expression. It operates on `_Complex` or
192+
* `__complex__` numbers.
193+
* ```
194+
* #include <complex.h>
195+
* _Complex double f = CMPLX( 2.0, 3.0 );
196+
* double d = __imag(f); // 3.0
197+
* ```
154198
*/
155199
class ImaginaryPartExpr extends UnaryArithmeticOperation, @imagpartexpr {
156200
override string getOperator() { result = "__imag" }
201+
202+
override string getCanonicalQLClass() { result = "ImaginaryPartExpr" }
157203
}
158204

159205
/**
160206
* A C/C++ binary arithmetic operation.
207+
*
208+
* This is an abstract base QL class for all binary arithmetic operations.
161209
*/
162210
abstract class BinaryArithmeticOperation extends BinaryOperation {
163211
}
164212

165213
/**
166214
* A C/C++ add expression.
215+
* ```
216+
* c = a + b;
217+
* ```
167218
*/
168219
class AddExpr extends BinaryArithmeticOperation, @addexpr {
169220
override string getOperator() { result = "+" }
170221

171222
override string getCanonicalQLClass() { result = "AddExpr" }
172-
223+
173224
override int getPrecedence() { result = 12 }
174225
}
175226

176227
/**
177228
* A C/C++ subtract expression.
229+
* ```
230+
* c = a - b;
231+
* ```
178232
*/
179233
class SubExpr extends BinaryArithmeticOperation, @subexpr {
180234
override string getOperator() { result = "-" }
181235

182236
override string getCanonicalQLClass() { result = "SubExpr" }
183-
237+
184238
override int getPrecedence() { result = 12 }
185239
}
186240

187241
/**
188242
* A C/C++ multiply expression.
243+
* ```
244+
* c = a * b;
245+
* ```
189246
*/
190247
class MulExpr extends BinaryArithmeticOperation, @mulexpr {
191248
override string getOperator() { result = "*" }
192249

193250
override string getCanonicalQLClass() { result = "MulExpr" }
194-
251+
195252
override int getPrecedence() { result = 13 }
196253
}
197254

198255
/**
199256
* A C/C++ divide expression.
257+
* ```
258+
* c = a / b;
259+
* ```
200260
*/
201261
class DivExpr extends BinaryArithmeticOperation, @divexpr {
202262
override string getOperator() { result = "/" }
203263

204264
override string getCanonicalQLClass() { result = "DivExpr" }
205-
265+
206266
override int getPrecedence() { result = 13 }
207267
}
208268

209269
/**
210270
* A C/C++ remainder expression.
271+
* ```
272+
* c = a % b;
273+
* ```
211274
*/
212275
class RemExpr extends BinaryArithmeticOperation, @remexpr {
213276
override string getOperator() { result = "%" }
214277

215278
override string getCanonicalQLClass() { result = "RemExpr" }
216-
279+
217280
override int getPrecedence() { result = 13 }
218281
}
219282

@@ -224,6 +287,8 @@ class ImaginaryMulExpr extends BinaryArithmeticOperation, @jmulexpr {
224287
override string getOperator() { result = "*" }
225288

226289
override int getPrecedence() { result = 13 }
290+
291+
override string getCanonicalQLClass() { result = "ImaginaryMulExpr" }
227292
}
228293

229294
/**
@@ -233,6 +298,8 @@ class ImaginaryDivExpr extends BinaryArithmeticOperation, @jdivexpr {
233298
override string getOperator() { result = "/" }
234299

235300
override int getPrecedence() { result = 13 }
301+
302+
override string getCanonicalQLClass() { result = "ImaginaryDivExpr" }
236303
}
237304

238305
/**
@@ -242,6 +309,8 @@ class RealImaginaryAddExpr extends BinaryArithmeticOperation, @fjaddexpr {
242309
override string getOperator() { result = "+" }
243310

244311
override int getPrecedence() { result = 12 }
312+
313+
override string getCanonicalQLClass() { result = "RealImaginaryAddExpr" }
245314
}
246315

247316
/**
@@ -251,6 +320,8 @@ class ImaginaryRealAddExpr extends BinaryArithmeticOperation, @jfaddexpr {
251320
override string getOperator() { result = "+" }
252321

253322
override int getPrecedence() { result = 12 }
323+
324+
override string getCanonicalQLClass() { result = "ImaginaryRealAddExpr" }
254325
}
255326

256327
/**
@@ -260,6 +331,8 @@ class RealImaginarySubExpr extends BinaryArithmeticOperation, @fjsubexpr {
260331
override string getOperator() { result = "-" }
261332

262333
override int getPrecedence() { result = 12 }
334+
335+
override string getCanonicalQLClass() { result = "RealImaginarySubExpr" }
263336
}
264337

265338
/**
@@ -269,20 +342,32 @@ class ImaginaryRealSubExpr extends BinaryArithmeticOperation, @jfsubexpr {
269342
override string getOperator() { result = "-" }
270343

271344
override int getPrecedence() { result = 12 }
345+
346+
override string getCanonicalQLClass() { result = "ImaginaryRealSubExpr" }
272347
}
273348

274349
/**
275350
* A C/C++ GNU min expression.
351+
* ```
352+
* c = a <? b;
353+
* ```
276354
*/
277355
class MinExpr extends BinaryArithmeticOperation, @minexpr {
278356
override string getOperator() { result = "<?" }
357+
358+
override string getCanonicalQLClass() { result = "MinExpr" }
279359
}
280360

281361
/**
282362
* A C/C++ GNU max expression.
363+
* ```
364+
* c = a >? b;
365+
* ```
283366
*/
284367
class MaxExpr extends BinaryArithmeticOperation, @maxexpr {
285368
override string getOperator() { result = ">?" }
369+
370+
override string getCanonicalQLClass() { result = "MaxExpr" }
286371
}
287372

288373
/**
@@ -293,33 +378,45 @@ abstract class PointerArithmeticOperation extends BinaryArithmeticOperation {
293378

294379
/**
295380
* A C/C++ pointer add expression.
381+
* ```
382+
* foo *ptr = &f[0];
383+
* ptr = ptr + 2;
384+
* ```
296385
*/
297386
class PointerAddExpr extends PointerArithmeticOperation, @paddexpr {
298387
override string getOperator() { result = "+" }
299388

300389
override string getCanonicalQLClass() { result = "PointerAddExpr" }
301-
390+
302391
override int getPrecedence() { result = 12 }
303392
}
304393

305394
/**
306395
* A C/C++ pointer subtract expression.
396+
* ```
397+
* foo *ptr = &f[3];
398+
* ptr = ptr - 2;
399+
* ```
307400
*/
308401
class PointerSubExpr extends PointerArithmeticOperation, @psubexpr {
309402
override string getOperator() { result = "-" }
310403

311404
override string getCanonicalQLClass() { result = "PointerSubExpr" }
312-
405+
313406
override int getPrecedence() { result = 12 }
314407
}
315408

316409
/**
317410
* A C/C++ pointer difference expression.
411+
* ```
412+
* foo *start = &f[0], *end = &f[4];
413+
* int size = end - size;
414+
* ```
318415
*/
319416
class PointerDiffExpr extends PointerArithmeticOperation, @pdiffexpr {
320417
override string getOperator() { result = "-" }
321418

322419
override string getCanonicalQLClass() { result = "PointerDiffExpr" }
323-
420+
324421
override int getPrecedence() { result = 12 }
325422
}

0 commit comments

Comments
 (0)