Skip to content

Commit f739b65

Browse files
committed
Add RULE-13-3-3
1 parent 25403fc commit f739b65

File tree

10 files changed

+165
-1
lines changed

10 files changed

+165
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations1Query = TDeclarationsOfAFunctionSameParameterNameQuery()
7+
8+
predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `declarationsOfAFunctionSameParameterName` query
11+
Declarations1Package::declarationsOfAFunctionSameParameterNameQuery() and
12+
queryId =
13+
// `@id` for the `declarationsOfAFunctionSameParameterName` query
14+
"cpp/misra/declarations-of-a-function-same-parameter-name" and
15+
ruleId = "RULE-13-3-3" and
16+
category = "required"
17+
}
18+
19+
module Declarations1Package {
20+
Query declarationsOfAFunctionSameParameterNameQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `declarationsOfAFunctionSameParameterName` query
24+
TQueryCPP(TDeclarations1PackageQuery(TDeclarationsOfAFunctionSameParameterNameQuery()))
25+
}
26+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import DeadCode7
2727
import DeadCode8
2828
import DeadCode9
2929
import Declarations
30+
import Declarations1
3031
import ExceptionSafety
3132
import Exceptions1
3233
import Exceptions2
@@ -116,6 +117,7 @@ newtype TCPPQuery =
116117
TDeadCode8PackageQuery(DeadCode8Query q) or
117118
TDeadCode9PackageQuery(DeadCode9Query q) or
118119
TDeclarationsPackageQuery(DeclarationsQuery q) or
120+
TDeclarations1PackageQuery(Declarations1Query q) or
119121
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
120122
TExceptions1PackageQuery(Exceptions1Query q) or
121123
TExceptions2PackageQuery(Exceptions2Query q) or
@@ -205,6 +207,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
205207
isDeadCode8QueryMetadata(query, queryId, ruleId, category) or
206208
isDeadCode9QueryMetadata(query, queryId, ruleId, category) or
207209
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
210+
isDeclarations1QueryMetadata(query, queryId, ruleId, category) or
208211
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
209212
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
210213
isExceptions2QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @id cpp/misra/declarations-of-a-function-same-parameter-name
3+
* @name RULE-13-3-3: The parameters in all declarations or overrides of a function shall either be unnamed or have identical names
4+
* @description Parameters in some number of declarations or overrides of a function that do not
5+
* have identical names can lead to developer confusion.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-13-3-3
10+
* maintainability
11+
* readability
12+
* scope/system
13+
* external/misra/enforcement/decidable
14+
* external/misra/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.misra
19+
import codingstandards.cpp.types.Compatible
20+
21+
predicate parameterNamesUnmatchedOverrides(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
22+
pragma[only_bind_into](f1).getFunction().(MemberFunction).getAnOverridingFunction*() =
23+
pragma[only_bind_into](f2).getFunction() and
24+
exists(string p1Name, string p2Name, int i |
25+
p1Name = f1.getParameterDeclarationEntry(i).getName() and
26+
p2Name = f2.getParameterDeclarationEntry(i).getName()
27+
|
28+
not p1Name = p2Name
29+
)
30+
}
31+
32+
from FunctionDeclarationEntry f1, FunctionDeclarationEntry f2, string case
33+
where
34+
not isExcluded(f1, Declarations1Package::declarationsOfAFunctionSameParameterNameQuery()) and
35+
not isExcluded(f2, Declarations1Package::declarationsOfAFunctionSameParameterNameQuery()) and
36+
not f1 = f2 and
37+
(
38+
f1.getDeclaration() = f2.getDeclaration() and
39+
parameterNamesUnmatched(f1, f2) and
40+
case = "re-declaration"
41+
or
42+
f1.getFunction().(MemberFunction).getAnOverridingFunction*() = f2.getFunction() and
43+
parameterNamesUnmatchedOverrides(f1, f2) and
44+
case = "override"
45+
)
46+
select f1,
47+
"The parameter names of " + case + " of $@ do" + " not use the same names as declaration $@", f1,
48+
f1.getName(), f2, f2.getName()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| functions1.cpp:5:6:5:7 | declaration of f4 | The parameter names of re-declaration of $@ do not use the same names as declaration $@ | functions1.cpp:5:6:5:7 | declaration of f4 | f4 | functions2.cpp:6:6:6:7 | declaration of f4 | f4 |
2+
| functions1.cpp:6:6:6:7 | declaration of f5 | The parameter names of re-declaration of $@ do not use the same names as declaration $@ | functions1.cpp:6:6:6:7 | declaration of f5 | f5 | functions2.cpp:7:6:7:7 | declaration of f5 | f5 |
3+
| functions1.cpp:16:6:16:7 | definition of f7 | The parameter names of re-declaration of $@ do not use the same names as declaration $@ | functions1.cpp:16:6:16:7 | definition of f7 | f7 | functions2.cpp:15:13:15:14 | declaration of f7 | f7 |
4+
| functions1.cpp:29:16:29:22 | declaration of methodA | The parameter names of override of $@ do not use the same names as declaration $@ | functions1.cpp:29:16:29:22 | declaration of methodA | methodA | functions1.cpp:34:8:34:14 | declaration of methodA | methodA |
5+
| functions2.cpp:6:6:6:7 | declaration of f4 | The parameter names of re-declaration of $@ do not use the same names as declaration $@ | functions2.cpp:6:6:6:7 | declaration of f4 | f4 | functions1.cpp:5:6:5:7 | declaration of f4 | f4 |
6+
| functions2.cpp:7:6:7:7 | declaration of f5 | The parameter names of re-declaration of $@ do not use the same names as declaration $@ | functions2.cpp:7:6:7:7 | declaration of f5 | f5 | functions1.cpp:6:6:6:7 | declaration of f5 | f5 |
7+
| functions2.cpp:15:13:15:14 | declaration of f7 | The parameter names of re-declaration of $@ do not use the same names as declaration $@ | functions2.cpp:15:13:15:14 | declaration of f7 | f7 | functions1.cpp:16:6:16:7 | definition of f7 | f7 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-13-3-3/DeclarationsOfAFunctionSameParameterName.ql
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
void f1(int a); // COMPLIANT -- same name
2+
void f2(int a); // COMPLIANT -- unnamed is fine
3+
4+
void f3(int a); // COMPLIANT -- diff number but for those that exist, same
5+
void f4(int p, int b); // NON_COMPLIANT -- diff name
6+
void f5(int b, int a); // NON_COMPLIANT -- swapped names
7+
8+
typedef int wi;
9+
typedef int hi;
10+
typedef long a;
11+
12+
a f6(wi w, wi h) { // NON_COMPLIANT
13+
return (a)w * h;
14+
}
15+
16+
void f7(int b, int a) { // NON_COMPLIANT
17+
return;
18+
}
19+
20+
void f8(int a) { // COMPLIANT
21+
return;
22+
}
23+
24+
template <class T> void f9(T t); // COMPLIANT
25+
template <>
26+
void f9<int>(int i); // COMPLIANT - specialization is a diff declaration
27+
28+
class ClassA {
29+
virtual void methodA(int i); // NON_COMPLIANT
30+
virtual void methodB(int i); // COMPLIANT
31+
};
32+
33+
class ClassB : ClassA {
34+
void methodA(int d) override;
35+
void methodB(int i) override;
36+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void f1(int a); // COMPLIANT -- same name
2+
void f2(int); // COMPLIANT -- unnamed is fine
3+
4+
void f3(int a,
5+
int b); // COMPLIANT -- diff number but for those that exist, same
6+
void f4(int a, int b); // NON_COMPLIANT -- diff name
7+
void f5(int a, int b); // NON_COMPLIANT -- swapped names
8+
9+
typedef int wi;
10+
typedef int hi;
11+
typedef long a;
12+
13+
extern a f6(wi w, hi h); // NON_COMPLIANT
14+
15+
extern void f7(int a, int b); // NON_COMPLIANT
16+
17+
extern void f8(int); // COMPLIANT

cpp/misra/test/rules/RULE-13-3-3/test.cpp

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-13-3-3": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "required"
7+
},
8+
"queries": [
9+
{
10+
"description": "Parameters in some number of declarations or overrides of a function that do not have identical names can lead to developer confusion.",
11+
"kind": "problem",
12+
"name": "The parameters in all declarations or overrides of a function shall either be unnamed or have identical names",
13+
"precision": "very-high",
14+
"severity": "error",
15+
"short_name": "DeclarationsOfAFunctionSameParameterName",
16+
"tags": [
17+
"maintainability",
18+
"readability",
19+
"scope/system"
20+
]
21+
}
22+
],
23+
"title": "The parameters in all declarations or overrides of a function shall either be unnamed or have identical names"
24+
}
25+
}
26+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ cpp,MISRA-C++-2023,RULE-13-1-1,Yes,Advisory,Decidable,Single Translation Unit,Cl
935935
cpp,MISRA-C++-2023,RULE-13-1-2,Yes,Required,Decidable,Single Translation Unit,An accessible base class shall not be both virtual and non-virtual in the same hierarchy,M10-1-3,ImportMisra23,Import,
936936
cpp,MISRA-C++-2023,RULE-13-3-1,Yes,Required,Decidable,Single Translation Unit,"User-declared member functions shall use the virtual, override and final specifiers appropriately",,Classes2,Easy,
937937
cpp,MISRA-C++-2023,RULE-13-3-2,Yes,Required,Decidable,Single Translation Unit,Parameters in an overriding virtual function shall not specify different default arguments,M8-3-1,ImportMisra23,Import,
938-
cpp,MISRA-C++-2023,RULE-13-3-3,Yes,Required,Decidable,System,The parameters in all declarations or overrides of a function shall either be unnamed or have identical names,RULE-8-3,Declarations2,Easy,
938+
cpp,MISRA-C++-2023,RULE-13-3-3,Yes,Required,Decidable,System,The parameters in all declarations or overrides of a function shall either be unnamed or have identical names,RULE-8-3,Declarations1,Easy,
939939
cpp,MISRA-C++-2023,RULE-13-3-4,Yes,Required,Decidable,Single Translation Unit,A comparison of a potentially virtual pointer to member function shall only be with nullptr,A5-10-1,ImportMisra23,Import,
940940
cpp,MISRA-C++-2023,RULE-14-1-1,Yes,Advisory,Decidable,Single Translation Unit,Non-static data members should be either all private or all public,,Classes2,Easy,
941941
cpp,MISRA-C++-2023,RULE-15-0-1,Yes,Required,Decidable,Single Translation Unit,Special member functions shall be provided appropriately,A12-0-1,Classes3,Medium,

0 commit comments

Comments
 (0)