-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathStmt.qll
1470 lines (1266 loc) · 36.6 KB
/
Stmt.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
/**
* Provides all statement classes.
*
* All statements have the common base class `Stmt`.
*/
import Element
import Location
import Member
import exprs.Expr
private import semmle.code.csharp.ExprOrStmtParent
private import semmle.code.csharp.frameworks.System
private import TypeRef
/**
* A statement.
*
* Either a block statement (`BlockStmt`), an expression statement (`ExprStmt`),
* a selection statement (`SelectionStmt`), a labeled statement (`LabeledStmt`),
* a loop statement (`LoopStmt`), a jump statement (`JumpStmt`), a `try` statement
* (`TryStmt`), a `catch` clause (`CatchClause`), a `checked` statement
* (`CheckedStmt`), an `unchecked` statement (`UncheckedStmt`), a `lock`
* statement (`LockStmt`), a `using` statement (`UsingStmt`), a local
* variable declaration statement (`LocalVariableDeclStmt`), an empty statement
* (`EmptyStmt`), an `unsafe` statement (`UnsafeStmt`), or a `fixed` statement
* (`FixedStmt`).
*/
class Stmt extends ControlFlowElement, @stmt {
/** Gets the enclosing callable of this statement. */
override Callable getEnclosingCallable() { enclosingCallable(this, result) }
override string toString() { result = "Statement" }
override Location getALocation() { stmt_location(this, result) }
/** Holds if this statement is a global statement. */
predicate isGlobal() { this.getParent().(BlockStmt).isGlobalStatementContainer() }
/**
* Gets the singleton statement contained in this statement, by removing
* enclosing block statements.
*
* For example converts `{ { return x; } }` to `return x;`.
*/
Stmt stripSingletonBlocks() { result = this }
/** Holds if this statement is compiler generated. */
predicate isCompilerGenerated() { compiler_generated(this) }
}
/**
* A block statement, for example
*
* ```csharp
* {
* ...
* }
* ```
*/
class BlockStmt extends Stmt, @block_stmt {
/** Gets a statement in this block, if any. */
Stmt getAStmt() { result.getParent() = this }
/** Gets the `n`th statement in this block. */
Stmt getStmt(int n) { result = this.getChild(n) }
/** Gets the number of statements in this block. */
int getNumberOfStmts() { result = count(this.getAStmt()) }
/** Gets the first statement in this block, if any. */
Stmt getFirstStmt() { result = this.getStmt(0) }
/** Gets the last statement in this block, if any. */
Stmt getLastStmt() { result = this.getStmt(this.getNumberOfStmts() - 1) }
/** Holds if this block is an empty block with no statements. */
predicate isEmpty() { not exists(this.getAStmt()) }
/** Holds if this block is the container of the global statements. */
predicate isGlobalStatementContainer() {
this.getEnclosingCallable().hasFullyQualifiedName("Program", "<Main>$")
}
override Stmt stripSingletonBlocks() {
if this.getNumberOfStmts() = 1
then result = this.getAChildStmt().stripSingletonBlocks()
else result = this
}
override string toString() { result = "{...}" }
override string getAPrimaryQlClass() { result = "BlockStmt" }
}
/**
* An expression statement, for example `M1()` on line 5
*
* ```csharp
* class C {
* int M1() { ... }
*
* void M2() {
* M1();
* }
* }
* ```
*/
class ExprStmt extends Stmt, @expr_stmt {
/** Gets the expression in this expression statement. */
Expr getExpr() { result.getParent() = this }
override string toString() { result = "...;" }
override string getAPrimaryQlClass() { result = "ExprStmt" }
}
/**
* A conditional statement.
*
* Either an `if` statement (`IfStmt`) or a `switch` statement (`SwitchStmt`).
*/
class SelectionStmt extends Stmt, @cond_stmt {
/** Gets the condition of this selection statement. */
Expr getCondition() { none() }
}
/**
* An `if` statement, for example
*
* ```csharp
* if (x==0) {
* ...
* } else {
* ...
* }
* ```
*
* The `else` part is optional.
*/
class IfStmt extends SelectionStmt, @if_stmt {
override Expr getCondition() { result = this.getChild(0) }
/** Gets the `then` (true) branch of this `if` statement. */
Stmt getThen() { result = this.getChild(1) }
/** Gets the `else` (false) branch of this `if` statement, if any. */
Stmt getElse() { result = this.getChild(2) }
override string toString() { result = "if (...) ..." }
override string getAPrimaryQlClass() { result = "IfStmt" }
}
/**
* A `switch` statement, for example
*
* ```csharp
* switch (instruction) {
* ...
* }
* ```
*/
class SwitchStmt extends SelectionStmt, Switch, @switch_stmt {
override Expr getExpr() { result = this.getChild(0) }
override Expr getCondition() { result = this.getExpr() }
/**
* Gets the `i`th `case` statement in the body of this `switch` statement.
*
* Example:
*
* ```csharp
* switch (x) {
* case "abc": // i = 0
* return 0;
* case int i when i > 0: // i = 1
* return 1;
* case string s: // i = 2
* Console.WriteLine(s);
* return 2;
* default: // i = 3
* return 3;
* }
* ```
* Note that this reorders the `default` case to always be at the end.
*/
override CaseStmt getCase(int i) { result = SwithStmtInternal::getCase(this, i) }
/** Gets a case of this `switch` statement. */
override CaseStmt getACase() { result = this.getCase(_) }
/** Gets a constant value case of this `switch` statement, if any. */
ConstCase getAConstCase() { result = this.getACase() }
/** Gets the default case of this `switch` statement, if any. */
DefaultCase getDefaultCase() { result = this.getACase() }
override string toString() { result = "switch (...) {...}" }
override string getAPrimaryQlClass() { result = "SwitchStmt" }
/**
* Gets the `i`th statement in the body of this `switch` statement.
*
* Example:
*
* ```csharp
* switch (x) {
* case "abc": // i = 0
* return 0;
* case int i when i > 0: // i = 1
* return 1;
* case string s: // i = 2
* Console.WriteLine(s);
* return 2; // i = 3
* default: // i = 4
* return 3; // i = 5
* }
* ```
*
* Note that each non-`default` case is a labeled statement, so the statement
* that follows is a child of the labeled statement, and not the `switch` block.
*/
Stmt getStmt(int i) { result = SwithStmtInternal::getStmt(this, i) }
/** Gets a statement in the body of this `switch` statement. */
Stmt getAStmt() { result = this.getStmt(_) }
}
cached
private module SwithStmtInternal {
cached
CaseStmt getCase(SwitchStmt ss, int i) {
exists(int index, int rankIndex |
caseIndex(ss, result, index) and
rankIndex = i + 1 and
index = rank[rankIndex](int j, CaseStmt cs | caseIndex(ss, cs, j) | j)
)
}
/** Implicitly reorder case statements to put the default case last if needed. */
private predicate caseIndex(SwitchStmt ss, CaseStmt case, int index) {
exists(int i | case = ss.getChildStmt(i) |
if case instanceof DefaultCase
then index = max(int j | exists(ss.getChildStmt(j))) + 1
else index = i
)
}
cached
Stmt getStmt(SwitchStmt ss, int i) {
exists(int index, int rankIndex |
result = ss.getChildStmt(index) and
rankIndex = i + 1 and
index =
rank[rankIndex](int j, Stmt s |
// `getChild` includes both labeled statements and the targeted
// statements of labeled statement as separate children, but we
// only want the labeled statement
s = getLabeledStmt(ss, j)
|
j
)
)
}
private Stmt getLabeledStmt(SwitchStmt ss, int i) {
result = ss.getChildStmt(i) and
not result = any(CaseStmt cs).getBody()
}
}
/** A `case` statement. */
class CaseStmt extends Case, @case_stmt {
override Expr getExpr() { result = any(SwitchStmt ss | ss.getACase() = this).getExpr() }
override PatternExpr getPattern() { result = this.getChild(0) }
override Stmt getBody() {
exists(int i |
this = this.getParent().getChild(i) and
result = this.getParent().getChild(i + 1)
)
}
/**
* Gets the condition on this case, if any. For example, the type case on line 3
* has no condition, and the type case on line 4 has condition `s.Length > 0`, in
*
* ```csharp
* switch(p)
* {
* case int i:
* case string s when s.Length > 0:
* break;
* ...
* }
* ```
*/
override Expr getCondition() { result = this.getChild(1) }
/** Gets the `switch` statement that this `case` statement belongs to. */
SwitchStmt getSwitchStmt() { result.getACase() = this }
override string toString() { result = "case ...:" }
override string getAPrimaryQlClass() { result = "CaseStmt" }
}
/**
* A constant case of a `switch` statement, for example `case OpCode.Nop:`
* on line 2 in
*
* ```csharp
* switch (instruction) {
* case OpCode.Nop: ...
* default: ...
* }
* ```
*/
class ConstCase extends CaseStmt, LabeledStmt {
private ConstantPatternExpr p;
ConstCase() { p = this.getPattern() }
override string getLabel() { result = p.getValue() }
override string toString() { result = CaseStmt.super.toString() }
override string getAPrimaryQlClass() { result = "ConstCase" }
}
/**
* A default case of a `switch` statement, for example `default:` on
* line 3 in
*
* ```csharp
* switch (instruction) {
* case OpCode.Nop: ...
* default: ...
* }
* ```
*/
class DefaultCase extends CaseStmt, LabeledStmt {
DefaultCase() { not exists(Expr e | e.getParent() = this) }
override string getLabel() { result = "default" }
override string toString() { result = "default:" }
override string getAPrimaryQlClass() { result = "DefaultCase" }
}
/**
* A loop statement.
*
* Either a `while` statement (`WhileStmt`), a `do`-`while` statement
* (`DoStmt`), a `for` statement (`ForStmt`), or a `foreach` statement
* (`ForeachStmt`).
*/
class LoopStmt extends Stmt, @loop_stmt {
/** Gets the body of this loop statement. */
Stmt getBody() { result.getParent() = this }
/** Gets the condition of this loop statement, if any. */
Expr getCondition() { none() }
}
/**
* A `while` statement, for example
*
* ```csharp
* while (remaining > 0) {
* ...
* }
* ```
*/
class WhileStmt extends LoopStmt, @while_stmt {
override Expr getCondition() { result.getParent() = this }
override string toString() { result = "while (...) ..." }
override string getAPrimaryQlClass() { result = "WhileStmt" }
}
/**
* A `do`-`while` statement, for example
*
* ```csharp
* do {
* ...
* }
* while (remaining > 0);
* ```
*/
class DoStmt extends LoopStmt, @do_stmt {
override Expr getCondition() { result.getParent() = this }
override string toString() { result = "do ... while (...);" }
override string getAPrimaryQlClass() { result = "DoStmt" }
}
/**
* A `for` loop, for example
*
* ```csharp
* for (int i = 0; i < 10; i++) {
* ...
* }
* ```
*/
class ForStmt extends LoopStmt, @for_stmt {
/**
* Gets an initializer expression of this `for` loop, if any.
*
* For example, `i = 0` in
*
* ```csharp
* for (int i = 0; i < 10; i++) {
* ...
* }
* ```
*/
Expr getAnInitializer() { result = this.getInitializer(_) }
/**
* Gets the `n`th initializer expression of this `for` loop
* (starting at index 0).
*
* For example, the second (`n = 1`) initializer is `j = 10` in
*
* ```csharp
* for (int i = 0, j = 10; i < j; i++) {
* ...
* }
* ```
*/
Expr getInitializer(int n) {
exists(int i | result = this.getChild(i) and n = -1 - i and i <= -1)
}
override Expr getCondition() { result = this.getChild(0) }
/**
* Gets an update expression of this `for` loop, if any.
*
* For example, `i++` in
*
* ```csharp
* for (int i = 0; i < 10; i++) {
* ...
* }
* ```
*/
Expr getAnUpdate() { result = this.getUpdate(_) }
/**
* Gets the `n`th update expression of this `for` loop (starting at index 0).
*
* For example, the second (`n = 1`) update expression is `j--` in
*
* ```csharp
* for (int i = 0, j = 10; i < j; i++, j--) {
* ...
* }
* ```
*/
Expr getUpdate(int n) { exists(int i | result = this.getChild(i) and n = i - 1 and i >= 1) }
override string toString() { result = "for (...;...;...) ..." }
override string getAPrimaryQlClass() { result = "ForStmt" }
}
/**
* A `foreach` loop, for example
*
* ```csharp
* foreach (var item in items) {
* ...
* }
* ```
*/
class ForeachStmt extends LoopStmt, @foreach_stmt {
/**
* Gets the local variable of this `foreach` loop, if any.
*
* For example, `item` in
*
* ```csharp
* foreach (var item in items) {
* ...
* }
* ```
*/
LocalVariable getVariable() { result = this.getVariableDeclExpr().getVariable() }
/**
* Gets the local variable declaration of this `foreach` loop, if any.
*
* For example, `var item` in
*
* ```csharp
* foreach (var item in items) {
* ...
* }
* ```
*/
LocalVariableDeclExpr getVariableDeclExpr() { result = this.getChild(0) }
/**
* Gets the `i`th local variable declaration of this `foreach` loop.
*
* For example, `int a` is the 0th local variable declaration in
*
* ```csharp
* foreach ((int a, int b) in items) {
* ...
* }
* ```
*/
LocalVariableDeclExpr getVariableDeclExpr(int i) {
result = this.getVariableDeclTuple().getArgument(i)
or
i = 0 and result = this.getChild(0)
}
/**
* Gets the local variable declaration tuple of this `foreach` loop, if any.
* For example, `(int a, int b)` in
*
* ```csharp
* foreach ((int a, int b) in items) {
* ...
* }
* ```
*/
TupleExpr getVariableDeclTuple() { result = this.getChild(0) }
/**
* Gets the `i`th local variable of this `foreach` loop.
*
* For example, `a` is the 0th local variable in
*
* ```csharp
* foreach ((int a, int b) in items) {
* ...
* }
* ```
*/
LocalVariable getVariable(int i) { result = this.getVariableDeclExpr(i).getVariable() }
/**
* Gets a local variable of this `foreach` loop.
*
* For example, `a` and `b` in
*
* ```csharp
* foreach ((int a, int b) in items) {
* ...
* }
* ```
*/
LocalVariable getAVariable() { result = this.getVariable(_) }
/**
* Gets a local variable declaration of this `foreach` loop.
*
* For example, `int a` and `int b` in
*
* ```csharp
* foreach ((int a, int b) in items) {
* ...
* }
* ```
*/
LocalVariableDeclExpr getAVariableDeclExpr() { result = this.getVariableDeclExpr(_) }
override Expr getCondition() { none() }
/**
* Gets the expression that this `foreach` loop iterates over.
*
* For example, `items` in
*
* ```csharp
* foreach (var item in items) {
* ...
* }
* ```
*/
Expr getIterableExpr() { result = this.getChild(1) }
/** Gets the called `GetEnumerator` method. */
Method getGetEnumerator() { foreach_stmt_desugar(this, result, 1) }
/** Gets the called `MoveNext` or `MoveNextAsync` method. */
Method getMoveNext() { foreach_stmt_desugar(this, result, 3) }
/** Gets the called `Dispose` or `DisposeAsync` method, if any. */
Method getDispose() { foreach_stmt_desugar(this, result, 4) }
/** Gets the called `Current` property. */
Property getCurrent() { foreach_stmt_desugar(this, result, 2) }
/**
* Gets the intermediate type to which the `Current` property is converted before
* being converted to the iteration variable type.
*/
Type getElementType() { foreach_stmt_desugar(this, result, 5) }
/** Holds if this `foreach` statement is asynchronous. */
predicate isAsync() { foreach_stmt_info(this, 2) }
override string toString() { result = "foreach (... ... in ...) ..." }
override string getAPrimaryQlClass() { result = "ForeachStmt" }
}
/**
* A statement that changes the control flow and jumps to another statement.
*
* Either a `break` statement (`BreakStmt`), a `continue` statement (`ContinueStmt`),
* a `goto` statement (`GotoStmt`), a `throw` statement (`ThrowStmt`), a `return` statement
* (`ReturnStmt`), or a `yield` statement (`YieldStmt`).
*/
class JumpStmt extends Stmt, @jump_stmt { }
/**
* A `break` statement, for example line 4 in
*
* ```csharp
* while (true) {
* ...
* if (done)
* break;
* }
* ```
*/
class BreakStmt extends JumpStmt, @break_stmt {
override string toString() { result = "break;" }
override string getAPrimaryQlClass() { result = "BreakStmt" }
}
/**
* A `continue` statement, for example line 4 in
*
* ```csharp
* while (true) {
* ...
* if (!done)
* continue;
* ...
* }
* ```
*/
class ContinueStmt extends JumpStmt, @continue_stmt {
override string toString() { result = "continue;" }
override string getAPrimaryQlClass() { result = "ContinueStmt" }
}
/**
* A `goto` statement.
*
* Either a `goto` label (`GotoLabelStmt`), a `goto case` (`GotoCaseStmt`), or
* a `goto default` (`GotoDefaultStmt`).
*/
class GotoStmt extends JumpStmt, @goto_any_stmt {
/** Gets the label that this `goto` statement jumps to. */
string getLabel() { none() }
}
/**
* A `goto` statement that jumps to a labeled statement, for example line 4 in
*
* ```csharp
* while (true) {
* ...
* if (done)
* goto exit;
* }
* exit: ...
* ```
*/
class GotoLabelStmt extends GotoStmt, @goto_stmt {
override string getLabel() { exprorstmt_name(this, result) }
override string toString() { result = "goto ...;" }
/** Gets the target statement that this `goto` statement jumps to. */
LabeledStmt getTarget() {
result.getEnclosingCallable() = this.getEnclosingCallable() and
result.getLabel() = this.getLabel()
}
override string getAPrimaryQlClass() { result = "GotoLabelStmt" }
}
/**
* A `goto case` statement that jumps to a `switch` case.
*
* For example, line 5 in
*
* ```csharp
* switch (x) {
* case 0 :
* return 1;
* case 1 :
* goto case 0;
* default :
* return -1;
* }
* ```
*/
class GotoCaseStmt extends GotoStmt, @goto_case_stmt {
/** Gets the constant expression that this `goto case` statement jumps to. */
Expr getExpr() { result = this.getChild(0) }
override string getLabel() { result = this.getExpr().getValue() }
override string toString() { result = "goto case ...;" }
override string getAPrimaryQlClass() { result = "GotoCaseStmt" }
}
/**
* A `goto default` statement that jumps to the `default` case.
*
* For example, line 5 in
*
* ```csharp
* switch (x) {
* case 0 :
* return 1;
* case 1 :
* goto default;
* default :
* return -1;
* }
* ```
*/
class GotoDefaultStmt extends GotoStmt, @goto_default_stmt {
override string toString() { result = "goto default;" }
override string getLabel() { result = "default" }
override string getAPrimaryQlClass() { result = "GotoDefaultStmt" }
}
/**
* A `throw` statement, for example line 3 in
*
* ```csharp
* void M(string s) {
* if (s == null)
* throw new ArgumentNullException(nameof(s));
* }
* ```
*/
class ThrowStmt extends JumpStmt, ThrowElement, @throw_stmt {
override string toString() { result = "throw ...;" }
override ExceptionClass getThrownExceptionType() {
result = ThrowElement.super.getThrownExceptionType()
or
result = this.getRethrowParent().(CatchClause).getCaughtExceptionType()
}
private ControlFlowElement getRethrowParent() {
result = this and not exists(this.getExpr())
or
exists(ControlFlowElement mid |
mid = this.getRethrowParent() and
not mid instanceof CatchClause and
result = mid.getParent()
)
}
override string getAPrimaryQlClass() { result = "ThrowStmt" }
}
/**
* A class that derives from `System.Exception`,
* and may be thrown as an exception.
*/
class ExceptionClass extends Class {
ExceptionClass() { this.getBaseClass*() instanceof SystemExceptionClass }
}
/**
* A `return` statement, for example line 2 in
*
* ```csharp
* int M() {
* return 0;
* }
* ```
*/
class ReturnStmt extends JumpStmt, @return_stmt {
/** Gets the expression being returned, if any. */
Expr getExpr() { result.getParent() = this }
override string toString() { result = "return ...;" }
override string getAPrimaryQlClass() { result = "ReturnStmt" }
}
/**
* A `yield` statement.
*
* Either a `yield return` statement (`YieldReturnStmt`) or a
* `yield break` statement (`YieldBreakStmt`).
*/
class YieldStmt extends JumpStmt, @yield_stmt {
/** Gets the expression being yielded, if any. */
Expr getExpr() { result.getParent() = this }
}
/**
* A `yield break` statement, for example line 6 in
*
* ```csharp
* IEnumerable<int> DownFrom(int i) {
* while (true) {
* if (i > 0)
* yield return i--;
* else
* yield break;
* }
* }
* ```
*/
class YieldBreakStmt extends YieldStmt {
YieldBreakStmt() { not exists(this.getExpr()) }
override string toString() { result = "yield break;" }
override string getAPrimaryQlClass() { result = "YieldBreakStmt" }
}
/**
* A `yield return` statement, for example line 4 in
*
* ```csharp
* IEnumerable<int> DownFrom(int i) {
* while (true) {
* if (i > 0)
* yield return i--;
* else
* yield break;
* }
* }
* ```
*/
class YieldReturnStmt extends YieldStmt {
YieldReturnStmt() { exists(this.getExpr()) }
override string toString() { result = "yield return ...;" }
override string getAPrimaryQlClass() { result = "YieldReturnStmt" }
}
bindingset[cfe1, cfe2]
pragma[inline_late]
private predicate sameCallable(ControlFlowElement cfe1, ControlFlowElement cfe2) {
cfe1.getEnclosingCallable() = cfe2.getEnclosingCallable()
}
/**
* A `try` statement, for example
*
* ```csharp
* try {
* ...
* }
* catch (Exception e) {
* ...
* }
* finally {
* ...
* }
* ```
*/
class TryStmt extends Stmt, @try_stmt {
/** Gets the block of this `try` statement. */
BlockStmt getBlock() { result = this.getChild(0) }
/** Gets a `catch` clause of this `try` statement, if any. */
CatchClause getACatchClause() { result = this.getAChild() }
/** Gets the `n`th catch clause of this `try` statement. */
CatchClause getCatchClause(int n) { exists(int i | result = this.getChild(i) and n = i - 1) }
/** Gets the `finally` block of this `try` statement, if any. */
BlockStmt getFinally() { result = this.getChild(-1) }
/** Holds if this `try` statement has a `finally` block. */
predicate hasFinally() { exists(this.getFinally()) }
override string toString() { result = "try {...} ..." }
override string getAPrimaryQlClass() { result = "TryStmt" }
/** Gets the `catch` clause that handles an exception of type `ex`, if any. */
CatchClause getAnExceptionHandler(ExceptionClass ex) {
result = this.clauseHandlesException(ex, 0)
}
/**
* Holds if catch clause `cc` definitely handles exceptions of type `ex`.
*/
predicate definitelyHandles(ExceptionClass ex, CatchClause cc) {
cc = this.getACatchClause() and
not exists(cc.getFilterClause()) and
(
cc.getCaughtExceptionType() = ex.getBaseClass*()
or
cc instanceof GeneralCatchClause
)
}
private predicate maybeHandles(ExceptionClass ex, CatchClause cc) {
cc = this.getACatchClause() and
cc.getCaughtExceptionType().getBaseClass*() = ex
}
private CatchClause clauseHandlesException(ExceptionClass ex, int n) {
exists(CatchClause clause | clause = this.getCatchClause(n) |
if this.definitelyHandles(ex, clause)
then result = clause
else
if this.maybeHandles(ex, clause)
then
result = clause or
result = this.clauseHandlesException(ex, n + 1)
else
// Does not handle
result = this.clauseHandlesException(ex, n + 1)
)
}
/**
* Gets a control flow element that is tried by this `try` statement.
*
* That is, a child element that is not tried by another nested
* `try` statement.
*/
ControlFlowElement getATriedElement() {
result = this.getBlock()
or
exists(ControlFlowElement mid |
mid = this.getATriedElement() and
not mid instanceof TryStmt and
result = mid.getAChild() and
sameCallable(mid, result)
)
}
}
/**
* A `catch` clause within a `try` statement.
*
* Either a specific `catch` clause (`SpecificCatchClause`) or a
* general `catch` clause (`GeneralCatchClause`).
*/
class CatchClause extends Stmt, @catch {
/** Gets the `try` statement that this `catch` clause belongs to. */
TryStmt getTryStmt() { result.getACatchClause() = this }
/** Gets the block of this `catch` clause. */
BlockStmt getBlock() { result.getParent() = this }
/**
* Gets the type of the exception caught. For example, the type of the exception
* caught on line 4 is `System.IO.IOException` in
*
* ```csharp
* try {
* ...
* }
* catch (System.IO.IOException ex) {
* ...
* }
* ```
*/
ExceptionClass getCaughtExceptionType() {
catch_type(this, result, _)
or
not catch_type(this, any(Type t), _) and
catch_type(this, getTypeRef(result), _)
}
/**
* Gets the `catch` filter clause, if any. For example, the filter expression
* of the catch clause on line 4 is `ex.HResult == 1` in
*