Skip to content

Commit ebae3e6

Browse files
committed
EssentialTypes: Implement Rule 14.1
Adds a query that finds loop counters which are essentially floating type.
1 parent ebc07fe commit ebae3e6

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/loop-over-essentially-float-type
3+
* @name RULE-14-1: A loop counter shall not have essentially floating type
4+
* @description
5+
* @kind problem
6+
* @precision high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-14-1
9+
* external/misra/obligation/required
10+
*/
11+
12+
import cpp
13+
import codingstandards.c.misra
14+
import codingstandards.c.misra.EssentialTypes
15+
import codingstandards.cpp.Loops
16+
17+
from ForStmt forLoop, Variable loopIterationVariable
18+
where
19+
not isExcluded(loopIterationVariable, EssentialTypesPackage::loopOverEssentiallyFloatTypeQuery()) and
20+
getAnIterationVariable(forLoop) = loopIterationVariable and
21+
getEssentialTypeCategory(loopIterationVariable.getType()) = EssentiallyFloatingType()
22+
select loopIterationVariable,
23+
"Loop iteration variable " + loopIterationVariable.getName() + " is essentially Floating type."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:4:14:4:14 | f | Loop iteration variable f is essentially Floating type. |
2+
| test.c:6:15:6:15 | d | Loop iteration variable d is essentially Floating type. |
3+
| test.c:8:18:8:18 | f | Loop iteration variable f is essentially Floating type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-14-1/LoopOverEssentiallyFloatType.ql

c/misra/test/rules/RULE-14-1/test.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
typedef float float32_t;
2+
3+
void test_floating_point_loop() {
4+
for (float f = 0.0F; f < 10.0F; f += 0.2F) { // NON_COMPLIANT
5+
}
6+
for (double d = 0.0F; d < 10.0F; d += 0.2F) { // NON_COMPLIANT
7+
}
8+
for (float32_t f = 0.0F; f < 10.0F; f += 0.2F) { // NON_COMPLIANT
9+
}
10+
}
11+
12+
void test_non_floating_point_loop() {
13+
for (int i = 0; i < 10; i++) { // COMPLIANT
14+
}
15+
}

0 commit comments

Comments
 (0)