Skip to content

Commit e3c2298

Browse files
committed
Added implementation for 15-3
1 parent e71e36d commit e3c2298

4 files changed

Lines changed: 103 additions & 11 deletions

File tree

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
/**
22
* @id c/misra/goto-label-block-condition
3-
* @name RULE-15-3: Any label referenced by a goto statement shall be declared in the same block, or in any block
3+
* @name RULE-15-3: The goto statement and any of its label shall be declared or enclosed in the same block.
44
* @description Any label referenced by a goto statement shall be declared in the same block, or in
55
* any block enclosing the goto statement
66
* @kind problem
7-
* @precision very-high
8-
* @problem.severity error
7+
* @precision high
8+
* @problem.severity recommendation
99
* @tags external/misra/id/rule-15-3
10+
* maintainability
11+
* readability
1012
* external/misra/obligation/required
1113
*/
1214

1315
import cpp
1416
import codingstandards.c.misra
1517

16-
from
18+
from GotoStmt goto
1719
where
18-
not isExcluded(x, Statements2Package::gotoLabelBlockConditionQuery()) and
19-
select
20+
not isExcluded(goto, Statements2Package::gotoLabelBlockConditionQuery()) and
21+
not goto.getEnclosingBlock+() = goto.getTarget().getEnclosingBlock()
22+
or
23+
exists(SwitchStmt switch, int caseLocation, int nextCaseLocation |
24+
switch.getAChild*() = goto and
25+
switch.getASwitchCase().getLocation().getStartLine() = caseLocation and
26+
switch.getASwitchCase().getNextSwitchCase().getLocation().getStartLine() = nextCaseLocation and
27+
goto.getLocation().getStartLine() > caseLocation and
28+
goto.getLocation().getStartLine() < nextCaseLocation and
29+
(
30+
goto.getTarget().getLocation().getStartLine() < caseLocation
31+
or
32+
goto.getTarget().getLocation().getStartLine() > nextCaseLocation
33+
) and
34+
goto.getTarget().getLocation().getStartLine() > switch.getLocation().getStartLine()
35+
)
36+
select goto, "The $@ statement and its $@ are not declared or enclosed in the same block.", goto,
37+
"goto", goto.getTarget(), "label"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
No expected results have yet been specified
1+
| test.c:2:3:2:10 | goto ... | The $@ statement and its $@ are not declared or enclosed in the same block. | test.c:2:3:2:10 | goto ... | goto | test.c:4:3:4:5 | label ...: | label |
2+
| test.c:37:3:37:10 | goto ... | The $@ statement and its $@ are not declared or enclosed in the same block. | test.c:37:3:37:10 | goto ... | goto | test.c:41:3:41:5 | label ...: | label |
3+
| test.c:52:5:52:12 | goto ... | The $@ statement and its $@ are not declared or enclosed in the same block. | test.c:52:5:52:12 | goto ... | goto | test.c:55:3:55:5 | label ...: | label |
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
void f1() {
2+
goto L1;
3+
for (int i = 0; i < 100; i++) {
4+
L1: // NON_COMPLIANT
5+
}
6+
}
7+
8+
void f2() {
9+
int i = 0;
10+
if (i >= 0) {
11+
for (int j = 0; j < 10; j++) {
12+
goto L2;
13+
}
14+
}
15+
L2: // COMPLIANT
16+
}
17+
18+
void f3() {
19+
int i = 0;
20+
if (i >= 0) {
21+
for (int j = 0; j < 10; j++) {
22+
goto L3;
23+
L3: // COMPLIANT
24+
}
25+
}
26+
}
27+
28+
void f4() {
29+
int i = 0;
30+
L4: // COMPLIANT
31+
if (i >= 0) {
32+
goto L4;
33+
}
34+
}
35+
36+
void f5(int p) {
37+
goto L1;
38+
39+
switch (p) {
40+
case 0:
41+
L1:; // NON_COMPLIANT
42+
break;
43+
default:
44+
break;
45+
}
46+
}
47+
48+
void f6(int p) {
49+
50+
switch (p) {
51+
case 0:
52+
goto L1;
53+
break;
54+
default:
55+
L1: // NON_COMPLIANT
56+
break;
57+
}
58+
}
59+
60+
void f7(int p) {
61+
L1: // COMPLIANT
62+
switch (p) {
63+
case 0:
64+
goto L1;
65+
break;
66+
default:
67+
break;
68+
}
69+
}

rule_packages/c/Statements2.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@
2929
{
3030
"description": "Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement",
3131
"kind": "problem",
32-
"name": "Any label referenced by a goto statement shall be declared in the same block, or in any block",
33-
"precision": "very-high",
34-
"severity": "error",
32+
"name": "The goto statement and any of its label shall be declared or enclosed in the same block. ",
33+
"precision": "high",
34+
"severity": "recommendation",
3535
"short_name": "GotoLabelBlockCondition",
36-
"tags": []
36+
"tags": [
37+
"maintainability",
38+
"readability"
39+
]
3740
}
3841
],
3942
"title": "Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement"

0 commit comments

Comments
 (0)