Skip to content

Commit 88febcb

Browse files
committed
added RULE-15-5
1 parent 9748585 commit 88febcb

File tree

8 files changed

+192
-3
lines changed

8 files changed

+192
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/function-return-condition
3+
* @name RULE-15-5: A function should have a single point of exit at the end
4+
* @description Not having a single point of exit in a function can lead to unintentional behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/rule-15-5
9+
* maintainability
10+
* readability
11+
* correctness
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from Function func, string message
19+
where
20+
not isExcluded(func, Statements5Package::functionReturnConditionQuery()) and
21+
count(ReturnStmt return | return.getEnclosingFunction() = func) > 1 and
22+
message = "Function has more than on return statement."
23+
or
24+
not func.getBlock().getLastStmt() instanceof ReturnStmt and
25+
message = "The last statement of the function is not a return statement."
26+
select func, message
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:1:6:1:7 | f1 | Function has more than on return statement. |
2+
| test.c:14:6:14:7 | f3 | The last statement of the function is not a return statement. |
3+
| test.c:21:6:21:7 | f4 | Function has more than on return statement. |
4+
| test.c:21:6:21:7 | f4 | The last statement of the function is not a return statement. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-15-5/FunctionReturnCondition.ql
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
void f1(int p1) { // NON_COMPLIANT
2+
if (p1) {
3+
return;
4+
}
5+
return;
6+
}
7+
8+
void f2(int p1) { // COMPLIANT
9+
if (p1) {
10+
}
11+
return;
12+
}
13+
14+
void f3(int p1) { // NON_COMPLIANT
15+
if (p1) {
16+
}
17+
return;
18+
p1++;
19+
}
20+
21+
void f4(int p1) { // NON_COMPLIANT
22+
if (p1) {
23+
return;
24+
}
25+
return;
26+
p1++;
27+
}

cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Statements1
4646
import Statements2
4747
import Statements3
4848
import Statements4
49+
import Statements5
4950
import Strings1
5051
import Strings2
5152
import Strings3
@@ -97,6 +98,7 @@ newtype TCQuery =
9798
TStatements2PackageQuery(Statements2Query q) or
9899
TStatements3PackageQuery(Statements3Query q) or
99100
TStatements4PackageQuery(Statements4Query q) or
101+
TStatements5PackageQuery(Statements5Query q) or
100102
TStrings1PackageQuery(Strings1Query q) or
101103
TStrings2PackageQuery(Strings2Query q) or
102104
TStrings3PackageQuery(Strings3Query q) or
@@ -148,6 +150,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
148150
isStatements2QueryMetadata(query, queryId, ruleId, category) or
149151
isStatements3QueryMetadata(query, queryId, ruleId, category) or
150152
isStatements4QueryMetadata(query, queryId, ruleId, category) or
153+
isStatements5QueryMetadata(query, queryId, ruleId, category) or
151154
isStrings1QueryMetadata(query, queryId, ruleId, category) or
152155
isStrings2QueryMetadata(query, queryId, ruleId, category) or
153156
isStrings3QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Statements5Query =
7+
TControllingExpInvariantConditionQuery() or
8+
TFunctionReturnConditionQuery() or
9+
TNonVoidFunctionReturnConditionQuery()
10+
11+
predicate isStatements5QueryMetadata(Query query, string queryId, string ruleId, string category) {
12+
query =
13+
// `Query` instance for the `controllingExpInvariantCondition` query
14+
Statements5Package::controllingExpInvariantConditionQuery() and
15+
queryId =
16+
// `@id` for the `controllingExpInvariantCondition` query
17+
"c/misra/controlling-exp-invariant-condition" and
18+
ruleId = "RULE-14-3" and
19+
category = "required"
20+
or
21+
query =
22+
// `Query` instance for the `functionReturnCondition` query
23+
Statements5Package::functionReturnConditionQuery() and
24+
queryId =
25+
// `@id` for the `functionReturnCondition` query
26+
"c/misra/function-return-condition" and
27+
ruleId = "RULE-15-5" and
28+
category = "advisory"
29+
or
30+
query =
31+
// `Query` instance for the `nonVoidFunctionReturnCondition` query
32+
Statements5Package::nonVoidFunctionReturnConditionQuery() and
33+
queryId =
34+
// `@id` for the `nonVoidFunctionReturnCondition` query
35+
"c/misra/non-void-function-return-condition" and
36+
ruleId = "RULE-17-4" and
37+
category = "mandatory"
38+
}
39+
40+
module Statements5Package {
41+
Query controllingExpInvariantConditionQuery() {
42+
//autogenerate `Query` type
43+
result =
44+
// `Query` type for `controllingExpInvariantCondition` query
45+
TQueryC(TStatements5PackageQuery(TControllingExpInvariantConditionQuery()))
46+
}
47+
48+
Query functionReturnConditionQuery() {
49+
//autogenerate `Query` type
50+
result =
51+
// `Query` type for `functionReturnCondition` query
52+
TQueryC(TStatements5PackageQuery(TFunctionReturnConditionQuery()))
53+
}
54+
55+
Query nonVoidFunctionReturnConditionQuery() {
56+
//autogenerate `Query` type
57+
result =
58+
// `Query` type for `nonVoidFunctionReturnCondition` query
59+
TQueryC(TStatements5PackageQuery(TNonVoidFunctionReturnConditionQuery()))
60+
}
61+
}

rule_packages/c/Statements5.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-14-3": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "If a controlling expression has an invariant value then it is possible that there is a programming error.",
10+
"kind": "problem",
11+
"name": "Controlling expressions shall not be invariant",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "ControllingExpInvariantCondition",
15+
"tags": [
16+
"correctness",
17+
"maintainability",
18+
"readability"
19+
]
20+
}
21+
],
22+
"title": "Controlling expressions shall not be invariant"
23+
},
24+
"RULE-15-5": {
25+
"properties": {
26+
"obligation": "advisory"
27+
},
28+
"queries": [
29+
{
30+
"description": "Not having a single point of exit in a function can lead to unintentional behaviour.",
31+
"kind": "problem",
32+
"name": "A function should have a single point of exit at the end",
33+
"precision": "very-high",
34+
"severity": "recommendation",
35+
"short_name": "FunctionReturnCondition",
36+
"tags": [
37+
"maintainability",
38+
"readability",
39+
"correctness"
40+
]
41+
}
42+
],
43+
"title": "A function should have a single point of exit at the end"
44+
},
45+
"RULE-17-4": {
46+
"properties": {
47+
"obligation": "mandatory"
48+
},
49+
"queries": [
50+
{
51+
"description": "Not returning with an expression from a non-void function can lead to undefined behaviour.",
52+
"kind": "problem",
53+
"name": "All exit paths from a function with non-void return type shall have an explicit return statement",
54+
"precision": "very-high",
55+
"severity": "error",
56+
"short_name": "NonVoidFunctionReturnCondition",
57+
"tags": [
58+
"correctness",
59+
"maintainability",
60+
"readability"
61+
]
62+
}
63+
],
64+
"title": "All exit paths from a function with non-void return type shall have an explicit return statement with an expression"
65+
}
66+
}
67+
}

rules.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,13 @@ c,MISRA-C-2012,RULE-13-5,Yes,Required,,,The right hand operand of a logical && o
695695
c,MISRA-C-2012,RULE-13-6,Yes,Mandatory,,,The operand of the sizeof operator shall not contain any expressiosn which has potential side effects,M5-3-4,SideEffects1,Import,
696696
c,MISRA-C-2012,RULE-14-1,Yes,Required,,,A loop counter shall not have essentially floating type,FLP30-C A6-5-2,Types,Hard,
697697
c,MISRA-C-2012,RULE-14-2,Yes,Required,,,A for loop shall be well-formed,M6-5-1...M6-5-6,Statements4,Medium,
698-
c,MISRA-C-2012,RULE-14-3,Yes,Required,,,Controlling expressions shall not be invariant,,Statements,Medium,
698+
c,MISRA-C-2012,RULE-14-3,Yes,Required,,,Controlling expressions shall not be invariant,,Statements5,Medium,
699699
c,MISRA-C-2012,RULE-14-4,Yes,Required,,,The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type,A5-0-2,Statements4,Medium,
700700
c,MISRA-C-2012,RULE-15-1,No,Advisory,,,The goto statement should not be used,A6-6-1,,Import,
701701
c,MISRA-C-2012,RULE-15-2,Yes,Required,,,The goto statement shall jump to a label declared later in the same function,M6-6-2,Statements2,Import,
702702
c,MISRA-C-2012,RULE-15-3,Yes,Required,,,"Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement",M6-6-1,Statements2,Import,
703703
c,MISRA-C-2012,RULE-15-4,Yes,Advisory,,,There should be no more than one break or goto statement used to terminate any iteration statement,,Statements2,Medium,
704-
c,MISRA-C-2012,RULE-15-5,Yes,Advisory,,,A function should have a single point of exit at the end,,Statements,Medium,
704+
c,MISRA-C-2012,RULE-15-5,Yes,Advisory,,,A function should have a single point of exit at the end,,Statements5,Medium,
705705
c,MISRA-C-2012,RULE-15-6,Yes,Required,,,The body of an iteration-statement or a selection-statement shall be a compund-statement,M6-3-1,Statements3,Import,
706706
c,MISRA-C-2012,RULE-15-7,Yes,Required,,,All if / else if constructs shall be terminated with an else statement,M6-4-2,Statements3,Import,
707707
c,MISRA-C-2012,RULE-16-1,Yes,Required,,,All switch statements shall be well-formed,M6-4-3,Statements3,Import,
@@ -714,7 +714,7 @@ c,MISRA-C-2012,RULE-16-7,Yes,Required,,,A switch-expression shall not have essen
714714
c,MISRA-C-2012,RULE-17-1,Yes,Required,,,The features of <stdarg.h> shall not be used,,Banned,Easy,
715715
c,MISRA-C-2012,RULE-17-2,Yes,Required,,,"Functions shall not call themselves, either directly or indirectly",A7-5-2,Statements3,Import,
716716
c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations6,Medium,
717-
c,MISRA-C-2012,RULE-17-4,Yes,Mandatory,,,All exit paths from a function with non-void return type shall have an explicit return statement with an expression,MSC52-CPP,Statements,Medium,
717+
c,MISRA-C-2012,RULE-17-4,Yes,Mandatory,,,All exit paths from a function with non-void return type shall have an explicit return statement with an expression,MSC52-CPP,Statements5,Medium,
718718
c,MISRA-C-2012,RULE-17-5,Yes,Advisory,,,The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements,,Contracts,Hard,
719719
c,MISRA-C-2012,RULE-17-6,No,Mandatory,,,The declaration of an array parameter shall not contain the static keyword between the [ ],,,,
720720
c,MISRA-C-2012,RULE-17-7,Yes,Required,,,The value returned by a function having non-void return type shall be used,A0-1-2,Contracts,Import,

0 commit comments

Comments
 (0)