Skip to content

Commit 4599a73

Browse files
committed
Added RULE-15-6
1 parent 0ef25b2 commit 4599a73

15 files changed

Lines changed: 446 additions & 5 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/loop-compound-condition
3+
* @name RULE-15-6: the statement forming the body of a loop shall be a compound statement
4+
* @description if the body of a loop is not enclosed in braces, then this can lead to incorrect
5+
* execution, and is hard for developers to maintain.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-15-6
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from Loop loop
19+
where
20+
not isExcluded(loop, Statements3Package::loopCompoundConditionQuery()) and
21+
not loop.getStmt() instanceof BlockStmt
22+
select loop, "Loop body not enclosed within braces."
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/selection-compound-condition
3+
* @name RULE-15-6: the statement forming the body of a loop shall be a compound statement
4+
* @description if the body of a selection statement is not enclosed in braces, then this can lead
5+
* to incorrect execution, and is hard for developers to maintain.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-15-6
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from IfStmt ifStmt
19+
where
20+
not isExcluded(ifStmt, Statements3Package::selectionCompoundConditionQuery()) and
21+
not ifStmt.getChildStmt() instanceof BlockStmt
22+
select ifStmt, "If statement not enclosed within braces."
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/switch-compound-condition
3+
* @name RULE-15-6: The statement forming the body of a switch shall be a compound statement
4+
* @description If the body of a switch is not enclosed in braces, then this can lead to incorrect
5+
* execution, and is hard for developers to maintain.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-15-6
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.SwitchStatement
18+
19+
from SwitchStmt switch
20+
where
21+
not isExcluded(switch, Statements3Package::switchCompoundConditionQuery()) and
22+
(
23+
switch.getStmt() instanceof ArtificialBlock or
24+
not switch.getStmt() instanceof BlockStmt
25+
)
26+
select switch, "Switch body not enclosed within braces."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:4:3:5:9 | while (...) ... | Loop body not enclosed within braces. |
2+
| test.c:7:3:8:5 | while (...) ... | Loop body not enclosed within braces. |
3+
| test.c:11:3:12:9 | for(...;...;...) ... | Loop body not enclosed within braces. |
4+
| test.c:14:3:15:5 | while (...) ... | Loop body not enclosed within braces. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-15-6/LoopCompoundCondition.ql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:29:3:32:5 | if (...) ... | If statement not enclosed within braces. |
2+
| test.c:34:3:41:7 | if (...) ... | If statement not enclosed within braces. |
3+
| test.c:36:8:41:7 | if (...) ... | If statement not enclosed within braces. |
4+
| test.c:37:5:41:7 | if (...) ... | If statement not enclosed within braces. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-15-6/SelectionCompoundCondition.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:75:3:79:5 | switch (...) ... | Switch body not enclosed within braces. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-15-6/SwitchCompoundCondition.ql
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
void f1();
2+
3+
void f2(int p1) {
4+
while (p1) // NON_COMPLIANT
5+
f1();
6+
7+
while (p1) // NON_COMPLIANT
8+
;
9+
f1();
10+
11+
for (int i = 0; i < p1; i++) // NON_COMPLIANT
12+
f1();
13+
14+
while (p1)
15+
;
16+
{ // NON_COMPLIANT
17+
;
18+
}
19+
20+
while (p1) { // COMPLIANT
21+
;
22+
}
23+
for (int i = 0; i < p1; i++) { // COMPLIANT
24+
;
25+
}
26+
}
27+
28+
void f3(int p1) {
29+
if (p1) // NON_COMPLIANT
30+
;
31+
else
32+
;
33+
34+
if (p1) // NON_COMPLIANT
35+
;
36+
else if (p1) // NON_COMPLIANT
37+
if (p1) // NON_COMPLIANT
38+
39+
if (p1) { // COMPLIANT
40+
;
41+
}
42+
43+
if (p1) { // COMPLIANT
44+
;
45+
} else { // COMPLIANT
46+
;
47+
}
48+
49+
if (p1) { // COMPLIANT
50+
;
51+
} else if (p1) { // COMPLIANT
52+
;
53+
} else { // COMPLIANT
54+
;
55+
}
56+
}
57+
58+
void f4(int p1) {
59+
60+
switch (p1) { // COMPLIANT
61+
case 0:
62+
while (p1) {
63+
;
64+
}
65+
break;
66+
case 1:
67+
if (p1) {
68+
;
69+
}
70+
break;
71+
default:
72+
break;
73+
}
74+
75+
switch (p1) // NON_COMPLIANT
76+
case 0:
77+
while (p1) {
78+
;
79+
}
80+
}

0 commit comments

Comments
 (0)