1+ void f (int &x) {} // Function that takes a non-const integer reference
2+ void g (int *x) {} // Function that takes a non-const integer pointer
3+
4+ int main () {
5+ int j = 5 ;
6+
7+ /* ========== 1. Type of the initialized counter variable ========== */
8+
9+ for (int i = 0 ; i < 10 ; i++) { // COMPLIANT: `i` has an integer type
10+ }
11+
12+ for (float i = 0.0 ; i < 10 ;
13+ i++) { // NON_COMPLIANT: `i` has a non-integer type
14+ }
15+
16+ /* ========== 2. Termination condition ========== */
17+
18+ for (int i = 0 ; i < 10 ; i++) { // COMPLIANT: `<` is a relational operator
19+ }
20+
21+ for (int i = 0 ; i == 10 ;
22+ i++) { // NON_COMPLIANT: `==` is not a relational operator
23+ }
24+
25+ for (int i = 0 ; j < 10 ; i++) { // NON_COMPLIANT: `j` is not the loop counter
26+ j++;
27+ }
28+
29+ /* ========== 3. Updating expression ========== */
30+
31+ for (int i = 0 ; i < 10 ;
32+ ++i) { // COMPLIANT: Pre-increment operator used as the update expression
33+ }
34+
35+ for (int i = 0 ; i < 10 ; i++) { // COMPLIANT: Post-increment operator used as
36+ // the update expression
37+ }
38+
39+ for (int i = 0 ; i < 10 ; i += 3 ) { // COMPLIANT: Add-and-assign operator used
40+ // as the update expression with loop step 3
41+ }
42+
43+ for (int i = 0 ; i < 10 ;
44+ i *= 2 ) { // NON_COMPLIANT: Mutiplication is not incrementing
45+ }
46+
47+ /* ========== 4. Type of the loop counter and the loop bound ========== */
48+
49+ for (int i = 0 ; i < 10 ; i++) { // COMPLIANT: 0 and 10 are of same type
50+ }
51+
52+ for (unsigned long long int i = 0 ; i < 10 ;
53+ i++) { // COMPLIANT: The loop counter has type bigger than that of the
54+ // loop bound
55+ }
56+
57+ for (int i = 0 ; i < 10ull ;
58+ i++) { // NON_COMPLIANT: The type of the loop counter is not bigger
59+ // than that of the loop bound
60+ }
61+
62+ for (int i = 0 ; i < j;
63+ i++) { // NON_COMPLIANT: The loop bound is not a constant
64+ }
65+
66+ /* ========== 5. Immutability of the loop bound and the loop step ==========
67+ */
68+
69+ for (int i = 0 ; i < 10 ;
70+ i++) { // COMPLIANT: The update expression is an post-increment operation
71+ // and its loop step is always 1
72+ }
73+
74+ for (int i = 0 ; i < 10 ; i += 2 ) { // COMPLIANT: The loop step is always 2
75+ }
76+
77+ for (int i = 0 ; i < 10 ;
78+ i +=
79+ j) { // COMPLIANT: The loop step `j` is not mutated anywhere in the loop
80+ }
81+
82+ for (int i = 0 ; i < 10 ;
83+ i += j) { // NON_COMPLIANT: The loop step `j` is mutated in the loop
84+ j++;
85+ }
86+
87+ for (int i = 0 ; i < 10 ;
88+ i += j, j++) { // NON_COMPLIANT: The loop step `j` is mutated in the loop
89+ }
90+
91+ for (int i = 0 ; i < j; i++) { // COMPLIANT: The loop bound `j` is not mutated
92+ // anywhere in the loop
93+ }
94+
95+ for (int i = 0 ; i < j; i++) { // COMPLIANT: The loop bound `j` is not mutated
96+ // anywhere in the loop
97+ }
98+
99+ for (int i = 0 ; i < j;
100+ i++) { // NON_COMPLIANT: The loop bound `j` is mutated in the loop
101+ j++;
102+ }
103+
104+ /* ========== 6. Existence of pointers to the loop counter, loop bound, and
105+ * loop step ========== */
106+
107+ int k = 10 ;
108+ int l = 2 ;
109+
110+ for (int i = 0 ; i < k; i += l) { // COMPLIANT: The loop counter, bound, and
111+ // step are not taken addresses of
112+ }
113+
114+ for (int i = j; i < k; i += l) { // NON_COMPLIANT: The loop counter is passed
115+ // as a non-const reference
116+ f (j);
117+ }
118+
119+ for (int i = j; i < k; i += l) { // NON_COMPLIANT: The loop counter is passed
120+ // as a non-const pointer
121+ g (&j);
122+ }
123+
124+ for (int i = j; i < k;
125+ i +=
126+ l) { // NON_COMPLIANT: The loop bound is passed as a non-const pointer
127+ f (k);
128+ }
129+
130+ for (int i = j; i < k;
131+ i +=
132+ l) { // NON_COMPLIANT: The loop bound is passed as a non-const pointer
133+ g (&k);
134+ }
135+
136+ for (int i = j; i < k;
137+ i +=
138+ l) { // NON_COMPLIANT: The loop step is passed as a non-const pointer
139+ f (l);
140+ }
141+
142+ for (int i = j; i < k;
143+ i +=
144+ l) { // NON_COMPLIANT: The loop step is passed as a non-const pointer
145+ g (&l);
146+ }
147+ }
0 commit comments