Skip to content

Commit d37bc70

Browse files
committed
Use different range predicate and update tests
1 parent 59a4096 commit d37bc70

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ private newtype TAlertType =
187187
forLoopCondition = forLoop.getCondition() and
188188
exists(Expr loopCounter, Expr loopBound |
189189
loopCounter = forLoopCondition.getLoopCounter() and
190-
loopBound = forLoopCondition.getLoopBound() and
191-
upperBound(loopCounter.getFullyConverted()) < upperBound(loopBound.getFullyConverted())
190+
loopBound = forLoopCondition.getLoopBound()
191+
|
192+
typeUpperBound(loopCounter.getType()) < upperBound(loopBound.getFullyConverted())
192193
)
193194
} or
194195
/* 5-1-1. The loop bound is a variable that is mutated in the for loop. */
Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
| test2.cpp:8:5:11:5 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test2.cpp:8:14:8:14 | i | counter variable | test2.cpp:9:9:9:9 | call to f | expression |
2-
| test2.cpp:8:5:11:5 | for(...;...;...) ... | The $@ may be mutated in a location other than its update expression. | test2.cpp:8:14:8:14 | i | counter variable | test2.cpp:8:14:8:14 | i | N/A |
3-
| test.cpp:22:3:24:3 | for(...;...;...) ... | The $@ is not of an integer type. | test.cpp:22:14:22:14 | i | counter variable | test.cpp:22:14:22:14 | i | N/A |
4-
| test.cpp:31:3:33:3 | for(...;...;...) ... | The $@ does not compare the counter variable to an expression using a relational operator. | test.cpp:31:19:31:25 | ... == ... | loop condition | test.cpp:31:19:31:25 | ... == ... | N/A |
5-
| test.cpp:35:3:37:3 | for(...;...;...) ... | The $@ does not compare the counter variable to an expression using a relational operator. | test.cpp:35:19:35:24 | ... < ... | loop condition | test.cpp:35:19:35:24 | ... < ... | N/A |
6-
| test.cpp:76:3:78:3 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test.cpp:76:12:76:12 | i | counter variable | test.cpp:77:8:77:13 | ... *= ... | expression |
7-
| test.cpp:90:3:93:3 | for(...;...;...) ... | The $@ has a smaller type than that of the $@. | test.cpp:90:19:90:19 | i | counter variable | test.cpp:90:23:90:27 | 10 | loop bound |
8-
| test.cpp:115:3:118:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:116:13:116:13 | j | loop step | test.cpp:117:5:117:7 | ... ++ | mutated |
9-
| test.cpp:120:3:122:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:121:13:121:13 | j | loop step | test.cpp:121:8:121:18 | ... , ... | mutated |
10-
| test.cpp:120:3:122:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:121:13:121:13 | j | loop step | test.cpp:121:16:121:18 | ... ++ | mutated |
11-
| test.cpp:120:3:122:3 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test.cpp:120:12:120:12 | i | counter variable | test.cpp:121:8:121:18 | ... , ... | expression |
12-
| test.cpp:132:3:135:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:132:23:132:23 | j | loop bound | test.cpp:134:5:134:7 | ... ++ | mutated |
13-
| test.cpp:139:3:142:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:139:23:139:23 | k | loop bound | test.cpp:141:5:141:26 | ... += ... | mutated |
14-
| test.cpp:144:3:147:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:145:13:145:13 | l | loop step | test.cpp:146:5:146:26 | ... += ... | mutated |
15-
| test.cpp:149:3:151:3 | for(...;...;...) ... | The $@ is a $@. | test.cpp:149:23:149:24 | call to h1 | loop bound | test.cpp:149:23:149:24 | call to h1 | non-const expression |
16-
| test.cpp:157:3:159:3 | for(...;...;...) ... | The $@ is a $@. | test.cpp:158:13:158:14 | call to h1 | loop step | test.cpp:158:13:158:14 | call to h1 | non-const expression |
17-
| test.cpp:172:3:175:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:172:19:172:19 | i | loop counter | test.cpp:172:19:172:19 | i | N/A |
18-
| test.cpp:177:3:180:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:177:19:177:19 | i | loop counter | test.cpp:177:19:177:19 | i | N/A |
19-
| test.cpp:192:3:195:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:192:23:192:23 | k | loop bound | test.cpp:192:23:192:23 | k | N/A |
20-
| test.cpp:197:3:200:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:197:23:197:23 | k | loop bound | test.cpp:197:23:197:23 | k | N/A |
21-
| test.cpp:212:3:215:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:212:31:212:31 | l | loop step | test.cpp:212:31:212:31 | l | N/A |
22-
| test.cpp:217:3:220:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:217:31:217:31 | l | loop step | test.cpp:217:31:217:31 | l | N/A |
23-
| test.cpp:232:3:235:3 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test.cpp:232:12:232:12 | i | counter variable | test.cpp:234:5:234:6 | call to f1 | expression |
24-
| test.cpp:232:3:235:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:232:19:232:19 | i | loop counter | test.cpp:232:19:232:19 | i | N/A |
25-
| test.cpp:232:3:235:3 | for(...;...;...) ... | The $@ may be mutated in a location other than its update expression. | test.cpp:232:12:232:12 | i | counter variable | test.cpp:232:12:232:12 | i | N/A |
26-
| test.cpp:237:3:240:3 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test.cpp:237:12:237:12 | i | counter variable | test.cpp:239:5:239:6 | call to g1 | expression |
27-
| test.cpp:237:3:240:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:237:19:237:19 | i | loop counter | test.cpp:237:19:237:19 | i | N/A |
28-
| test.cpp:237:3:240:3 | for(...;...;...) ... | The $@ may be mutated in a location other than its update expression. | test.cpp:237:12:237:12 | i | counter variable | test.cpp:237:12:237:12 | i | N/A |
29-
| test.cpp:252:3:255:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:252:23:252:23 | k | loop bound | test.cpp:254:5:254:6 | call to f1 | mutated |
30-
| test.cpp:252:3:255:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:252:23:252:23 | k | loop bound | test.cpp:252:23:252:23 | k | N/A |
31-
| test.cpp:257:3:260:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:257:23:257:23 | k | loop bound | test.cpp:259:5:259:6 | call to g1 | mutated |
32-
| test.cpp:257:3:260:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:257:23:257:23 | k | loop bound | test.cpp:257:23:257:23 | k | N/A |
33-
| test.cpp:273:3:276:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:273:31:273:31 | l | loop step | test.cpp:275:5:275:6 | call to f1 | mutated |
34-
| test.cpp:273:3:276:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:273:31:273:31 | l | loop step | test.cpp:273:31:273:31 | l | N/A |
35-
| test.cpp:278:3:281:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that is $@ in the loop. | test.cpp:278:31:278:31 | l | loop step | test.cpp:280:5:280:6 | call to g1 | mutated |
36-
| test.cpp:278:3:281:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:278:31:278:31 | l | loop step | test.cpp:278:31:278:31 | l | N/A |
1+
| test.cpp:23:3:25:3 | for(...;...;...) ... | The $@ is not of an integer type. | test.cpp:23:14:23:14 | i | counter variable | test.cpp:23:14:23:14 | i | N/A |
2+
| test.cpp:32:3:34:3 | for(...;...;...) ... | The $@ does not determine termination based only on a comparison against the value of the counter variable. | test.cpp:32:19:32:25 | ... == ... | loop condition | test.cpp:32:19:32:25 | ... == ... | N/A |
3+
| test.cpp:36:3:38:3 | for(...;...;...) ... | The $@ does not determine termination based only on a comparison against the value of the counter variable. | test.cpp:36:19:36:24 | ... < ... | loop condition | test.cpp:36:19:36:24 | ... < ... | N/A |
4+
| test.cpp:77:3:79:3 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test.cpp:77:12:77:12 | i | counter variable | test.cpp:78:8:78:13 | ... *= ... | expression |
5+
| test.cpp:91:3:94:3 | for(...;...;...) ... | The $@ has a smaller type than that of the $@. | test.cpp:91:21:91:21 | i | counter variable | test.cpp:91:25:91:53 | call to max | loop bound |
6+
| test.cpp:116:3:119:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:117:13:117:13 | j | loop step | test.cpp:118:5:118:7 | ... ++ | mutated |
7+
| test.cpp:121:3:123:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:122:13:122:13 | j | loop step | test.cpp:122:8:122:18 | ... , ... | mutated |
8+
| test.cpp:121:3:123:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:122:13:122:13 | j | loop step | test.cpp:122:16:122:18 | ... ++ | mutated |
9+
| test.cpp:121:3:123:3 | for(...;...;...) ... | The $@ is not updated with an $@ other than addition or subtraction. | test.cpp:121:12:121:12 | i | counter variable | test.cpp:122:8:122:18 | ... , ... | expression |
10+
| test.cpp:133:3:136:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:133:23:133:23 | j | loop bound | test.cpp:135:5:135:7 | ... ++ | mutated |
11+
| test.cpp:140:3:143:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:140:23:140:23 | k | loop bound | test.cpp:142:5:142:26 | ... += ... | mutated |
12+
| test.cpp:145:3:148:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:146:13:146:13 | l | loop step | test.cpp:147:5:147:26 | ... += ... | mutated |
13+
| test.cpp:150:3:152:3 | for(...;...;...) ... | The $@ is a $@. | test.cpp:150:23:150:24 | call to h1 | loop bound | test.cpp:150:23:150:24 | call to h1 | non-const expression |
14+
| test.cpp:158:3:160:3 | for(...;...;...) ... | The $@ is a $@. | test.cpp:159:13:159:14 | call to h1 | loop step | test.cpp:159:13:159:14 | call to h1 | non-const expression |
15+
| test.cpp:173:3:176:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:173:19:173:19 | i | loop counter | test.cpp:173:19:173:19 | i | N/A |
16+
| test.cpp:178:3:181:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:178:19:178:19 | i | loop counter | test.cpp:178:19:178:19 | i | N/A |
17+
| test.cpp:193:3:196:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:193:23:193:23 | k | loop bound | test.cpp:193:23:193:23 | k | N/A |
18+
| test.cpp:198:3:201:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:198:23:198:23 | k | loop bound | test.cpp:198:23:198:23 | k | N/A |
19+
| test.cpp:213:3:216:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:213:31:213:31 | l | loop step | test.cpp:213:31:213:31 | l | N/A |
20+
| test.cpp:218:3:221:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:218:31:218:31 | l | loop step | test.cpp:218:31:218:31 | l | N/A |
21+
| test.cpp:233:3:236:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:233:19:233:19 | i | loop counter | test.cpp:233:19:233:19 | i | N/A |
22+
| test.cpp:233:3:236:3 | for(...;...;...) ... | The $@ may be mutated in a location other than its update expression. | test.cpp:233:12:233:12 | i | counter variable | test.cpp:233:12:233:12 | i | N/A |
23+
| test.cpp:238:3:241:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:238:19:238:19 | i | loop counter | test.cpp:238:19:238:19 | i | N/A |
24+
| test.cpp:238:3:241:3 | for(...;...;...) ... | The $@ may be mutated in a location other than its update expression. | test.cpp:238:12:238:12 | i | counter variable | test.cpp:238:12:238:12 | i | N/A |
25+
| test.cpp:253:3:256:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:253:23:253:23 | k | loop bound | test.cpp:255:5:255:6 | call to f1 | mutated |
26+
| test.cpp:253:3:256:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:253:23:253:23 | k | loop bound | test.cpp:253:23:253:23 | k | N/A |
27+
| test.cpp:258:3:261:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:258:23:258:23 | k | loop bound | test.cpp:260:5:260:6 | call to g1 | mutated |
28+
| test.cpp:258:3:261:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:258:23:258:23 | k | loop bound | test.cpp:258:23:258:23 | k | N/A |
29+
| test.cpp:274:3:277:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:274:31:274:31 | l | loop step | test.cpp:276:5:276:6 | call to f1 | mutated |
30+
| test.cpp:274:3:277:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:274:31:274:31 | l | loop step | test.cpp:274:31:274:31 | l | N/A |
31+
| test.cpp:279:3:282:3 | for(...;...;...) ... | The $@ is a non-const expression, or a variable that may be $@ in the loop. | test.cpp:279:31:279:31 | l | loop step | test.cpp:281:5:281:6 | call to g1 | mutated |
32+
| test.cpp:279:3:282:3 | for(...;...;...) ... | The $@ is taken as a mutable reference or its address to a mutable pointer. | test.cpp:279:31:279:31 | l | loop step | test.cpp:279:31:279:31 | l | N/A |

cpp/misra/test/rules/RULE-9-5-1/test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <limits>
12
#include <string>
23
#include <string_view>
34

@@ -87,7 +88,7 @@ int main() {
8788
// loop bound
8889
}
8990

90-
for (int i = 0; i < 10ull;
91+
for (short i = 0; i < std::numeric_limits<int>::max();
9192
i++) { // NON_COMPLIANT: The type of the loop counter is not bigger
9293
// than that of the loop bound
9394
}

0 commit comments

Comments
 (0)