-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStmt.qll
More file actions
2198 lines (1952 loc) · 55 KB
/
Stmt.qll
File metadata and controls
2198 lines (1952 loc) · 55 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 a hierarchy of classes for modeling C/C++ statements.
*/
import semmle.code.cpp.Element
private import semmle.code.cpp.Enclosing
private import semmle.code.cpp.internal.ResolveClass
/**
* A C/C++ statement.
*/
class Stmt extends StmtParent, @stmt {
/** Gets the `n`th child of this statement. */
Element getChild(int n) {
stmtparents(unresolveElement(result), n, underlyingElement(this)) or
exprparents(unresolveElement(result), n, underlyingElement(this))
}
/** Holds if `e` is the `n`th child of this statement. */
predicate hasChild(Element e, int n) { this.getChild(n) = e }
/** Gets the enclosing function of this statement, if any. */
Function getEnclosingFunction() { result = stmtEnclosingElement(this) }
/**
* Gets the nearest enclosing block of this statement in the source, if any.
*/
BlockStmt getEnclosingBlock() {
if
this.getParentStmt() instanceof BlockStmt and
not this.getParentStmt().(BlockStmt).getLocation() instanceof UnknownLocation
then result = this.getParentStmt()
else result = this.getParentStmt().getEnclosingBlock()
}
/** Gets a child of this statement. */
Element getAChild() { result = this.getChild(_) }
/** Gets the parent of this statement, if any. */
StmtParent getParent() { stmtparents(underlyingElement(this), _, unresolveElement(result)) }
/** Gets the parent statement of this statement, if any. */
Stmt getParentStmt() { stmtparents(underlyingElement(this), _, unresolveElement(result)) }
/** Gets a child statement of this statement. */
Stmt getChildStmt() { result.getParentStmt() = this }
/**
* Gets the statement following this statement in the same block, if any.
*
* Note that this is not widely useful, because this doesn't have a result for
* the last statement of a block. Consider using the `ControlFlowNode` class
* to trace the flow of control instead.
*/
Stmt getFollowingStmt() {
exists(BlockStmt b, int i |
this = b.getStmt(i) and
result = b.getStmt(i + 1)
)
}
/**
* Gets the `n`th compiler-generated destructor call that is performed after this statement, in
* order of destruction.
*
* For instance, in the following code, `getImplicitDestructorCall(0)` for the block will be the
* destructor call for `c2`:
* ```cpp
* {
* MyClass c1;
* MyClass c2;
* }
* ```
*/
DestructorCall getImplicitDestructorCall(int n) {
synthetic_destructor_call(this, max(int i | synthetic_destructor_call(this, i, _)) - n, result)
}
/**
* Gets a compiler-generated destructor call that is performed after this statement.
*/
DestructorCall getAnImplicitDestructorCall() { synthetic_destructor_call(this, _, result) }
override Location getLocation() { stmts(underlyingElement(this), _, result) }
override string toString() { none() }
override Function getControlFlowScope() { result = this.getEnclosingFunction() }
override Stmt getEnclosingStmt() { result = this }
/**
* Holds if this statement is side-effect free (a conservative
* approximation; that is, it may be side-effect free even if this
* predicate doesn't hold).
*
* This predicate cannot be overridden; override `mayBeImpure()`
* instead.
*
* Note that this predicate only considers whether the statement has
* any side-effects, such as writing to a file. Even if it holds, the
* statement may be impure in the sense that its behavior is affected
* by external factors, such as the contents of global variables.
*/
final predicate isPure() { not this.mayBeImpure() }
/**
* Holds if it is possible that this statement is impure. If we are not
* sure, then it holds.
*/
predicate mayBeImpure() { any() }
/**
* Holds if it is possible that this statement is globally impure.
*
* Similar to `mayBeImpure()`, except that `mayBeGloballyImpure()`
* does not consider modifications to temporary local variables to be
* impure. That is, if you call a function in which
* `mayBeGloballyImpure()` doesn't hold for any statement, then the
* function as a whole will have no side-effects, even if it mutates
* its own fresh stack variables.
*/
predicate mayBeGloballyImpure() { any() }
/**
* Gets an attribute of this statement, for example
* `[[clang::fallthrough]]`.
*/
Attribute getAnAttribute() { stmtattributes(underlyingElement(this), unresolveElement(result)) }
/**
* Gets a macro invocation that generates this entire statement.
*
* For example, given
* ```
* #define SOMEFUN a()
* #define FOO do { SOMEFUN; b(); } while (0)
* void f(void) {
* FOO;
* }
* ```
* this predicate would have results of `SOMEFUN` and `FOO` for the
* function call `a()`, and just `FOO` for the function call `b()`,
* the block within the 'do' statement, and the entire 'do' statement.
*
* Note that, unlike `isInMacroExpansion()` it is not necessary for
* the macro to generate the terminating semi-colon.
*/
MacroInvocation getGeneratingMacro() { result.getAnExpandedElement() = this }
/** Holds if this statement was generated by the compiler. */
predicate isCompilerGenerated() { compgenerated(underlyingElement(this)) }
}
private class TStmtParent = @stmt or @expr;
/**
* An element that is the parent of a statement in the C/C++ AST.
*
* This is normally a statement, but may be a `StmtExpr`.
*/
class StmtParent extends ControlFlowNode, TStmtParent { }
/**
* A C/C++ 'expression' statement.
*
* For example,
* ```
* x = 1;
* ```
* is an assignment expression inside an 'expression' statement.
*/
class ExprStmt extends Stmt, @stmt_expr {
override string getAPrimaryQlClass() { result = "ExprStmt" }
/**
* Gets the expression of this 'expression' statement.
*
* For example, for
* ```
* x = 1;
* ```
* the result would be an `AssignExpr`.
*/
Expr getExpr() { result = this.getChild(0) }
override string toString() { result = "ExprStmt" }
override predicate mayBeImpure() { this.getExpr().mayBeImpure() }
override predicate mayBeGloballyImpure() { this.getExpr().mayBeGloballyImpure() }
override MacroInvocation getGeneratingMacro() {
// We only need the expression to be in the macro, not the semicolon.
result.getAnExpandedElement() = this.getExpr()
}
}
private class TControlStructure = TConditionalStmt or TLoop;
/**
* A C/C++ control structure, that is, either a conditional statement or
* a loop.
*/
class ControlStructure extends Stmt, TControlStructure {
/**
* Gets the controlling expression of this control structure.
*
* This is the condition of 'if' statements and loops, and the
* switched expression for 'switch' statements.
*/
Expr getControllingExpr() { none() } // overridden by subclasses
/** Gets a child declaration of this scope. */
Declaration getADeclaration() { none() }
}
private class TConditionalStmt = @stmt_if or @stmt_constexpr_if or @stmt_switch;
/**
* A C/C++ conditional statement, that is, either an 'if' statement or a
* 'switch' statement.
*/
class ConditionalStmt extends ControlStructure, TConditionalStmt { }
/**
* A C/C++ 'if' statement. For example, the `if` statement in the following
* code:
* ```
* if (x == 1) {
* ...
* }
* ```
*/
class IfStmt extends ConditionalStmt, @stmt_if {
override string getAPrimaryQlClass() { result = "IfStmt" }
/**
* Gets the initialization statement of this 'if' statement, if any.
*
* For example, for
* ```
* if (int x = y; b) { f(); }
* ```
* the result is `int x = y;`.
*
* Does not hold if the initialization statement is missing or an empty statement, as in
* ```
* if (b) { f(); }
* ```
* or
* ```
* if (; b) { f(); }
* ```
*/
Stmt getInitialization() { if_initialization(underlyingElement(this), unresolveElement(result)) }
/**
* Gets the condition expression of this 'if' statement.
*
* For example, for
* ```
* if (b) { x = 1; }
* ```
* the result is `b`.
*/
Expr getCondition() { result = this.getChild(1) }
override Expr getControllingExpr() { result = this.getCondition() }
/**
* Gets the 'then' statement of this 'if' statement.
*
* For example, for
* ```
* if (b) { x = 1; }
* ```
* the result is the `BlockStmt` `{ x = 1; }`.
*/
Stmt getThen() { if_then(underlyingElement(this), unresolveElement(result)) }
/**
* Gets the 'else' statement of this 'if' statement, if any.
*
* For example, for
* ```
* if (b) { x = 1; } else { x = 2; }
* ```
* the result is the `BlockStmt` `{ x = 2; }`, and for
* ```
* if (b) { x = 1; }
* ```
* there is no result.
*/
Stmt getElse() { if_else(underlyingElement(this), unresolveElement(result)) }
/**
* Holds if this 'if' statement has an 'else' statement.
*
* For example, this holds for
* ```
* if (b) { x = 1; } else { x = 2; }
* ```
* but not for
* ```
* if (b) { x = 1; }
* ```
*/
predicate hasElse() { exists(this.getElse()) }
override string toString() { result = "if (...) ... " }
override predicate mayBeImpure() {
this.getCondition().mayBeImpure() or
this.getThen().mayBeImpure() or
this.getElse().mayBeImpure()
}
override predicate mayBeGloballyImpure() {
this.getCondition().mayBeGloballyImpure() or
this.getThen().mayBeGloballyImpure() or
this.getElse().mayBeGloballyImpure()
}
override MacroInvocation getGeneratingMacro() {
result.getAnExpandedElement() = this.getCondition() and
this.getThen().getGeneratingMacro() = result and
(this.hasElse() implies this.getElse().getGeneratingMacro() = result)
}
}
/**
* A C/C++ 'constexpr if' statement. For example, the `if constexpr` statement
* in the following code:
* ```
* if constexpr (x) {
* ...
* }
* ```
*/
class ConstexprIfStmt extends ConditionalStmt, @stmt_constexpr_if {
override string getAPrimaryQlClass() { result = "ConstexprIfStmt" }
/**
* Gets the initialization statement of this 'constexpr if' statement, if any.
*
* For example, for
* ```
* if constexpr (int x = y; b) { f(); }
* ```
* the result is `int x = y;`.
*
* Does not hold if the initialization statement is missing or an empty statement, as in
* ```
* if constexpr (b) { f(); }
* ```
* or
* ```
* if constexpr (; b) { f(); }
* ```
*/
Stmt getInitialization() {
constexpr_if_initialization(underlyingElement(this), unresolveElement(result))
}
/**
* Gets the condition expression of this 'constexpr if' statement.
*
* For example, for
* ```
* if constexpr (b) { x = 1; }
* ```
* the result is `b`.
*/
Expr getCondition() { result = this.getChild(1) }
override Expr getControllingExpr() { result = this.getCondition() }
/**
* Gets the 'then' statement of this 'constexpr if' statement.
*
* For example, for
* ```
* if constexpr (b) { x = 1; }
* ```
* the result is the `BlockStmt` `{ x = 1; }`.
*/
Stmt getThen() { constexpr_if_then(underlyingElement(this), unresolveElement(result)) }
/**
* Gets the 'else' statement of this 'constexpr if' statement, if any.
*
* For example, for
* ```
* if constexpr (b) { x = 1; } else { x = 2; }
* ```
* the result is the `BlockStmt` `{ x = 2; }`, and for
* ```
* if constexpr (b) { x = 1; }
* ```
* there is no result.
*/
Stmt getElse() { constexpr_if_else(underlyingElement(this), unresolveElement(result)) }
/**
* Holds if this 'constexpr if' statement has an 'else' statement.
*
* For example, this holds for
* ```
* if constexpr (b) { x = 1; } else { x = 2; }
* ```
* but not for
* ```
* if constexpr (b) { x = 1; }
* ```
*/
predicate hasElse() { exists(this.getElse()) }
override string toString() { result = "if constexpr (...) ... " }
override predicate mayBeImpure() {
this.getCondition().mayBeImpure() or
this.getThen().mayBeImpure() or
this.getElse().mayBeImpure()
}
override predicate mayBeGloballyImpure() {
this.getCondition().mayBeGloballyImpure() or
this.getThen().mayBeGloballyImpure() or
this.getElse().mayBeGloballyImpure()
}
override MacroInvocation getGeneratingMacro() {
result.getAnExpandedElement() = this.getCondition() and
this.getThen().getGeneratingMacro() = result and
(this.hasElse() implies this.getElse().getGeneratingMacro() = result)
}
}
private class TLoop = @stmt_while or @stmt_end_test_while or @stmt_range_based_for or @stmt_for;
/**
* A C/C++ loop, that is, either a 'while' loop, a 'for' loop, or a
* 'do' loop.
*/
class Loop extends ControlStructure, TLoop {
/** Gets the condition expression of this loop. */
Expr getCondition() { none() } // overridden in subclasses
/** Gets the body statement of this loop. */
Stmt getStmt() { none() } // overridden in subclasses
}
/**
* A C/C++ 'while' statement.
*
* For example, the `while` statement in the following code:
* ```
* while (b) {
* f();
* }
* ```
*/
class WhileStmt extends Loop, @stmt_while {
override string getAPrimaryQlClass() { result = "WhileStmt" }
override Expr getCondition() { result = this.getChild(0) }
override Expr getControllingExpr() { result = this.getCondition() }
override Stmt getStmt() { while_body(underlyingElement(this), unresolveElement(result)) }
override string toString() { result = "while (...) ..." }
override predicate mayBeImpure() {
this.getCondition().mayBeImpure() or
this.getStmt().mayBeImpure()
}
override predicate mayBeGloballyImpure() {
this.getCondition().mayBeGloballyImpure() or
this.getStmt().mayBeGloballyImpure()
}
override MacroInvocation getGeneratingMacro() {
result.getAnExpandedElement() = this.getCondition() and
this.getStmt().getGeneratingMacro() = result
}
/**
* Holds if the loop condition is provably `true`.
*
* For example, this holds for
* ```
* while(1) { ...; if(b) break; ...; }
* ```
*/
predicate conditionAlwaysTrue() { conditionAlwaysTrue(this.getCondition()) }
/**
* Holds if the loop condition is provably `false`.
*
* For example, this holds for
* ```
* while(0) { ...; }
* ```
*/
predicate conditionAlwaysFalse() { conditionAlwaysFalse(this.getCondition()) }
/**
* Holds if the loop condition is provably `true` upon entry,
* that is, at least one iteration of the loop is guaranteed.
*
* For example, with
* ```
* bool done = false;
* while (!done) { ... done = true; ... }
* ```
* the condition `!done` always evaluates to `true` upon entry since
* `done = false`, but the condition may evaluate to `false` after
* some iterations.
*/
predicate conditionAlwaysTrueUponEntry() { loopConditionAlwaysTrueUponEntry(this, _) }
}
/**
* A C/C++ jump statement.
*/
class JumpStmt extends Stmt, @jump {
override string getAPrimaryQlClass() { result = "JumpStmt" }
/** Gets the target of this jump statement. */
Stmt getTarget() { jumpinfo(underlyingElement(this), _, unresolveElement(result)) }
}
/**
* A C/C++ 'goto' statement which jumps to a label.
*
* For example, the `goto` statement in the following code:
* ```
* goto someLabel;
* ...
* somelabel:
* ```
*/
class GotoStmt extends JumpStmt, @stmt_goto {
override string getAPrimaryQlClass() { result = "GotoStmt" }
/**
* Gets the name of the label this 'goto' statement refers to.
*
* For example, for
* ```
* goto someLabel;
* ```
* the result is `"someLabel"`.
*/
string getName() { jumpinfo(underlyingElement(this), result, _) and result != "" }
/** Holds if this 'goto' statement refers to a label. */
predicate hasName() { exists(string s | jumpinfo(underlyingElement(this), s, _) and s != "") }
override string toString() { result = "goto ..." }
/**
* Holds if this 'goto' statement breaks out of two or more nested
* loops.
*
* For example, for
* ```
* while(b) {
* while(b) {
* if(b) goto middle;
* if(b) goto end;
* }
* if(b) goto end;
* middle:
* }
* end:
* ```
* this holds for the second `goto`, but not the first or third.
*/
predicate breaksFromNestedLoops() {
exists(Loop l1, Loop l2 |
this.getParentStmt+() = l1 and
l1.getParentStmt+() = l2 and
l2.getParentStmt+() = this.getASuccessor().(Stmt).getParentStmt()
)
}
override predicate mayBeImpure() { none() }
override predicate mayBeGloballyImpure() { none() }
}
/**
* A 'goto' statement whose target is computed by a non-constant
* expression (a non-standard extension to C/C++).
*
* For example, the `goto` statement in the following code:
* ```
* goto *ptr;
* ```
*/
class ComputedGotoStmt extends Stmt, @stmt_assigned_goto {
/**
* Gets the expression used to compute the target of this 'goto'
* statement.
*
* For example, for
* ```
* goto *ptr;
* ```
* the result is `ptr`.
*/
Expr getExpr() { result = this.getChild(0) }
override string toString() { result = "computed goto ..." }
override predicate mayBeImpure() { this.getExpr().mayBeImpure() }
override predicate mayBeGloballyImpure() { this.getExpr().mayBeGloballyImpure() }
override MacroInvocation getGeneratingMacro() {
// We only need the expression to be in the macro, not the semicolon.
result.getAnExpandedElement() = this.getExpr()
}
}
/**
* A C/C++ 'continue' statement.
*
* For example, the `continue` statement in the following code:
* ```
* while (x) {
* if (arr[x] < 0) continue;
* ...
* }
* ```
*/
class ContinueStmt extends JumpStmt, @stmt_continue {
override string getAPrimaryQlClass() { result = "ContinueStmt" }
override string toString() { result = "continue;" }
override predicate mayBeImpure() { none() }
override predicate mayBeGloballyImpure() { none() }
/**
* Gets the loop that this continue statement will jump to the beginning of.
*/
Stmt getContinuable() { result = getEnclosingContinuable(this) }
}
private Stmt getEnclosingContinuable(Stmt s) {
if s.getParent().getEnclosingStmt() instanceof Loop
then result = s.getParent().getEnclosingStmt()
else result = getEnclosingContinuable(s.getParent().getEnclosingStmt())
}
/**
* A C/C++ 'break' statement.
*
* For example, the `break` statement in the following code:
* ```
* while (x) {
* if (arr[x] == 0) break;
* ...
* }
* ```
*/
class BreakStmt extends JumpStmt, @stmt_break {
override string getAPrimaryQlClass() { result = "BreakStmt" }
override string toString() { result = "break;" }
override predicate mayBeImpure() { none() }
override predicate mayBeGloballyImpure() { none() }
/**
* Gets the loop or switch statement that this break statement will exit.
*/
Stmt getBreakable() { result = getEnclosingBreakable(this) }
}
private Stmt getEnclosingBreakable(Stmt s) {
if
s.getParent().getEnclosingStmt() instanceof Loop or
s.getParent().getEnclosingStmt() instanceof SwitchStmt
then result = s.getParent().getEnclosingStmt()
else result = getEnclosingBreakable(s.getParent().getEnclosingStmt())
}
/**
* A C/C++ 'label' statement.
*
* For example, the `somelabel:` statement in the following code:
* ```
* goto someLabel;
* ...
* somelabel:
* ```
*/
class LabelStmt extends Stmt, @stmt_label {
override string getAPrimaryQlClass() { result = "LabelStmt" }
/** Gets the name of this 'label' statement. */
string getName() { jumpinfo(underlyingElement(this), result, _) and result != "" }
/** Holds if this 'label' statement is named. */
predicate isNamed() { exists(this.getName()) }
override string toString() { result = "label ...:" }
override predicate mayBeImpure() { none() }
override predicate mayBeGloballyImpure() { none() }
}
/**
* A C/C++ `co_return` statement.
*
* For example:
* ```
* co_return 1+2;
* ```
* or
* ```
* co_return;
* ```
*/
class CoReturnStmt extends Stmt, @stmt_co_return {
override string getAPrimaryQlClass() { result = "CoReturnStmt" }
/**
* Gets the operand of this `co_return` statement.
*
* For example, for
* ```
* co_return 1+2;
* ```
* the operand is a function call `return_value(1+2)`, and for
* ```
* co_return;
* ```
* the operand is a function call `return_void()`.
*/
FunctionCall getOperand() { result = this.getChild(0) }
/**
* Gets the expression of this `co_return` statement, if any.
*
* For example, for
* ```
* co_return 1+2;
* ```
* the result is `1+2`, and there is no result for
* ```
* co_return;
* ```
*/
Expr getExpr() { result = this.getOperand().getArgument(0) }
/**
* Holds if this `co_return` statement has an expression.
*
* For example, this holds for
* ```
* co_return 1+2;
* ```
* but not for
* ```
* co_return;
* ```
*/
predicate hasExpr() { exists(this.getExpr()) }
override string toString() { result = "co_return ..." }
}
/**
* A C/C++ 'return' statement.
*
* For example:
* ```
* return 1+2;
* ```
* or
* ```
* return;
* ```
*/
class ReturnStmt extends Stmt, @stmt_return {
override string getAPrimaryQlClass() { result = "ReturnStmt" }
/**
* Gets the expression of this 'return' statement.
*
* For example, for
* ```
* return 1+2;
* ```
* the result is `1+2`, and there is no result for
* ```
* return;
* ```
*/
Expr getExpr() { result = this.getChild(0) }
/**
* Holds if this 'return' statement has an expression.
*
* For example, this holds for
* ```
* return 1+2;
* ```
* but not for
* ```
* return;
* ```
*/
predicate hasExpr() { exists(this.getExpr()) }
override string toString() { result = "return ..." }
override predicate mayBeImpure() { this.getExpr().mayBeImpure() }
override predicate mayBeGloballyImpure() { this.getExpr().mayBeGloballyImpure() }
}
/**
* A C/C++ 'do' statement.
*
* For example, the `do` ... `while` in the following code:
* ```
* do {
* x = x + 1;
* } while (x < 10);
* ```
*/
class DoStmt extends Loop, @stmt_end_test_while {
override string getAPrimaryQlClass() { result = "DoStmt" }
override Expr getCondition() { result = this.getChild(0) }
override Expr getControllingExpr() { result = this.getCondition() }
override Stmt getStmt() { do_body(underlyingElement(this), unresolveElement(result)) }
override string toString() { result = "do (...) ..." }
override predicate mayBeImpure() {
this.getCondition().mayBeImpure() or
this.getStmt().mayBeImpure()
}
override predicate mayBeGloballyImpure() {
this.getCondition().mayBeGloballyImpure() or
this.getStmt().mayBeGloballyImpure()
}
override MacroInvocation getGeneratingMacro() {
result.getAnExpandedElement() = this.getCondition() and
this.getStmt().getGeneratingMacro() = result
}
}
/**
* A C++11 range-based 'for' statement.
*
* For example,
* ```
* for (int x : xs) { y += x; }
* ```
*
* This example would be desugared to
* ```
* {
* auto && __range = xs;
* for (auto __begin = begin_expr, __end = end_expr;
* __begin != __end;
* ++__begin) {
* int x = *__begin;
* y += x;
* }
* }
* ```
* where `begin_expr` and `end_expr` depend on the type of `xs`.
*/
class RangeBasedForStmt extends Loop, @stmt_range_based_for {
override string getAPrimaryQlClass() { result = "RangeBasedForStmt" }
/**
* Gets the initialization statement of this 'for' statement, if any.
*
* For example, for
* ```
* for (int x = y; auto z : ... ) { }
* ```
* the result is `int x = y;`.
*
* Does not hold if the initialization statement is missing or an empty statement, as in
* ```
* for (auto z : ...) { }
* ```
* or
* ```
* for (; auto z : ) { }
* ```
*/
Stmt getInitialization() { for_initialization(underlyingElement(this), unresolveElement(result)) }
/**
* Gets the 'body' statement of this range-based 'for' statement.
*
* For example, for
* ```
* for (int x : xs) { y += x; }
* ```
* the result is the `BlockStmt` `{ y += x; }`.
*/
override Stmt getStmt() { result = this.getChild(6) }
override string toString() { result = "for(...:...) ..." }
/**
* Gets the variable introduced by the for-range-declaration.
*
* For example, for
* ```
* for (int x : xs) { y += x; }
* ```
* the result is `int x`.
*/
LocalVariable getVariable() { result = this.getChild(5).(DeclStmt).getADeclaration() }
/**
* Gets the expression giving the range to iterate over.
*
* For example, for
* ```
* for (int x : xs) { y += x; }
* ```
* the result is `xs`.
*/
Expr getRange() { result = this.getRangeVariable().getInitializer().getExpr() }
/** Gets the compiler-generated `__range` variable after desugaring. */
LocalVariable getRangeVariable() { result = this.getChild(1).(DeclStmt).getADeclaration() }
/**
* Gets the compiler-generated `__begin != __end` which is the
* condition expression of this for statement after desugaring.
* It will be either an `NEExpr` or a call to a user-defined
* `operator!=`.
*/
override Expr getCondition() { result = this.getChild(3) }
override Expr getControllingExpr() { result = this.getCondition() }
/**
* Gets a declaration statement that declares first `__begin` and then
* `__end`, initializing them to the values they have before entering the
* desugared loop.
*/
DeclStmt getBeginEndDeclaration() { result = this.getChild(2) }
/** Gets the compiler-generated `__begin` variable after desugaring. */
LocalVariable getBeginVariable() { result = this.getBeginEndDeclaration().getDeclaration(0) }
/** Gets the compiler-generated `__end` variable after desugaring. */
LocalVariable getEndVariable() { result = this.getBeginEndDeclaration().getDeclaration(1) }
/**
* Gets the compiler-generated `++__begin` which is the update
* expression of this for statement after desugaring. It will
* be either a `PrefixIncrExpr` or a call to a user-defined
* `operator++`.
*/
Expr getUpdate() { result = this.getChild(4) }
/** Gets the compiler-generated `__begin` variable after desugaring. */
LocalVariable getAnIterationVariable() { result = this.getBeginVariable() }
}
/**
* A C/C++ 'for' statement.
*
* This only represents "traditional" 'for' statements and not C++11
* range-based 'for' statements or Objective C 'for-in' statements.
*
* For example, the `for` statement in:
* ```
* for (i = 0; i < 10; i++) { j++; }
* ```
*/
class ForStmt extends Loop, @stmt_for {
override string getAPrimaryQlClass() { result = "ForStmt" }