-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathLambdaEquivalence.qll
1704 lines (1549 loc) · 61.2 KB
/
LambdaEquivalence.qll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cpp
/**
* This is a copy of HashCons module found the CodeQL standard library with the following changes:
* - Extended the HashCons computation to statements and functions by implementing `HashConsStmt` and `HashConsFunc`.
* - Modified how the `HCExpr` computes the hashcons for a `Variable`. Since we no longer compute hashcons in a single function
* we base the hascons on the name and type of a variable.
*/
private module HashCons {
/*
* Note to developers: the correctness of this module depends on the
* definitions of HC, hashConsExpr, and analyzableExpr being kept in sync with
* each other. If you change this module then make sure that the change is
* symmetric across all three.
*/
/** Used to represent the hash-cons of an expression. */
cached
private newtype HCExpr =
HC_IntLiteral(int val, Type t) { mk_IntLiteral(val, t, _) } or
HC_EnumConstantAccess(EnumConstant val, Type t) { mk_EnumConstantAccess(val, t, _) } or
HC_FloatLiteral(float val, Type t) { mk_FloatLiteral(val, t, _) } or
HC_StringLiteral(string val, Type t) { mk_StringLiteral(val, t, _) } or
HC_Nullptr() { mk_Nullptr(_) } or
HC_Variable(Type t, string name) { mk_Variable(t, name, _) } or
HC_FieldAccess(HashConsExpr s, Field f) { mk_DotFieldAccess(s, f, _) } or
HC_Deref(HashConsExpr p) { mk_Deref(p, _) } or
HC_PointerFieldAccess(HashConsExpr qual, Field target) {
mk_PointerFieldAccess(qual, target, _)
} or
HC_ThisExpr(Function fcn) { mk_ThisExpr(fcn, _) } or
HC_ImplicitThisFieldAccess(Function fcn, Field target) {
mk_ImplicitThisFieldAccess(fcn, target, _)
} or
HC_Conversion(Type t, HashConsExpr child) { mk_Conversion(t, child, _) } or
HC_BinaryOp(HashConsExpr lhs, HashConsExpr rhs, string opname) {
mk_BinaryOp(lhs, rhs, opname, _)
} or
HC_UnaryOp(HashConsExpr child, string opname) { mk_UnaryOp(child, opname, _) } or
HC_ArrayAccess(HashConsExpr x, HashConsExpr i) { mk_ArrayAccess(x, i, _) } or
HC_NonmemberFunctionCall(Function fcn, HC_Args args) { mk_NonmemberFunctionCall(fcn, args, _) } or
HC_ExprCall(HashConsExpr hc, HC_Args args) { mk_ExprCall(hc, args, _) } or
HC_MemberFunctionCall(Function trg, HashConsExpr qual, HC_Args args) {
mk_MemberFunctionCall(trg, qual, args, _)
} or
// Hack to get around argument 0 of allocator calls being an error expression
HC_AllocatorArgZero(Type t) { mk_AllocatorArgZero(t, _) } or
HC_NewExpr(Type t, HC_Alloc alloc, HC_Init init) { mk_NewExpr(t, alloc, init, _) } or
HC_NewArrayExpr(Type t, HC_Alloc alloc, HC_Extent extent, HC_Init init) {
mk_NewArrayExpr(t, alloc, extent, init, _)
} or
HC_SizeofType(Type t) { mk_SizeofType(t, _) } or
HC_SizeofExpr(HashConsExpr child) { mk_SizeofExpr(child, _) } or
HC_AlignofType(Type t) { mk_AlignofType(t, _) } or
HC_AlignofExpr(HashConsExpr child) { mk_AlignofExpr(child, _) } or
HC_UuidofOperator(Type t) { mk_UuidofOperator(t, _) } or
HC_TypeidType(Type t) { mk_TypeidType(t, _) } or
HC_TypeidExpr(HashConsExpr child) { mk_TypeidExpr(child, _) } or
HC_ClassAggregateLiteral(Class c, HC_Fields hcf) { mk_ClassAggregateLiteral(c, hcf, _) } or
HC_ArrayAggregateLiteral(Type t, HC_Array hca) { mk_ArrayAggregateLiteral(t, hca, _) } or
HC_DeleteExpr(HashConsExpr child) { mk_DeleteExpr(child, _) } or
HC_DeleteArrayExpr(HashConsExpr child) { mk_DeleteArrayExpr(child, _) } or
HC_ThrowExpr(HashConsExpr child) { mk_ThrowExpr(child, _) } or
HC_ReThrowExpr() or
HC_ConditionalExpr(HashConsExpr cond, HashConsExpr trueHC, HashConsExpr falseHC) {
mk_ConditionalExpr(cond, trueHC, falseHC, _)
} or
HC_NoExceptExpr(HashConsExpr child) { mk_NoExceptExpr(child, _) } or
// Any expression that is not handled by the cases above is
// given a unique number based on the expression itself.
HC_Unanalyzable(Expr e) { not analyzableExpr(e, _) }
/** Used to implement optional init on `new` expressions */
private newtype HC_Init =
HC_NoInit() or
HC_HasInit(HashConsExpr hc) { mk_HasInit(hc, _) }
/**
* Used to implement optional allocator call on `new` expressions
*/
private newtype HC_Alloc =
HC_NoAlloc() or
HC_HasAlloc(HashConsExpr hc) { mk_HasAlloc(hc, _) }
/**
* Used to implement optional extent expression on `new[]` exprtessions
*/
private newtype HC_Extent =
HC_NoExtent() or
HC_HasExtent(HashConsExpr hc) { mk_HasExtent(hc, _) }
/** Used to implement hash-consing of argument lists */
private newtype HC_Args =
HC_EmptyArgs() { any() } or
HC_ArgCons(HashConsExpr hc, int i, HC_Args list) { mk_ArgCons(hc, i, list, _) }
/**
* Used to implement hash-consing of struct initizializers.
*/
private newtype HC_Fields =
HC_EmptyFields(Class c) { exists(ClassAggregateLiteral cal | c = cal.getUnspecifiedType()) } or
HC_FieldCons(Class c, int i, Field f, HashConsExpr hc, HC_Fields hcf) {
mk_FieldCons(c, i, f, hc, hcf, _)
}
private newtype HC_Array =
HC_EmptyArray(Type t) { exists(ArrayAggregateLiteral aal | aal.getUnspecifiedType() = t) } or
HC_ArrayCons(Type t, int i, HashConsExpr hc, HC_Array hca) { mk_ArrayCons(t, i, hc, hca, _) }
private newtype HCStmt =
//HC_AsmStmt(AsmStmt s) or
HC_BlockStmt(HC_Stmts stmts) { mk_BlockStmtCons(stmts, _) } or
HC_CoReturnStmt(HashConsExpr operand, HC_OptCoReturnExpr expr) {
mk_CoReturnStmtCons(operand, expr, _)
} or
HC_ComputedGotoStmt(HashConsExpr target) { mk_ComputedGotoStmtCons(target, _) } or
/* Control Structure */
/* ConditionalStmt */
HC_ConstexprIfStmt(HashConsExpr condition, HashConsStmt thenBranch, HC_OptElseStmt elseBranch) {
mk_ConstexprIfStmtCons(condition, thenBranch, elseBranch, _)
} or
HC_IfStmt(HashConsExpr condition, HashConsStmt thenBranch, HC_OptElseStmt elseBranch) {
mk_IfStmtCons(condition, thenBranch, elseBranch, _)
} or
HC_SwitchStmt(HashConsExpr condition, HashConsStmt body) {
mk_SwitchStmtCons(condition, body, _)
} or
/* End ConditionalStmt */
/* Loop */
HC_DoStmt(HashConsExpr condition, HashConsStmt body) { mk_DoStmtCons(condition, body, _) } or
HC_ForStmt(HashConsStmt init, HashConsExpr condition, HashConsExpr update, HashConsStmt body) {
mk_ForStmtCons(init, condition, update, body, _)
} or
HC_RangeBasedForStmt(HashConsExpr variable, HashConsExpr range, HashConsStmt body) {
mk_RangeBasedForStmtCons(variable, range, body, _)
} or
HC_WhileStmt(HashConsExpr condition, HashConsStmt body) { mk_WhileStmtCons(condition, body, _) } or
/* End Loop */
/* End Control Structure */
// HC_DeclStmt(DeclStmt s) or
// HC_EmptyStmt(EmptyStmt s) or
HC_ExprStmt(HashConsExpr e) { mk_ExprStmtCons(e, _) } or
// Handler
/* JumpStmt */
HC_BreakStmt(HashConsStmt breakable) { mk_BreakStmtCons(breakable, _) } or
HC_ContinueStmt(HashConsStmt continueable) { mk_ContinueStmtCons(continueable, _) } or
HC_GotoStmt(HashConsStmt target, HC_OptGotoLabel label) { mk_GotoStmtCons(target, label, _) } or
/* End JumpStmt */
HC_LabelStmt(HC_OptLabelName name) { mk_LabelStmtCons(name, _) } or
HC_MicrosoftTryStmt(HashConsStmt body) { mk_MicrosoftTryStmtCons(body, _) } or
HC_ReturnStmt(HC_OptReturnExpr e) { mk_ReturnStmtCons(e, _) } or
HC_SwitchCase(HashConsExpr e) { mk_SwitchCaseCons(e, _) } or
// TODO: This relies on the TODO for the switch case where we need to hashcons the statements in a case.
//HC_SwitchDefaultCase(DefaultCase s) or
HC_TryStmt(HashConsStmt body) { mk_TryStmtCons(body, _) } or
HC_CatchStmt(HashConsExpr parameter, HashConsStmt body) { mk_CatchStmtCons(parameter, body, _) } or
// TODO implement hashcons for
//HC_VlaDeclStmt(VlaDeclStmt s) or
//HC_VlaDimensionStmt(VlaDimensionStmt s)
HC_DeclStmt(HC_Decls d) { mk_DeclStmt(d, _) }
private newtype HC_Decl =
HC_VariableDecl(Type t, string name, HC_OptInitializer init) {
mk_VariableDecl(t, name, init, _)
}
private newtype HC_Decls =
HC_EmptyDecls() or
HC_DeclCons(HC_Decl head, int i, HC_Decls list) { mk_DeclCons(head, i, list, _) }
/* HashCons for sequences of statements. */
private newtype HC_Stmts =
HC_EmptyStmts() or
HC_StmtCons(HashConsStmt hc, int i, HC_Stmts list) { mk_StmtCons(hc, i, list, _) }
private newtype HC_OptCoReturnExpr =
HC_NoCoReturnExpr() or
HC_HasCoReturnExpr(HashConsExpr e) { exists(CoReturnStmt s | e = hashConsExpr(s.getExpr())) }
private newtype HC_OptElseStmt =
HC_NoElseStmt() or
HC_HasElseStmt(HashConsStmt s) {
exists(IfStmt ifStatement | s = hashConsStmt(ifStatement.getElse()))
or
exists(ConstexprIfStmt ifStatement | s = hashConsStmt(ifStatement.getElse()))
}
/* HashCons for optional expression. */
private newtype HC_OptReturnExpr =
HC_NoReturnExpr() or
HC_HasReturnExpr(HashConsExpr hc) { exists(ReturnStmt s | hc = hashConsExpr(s.getExpr())) }
private newtype HC_OptGotoLabel =
HC_NoGotoLabel() or
HC_HasGotoLabel(string label) { exists(GotoStmt s | label = s.getName()) }
private newtype HC_OptLabelName =
HC_NoLabelName() or
HC_HasLabelName(string name) { exists(LabelStmt s | name = s.getName()) }
private newtype HC_OptInitializer =
HC_NoInitializer() or
HC_HasInitializer(HashConsExpr hc) {
exists(Variable v | hc = hashConsExpr(v.getInitializer().getExpr()))
}
private newtype HCFunc =
HC_Function(Type t, string name, HC_Params params, HashConsStmt body) {
mk_FunctionCons(t, name, params, body, _)
}
private newtype HC_Params =
HC_NoParams() or
HC_ParamCons(Type t, string name, int i, HC_Params list) { mk_ParamCons(t, name, i, list, _) }
/**
* HashConsExpr is the hash-cons of an expression. The relationship between `Expr`
* and `HC` is many-to-one: every `Expr` has exactly one `HC`, but multiple
* expressions can have the same `HC`. If two expressions have the same
* `HC`, it means that they are structurally equal. The `HC` is an opaque
* value. The only use for the `HC` of an expression is to find other
* expressions that are structurally equal to it. Use the predicate
* `hashConsExpr` to get the `HC` for an `Expr`.
*
* Note: `HC` has `toString` and `getLocation` methods, so that it can be
* displayed in a results list. These work by picking an arbitrary
* expression with this `HC` and using its `toString` and `getLocation`
* methods.
*/
class HashConsExpr extends HCExpr {
/** Gets an expression that has this HC. */
Expr getAnExpr() { this = hashConsExpr(result) }
/** Gets the kind of the HC. This can be useful for debugging. */
string getKind() {
if this instanceof HC_IntLiteral
then result = "IntLiteral"
else
if this instanceof HC_EnumConstantAccess
then result = "EnumConstantAccess"
else
if this instanceof HC_FloatLiteral
then result = "FloatLiteral"
else
if this instanceof HC_StringLiteral
then result = "StringLiteral"
else
if this instanceof HC_Nullptr
then result = "Nullptr"
else
if this instanceof HC_Variable
then result = "Variable"
else
if this instanceof HC_FieldAccess
then result = "FieldAccess"
else
if this instanceof HC_Deref
then result = "Deref"
else
if this instanceof HC_ThisExpr
then result = "ThisExpr"
else
if this instanceof HC_Conversion
then result = "Conversion"
else
if this instanceof HC_BinaryOp
then result = "BinaryOp"
else
if this instanceof HC_UnaryOp
then result = "UnaryOp"
else
if this instanceof HC_ArrayAccess
then result = "ArrayAccess"
else
if this instanceof HC_Unanalyzable
then result = "Unanalyzable"
else
if this instanceof HC_NonmemberFunctionCall
then result = "NonmemberFunctionCall"
else
if this instanceof HC_MemberFunctionCall
then result = "MemberFunctionCall"
else
if this instanceof HC_NewExpr
then result = "NewExpr"
else
if this instanceof HC_NewArrayExpr
then result = "NewArrayExpr"
else
if this instanceof HC_SizeofType
then result = "SizeofTypeOperator"
else
if this instanceof HC_SizeofExpr
then result = "SizeofExprOperator"
else
if this instanceof HC_AlignofType
then result = "AlignofTypeOperator"
else
if this instanceof HC_AlignofExpr
then result = "AlignofExprOperator"
else
if this instanceof HC_UuidofOperator
then result = "UuidofOperator"
else
if this instanceof HC_TypeidType
then result = "TypeidType"
else
if this instanceof HC_TypeidExpr
then result = "TypeidExpr"
else
if this instanceof HC_ArrayAggregateLiteral
then result = "ArrayAggregateLiteral"
else
if
this instanceof HC_ClassAggregateLiteral
then result = "ClassAggregateLiteral"
else
if this instanceof HC_DeleteExpr
then result = "DeleteExpr"
else
if this instanceof HC_DeleteArrayExpr
then result = "DeleteArrayExpr"
else
if this instanceof HC_ThrowExpr
then result = "ThrowExpr"
else
if this instanceof HC_ReThrowExpr
then result = "ReThrowExpr"
else
if this instanceof HC_ExprCall
then result = "ExprCall"
else
if
this instanceof
HC_ConditionalExpr
then
result = "ConditionalExpr"
else
if
this instanceof
HC_NoExceptExpr
then result = "NoExceptExpr"
else
if
this instanceof
HC_AllocatorArgZero
then
result =
"AllocatorArgZero"
else result = "error"
}
/**
* Gets an example of an expression with this HC.
* This is useful for things like implementing toString().
*/
private Expr exampleExpr() {
// Pick the expression with the minimum source location string. This is
// just an arbitrary way to pick an expression with this `HC`.
result =
min(Expr e |
this = hashConsExpr(e)
|
e
order by
exampleLocationString(e.getLocation()), e.getLocation().getStartColumn(),
e.getLocation().getEndLine(), e.getLocation().getEndColumn()
)
}
/** Gets a textual representation of this element. */
string toString() { result = exampleExpr().toString() }
/** Gets the primary location of this element. */
Location getLocation() { result = exampleExpr().getLocation() }
}
/**
* HashConsStmt is the hash-cons of an statement. The relationship between `Stmt`
* and `HC` is many-to-one: every `Stmt` has exactly one `HC`, but multiple
* statements can have the same `HC`. If two statements have the same
* `HC`, it means that they are structurally equal. The `HC` is an opaque
* value. The only use for the `HC` of an statement is to find other
* statements that are structurally equal to it. Use the predicate
* `hashConsStmt` to get the `HC` for an `Stmt`.
*
* Note: `HC` has `toString` and `getLocation` methods, so that it can be
* displayed in a results list. These work by picking an arbitrary
* statement with this `HC` and using its `toString` and `getLocation`
* methods.
*/
class HashConsStmt extends HCStmt {
Stmt getAStmt() { this = hashConsStmt(result) }
/** Gets the kind of the HC. This can be useful for debugging. */
string getKind() {
if this instanceof HC_BlockStmt
then result = "BlockStmt"
else
if this instanceof HC_CoReturnStmt
then result = "CoReturnStmt"
else
if this instanceof HC_ComputedGotoStmt
then result = "ComputedGotoStmt"
else
if this instanceof HC_ConstexprIfStmt
then result = "ConstexprIfStmt"
else
if this instanceof HC_IfStmt
then result = "IfStmt"
else
if this instanceof HC_SwitchStmt
then result = "SwitchStmt"
else
if this instanceof HC_DoStmt
then result = "DoStmt"
else
if this instanceof HC_ForStmt
then result = "ForStmt"
else
if this instanceof HC_RangeBasedForStmt
then result = "RangeBasedForStmt"
else
if this instanceof HC_WhileStmt
then result = "WhileStmt"
else
if this instanceof HC_ExprStmt
then result = "ExprStmt"
else
if this instanceof HC_BreakStmt
then result = "BreakStmt"
else
if this instanceof HC_ContinueStmt
then result = "ContinueStmt"
else
if this instanceof HC_GotoStmt
then result = "GotoStmt"
else
if this instanceof HC_LabelStmt
then result = "LabelStmt"
else
if this instanceof HC_MicrosoftTryStmt
then result = "MicrosoftTryStmt"
else
if this instanceof HC_ReturnStmt
then result = "ReturnStmt"
else
if this instanceof HC_SwitchCase
then result = "SwitchCase"
else
if this instanceof HC_TryStmt
then result = "TryStmt"
else
if this instanceof HC_CatchStmt
then result = "CatchStmt"
else
if this instanceof HC_DeclStmt
then result = "DeclStmt"
else result = "error"
}
/**
* Gets an example of a statement with this HC.
* This is useful for things like implementing toString().
*/
private Stmt exampleStmt() {
// Pick the statement with the minimum source location string. This is
// just an arbitrary way to pick an statement with this `HC`.
result =
min(Stmt s |
this = hashConsStmt(s)
|
s
order by
exampleLocationString(s.getLocation()), s.getLocation().getStartColumn(),
s.getLocation().getEndLine(), s.getLocation().getEndColumn()
)
}
/** Gets a textual representation of this element. */
string toString() { result = exampleStmt().toString() }
/** Gets the primary location of this element. */
Location getLocation() { result = exampleStmt().getLocation() }
}
class HashConsFunc extends HCFunc {
Function getAFunction() { this = hashConsFunc(result) }
/** Gets the kind of the HC. This can be useful for debugging. */
string getKind() {
if this instanceof HC_Function then result = "Function" else result = "error"
}
/**
* Gets an example of a statement with this HC.
* This is useful for things like implementing toString().
*/
private Function exampleFunc() {
// Pick the statement with the minimum source location string. This is
// just an arbitrary way to pick an statement with this `HC`.
result =
min(Function f |
this = hashConsFunc(f)
|
f
order by
exampleLocationString(f.getLocation()), f.getLocation().getStartColumn(),
f.getLocation().getEndLine(), f.getLocation().getEndColumn()
)
}
/** Gets a textual representation of this element. */
string toString() { result = exampleFunc().toString() }
/** Gets the primary location of this element. */
Location getLocation() { result = exampleFunc().getLocation() }
}
/**
* Gets the absolute path of a known location or "~" for an unknown location. This ensures that
* expressions with unknown locations are ordered after expressions with known locations when
* selecting an example expression for a HashConsExpr value.
*/
private string exampleLocationString(Location l) {
if l instanceof UnknownLocation then result = "~" else result = l.getFile().getAbsolutePath()
}
private predicate analyzableIntLiteral(Literal e) {
strictcount(e.getValue().toInt()) = 1 and
strictcount(e.getUnspecifiedType()) = 1 and
e.getUnspecifiedType() instanceof IntegralType
}
private predicate mk_IntLiteral(int val, Type t, Expr e) {
analyzableIntLiteral(e) and
val = e.getValue().toInt() and
t = e.getUnspecifiedType()
}
private predicate analyzableEnumConstantAccess(EnumConstantAccess e) {
strictcount(e.getValue().toInt()) = 1 and
strictcount(e.getUnspecifiedType()) = 1 and
e.getUnspecifiedType() instanceof Enum
}
private predicate mk_EnumConstantAccess(EnumConstant val, Type t, Expr e) {
analyzableEnumConstantAccess(e) and
val = e.(EnumConstantAccess).getTarget() and
t = e.getUnspecifiedType()
}
private predicate analyzableFloatLiteral(Literal e) {
strictcount(e.getValue().toFloat()) = 1 and
strictcount(e.getUnspecifiedType()) = 1 and
e.getUnspecifiedType() instanceof FloatingPointType
}
private predicate mk_FloatLiteral(float val, Type t, Expr e) {
analyzableFloatLiteral(e) and
val = e.getValue().toFloat() and
t = e.getUnspecifiedType()
}
private predicate analyzableNullptr(NullValue e) {
strictcount(e.getUnspecifiedType()) = 1 and
e.getType() instanceof NullPointerType
}
private predicate mk_Nullptr(Expr e) { analyzableNullptr(e) }
private predicate analyzableStringLiteral(Literal e) {
strictcount(e.getValue()) = 1 and
strictcount(e.getUnspecifiedType()) = 1 and
e.getUnspecifiedType().(ArrayType).getBaseType() instanceof CharType
}
private predicate mk_StringLiteral(string val, Type t, Expr e) {
analyzableStringLiteral(e) and
val = e.getValue() and
t = e.getUnspecifiedType() and
t.(ArrayType).getBaseType() instanceof CharType
}
private predicate analyzableDotFieldAccess(DotFieldAccess access) {
strictcount(access.getTarget()) = 1 and
strictcount(access.getQualifier().getFullyConverted()) = 1
}
private predicate mk_DotFieldAccess(HashConsExpr qualifier, Field target, DotFieldAccess access) {
analyzableDotFieldAccess(access) and
target = access.getTarget() and
qualifier = hashConsExpr(access.getQualifier().getFullyConverted())
}
private predicate analyzablePointerFieldAccess(PointerFieldAccess access) {
strictcount(access.getTarget()) = 1 and
strictcount(access.getQualifier().getFullyConverted()) = 1
}
private predicate mk_PointerFieldAccess(
HashConsExpr qualifier, Field target, PointerFieldAccess access
) {
analyzablePointerFieldAccess(access) and
target = access.getTarget() and
qualifier = hashConsExpr(access.getQualifier().getFullyConverted())
}
private predicate analyzableImplicitThisFieldAccess(ImplicitThisFieldAccess access) {
strictcount(access.getTarget()) = 1 and
strictcount(access.getEnclosingFunction()) = 1
}
private predicate mk_ImplicitThisFieldAccess(
Function fcn, Field target, ImplicitThisFieldAccess access
) {
analyzableImplicitThisFieldAccess(access) and
target = access.getTarget() and
fcn = access.getEnclosingFunction()
}
private predicate analyzableVariable(VariableAccess access) {
not access instanceof FieldAccess and
strictcount(access.getTarget()) = 1
}
/**
* Gets the name of a variable.
*
* Extracted for performance reasons, to avoid magic, which was causing performance issues in getParameter(int i).
*/
pragma[nomagic]
private string getVariableName(Variable v) { result = v.getName() }
/* Note: This changed from the original HashCons module to be able to find structural equivalent expression. */
private predicate mk_Variable(Type t, string name, VariableAccess access) {
analyzableVariable(access) and
exists(Variable v |
v = access.getTarget() and
t = v.getUnspecifiedType() and
name = getVariableName(v)
)
}
private predicate analyzableConversion(Conversion conv) {
strictcount(conv.getUnspecifiedType()) = 1 and
strictcount(conv.getExpr()) = 1
}
private predicate mk_Conversion(Type t, HashConsExpr child, Conversion conv) {
analyzableConversion(conv) and
t = conv.getUnspecifiedType() and
child = hashConsExpr(conv.getExpr())
}
private predicate analyzableBinaryOp(BinaryOperation op) {
strictcount(op.getLeftOperand().getFullyConverted()) = 1 and
strictcount(op.getRightOperand().getFullyConverted()) = 1 and
strictcount(op.getOperator()) = 1
}
private predicate mk_BinaryOp(
HashConsExpr lhs, HashConsExpr rhs, string opname, BinaryOperation op
) {
analyzableBinaryOp(op) and
lhs = hashConsExpr(op.getLeftOperand().getFullyConverted()) and
rhs = hashConsExpr(op.getRightOperand().getFullyConverted()) and
opname = op.getOperator()
}
private predicate analyzableUnaryOp(UnaryOperation op) {
not op instanceof PointerDereferenceExpr and
strictcount(op.getOperand().getFullyConverted()) = 1 and
strictcount(op.getOperator()) = 1
}
private predicate mk_UnaryOp(HashConsExpr child, string opname, UnaryOperation op) {
analyzableUnaryOp(op) and
child = hashConsExpr(op.getOperand().getFullyConverted()) and
opname = op.getOperator()
}
private predicate analyzableThisExpr(ThisExpr thisExpr) {
strictcount(thisExpr.getEnclosingFunction()) = 1
}
private predicate mk_ThisExpr(Function fcn, ThisExpr thisExpr) {
analyzableThisExpr(thisExpr) and
fcn = thisExpr.getEnclosingFunction()
}
private predicate analyzableArrayAccess(ArrayExpr ae) {
strictcount(ae.getArrayBase().getFullyConverted()) = 1 and
strictcount(ae.getArrayOffset().getFullyConverted()) = 1
}
private predicate mk_ArrayAccess(HashConsExpr base, HashConsExpr offset, ArrayExpr ae) {
analyzableArrayAccess(ae) and
base = hashConsExpr(ae.getArrayBase().getFullyConverted()) and
offset = hashConsExpr(ae.getArrayOffset().getFullyConverted())
}
private predicate analyzablePointerDereferenceExpr(PointerDereferenceExpr deref) {
strictcount(deref.getOperand().getFullyConverted()) = 1
}
private predicate mk_Deref(HashConsExpr p, PointerDereferenceExpr deref) {
analyzablePointerDereferenceExpr(deref) and
p = hashConsExpr(deref.getOperand().getFullyConverted())
}
private predicate analyzableNonmemberFunctionCall(FunctionCall fc) {
forall(int i | i in [0 .. fc.getNumberOfArguments() - 1] |
strictcount(fc.getArgument(i).getFullyConverted()) = 1
) and
strictcount(fc.getTarget()) = 1 and
not exists(fc.getQualifier())
}
private predicate mk_NonmemberFunctionCall(Function fcn, HC_Args args, FunctionCall fc) {
fc.getTarget() = fcn and
analyzableNonmemberFunctionCall(fc) and
(
exists(HashConsExpr head, HC_Args tail |
mk_ArgConsInner(head, tail, fc.getNumberOfArguments() - 1, args, fc)
)
or
fc.getNumberOfArguments() = 0 and
args = HC_EmptyArgs()
)
}
private predicate analyzableExprCall(ExprCall ec) {
forall(int i | i in [0 .. ec.getNumberOfArguments() - 1] |
strictcount(ec.getArgument(i).getFullyConverted()) = 1
) and
strictcount(ec.getExpr().getFullyConverted()) = 1
}
private predicate mk_ExprCall(HashConsExpr hc, HC_Args args, ExprCall ec) {
hc.getAnExpr() = ec.getExpr() and
(
exists(HashConsExpr head, HC_Args tail |
mk_ArgConsInner(head, tail, ec.getNumberOfArguments() - 1, args, ec)
)
or
ec.getNumberOfArguments() = 0 and
args = HC_EmptyArgs()
)
}
private predicate analyzableMemberFunctionCall(FunctionCall fc) {
forall(int i | i in [0 .. fc.getNumberOfArguments() - 1] |
strictcount(fc.getArgument(i).getFullyConverted()) = 1
) and
strictcount(fc.getTarget()) = 1 and
strictcount(fc.getQualifier().getFullyConverted()) = 1
}
private predicate mk_MemberFunctionCall(
Function fcn, HashConsExpr qual, HC_Args args, FunctionCall fc
) {
fc.getTarget() = fcn and
analyzableMemberFunctionCall(fc) and
hashConsExpr(fc.getQualifier().getFullyConverted()) = qual and
(
exists(HashConsExpr head, HC_Args tail |
mk_ArgConsInner(head, tail, fc.getNumberOfArguments() - 1, args, fc)
)
or
fc.getNumberOfArguments() = 0 and
args = HC_EmptyArgs()
)
}
private predicate analyzableCall(Call c) {
analyzableNonmemberFunctionCall(c)
or
analyzableMemberFunctionCall(c)
or
analyzableExprCall(c)
}
/**
* Holds if `fc` is a call to `fcn`, `fc`'s first `i` arguments have hash-cons
* `list`, and `fc`'s argument at index `i` has hash-cons `hc`.
*/
private predicate mk_ArgCons(HashConsExpr hc, int i, HC_Args list, Call c) {
analyzableCall(c) and
hc = hashConsExpr(c.getArgument(i).getFullyConverted()) and
(
exists(HashConsExpr head, HC_Args tail |
mk_ArgConsInner(head, tail, i - 1, list, c) and
i > 0
)
or
i = 0 and
list = HC_EmptyArgs()
)
}
// avoid a join ordering issue
pragma[noopt]
private predicate mk_ArgConsInner(HashConsExpr head, HC_Args tail, int i, HC_Args list, Call c) {
list = HC_ArgCons(head, i, tail) and
mk_ArgCons(head, i, tail, c)
}
/**
* The 0th argument of an allocator call in a new expression is always an error expression;
* this works around it
*/
private predicate analyzableAllocatorArgZero(ErrorExpr e) {
exists(NewOrNewArrayExpr new |
new.getAllocatorCall().getChild(0) = e and
strictcount(new.getUnspecifiedType()) = 1
) and
strictcount(NewOrNewArrayExpr new | new.getAllocatorCall().getChild(0) = e) = 1
}
private predicate mk_AllocatorArgZero(Type t, ErrorExpr e) {
analyzableAllocatorArgZero(e) and
exists(NewOrNewArrayExpr new |
new.getAllocatorCall().getChild(0) = e and
t = new.getUnspecifiedType()
)
}
private predicate mk_HasInit(HashConsExpr hc, NewOrNewArrayExpr new) {
hc = hashConsExpr(new.(NewExpr).getInitializer().getFullyConverted()) or
hc = hashConsExpr(new.(NewArrayExpr).getInitializer().getFullyConverted())
}
private predicate mk_HasAlloc(HashConsExpr hc, NewOrNewArrayExpr new) {
hc = hashConsExpr(new.(NewExpr).getAllocatorCall().getFullyConverted()) or
hc = hashConsExpr(new.(NewArrayExpr).getAllocatorCall().getFullyConverted())
}
private predicate mk_HasExtent(HashConsExpr hc, NewArrayExpr new) {
hc = hashConsExpr(new.(NewArrayExpr).getExtent().getFullyConverted())
}
private predicate analyzableNewExpr(NewExpr new) {
strictcount(new.getAllocatedType().getUnspecifiedType()) = 1 and
count(new.getAllocatorCall().getFullyConverted()) <= 1 and
count(new.getInitializer().getFullyConverted()) <= 1
}
private predicate mk_NewExpr(Type t, HC_Alloc alloc, HC_Init init, NewExpr new) {
analyzableNewExpr(new) and
t = new.getAllocatedType().getUnspecifiedType() and
(
alloc = HC_HasAlloc(hashConsExpr(new.getAllocatorCall().getFullyConverted()))
or
not exists(new.getAllocatorCall().getFullyConverted()) and
alloc = HC_NoAlloc()
) and
(
init = HC_HasInit(hashConsExpr(new.getInitializer().getFullyConverted()))
or
not exists(new.getInitializer().getFullyConverted()) and
init = HC_NoInit()
)
}
private predicate analyzableNewArrayExpr(NewArrayExpr new) {
strictcount(new.getAllocatedType().getUnspecifiedType()) = 1 and
count(new.getAllocatorCall().getFullyConverted()) <= 1 and
count(new.getInitializer().getFullyConverted()) <= 1 and
count(new.(NewArrayExpr).getExtent().getFullyConverted()) <= 1
}
private predicate mk_NewArrayExpr(
Type t, HC_Alloc alloc, HC_Extent extent, HC_Init init, NewArrayExpr new
) {
analyzableNewArrayExpr(new) and
t = new.getAllocatedType() and
(
alloc = HC_HasAlloc(hashConsExpr(new.getAllocatorCall().getFullyConverted()))
or
not exists(new.getAllocatorCall().getFullyConverted()) and
alloc = HC_NoAlloc()
) and
(
init = HC_HasInit(hashConsExpr(new.getInitializer().getFullyConverted()))
or
not exists(new.getInitializer().getFullyConverted()) and
init = HC_NoInit()
) and
(
extent = HC_HasExtent(hashConsExpr(new.getExtent().getFullyConverted()))
or
not exists(new.getExtent().getFullyConverted()) and
extent = HC_NoExtent()
)
}
private predicate analyzableDeleteExpr(DeleteExpr e) {
strictcount(e.getAChild().getFullyConverted()) = 1
}
private predicate mk_DeleteExpr(HashConsExpr hc, DeleteExpr e) {
analyzableDeleteExpr(e) and
hc = hashConsExpr(e.getAChild().getFullyConverted())
}
private predicate analyzableDeleteArrayExpr(DeleteArrayExpr e) {
strictcount(e.getAChild().getFullyConverted()) = 1
}
private predicate mk_DeleteArrayExpr(HashConsExpr hc, DeleteArrayExpr e) {
analyzableDeleteArrayExpr(e) and
hc = hashConsExpr(e.getAChild().getFullyConverted())
}
private predicate analyzableSizeofType(SizeofTypeOperator e) {
strictcount(e.getUnspecifiedType()) = 1 and
strictcount(e.getTypeOperand()) = 1
}
private predicate mk_SizeofType(Type t, SizeofTypeOperator e) {
analyzableSizeofType(e) and
t = e.getTypeOperand()
}
private predicate analyzableSizeofExpr(Expr e) {
e instanceof SizeofExprOperator and
strictcount(e.getAChild().getFullyConverted()) = 1
}
private predicate mk_SizeofExpr(HashConsExpr child, SizeofExprOperator e) {
analyzableSizeofExpr(e) and
child = hashConsExpr(e.getAChild())
}
private predicate analyzableUuidofOperator(UuidofOperator e) {
strictcount(e.getTypeOperand()) = 1
}
private predicate mk_UuidofOperator(Type t, UuidofOperator e) {
analyzableUuidofOperator(e) and
t = e.getTypeOperand()
}
private predicate analyzableTypeidType(TypeidOperator e) {
count(e.getAChild()) = 0 and
strictcount(e.getResultType()) = 1
}
private predicate mk_TypeidType(Type t, TypeidOperator e) {
analyzableTypeidType(e) and
t = e.getResultType()
}
private predicate analyzableTypeidExpr(Expr e) {
e instanceof TypeidOperator and
strictcount(e.getAChild().getFullyConverted()) = 1
}
private predicate mk_TypeidExpr(HashConsExpr child, TypeidOperator e) {
analyzableTypeidExpr(e) and
child = hashConsExpr(e.getAChild())
}
private predicate analyzableAlignofType(AlignofTypeOperator e) {
strictcount(e.getUnspecifiedType()) = 1 and
strictcount(e.getTypeOperand()) = 1
}
private predicate mk_AlignofType(Type t, AlignofTypeOperator e) {
analyzableAlignofType(e) and
t = e.getTypeOperand()
}
private predicate analyzableAlignofExpr(AlignofExprOperator e) {
strictcount(e.getExprOperand()) = 1
}
private predicate mk_AlignofExpr(HashConsExpr child, AlignofExprOperator e) {
analyzableAlignofExpr(e) and
child = hashConsExpr(e.getAChild())
}
/**
* Gets the hash cons of field initializer expressions [0..i), where i > 0, for
* the class aggregate literal `cal` of type `c`, where `head` is the hash cons
* of the i'th initializer expression.
*/
HC_Fields aggInitExprsUpTo(ClassAggregateLiteral cal, Class c, int i) {
exists(Field f, HashConsExpr head, HC_Fields tail |
result = HC_FieldCons(c, i - 1, f, head, tail) and
mk_FieldCons(c, i - 1, f, head, tail, cal)
)
}
private predicate mk_FieldCons(
Class c, int i, Field f, HashConsExpr hc, HC_Fields hcf, ClassAggregateLiteral cal
) {
analyzableClassAggregateLiteral(cal) and
cal.getUnspecifiedType() = c and