Skip to content

Commit c76909c

Browse files
committed
Implementation for RULE-15-2
1 parent 80b891f commit c76909c

6 files changed

Lines changed: 56 additions & 48 deletions

File tree

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:5:3:5:10 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.c:5:3:5:10 | goto ... | L1 | test.c:2:1:2:3 | label ...: | label ...: |
2+
| test.c:14:3:14:10 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.c:14:3:14:10 | goto ... | L2 | test.c:12:1:12:3 | label ...: | label ...: |
3+
| test.c:16:3:16:10 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.c:16:3:16:10 | goto ... | L1 | test.c:11:1:11:3 | label ...: | label ...: |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void f1() {
2+
L1:;
3+
goto L2; // COMPLIANT
4+
;
5+
goto L1; // NON_COMPLIANT
6+
7+
L2:;
8+
}
9+
10+
void f2() {
11+
L1:;
12+
L2:
13+
goto L3; // COMPLIANT
14+
goto L2; // NON_COMPLIANT
15+
L3:
16+
goto L1; // NON_COMPLIANT
17+
}

c/misra/src/rules/RULE-15-2/GotoLabelLocationCondition.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
* @description Unconstrained use of goto can lead to unstructured code
55
* @kind problem
66
* @precision very-high
7-
* @problem.severity error
7+
* @problem.severity recommendation
88
* @tags external/misra/id/rule-15-2
9+
* maintainability
10+
* readability
911
* external/misra/obligation/required
1012
*/
1113

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| test.cpp:7:3:7:11 | goto ... | The goto jumps to the label $@ that is not declared later in the same function. | test.cpp:3:1:3:4 | label ...: | bad |
2-
| test.cpp:21:3:21:11 | goto ... | The goto jumps to the label $@ that is not declared later in the same function. | test.cpp:17:1:17:4 | label ...: | bad |
3-
| test.cpp:24:3:24:13 | goto ... | The goto jumps to the label $@ that is not declared later in the same function. | test.cpp:15:1:15:6 | label ...: | sobad |
4-
| test.cpp:31:3:31:11 | goto ... | The goto jumps to the label $@ that is not declared later in the same function. | test.cpp:29:1:29:4 | label ...: | bad |
1+
| test.cpp:7:3:7:11 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.cpp:7:3:7:11 | goto ... | bad | test.cpp:3:1:3:4 | label ...: | label ...: |
2+
| test.cpp:21:3:21:11 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.cpp:21:3:21:11 | goto ... | bad | test.cpp:17:1:17:4 | label ...: | label ...: |
3+
| test.cpp:24:3:24:13 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.cpp:24:3:24:13 | goto ... | sobad | test.cpp:15:1:15:6 | label ...: | label ...: |
4+
| test.cpp:31:3:31:11 | goto ... | The $@ statement jumps to a $@ that is not declared later in the same function. | test.cpp:31:3:31:11 | goto ... | bad | test.cpp:29:1:29:4 | label ...: | label ...: |
Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Provides a library which includes a `problems` predicate for reporting....
2+
* Provides a library which includes a `problems` predicate for reporting goto statements that jump to labels
3+
* declared later in the same funciton.
34
*/
45

56
import cpp
@@ -10,43 +11,26 @@ abstract class GotoStatementConditionSharedQuery extends Query { }
1011

1112
Query getQuery() { result instanceof GotoStatementConditionSharedQuery }
1213

13-
query predicate problems(GotoStmt goto, string message, Stmt target, string targetLabel) {
14-
not isExcluded(goto, getQuery()) and
15-
target = goto.getTarget() and
16-
exists(Location targetLoc, Location gotoLoc |
17-
targetLoc = target.getLocation() and
18-
gotoLoc = goto.getLocation() and
19-
targetLoc.getFile() = gotoLoc.getFile()
20-
|
21-
// Starts on a previous line
22-
targetLoc.getStartLine() < gotoLoc.getEndLine()
23-
or
24-
// Starts on the same line, but an earlier column
25-
targetLoc.getStartLine() = gotoLoc.getEndLine() and
26-
targetLoc.getEndColumn() < gotoLoc.getStartColumn()
27-
)
28-
and message = "The goto jumps to the label $@ that is not declared later in the same function." and targetLabel = target.toString()
14+
query predicate problems(
15+
GotoStmt goto, string message, GotoStmt gotoLocation, string gotoLabel, Stmt target,
16+
string targetLabel
17+
) {
18+
not isExcluded(goto, getQuery()) and
19+
target = goto.getTarget() and
20+
exists(Location targetLoc, Location gotoLoc |
21+
targetLoc = target.getLocation() and
22+
gotoLoc = goto.getLocation() and
23+
targetLoc.getFile() = gotoLoc.getFile()
24+
|
25+
// Starts on a previous line
26+
targetLoc.getStartLine() < gotoLoc.getEndLine()
27+
or
28+
// Starts on the same line, but an earlier column
29+
targetLoc.getStartLine() = gotoLoc.getEndLine() and
30+
targetLoc.getEndColumn() < gotoLoc.getStartColumn()
31+
) and
32+
goto = gotoLocation and
33+
message = "The $@ statement jumps to a $@ that is not declared later in the same function." and
34+
gotoLabel = goto.getName() and
35+
targetLabel = target.toString()
2936
}
30-
31-
32-
33-
34-
35-
// from GotoStmt goto, Stmt target
36-
// where
37-
// not isExcluded(goto, ConditionalsPackage::gotoStatementJumpConditionQuery()) and
38-
// target = goto.getTarget() and
39-
// exists(Location targetLoc, Location gotoLoc |
40-
// targetLoc = target.getLocation() and
41-
// gotoLoc = goto.getLocation() and
42-
// targetLoc.getFile() = gotoLoc.getFile()
43-
// |
44-
// // Starts on a previous line
45-
// targetLoc.getStartLine() < gotoLoc.getEndLine()
46-
// or
47-
// // Starts on the same line, but an earlier column
48-
// targetLoc.getStartLine() = gotoLoc.getEndLine() and
49-
// targetLoc.getEndColumn() < gotoLoc.getStartColumn()
50-
// )
51-
// select goto, "The goto jumps to the label $@ that is not declared later in the same function.",
52-
// target, goto.getName()

rule_packages/c/Statements2.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
"kind": "problem",
1111
"name": "The goto statement shall jump to a label declared later in the same function",
1212
"precision": "very-high",
13-
"severity": "error",
13+
"severity": "recommendation",
1414
"short_name": "GotoLabelLocationCondition",
1515
"shared_implementation_short_name": "GotoStatementCondition",
16-
"tags": []
16+
"tags": [
17+
"maintainability",
18+
"readability"
19+
]
1720
}
1821
],
1922
"title": "The goto statement shall jump to a label declared later in the same function"

0 commit comments

Comments
 (0)