Skip to content

Commit 7b494c0

Browse files
committed
C++: respond to PR comments
1 parent f9d6b27 commit 7b494c0

File tree

5 files changed

+74
-66
lines changed

5 files changed

+74
-66
lines changed

cpp/ql/src/semmle/code/cpp/valuenumbering/HashCons.qll

+55-63
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ private cached newtype HCBase =
5555
mk_Variable(x, _)
5656
}
5757
or
58-
HC_FieldAccess(HC s, Field f) {
58+
HC_FieldAccess(HashCons s, Field f) {
5959
mk_DotFieldAccess(s,f,_) or
6060
mk_PointerFieldAccess_with_deref(s,f,_) or
6161
mk_ImplicitThisFieldAccess_with_deref(s,f,_)
6262
}
6363
or
64-
HC_Deref(HC p) {
64+
HC_Deref(HashCons p) {
6565
mk_Deref(p,_) or
6666
mk_PointerFieldAccess(p,_,_) or
6767
mk_ImplicitThisFieldAccess_with_qualifier(p,_,_)
@@ -72,23 +72,23 @@ private cached newtype HCBase =
7272
mk_ImplicitThisFieldAccess(fcn,_,_)
7373
}
7474
or
75-
HC_Conversion(Type t, HC child) { mk_Conversion(t, child, _) }
75+
HC_Conversion(Type t, HashCons child) { mk_Conversion(t, child, _) }
7676
or
77-
HC_BinaryOp(HC lhs, HC rhs, string opname) {
77+
HC_BinaryOp(HashCons lhs, HashCons rhs, string opname) {
7878
mk_BinaryOp(lhs, rhs, opname, _)
7979
}
8080
or
81-
HC_UnaryOp(HC child, string opname) { mk_UnaryOp(child, opname, _) }
81+
HC_UnaryOp(HashCons child, string opname) { mk_UnaryOp(child, opname, _) }
8282
or
83-
HC_ArrayAccess(HC x, HC i) {
83+
HC_ArrayAccess(HashCons x, HashCons i) {
8484
mk_ArrayAccess(x,i,_)
8585
}
8686
or
8787
HC_NonmemberFunctionCall(Function fcn, HC_Args args) {
8888
mk_NonmemberFunctionCall(fcn, args, _)
8989
}
9090
or
91-
HC_MemberFunctionCall(Function trg, HC qual, HC_Args args) {
91+
HC_MemberFunctionCall(Function trg, HashCons qual, HC_Args args) {
9292
mk_MemberFunctionCall(trg, qual, args, _)
9393
}
9494
or
@@ -101,11 +101,11 @@ private cached newtype HC_Args =
101101
HC_EmptyArgs(Function fcn) {
102102
any()
103103
}
104-
or HC_ArgCons(Function fcn, HC hc, int i, HC_Args list) {
104+
or HC_ArgCons(Function fcn, HashCons hc, int i, HC_Args list) {
105105
mk_ArgCons(fcn, hc, i, list, _)
106106
}
107107
/**
108-
* HC is the hash-cons of an expression. The relationship between `Expr`
108+
* HashCons is the hash-cons of an expression. The relationship between `Expr`
109109
* and `HC` is many-to-one: every `Expr` has exactly one `HC`, but multiple
110110
* expressions can have the same `HC`. If two expressions have the same
111111
* `HC`, it means that they are structurally equal. The `HC` is an opaque
@@ -118,9 +118,7 @@ private cached newtype HC_Args =
118118
* expression with this `HC` and using its `toString` and `getLocation`
119119
* methods.
120120
*/
121-
class HC extends HCBase {
122-
HC() { this instanceof HCBase }
123-
121+
class HashCons extends HCBase {
124122
/** Gets an expression that has this HC. */
125123
Expr getAnExpr() {
126124
this = hashCons(result)
@@ -234,7 +232,7 @@ private predicate analyzableDotFieldAccess(DotFieldAccess access) {
234232
}
235233

236234
private predicate mk_DotFieldAccess(
237-
HC qualifier, Field target, DotFieldAccess access) {
235+
HashCons qualifier, Field target, DotFieldAccess access) {
238236
analyzableDotFieldAccess(access) and
239237
target = access.getTarget() and
240238
qualifier = hashCons(access.getQualifier().getFullyConverted())
@@ -246,9 +244,9 @@ private predicate analyzablePointerFieldAccess(PointerFieldAccess access) {
246244
}
247245

248246
private predicate mk_PointerFieldAccess(
249-
HC qualifier, Field target,
247+
HashCons qualifier, Field target,
250248
PointerFieldAccess access) {
251-
analyzablePointerFieldAccess(access) and
249+
analyzablePointerFieldAccess(access) and
252250
target = access.getTarget() and
253251
qualifier = hashCons(access.getQualifier().getFullyConverted())
254252
}
@@ -257,38 +255,35 @@ private predicate mk_PointerFieldAccess(
257255
* `obj->field` is equivalent to `(*obj).field`, so we need to wrap an
258256
* extra `HC_Deref` around the qualifier.
259257
*/
260-
private predicate mk_PointerFieldAccess_with_deref(
261-
HC new_qualifier, Field target, PointerFieldAccess access) {
262-
exists (HC qualifier
258+
private predicate mk_PointerFieldAccess_with_deref (HashCons new_qualifier, Field target,
259+
PointerFieldAccess access) {
260+
exists (HashCons qualifier
263261
| mk_PointerFieldAccess(qualifier, target, access) and
264262
new_qualifier = HC_Deref(qualifier))
265263
}
266264

267-
private predicate analyzableImplicitThisFieldAccess(
268-
ImplicitThisFieldAccess access) {
265+
private predicate analyzableImplicitThisFieldAccess(ImplicitThisFieldAccess access) {
269266
strictcount (access.getTarget()) = 1 and
270267
strictcount (access.getEnclosingFunction()) = 1
271268
}
272269

273-
private predicate mk_ImplicitThisFieldAccess(
274-
Function fcn, Field target,
270+
private predicate mk_ImplicitThisFieldAccess(Function fcn, Field target,
275271
ImplicitThisFieldAccess access) {
276272
analyzableImplicitThisFieldAccess(access) and
277273
target = access.getTarget() and
278274
fcn = access.getEnclosingFunction()
279275
}
280276

281-
private predicate mk_ImplicitThisFieldAccess_with_qualifier(
282-
HC qualifier, Field target,
277+
private predicate mk_ImplicitThisFieldAccess_with_qualifier( HashCons qualifier, Field target,
283278
ImplicitThisFieldAccess access) {
284279
exists (Function fcn
285280
| mk_ImplicitThisFieldAccess(fcn, target, access) and
286281
qualifier = HC_ThisExpr(fcn))
287282
}
288283

289-
private predicate mk_ImplicitThisFieldAccess_with_deref(
290-
HC new_qualifier, Field target, ImplicitThisFieldAccess access) {
291-
exists (HC qualifier
284+
private predicate mk_ImplicitThisFieldAccess_with_deref(HashCons new_qualifier, Field target,
285+
ImplicitThisFieldAccess access) {
286+
exists (HashCons qualifier
292287
| mk_ImplicitThisFieldAccess_with_qualifier(
293288
qualifier, target, access) and
294289
new_qualifier = HC_Deref(qualifier))
@@ -309,7 +304,7 @@ private predicate analyzableConversion(Conversion conv) {
309304
strictcount (conv.getExpr()) = 1
310305
}
311306

312-
private predicate mk_Conversion(Type t, HC child, Conversion conv) {
307+
private predicate mk_Conversion(Type t, HashCons child, Conversion conv) {
313308
analyzableConversion(conv) and
314309
t = conv.getType().getUnspecifiedType() and
315310
child = hashCons(conv.getExpr())
@@ -321,8 +316,7 @@ private predicate analyzableBinaryOp(BinaryOperation op) {
321316
strictcount (op.getOperator()) = 1
322317
}
323318

324-
private predicate mk_BinaryOp(
325-
HC lhs, HC rhs, string opname, BinaryOperation op) {
319+
private predicate mk_BinaryOp(HashCons lhs, HashCons rhs, string opname, BinaryOperation op) {
326320
analyzableBinaryOp(op) and
327321
lhs = hashCons(op.getLeftOperand().getFullyConverted()) and
328322
rhs = hashCons(op.getRightOperand().getFullyConverted()) and
@@ -335,7 +329,7 @@ private predicate analyzableUnaryOp(UnaryOperation op) {
335329
strictcount (op.getOperator()) = 1
336330
}
337331

338-
private predicate mk_UnaryOp(HC child, string opname, UnaryOperation op) {
332+
private predicate mk_UnaryOp(HashCons child, string opname, UnaryOperation op) {
339333
analyzableUnaryOp(op) and
340334
child = hashCons(op.getOperand().getFullyConverted()) and
341335
opname = op.getOperator()
@@ -355,40 +349,36 @@ private predicate analyzableArrayAccess(ArrayExpr ae) {
355349
strictcount (ae.getArrayOffset().getFullyConverted()) = 1
356350
}
357351

358-
private predicate mk_ArrayAccess(
359-
HC base, HC offset, ArrayExpr ae) {
352+
private predicate mk_ArrayAccess(HashCons base, HashCons offset, ArrayExpr ae) {
360353
analyzableArrayAccess(ae) and
361354
base = hashCons(ae.getArrayBase().getFullyConverted()) and
362355
offset = hashCons(ae.getArrayOffset().getFullyConverted())
363356
}
364357

365-
private predicate analyzablePointerDereferenceExpr(
366-
PointerDereferenceExpr deref) {
358+
private predicate analyzablePointerDereferenceExpr(PointerDereferenceExpr deref) {
367359
strictcount (deref.getOperand().getFullyConverted()) = 1
368360
}
369361

370-
private predicate mk_Deref(
371-
HC p, PointerDereferenceExpr deref) {
362+
private predicate mk_Deref(HashCons p, PointerDereferenceExpr deref) {
372363
analyzablePointerDereferenceExpr(deref) and
373364
p = hashCons(deref.getOperand().getFullyConverted())
374365
}
375366

376-
private predicate analyzableNonmemberFunctionCall(
377-
FunctionCall fc) {
378-
forall(int i | exists(fc.getArgument(i)) | strictcount(fc.getArgument(i).getFullyConverted()) = 1) and
367+
private predicate analyzableNonmemberFunctionCall(FunctionCall fc) {
368+
forall(int i |
369+
exists(fc.getArgument(i)) |
370+
strictcount(fc.getArgument(i).getFullyConverted()) = 1
371+
) and
379372
strictcount(fc.getTarget()) = 1 and
380373
not exists(fc.getQualifier())
381374
}
382375

383-
private predicate mk_NonmemberFunctionCall(
384-
Function fcn,
385-
HC_Args args,
386-
FunctionCall fc
376+
private predicate mk_NonmemberFunctionCall(Function fcn, HC_Args args, FunctionCall fc
387377
) {
388378
fc.getTarget() = fcn and
389379
analyzableNonmemberFunctionCall(fc) and
390380
(
391-
exists(HC head, HC_Args tail |
381+
exists(HashCons head, HC_Args tail |
392382
args = HC_ArgCons(fcn, head, fc.getNumberOfArguments() - 1, tail) and
393383
mk_ArgCons(fcn, head, fc.getNumberOfArguments() - 1, tail, fc)
394384
)
@@ -400,22 +390,25 @@ private predicate mk_NonmemberFunctionCall(
400390

401391
private predicate analyzableMemberFunctionCall(
402392
FunctionCall fc) {
403-
forall(int i | exists(fc.getArgument(i)) | strictcount(fc.getArgument(i).getFullyConverted()) = 1) and
393+
forall(int i |
394+
exists(fc.getArgument(i)) |
395+
strictcount(fc.getArgument(i).getFullyConverted()) = 1
396+
) and
404397
strictcount(fc.getTarget()) = 1 and
405398
strictcount(fc.getQualifier().getFullyConverted()) = 1
406399
}
407400

408401
private predicate mk_MemberFunctionCall(
409402
Function fcn,
410-
HC qual,
403+
HashCons qual,
411404
HC_Args args,
412405
FunctionCall fc
413406
) {
414407
fc.getTarget() = fcn and
415408
analyzableMemberFunctionCall(fc) and
416409
hashCons(fc.getQualifier().getFullyConverted()) = qual and
417410
(
418-
exists(HC head, HC_Args tail |
411+
exists(HashCons head, HC_Args tail |
419412
args = HC_ArgCons(fcn, head, fc.getNumberOfArguments() - 1, tail) and
420413
mk_ArgCons(fcn, head, fc.getNumberOfArguments() - 1, tail, fc)
421414
)
@@ -434,15 +427,15 @@ private predicate analyzableFunctionCall(
434427
}
435428

436429
/**
437-
* Holds if `fc` is a call to `fcn`, `fc`'s first `i-1` arguments have hash-cons
438-
* `list`, and `fc`'s `i`th argument has hash-cons `hc`
430+
* Holds if `fc` is a call to `fcn`, `fc`'s first `i` arguments have hash-cons
431+
* `list`, and `fc`'s argument at index `i` has hash-cons `hc`.
439432
*/
440-
private predicate mk_ArgCons(Function fcn, HC hc, int i, HC_Args list, FunctionCall fc) {
433+
private predicate mk_ArgCons(Function fcn, HashCons hc, int i, HC_Args list, FunctionCall fc) {
441434
analyzableFunctionCall(fc) and
442435
fc.getTarget() = fcn and
443436
hc = hashCons(fc.getArgument(i).getFullyConverted()) and
444437
(
445-
exists(HC head, HC_Args tail |
438+
exists(HashCons head, HC_Args tail |
446439
list = HC_ArgCons(fcn, head, i - 1, tail) and
447440
mk_ArgCons(fcn, head, i - 1, tail, fc) and
448441
i > 0
@@ -454,7 +447,7 @@ private predicate mk_ArgCons(Function fcn, HC hc, int i, HC_Args list, FunctionC
454447
}
455448

456449
/** Gets the hash-cons of expression `e`. */
457-
cached HC hashCons(Expr e) {
450+
cached HashCons hashCons(Expr e) {
458451
exists (int val, Type t
459452
| mk_IntLiteral(val, t, e) and
460453
result = HC_IntLiteral(val, t))
@@ -471,44 +464,43 @@ cached HC hashCons(Expr e) {
471464
| mk_StringLiteral(val, t, e) and
472465
result = HC_StringLiteral(val, t))
473466
or
474-
// Variable with no SSA information.
475467
exists (Variable x
476468
| mk_Variable(x, e) and
477469
result = HC_Variable(x))
478470
or
479-
exists (HC qualifier, Field target
471+
exists (HashCons qualifier, Field target
480472
| mk_DotFieldAccess(qualifier, target, e) and
481473
result = HC_FieldAccess(qualifier, target))
482474
or
483-
exists (HC qualifier, Field target
475+
exists (HashCons qualifier, Field target
484476
| mk_PointerFieldAccess_with_deref(qualifier, target, e) and
485477
result = HC_FieldAccess(qualifier, target))
486478
or
487-
exists (HC qualifier, Field target
479+
exists (HashCons qualifier, Field target
488480
| mk_ImplicitThisFieldAccess_with_deref(qualifier, target, e) and
489481
result = HC_FieldAccess(qualifier, target))
490482
or
491483
exists (Function fcn
492484
| mk_ThisExpr(fcn, e) and
493485
result = HC_ThisExpr(fcn))
494486
or
495-
exists (Type t, HC child
487+
exists (Type t, HashCons child
496488
| mk_Conversion(t, child, e) and
497489
result = HC_Conversion(t, child))
498490
or
499-
exists (HC lhs, HC rhs, string opname
491+
exists (HashCons lhs, HashCons rhs, string opname
500492
| mk_BinaryOp(lhs, rhs, opname, e) and
501493
result = HC_BinaryOp(lhs, rhs, opname))
502494
or
503-
exists (HC child, string opname
495+
exists (HashCons child, string opname
504496
| mk_UnaryOp(child, opname, e) and
505497
result = HC_UnaryOp(child, opname))
506498
or
507-
exists (HC x, HC i
499+
exists (HashCons x, HashCons i
508500
| mk_ArrayAccess(x, i, e) and
509501
result = HC_ArrayAccess(x, i))
510502
or
511-
exists (HC p
503+
exists (HashCons p
512504
| mk_Deref(p, e) and
513505
result = HC_Deref(p))
514506
or
@@ -517,7 +509,7 @@ cached HC hashCons(Expr e) {
517509
result = HC_NonmemberFunctionCall(fcn, args)
518510
)
519511
or
520-
exists(Function fcn, HC qual, HC_Args args
512+
exists(Function fcn, HashCons qual, HC_Args args
521513
| mk_MemberFunctionCall(fcn, qual, args, e) and
522514
result = HC_MemberFunctionCall(fcn, qual, args)
523515
)

cpp/ql/test/library-tests/valuenumbering/HashCons/HashCons.expected

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
| test.cpp:92:11:92:11 | x | 92:c11-c11 93:c10-c10 |
3939
| test.cpp:97:3:97:3 | x | 97:c3-c3 98:c3-c3 |
4040
| test.cpp:97:3:97:5 | ... ++ | 97:c3-c5 98:c3-c5 |
41-
| test.cpp:103:10:103:11 | 1 | 103:c10-c11 104:c7-c7 107:c7-c7 108:c7-c7 10:c16-c16 |
41+
| test.cpp:103:10:103:11 | 1 | 103:c10-c11 104:c7-c7 107:c7-c7 108:c7-c7 10:c16-c16 179:c21-c21 |
4242
| test.cpp:104:3:104:3 | x | 104:c3-c3 105:c3-c3 106:c3-c3 107:c3-c3 108:c3-c3 |
4343
| test.cpp:105:7:105:7 | 2 | 105:c7-c7 106:c7-c7 107:c11-c11 108:c11-c11 21:c16-c16 |
4444
| test.cpp:107:7:107:11 | ... + ... | 107:c7-c11 108:c7-c11 |
@@ -69,3 +69,7 @@
6969
| test.cpp:156:14:156:20 | 0 | 156:c14-c20 156:c3-c9 157:c10-c16 |
7070
| test.cpp:171:3:171:6 | (int)... | 171:c3-c6 172:c3-c6 |
7171
| test.cpp:171:3:171:6 | e1x1 | 171:c3-c6 172:c3-c6 |
72+
| test.cpp:179:10:179:22 | (...) | 179:c10-c22 179:c10-c22 |
73+
| test.cpp:179:17:179:17 | y | 179:c17-c17 179:c17-c17 |
74+
| test.cpp:179:17:179:21 | ... + ... | 179:c17-c21 179:c17-c21 |
75+
| test.cpp:185:17:185:17 | y | 185:c17-c17 185:c17-c17 |

cpp/ql/test/library-tests/valuenumbering/HashCons/HashCons.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cpp
22
import semmle.code.cpp.valuenumbering.HashCons
33

4-
from HC h
4+
from HashCons h
55
where strictcount(h.getAnExpr()) > 1
66
select
77
h,

cpp/ql/test/library-tests/valuenumbering/HashCons/Uniqueness.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import semmle.code.cpp.valuenumbering.HashCons
55
// So this query should have zero results.
66
from Expr e
77
where count(hashCons(e)) != 1
8-
select e, concat(HC h | h = hashCons(e) | h.getKind(), ", ")
8+
select e, concat(HashCons h | h = hashCons(e) | h.getKind(), ", ")

cpp/ql/test/library-tests/valuenumbering/HashCons/test.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,15 @@ int test12() {
172172
e1x1 == e2x2;
173173
return e1x2;
174174
}
175+
176+
#define SQUARE(x) ((x) * (x))
177+
178+
int test13(int y) {
179+
return SQUARE(y + 1);
180+
}
181+
182+
#define SQUARE(x) x * x
183+
184+
int test14(int y) {
185+
return SQUARE(y);
186+
}

0 commit comments

Comments
 (0)