-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathStatement.qll
More file actions
1020 lines (768 loc) · 33.6 KB
/
Statement.qll
File metadata and controls
1020 lines (768 loc) · 33.6 KB
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 classes and predicates for working with Java statements.
*/
overlay[local?]
module;
import Expr
import metrics.MetricStmt
private import semmle.code.java.Overlay
/** A common super-class of all statements. */
class Stmt extends StmtParent, ExprParent, @stmt {
/*abstract*/ override string toString() { result = "stmt" }
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
string pp() { result = "stmt" }
/**
* Gets the immediately enclosing callable (method or constructor)
* whose body contains this statement.
*/
Callable getEnclosingCallable() { stmts(this, _, _, _, result) }
/** Gets the index of this statement as a child of its parent. */
int getIndex() { stmts(this, _, _, result, _) }
/** Gets the parent of this statement. */
StmtParent getParent() { stmts(this, _, result, _, _) }
/**
* Gets the statement containing this statement, if any.
*/
Stmt getEnclosingStmt() {
result = this.getParent() or
result = this.getParent().(SwitchExpr).getEnclosingStmt() or
result = this.getParent().(WhenExpr).getEnclosingStmt()
}
/** Holds if this statement is the child of the specified parent at the specified (zero-based) position. */
predicate isNthChildOf(StmtParent parent, int index) {
this.getParent() = parent and this.getIndex() = index
}
/** Gets the compilation unit in which this statement occurs. */
CompilationUnit getCompilationUnit() { result = this.getFile() }
/** Gets a child of this statement, if any. */
Stmt getAChild() { result.getParent() = this }
/** Gets the basic block in which this statement occurs. */
BasicBlock getBasicBlock() { result.getANode().asStmt() = this }
/** Gets the `ControlFlowNode` corresponding to this statement. */
ControlFlowNode getControlFlowNode() { result.asStmt() = this }
/** Cast this statement to a class that provides access to metrics information. */
MetricStmt getMetrics() { result = this }
/** This statement's Halstead ID (used to compute Halstead metrics). */
string getHalsteadID() { result = "Stmt" }
}
/** A statement parent is any element that can have a statement as its child. */
class StmtParent extends @stmtparent, ExprParent { }
/**
* An error statement.
*
* These may be generated by upgrade or downgrade scripts when databases
* cannot be fully converted.
*/
class ErrorStmt extends Stmt, @errorstmt {
override string toString() { result = "<error stmt>" }
override string getAPrimaryQlClass() { result = "ErrorStmt" }
}
/** A block of statements. */
class BlockStmt extends Stmt, @block {
/** Gets a statement that is an immediate child of this block. */
Stmt getAStmt() { result.getParent() = this }
/** Gets the immediate child statement of this block that occurs at the specified (zero-based) position. */
Stmt getStmt(int index) { result.getIndex() = index and result.getParent() = this }
/** Gets the number of immediate child statements in this block. */
int getNumStmt() { result = count(this.getAStmt()) }
/** Gets the last statement in this block. */
Stmt getLastStmt() { result = this.getStmt(this.getNumStmt() - 1) }
override string pp() { result = "{ ... }" }
override string toString() { result = "{ ... }" }
override string getHalsteadID() { result = "BlockStmt" }
override string getAPrimaryQlClass() { result = "BlockStmt" }
}
/** A block with only a single statement. */
class SingletonBlock extends BlockStmt {
SingletonBlock() { this.getNumStmt() = 1 }
/** Gets the single statement in this block. */
Stmt getStmt() { result = this.getStmt(0) }
}
/**
* A conditional statement, including `if`, `for`,
* `while` and `dowhile` statements.
*/
abstract class ConditionalStmt extends Stmt {
/** Gets the boolean condition of this conditional statement. */
abstract Expr getCondition();
}
/** An `if` statement. */
class IfStmt extends ConditionalStmt, @ifstmt {
/** Gets the boolean condition of this `if` statement. */
override Expr getCondition() { result.isNthChildOf(this, 0) }
/** Gets the `then` branch of this `if` statement. */
Stmt getThen() { result.isNthChildOf(this, 1) }
/** Gets the `else` branch of this `if` statement. */
Stmt getElse() { result.isNthChildOf(this, 2) }
override string pp() {
result = "if (...) " + this.getThen().pp() + " else " + this.getElse().pp()
or
not exists(this.getElse()) and result = "if (...) " + this.getThen().pp()
}
override string toString() { result = "if (...)" }
override string getHalsteadID() { result = "IfStmt" }
override string getAPrimaryQlClass() { result = "IfStmt" }
}
/** A `for` loop. */
class ForStmt extends ConditionalStmt, LoopStmtImpl, @forstmt {
/**
* Gets an initializer expression of the loop.
*
* This may be an assignment expression or a
* local variable declaration expression.
*/
Expr getAnInit() { exists(int index | result.isNthChildOf(this, index) | index <= -1) }
/** Gets the initializer expression of the loop at the specified (zero-based) position. */
Expr getInit(int index) {
result = this.getAnInit() and
index = -1 - result.getIndex()
}
/** Gets the boolean condition of this `for` loop. */
override Expr getCondition() { result.isNthChildOf(this, 1) }
/** Gets an update expression of this `for` loop. */
Expr getAnUpdate() { exists(int index | result.isNthChildOf(this, index) | index >= 3) }
/** Gets the update expression of this loop at the specified (zero-based) position. */
Expr getUpdate(int index) {
result = this.getAnUpdate() and
index = result.getIndex() - 3
}
/**
* DEPRECATED: Use `getBody()` instead.
*
* Gets the body of this `for` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this and result.getIndex() = 2 }
/** Gets the body of this `for` loop. */
override Stmt getBody() { result.getParent() = this and result.getIndex() = 2 }
/**
* Gets a variable that is used as an iteration variable: it is defined,
* updated or tested in the head of the `for` statement.
*
* This only returns variables that are quite certainly loop variables;
* for complex iterations, it may not return anything.
*
* More precisely, it returns variables that are both accessed in the
* condition of this `for` statement and updated in the update expression
* of this for statement but may be initialized elsewhere.
*/
Variable getAnIterationVariable() {
// Check that the variable is assigned to, incremented or decremented in the update expression, and...
exists(Expr update | update = this.getAnUpdate().getAChildExpr*() |
update.(UnaryAssignExpr).getOperand() = result.getAnAccess() or
update = result.getAnAssignedValue()
) and
// ...that it is checked or used in the condition.
this.getCondition().getAChildExpr*() = result.getAnAccess()
}
override string pp() { result = "for (...;...;...) " + this.getBody().pp() }
override string toString() { result = "for (...;...;...)" }
override string getHalsteadID() { result = "ForStmt" }
override string getAPrimaryQlClass() { result = "ForStmt" }
}
/** An enhanced `for` loop. (Introduced in Java 5.) */
class EnhancedForStmt extends LoopStmtImpl, @enhancedforstmt {
/** Gets the local variable declaration expression of this enhanced `for` loop. */
LocalVariableDeclExpr getVariable() { result.getParent() = this }
/** Gets the expression over which this enhanced `for` loop iterates. */
Expr getExpr() { result.isNthChildOf(this, 1) }
/**
* DEPRECATED: Use `getBody()` instead.
*
* Gets the body of this enhanced `for` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this }
/** Gets the body of this enhanced `for` loop. */
override Stmt getBody() { result.getParent() = this }
override string pp() { result = "for (... : ...) " + this.getBody().pp() }
override string toString() { result = "for (... : ...)" }
override string getHalsteadID() { result = "EnhancedForStmt" }
override string getAPrimaryQlClass() { result = "EnhancedForStmt" }
}
/** A `while` loop. */
class WhileStmt extends ConditionalStmt, LoopStmtImpl, @whilestmt {
/** Gets the boolean condition of this `while` loop. */
override Expr getCondition() { result.getParent() = this }
/**
* DEPRECATED: Use `getBody()` instead.
*
* Gets the body of this `while` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this }
/** Gets the body of this `while` loop. */
override Stmt getBody() { result.getParent() = this }
override string pp() { result = "while (...) " + this.getBody().pp() }
override string toString() { result = "while (...)" }
override string getHalsteadID() { result = "WhileStmt" }
override string getAPrimaryQlClass() { result = "WhileStmt" }
}
/** A `do` loop. */
class DoStmt extends ConditionalStmt, LoopStmtImpl, @dostmt {
/** Gets the condition of this `do` loop. */
override Expr getCondition() { result.getParent() = this }
/**
* DEPRECATED: Use `getBody()` instead.
*
* Gets the body of this `do` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this }
/** Gets the body of this `do` loop. */
override Stmt getBody() { result.getParent() = this }
override string pp() { result = "do " + this.getBody().pp() + " while (...)" }
override string toString() { result = "do ... while (...)" }
override string getHalsteadID() { result = "DoStmt" }
override string getAPrimaryQlClass() { result = "DoStmt" }
}
/**
* A loop statement, including `for`, enhanced `for`,
* `while` and `do` statements.
*/
abstract private class LoopStmtImpl extends Stmt {
/** Gets the body of this loop statement. */
abstract Stmt getBody();
/** Gets the boolean condition of this loop statement. */
Expr getCondition() { none() }
}
final class LoopStmt = LoopStmtImpl;
/** A `try` statement. */
class TryStmt extends Stmt, @trystmt {
/** Gets the block of the `try` statement. */
Stmt getBlock() { result.isNthChildOf(this, -1) }
/** Gets a `catch` clause of this `try` statement. */
CatchClause getACatchClause() { result.getParent() = this }
/**
* Gets the `catch` clause at the specified (zero-based) position `index`
* in this `try` statement.
*/
CatchClause getCatchClause(int index) {
result = this.getACatchClause() and
result.getIndex() = index
}
/** Gets the `finally` block, if any. */
BlockStmt getFinally() { result.isNthChildOf(this, -2) }
/** Gets a resource variable declaration, if any. */
LocalVariableDeclStmt getAResourceDecl() { result.getParent() = this and result.getIndex() <= -3 }
/** Gets the resource variable declaration at the specified position `index` in this `try` statement. */
LocalVariableDeclStmt getResourceDecl(int index) {
result = this.getAResourceDecl() and
index = -3 - result.getIndex()
}
/** Gets a resource expression, if any. */
VarAccess getAResourceExpr() { result.getParent() = this and result.getIndex() <= -3 }
/** Gets the resource expression at the specified position `index` in this `try` statement. */
VarAccess getResourceExpr(int index) {
result = this.getAResourceExpr() and
index = -3 - result.getIndex()
}
/** Gets a resource in this `try` statement, if any. */
ExprParent getAResource() { result = this.getAResourceDecl() or result = this.getAResourceExpr() }
/** Gets the resource at the specified position `index` in this `try` statement. */
ExprParent getResource(int index) {
result = this.getResourceDecl(index) or result = this.getResourceExpr(index)
}
/** Gets a resource variable, if any, either from a resource variable declaration or resource expression. */
Variable getAResourceVariable() {
result = this.getAResourceDecl().getAVariable().getVariable() or
result = this.getAResourceExpr().getVariable()
}
override string pp() { result = "try " + this.getBlock().pp() + " catch (...)" }
override string toString() { result = "try ..." }
override string getHalsteadID() { result = "TryStmt" }
override string getAPrimaryQlClass() { result = "TryStmt" }
}
/** A `catch` clause in a `try` statement. */
class CatchClause extends Stmt, @catchclause {
/** Gets the block of this `catch` clause. */
BlockStmt getBlock() { result.getParent() = this }
/** Gets the `try` statement in which this `catch` clause occurs. */
TryStmt getTry() { this = result.getACatchClause() }
/** Gets the parameter of this `catch` clause. */
LocalVariableDeclExpr getVariable() { result.getParent() = this }
/** Holds if this `catch` clause is a _multi_-`catch` clause. */
predicate isMultiCatch() { this.getVariable().getTypeAccess() instanceof UnionTypeAccess }
/** Gets a type caught by this `catch` clause. */
RefType getACaughtType() {
exists(Expr ta | ta = this.getVariable().getTypeAccess() |
result = ta.(TypeAccess).getType() or
result = ta.(UnionTypeAccess).getAnAlternative().getType()
)
}
override string pp() { result = "catch (...) " + this.getBlock().pp() }
override string toString() { result = "catch (...)" }
override string getHalsteadID() { result = "CatchClause" }
override string getAPrimaryQlClass() { result = "CatchClause" }
}
/** A `switch` statement. */
class SwitchStmt extends Stmt, @switchstmt {
/** Gets an immediate child statement of this `switch` statement. */
Stmt getAStmt() { result.getParent() = this }
/**
* Gets the immediate child statement of this `switch` statement
* that occurs at the specified (zero-based) position.
*/
Stmt getStmt(int index) { result = this.getAStmt() and result.getIndex() = index }
/**
* Gets the `i`th case of this `switch` statement,
* which may be either a normal `case` or a `default`.
*/
SwitchCase getCase(int i) {
result =
rank[i + 1](SwitchCase case, int idx | case.isNthChildOf(this, idx) | case order by idx)
}
/**
* Gets a case of this `switch` statement,
* which may be either a normal `case` or a `default`.
*/
SwitchCase getACase() { result.getParent() = this }
/** Gets a (non-default) constant `case` of this `switch` statement. */
ConstCase getAConstCase() { result = this.getACase() }
/** Gets a (non-default) pattern `case` of this `switch` statement. */
PatternCase getAPatternCase() { result = this.getACase() }
/**
* Gets the `default` case of this switch statement, if any.
*
* Note this may be `default` or `case null, default`.
*/
DefaultCase getDefaultCase() { result = this.getACase() }
/** Gets the expression of this `switch` statement. */
Expr getExpr() { result.getParent() = this }
/** Holds if this switch has a case handling a null literal. */
predicate hasNullCase() {
this.getAConstCase().getValue(_) instanceof NullLiteral or
this.getACase() instanceof NullDefaultCase
}
override string pp() { result = "switch (...)" }
override string toString() { result = "switch (...)" }
override string getHalsteadID() { result = "SwitchStmt" }
override string getAPrimaryQlClass() { result = "SwitchStmt" }
}
/**
* A `switch` statement or expression.
*/
class SwitchBlock extends StmtParent {
SwitchBlock() { this instanceof SwitchStmt or this instanceof SwitchExpr }
}
/**
* A case of a `switch` statement or expression.
*
* This includes both normal `case`s and the `default` case.
*/
class SwitchCase extends Stmt, @case {
/** Gets the switch statement to which this case belongs, if any. */
SwitchStmt getSwitch() { result.getACase() = this }
/**
* Gets the switch expression to which this case belongs, if any.
*/
SwitchExpr getSwitchExpr() { result.getACase() = this }
/**
* Gets the expression of the surrounding switch that this case is compared
* against.
*/
Expr getSelectorExpr() {
result = this.getSwitch().getExpr() or result = this.getSwitchExpr().getExpr()
}
/**
* Gets this case's ordinal in its switch block.
*/
int getCaseIndex() {
this = any(SwitchStmt ss).getCase(result) or this = any(SwitchExpr se).getCase(result)
}
/**
* Holds if this is the `n`th case of switch block `parent`.
*/
pragma[nomagic]
predicate isNthCaseOf(SwitchBlock parent, int n) {
this.getCaseIndex() = n and this.getParent() = parent
}
/**
* Holds if this `case` is a switch labeled rule of the form `... -> ...`.
*/
predicate isRule() {
exists(Expr e | e.getParent() = this | e.getIndex() = -1)
or
exists(Stmt s | s.getParent() = this | s.getIndex() = -1)
}
/**
* Gets the expression on the right-hand side of the arrow, if any.
*
* Note, this predicate gets a value when this switch case is of the form
* `case e1 -> e2`, where `e2` is neither a block nor a throw statement.
* This predicate is mutually exclusive with `getRuleStatement`.
*/
Expr getRuleExpression() {
result.getParent() = this and result.getIndex() = -1
or
exists(ExprStmt es | es.getParent() = this and es.getIndex() = -1 | result = es.getExpr())
}
/**
* Gets the statement on the right-hand side of the arrow, if any.
*
* Note, this predicate gets a value when this switch case is of the form
* `case e1 -> { s1; s2; ... }` or `case e1 -> throw ...`.
* This predicate is mutually exclusive with `getRuleExpression`.
*/
Stmt getRuleStatement() {
result.getParent() = this and result.getIndex() = -1 and not result instanceof ExprStmt
}
}
/**
* A constant `case` of a switch statement.
*
* Note this excludes `case null, default` even though that includes a null constant. It
* does however include plain `case null`.
*/
class ConstCase extends SwitchCase {
ConstCase() {
exists(Expr e | e.getParent() = this and e.getIndex() >= 0 and not e instanceof PatternExpr) and
// For backward compatibility, we don't include `case null, default:` here, on the assumption
// this will come as a surprise to CodeQL that predates that statement's validity.
not isNullDefaultCase(this)
}
/** Gets the `case` constant at index 0. */
Expr getValue() { result.isNthChildOf(this, 0) }
/**
* Gets the `case` constant at index `i`.
*/
Expr getValue(int i) { result.isNthChildOf(this, i) and i >= 0 }
override string pp() { result = "case ..." }
override string toString() { result = "case ..." }
override string getHalsteadID() { result = "ConstCase" }
override string getAPrimaryQlClass() { result = "ConstCase" }
}
/** A pattern case of a `switch` statement */
class PatternCase extends SwitchCase {
PatternCase() { exists(PatternExpr pe | pe.isNthChildOf(this, _)) }
/**
* DEPRECATED: alias for getPattern(0)
*/
deprecated PatternExpr getPattern() { result = this.getPattern(0) }
/**
* Gets this case's `n`th pattern.
*/
PatternExpr getPattern(int n) { result.isNthChildOf(this, n) }
/**
* Gets any of this case's patterns.
*/
PatternExpr getAPattern() { result = this.getPattern(_) }
/**
* Gets this case's sole pattern, if there is exactly one.
*/
PatternExpr getUniquePattern() { result = unique(PatternExpr pe | pe = this.getAPattern()) }
/** Gets the guard applicable to this pattern case, if any. */
Expr getGuard() { result.isNthChildOf(this, -3) }
override string pp() { result = "case <Pattern>" }
override string toString() { result = "case <Pattern>" }
override string getHalsteadID() { result = "PatternCase" }
override string getAPrimaryQlClass() { result = "PatternCase" }
}
/**
* A `default` or `case null, default` case of a `switch` statement or expression.
*/
class DefaultCase extends SwitchCase {
DefaultCase() {
isNullDefaultCase(this)
or
not exists(Expr e | e.getParent() = this | e.getIndex() >= 0)
}
override string pp() { result = "default" }
override string toString() { result = "default" }
override string getHalsteadID() { result = "DefaultCase" }
override string getAPrimaryQlClass() { result = "DefaultCase" }
}
/** A `case null, default` statement of a `switch` statement or expression. */
class NullDefaultCase extends DefaultCase {
NullDefaultCase() { isNullDefaultCase(this) }
override string pp() { result = "case null, default" }
override string toString() { result = "case null, default" }
override string getHalsteadID() { result = "NullDefaultCase" }
override string getAPrimaryQlClass() { result = "NullDefaultCase" }
}
/** A `synchronized` statement. */
class SynchronizedStmt extends Stmt, @synchronizedstmt {
/** Gets the expression on which this `synchronized` statement synchronizes. */
Expr getExpr() { result.getParent() = this }
/** Gets the block of this `synchronized` statement. */
Stmt getBlock() { result.getParent() = this }
override string pp() { result = "synchronized (...) " + this.getBlock().pp() }
override string toString() { result = "synchronized (...)" }
override string getHalsteadID() { result = "SynchronizedStmt" }
override string getAPrimaryQlClass() { result = "SynchronizedStmt" }
}
/** A `return` statement. */
class ReturnStmt extends Stmt, @returnstmt {
/**
* DEPRECATED: Use `getExpr()` instead.
*
* Gets the expression returned by this `return` statement, if any.
*/
deprecated Expr getResult() { result.getParent() = this }
/** Gets the expression returned by this `return` statement, if any. */
Expr getExpr() { result.getParent() = this }
override string pp() { result = "return ..." }
override string toString() { result = "return ..." }
override string getHalsteadID() { result = "ReturnStmt" }
override string getAPrimaryQlClass() { result = "ReturnStmt" }
}
/** A `throw` statement. */
class ThrowStmt extends Stmt, @throwstmt {
/** Gets the expression thrown by this `throw` statement. */
Expr getExpr() { result.getParent() = this }
override string pp() { result = "throw ..." }
override string toString() { result = "throw ..." }
override string getHalsteadID() { result = "ThrowStmt" }
/** Gets the type of the expression thrown by this `throw` statement. */
RefType getThrownExceptionType() { result = this.getExpr().getType() }
/**
* Gets the `catch` clause that catches the exception
* thrown by this `throw` statement and occurs
* in the same method as this `throw` statement,
* provided such a `catch` exists.
*/
CatchClause getLexicalCatchIfAny() {
exists(TryStmt try | try = this.findEnclosing() and result = this.catchClauseForThis(try))
}
private Stmt findEnclosing() {
result = this.getEnclosingStmt()
or
exists(Stmt mid |
mid = this.findEnclosing() and
not exists(this.catchClauseForThis(mid)) and
result = mid.getEnclosingStmt()
)
}
private CatchClause catchClauseForThis(TryStmt try) {
result = try.getACatchClause() and
result.getEnclosingCallable() = this.getEnclosingCallable() and
this.getExpr().getType().(RefType).hasSupertype*(result.getVariable().getType()) and
not this.getEnclosingStmt+() = result
}
override string getAPrimaryQlClass() { result = "ThrowStmt" }
}
private class JumpStmt_ = @breakstmt or @yieldstmt or @continuestmt;
/** A `break`, `yield` or `continue` statement. */
class JumpStmt extends Stmt, JumpStmt_ {
/**
* Gets the labeled statement that this `break` or
* `continue` statement refers to, if any.
*/
LabeledStmt getTargetLabel() {
this.getEnclosingStmt+() = result and
namestrings(result.getLabel(), _, this)
}
private Stmt getLabelTarget() { result = this.getTargetLabel().getStmt() }
private Stmt getAPotentialTarget() {
this.getEnclosingStmt+() = result and
(
result instanceof LoopStmt
or
this instanceof BreakStmt and result instanceof SwitchStmt
)
}
private StmtParent getEnclosingTarget() {
result = this.getAPotentialTarget() and
not exists(Stmt other | other = this.getAPotentialTarget() | other.getEnclosingStmt+() = result)
}
/**
* Gets the statement or `switch` expression that this `break`, `yield` or `continue` jumps to.
*/
StmtParent getTarget() {
// Note: This implementation only considers `break` and `continue`; YieldStmt overrides this predicate
result = this.getLabelTarget()
or
not exists(this.getLabelTarget()) and result = this.getEnclosingTarget()
}
}
/** A `break` statement. */
class BreakStmt extends JumpStmt, @breakstmt {
/** Gets the label targeted by this `break` statement, if any. */
string getLabel() { namestrings(result, _, this) }
/** Holds if this `break` statement has an explicit label. */
predicate hasLabel() { exists(this.getLabel()) }
override string pp() {
if this.hasLabel() then result = "break " + this.getLabel() else result = "break"
}
override string toString() { result = "break" }
override string getHalsteadID() { result = "BreakStmt" }
override string getAPrimaryQlClass() { result = "BreakStmt" }
}
/**
* A `yield` statement.
*/
class YieldStmt extends JumpStmt, @yieldstmt {
/**
* Gets the value of this `yield` statement.
*/
Expr getValue() { result.getParent() = this }
/**
* Gets the `switch` expression target of this `yield` statement.
*/
override SwitchExpr getTarget() {
// Get the innermost enclosing SwitchExpr; this works because getParent() is defined for Stmt and
// therefore won't proceed after the innermost SwitchExpr (due to it being an Expr)
result = this.getParent+()
}
override string pp() { result = "yield ..." }
override string toString() { result = "yield ..." }
override string getHalsteadID() { result = "YieldStmt" }
override string getAPrimaryQlClass() { result = "YieldStmt" }
}
/** A `continue` statement. */
class ContinueStmt extends JumpStmt, @continuestmt {
/** Gets the label targeted by this `continue` statement, if any. */
string getLabel() { namestrings(result, _, this) }
/** Holds if this `continue` statement has an explicit label. */
predicate hasLabel() { exists(this.getLabel()) }
override string pp() {
if this.hasLabel() then result = "continue " + this.getLabel() else result = "continue"
}
override string toString() { result = "continue" }
override string getHalsteadID() { result = "ContinueStmt" }
override string getAPrimaryQlClass() { result = "ContinueStmt" }
}
/** The empty statement. */
class EmptyStmt extends Stmt, @emptystmt {
override string pp() { result = ";" }
override string toString() { result = ";" }
override string getHalsteadID() { result = "EmptyStmt" }
override string getAPrimaryQlClass() { result = "EmptyStmt" }
}
/**
* An expression statement.
*
* Certain kinds of expressions may be used as statements by appending a semicolon.
*/
class ExprStmt extends Stmt, @exprstmt {
/** Gets the expression of this expression statement. */
Expr getExpr() { result.getParent() = this }
override string pp() { result = "<Expr>;" }
override string toString() { result = "<Expr>;" }
override string getHalsteadID() { result = "ExprStmt" }
/** Holds if this statement represents a field declaration with an initializer. */
predicate isFieldDecl() {
this.getEnclosingCallable() instanceof InitializerMethod and
exists(FieldDeclaration fd, Location fdl, Location sl |
fdl = fd.getLocation() and sl = this.getLocation()
|
fdl.getFile() = sl.getFile() and
fdl.getStartLine() = sl.getStartLine() and
fdl.getStartColumn() = sl.getStartColumn()
)
}
override string getAPrimaryQlClass() { result = "ExprStmt" }
}
/** A labeled statement. */
class LabeledStmt extends Stmt, @labeledstmt {
/** Gets the statement of this labeled statement. */
Stmt getStmt() { result.getParent() = this }
/** Gets the label of this labeled statement. */
string getLabel() { namestrings(result, _, this) }
override string pp() { result = this.getLabel() + ": " + this.getStmt().pp() }
override string getHalsteadID() { result = this.getLabel() + ":" }
override string toString() { result = "<Label>: ..." }
override string getAPrimaryQlClass() { result = "LabeledStmt" }
}
/** An `assert` statement. */
class AssertStmt extends Stmt, @assertstmt {
/** Gets the boolean expression of this `assert` statement. */
Expr getExpr() { exprs(result, _, _, this, _) and result.getIndex() = 0 }
/** Gets the assertion message expression, if any. */
Expr getMessage() { exprs(result, _, _, this, _) and result.getIndex() = 1 }
override string pp() {
if exists(this.getMessage()) then result = "assert ... : ..." else result = "assert ..."
}
override string toString() { result = "assert ..." }
override string getHalsteadID() { result = "AssertStmt" }
override string getAPrimaryQlClass() { result = "AssertStmt" }
}
/** A statement that declares one or more local variables. */
class LocalVariableDeclStmt extends Stmt, @localvariabledeclstmt {
/** Gets a declared variable. */
LocalVariableDeclExpr getAVariable() { result.getParent() = this }
/** Gets the variable declared at the specified (one-based) position in this local variable declaration statement. */
LocalVariableDeclExpr getVariable(int index) {
result = this.getAVariable() and
result.getIndex() = index
}
/** Gets an index of a variable declared in this local variable declaration statement. */
int getAVariableIndex() { exists(this.getVariable(result)) }
override string pp() { result = "var ...;" }
override string toString() { result = "var ...;" }
override string getHalsteadID() { result = "LocalVariableDeclStmt" }
override string getAPrimaryQlClass() { result = "LocalVariableDeclStmt" }
}
/** A statement that declares a local class or interface. */
class LocalTypeDeclStmt extends Stmt, @localtypedeclstmt {
/** Gets the local type declared by this statement. */
LocalClassOrInterface getLocalType() { isLocalClassOrInterface(result, this) }
private string getDeclKeyword() {
result = "class" and this.getLocalType() instanceof Class
or
result = "interface" and this.getLocalType() instanceof Interface
}
override string pp() { result = this.getDeclKeyword() + " " + this.getLocalType().toString() }
override string toString() { result = this.getDeclKeyword() + " ..." }
override string getHalsteadID() { result = "LocalTypeDeclStmt" }
override string getAPrimaryQlClass() { result = "LocalTypeDeclStmt" }
}
/** An explicit `this(...)` constructor invocation. */
class ThisConstructorInvocationStmt extends Stmt, ConstructorCall, @constructorinvocationstmt {
/** Gets an argument of this constructor invocation. */
override Expr getAnArgument() { result.getIndex() >= 0 and result.getParent() = this }
/** Gets the argument at the specified (zero-based) position in this constructor invocation. */
override Expr getArgument(int index) {
result = this.getAnArgument() and
result.getIndex() = index
}
/** Gets a type argument of this constructor invocation. */
Expr getATypeArgument() { result.getIndex() <= -2 and result.getParent() = this }
/** Gets the type argument at the specified (zero-based) position in this constructor invocation. */
Expr getTypeArgument(int index) {
result = this.getATypeArgument() and
(-2 - result.getIndex()) = index
}
/** Gets the constructor invoked by this constructor invocation. */
override Constructor getConstructor() { callableBinding(this, result) }
override Expr getQualifier() { none() }
/** Gets the immediately enclosing callable of this constructor invocation. */
override Callable getEnclosingCallable() { result = Stmt.super.getEnclosingCallable() }
/** Gets the immediately enclosing statement of this constructor invocation. */
override Stmt getEnclosingStmt() { result = this }
/** Gets the `ControlFlowNode` corresponding to this call. */
override ControlFlowNode getControlFlowNode() { result = Stmt.super.getControlFlowNode() }
override string pp() { result = "this(...)" }
override string toString() { result = "this(...)" }
override string getHalsteadID() { result = "ConstructorInvocationStmt" }
override string getAPrimaryQlClass() { result = "ThisConstructorInvocationStmt" }
}
/** An explicit `super(...)` constructor invocation. */
class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstructorinvocationstmt {
/** Gets an argument of this constructor invocation. */
override Expr getAnArgument() { result.getIndex() >= 0 and result.getParent() = this }
/** Gets the argument at the specified (zero-based) position in this constructor invocation. */
override Expr getArgument(int index) {
result = this.getAnArgument() and
result.getIndex() = index
}
/** Gets a type argument of this constructor invocation. */
Expr getATypeArgument() { result.getIndex() <= -2 and result.getParent() = this }
/** Gets the type argument at the specified (zero-based) position in this constructor invocation. */
Expr getTypeArgument(int index) {
result = this.getATypeArgument() and
(-2 - result.getIndex()) = index
}
/** Gets the constructor invoked by this constructor invocation. */
override Constructor getConstructor() { callableBinding(this, result) }
/** Gets the qualifier expression of this `super(...)` constructor invocation, if any. */
override Expr getQualifier() { result.isNthChildOf(this, -1) }