Skip to content

Commit be0eca9

Browse files
committed
Declarations4: add RULE-8-2
1 parent 30811eb commit be0eca9

File tree

7 files changed

+103
-2
lines changed

7 files changed

+103
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @id c/misra/function-types-not-in-prototype-form
3+
* @name RULE-8-2: Function types shall be in prototype form with named parameters
4+
* @description Omission of parameter types or names prevents the compiler from doing type checking
5+
* when those functions are used and therefore may result in undefined behaviour.
6+
* @kind problem
7+
* @precision medium
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-2
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
/**
19+
* `Parameter`s without names
20+
*/
21+
class UnnamedParameter extends Parameter {
22+
UnnamedParameter() { not this.isNamed() }
23+
}
24+
25+
from Function f, string msg
26+
where
27+
not isExcluded(f, Declarations4Package::functionTypesNotInPrototypeFormQuery()) and
28+
f instanceof InterestingIdentifiers and
29+
(
30+
f.getAParameter() instanceof UnnamedParameter and
31+
msg = "Function " + f + " declares parameter that is unnamed."
32+
or
33+
//void keyword not present in function signature, no way to tell which
34+
not exists(f.getAParameter()) and
35+
msg =
36+
"Function " + f +
37+
" may not specify all parameter types or may not specifiy void for no parameters present."
38+
or
39+
exists(Parameter p |
40+
p.getFunction() = f and
41+
not p.getFile() = f.getFile() and
42+
msg = "Function " + f + " declares parameter in unsupported declaration list."
43+
)
44+
) and
45+
not f.isInMacroExpansion()
46+
select f, msg
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.c:2:6:2:7 | f0 | Function f0 may not specify all parameter types or may not specifiy void for no parameters present. |
2+
| test.c:3:6:3:7 | f1 | Function f1 declares parameter that is unnamed. |
3+
| test.c:4:6:4:7 | f2 | Function f2 may not specify all parameter types or may not specifiy void for no parameters present. |
4+
| test.c:5:6:5:7 | f3 | Function f3 may not specify all parameter types or may not specifiy void for no parameters present. |
5+
| test.c:7:5:7:6 | f5 | Function f5 declares parameter in unsupported declaration list. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-2/FunctionTypesNotInPrototypeForm.ql

c/misra/test/rules/RULE-8-2/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void f(int x); // COMPLIANT
2+
void f0(void); // COMPLIANT[FALSE_POSITIVE]
3+
void f1(int); // NON_COMPLIANT
4+
void f2(); // NON_COMPLIANT
5+
void f3(x); // NON_COMPLIANT
6+
void f4(const x); // NON_COMPLIANT[FALSE_NEGATIVE]
7+
int f5(x) // NON_COMPLIANT
8+
int x;
9+
{ return 1; }

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ import cpp
33
import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

6-
newtype Declarations4Query = TIdentifierWithExternalLinkageOneDefinitionQuery()
6+
newtype Declarations4Query =
7+
TFunctionTypesNotInPrototypeFormQuery() or
8+
TIdentifierWithExternalLinkageOneDefinitionQuery()
79

810
predicate isDeclarations4QueryMetadata(Query query, string queryId, string ruleId) {
11+
query =
12+
// `Query` instance for the `functionTypesNotInPrototypeForm` query
13+
Declarations4Package::functionTypesNotInPrototypeFormQuery() and
14+
queryId =
15+
// `@id` for the `functionTypesNotInPrototypeForm` query
16+
"c/misra/function-types-not-in-prototype-form" and
17+
ruleId = "RULE-8-2"
18+
or
919
query =
1020
// `Query` instance for the `identifierWithExternalLinkageOneDefinition` query
1121
Declarations4Package::identifierWithExternalLinkageOneDefinitionQuery() and
@@ -16,6 +26,13 @@ predicate isDeclarations4QueryMetadata(Query query, string queryId, string ruleI
1626
}
1727

1828
module Declarations4Package {
29+
Query functionTypesNotInPrototypeFormQuery() {
30+
//autogenerate `Query` type
31+
result =
32+
// `Query` type for `functionTypesNotInPrototypeForm` query
33+
TQueryC(TDeclarations4PackageQuery(TFunctionTypesNotInPrototypeFormQuery()))
34+
}
35+
1936
Query identifierWithExternalLinkageOneDefinitionQuery() {
2037
//autogenerate `Query` type
2138
result =

rule_packages/c/Declarations4.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
{
22
"MISRA-C-2012": {
3+
"RULE-8-2": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "Omission of parameter types or names prevents the compiler from doing type checking when those functions are used and therefore may result in undefined behaviour.",
10+
"kind": "problem",
11+
"name": "Function types shall be in prototype form with named parameters",
12+
"precision": "medium",
13+
"severity": "error",
14+
"short_name": "FunctionTypesNotInPrototypeForm",
15+
"tags": [
16+
"correctness"
17+
],
18+
"implementation_scope": {
19+
"description": "This query does not check for implicitly typed parameters and checks function declarations and definitions but not function pointer types. This query cannot determine when the keyword void is used in place of no parameter.",
20+
"items": []
21+
}
22+
}
23+
],
24+
"title": "Function types shall be in prototype form with named parameters"
25+
},
326
"RULE-8-6": {
427
"properties": {
528
"obligation": "required"

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ c,MISRA-C-2012,RULE-7-2,Yes,Required,,,A �u� or �U� suffix shall be appl
647647
c,MISRA-C-2012,RULE-7-3,Yes,Required,,,The lowercase character �l� shall not be used in a literal suffix,M2-13-4,Syntax,Easy,
648648
c,MISRA-C-2012,RULE-7-4,Yes,Required,,,A string literal shall not be assigned to an object unless the object�s type is �pointer to const-qualified char�,A2-13-4,Types,Easy,
649649
c,MISRA-C-2012,RULE-8-1,Yes,Required,,,Types shall be explicitly specified,,Declarations3,Medium,
650-
c,MISRA-C-2012,RULE-8-2,Yes,Required,,,Function types shall be in prototype form with named parameters,,Declarations,Medium,
650+
c,MISRA-C-2012,RULE-8-2,Yes,Required,,,Function types shall be in prototype form with named parameters,,Declarations4,Medium,
651651
c,MISRA-C-2012,RULE-8-3,Yes,Required,,,All declarations of an object or function shall use the same names and type qualifiers,M3-2-1,Declarations,Medium,
652652
c,MISRA-C-2012,RULE-8-4,Yes,Required,,,A compatible declaration shall be visible when an object or function with external linkage is defined,,Declarations,Medium,
653653
c,MISRA-C-2012,RULE-8-5,Yes,Required,,,An external object or function shall be declared once in one and only one file,,Declarations,Medium,

0 commit comments

Comments
 (0)