Skip to content

Commit 490b3fe

Browse files
committed
Added RULE-15-7 and moved M6-4-2 to shared folder
1 parent 6977d61 commit 490b3fe

File tree

14 files changed

+121
-12
lines changed

14 files changed

+121
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:16:3:20:3 | if (...) ... | The $@ construct does not terminate with else statement. | test.c:16:3:20:3 | if (...) ... | `if...else` |
2+
| test.c:33:5:37:5 | if (...) ... | The $@ construct does not terminate with else statement. | test.c:33:5:37:5 | if (...) ... | `if...else` |
3+
| test.c:45:3:55:3 | if (...) ... | The $@ construct does not terminate with else statement. | test.c:45:3:55:3 | if (...) ... | `if...else` |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.ifelseterminationconstruct.IfElseTerminationConstruct
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
void f1(int p1) {
2+
3+
if (p1) { // COMPLIANT
4+
;
5+
} else if (p1) {
6+
;
7+
} else {
8+
;
9+
}
10+
}
11+
12+
void f2(int p1) {
13+
if (p1) { // COMPLIANT
14+
;
15+
}
16+
if (p1) { // NON_COMPLIANT
17+
;
18+
} else if (p1) {
19+
;
20+
}
21+
}
22+
23+
void f3(int p1) {
24+
25+
if (p1) { // COMPLIANT
26+
;
27+
} else {
28+
;
29+
}
30+
if (p1) { // COMPLIANT
31+
;
32+
} else if (p1) {
33+
if (p1) { // NON_COMPLIANT
34+
;
35+
} else if (p1) {
36+
;
37+
}
38+
} else {
39+
;
40+
}
41+
}
42+
43+
void f4(int p1) {
44+
45+
if (p1) { // NON_COMPLIANT
46+
;
47+
} else if (p1) {
48+
if (p1) { // COMPLIANT
49+
;
50+
} else if (p1) {
51+
;
52+
} else {
53+
;
54+
}
55+
}
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/if-else-end-condition
3+
* @name RULE-15-7: All if / else if constructs shall be terminated with an else statement
4+
* @description Terminating an `if...else` construct is a defensive programming technique.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/rule-15-7
9+
* readability
10+
* maintainability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.ifelseterminationconstruct.IfElseTerminationConstruct
17+
18+
class IfElseEndConditionQuery extends IfElseTerminationConstructSharedQuery {
19+
IfElseEndConditionQuery() {
20+
this = Statements3Package::ifElseEndConditionQuery()
21+
}
22+
}

cpp/autosar/src/rules/M6-4-2/IfElseTerminationCondition.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
import cpp
1717
import codingstandards.cpp.autosar
18+
import codingstandards.cpp.rules.ifelseterminationconstruct.IfElseTerminationConstruct
1819

19-
from IfStmt ifStmt, IfStmt ifElse
20-
where
21-
not isExcluded(ifStmt, ConditionalsPackage::ifElseTerminationConditionQuery()) and
22-
ifStmt.getElse() = ifElse and
23-
not ifElse.hasElse()
24-
select ifStmt, "The $@ if statement does not terminate with an else construct.", ifElse, "if...else"
20+
class IfElseTerminationConditionQuery extends IfElseTerminationConstructSharedQuery {
21+
IfElseTerminationConditionQuery() {
22+
this = ConditionalsPackage::ifElseTerminationConditionQuery()
23+
}
24+
}

cpp/autosar/test/rules/M6-4-2/IfElseTerminationCondition.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

cpp/autosar/test/rules/M6-4-2/IfElseTerminationCondition.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/ifelseterminationconstruct/IfElseTerminationConstruct.ql
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting....
3+
*/
4+
5+
import cpp
6+
import codingstandards.cpp.Customizations
7+
import codingstandards.cpp.Exclusions
8+
9+
abstract class IfElseTerminationConstructSharedQuery extends Query { }
10+
11+
Query getQuery() { result instanceof IfElseTerminationConstructSharedQuery }
12+
13+
query predicate problems(IfStmt ifStmt, string message, IfStmt ifLocation, string ifElseString) {
14+
not isExcluded(ifStmt, getQuery()) and
15+
exists(IfStmt ifElse |
16+
ifStmt.getElse() = ifElse and
17+
not ifElse.hasElse()
18+
) and
19+
ifLocation = ifStmt and
20+
message = "The $@ construct does not terminate with else statement." and
21+
ifElseString = "`if...else`"
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.cpp:23:3:27:3 | if (...) ... | The $@ construct does not terminate with else statement. | test.cpp:23:3:27:3 | if (...) ... | `if...else` |
2+
| test.cpp:43:5:47:5 | if (...) ... | The $@ construct does not terminate with else statement. | test.cpp:43:5:47:5 | if (...) ... | `if...else` |
3+
| test.cpp:57:3:67:3 | if (...) ... | The $@ construct does not terminate with else statement. | test.cpp:57:3:67:3 | if (...) ... | `if...else` |

0 commit comments

Comments
 (0)