Skip to content

Commit b4af299

Browse files
committed
Add RULE-6-9-1
1 parent 69147ac commit b4af299

File tree

8 files changed

+147
-2
lines changed

8 files changed

+147
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations5Query =
7+
TMemberFunctionsRefqualifiedQuery() or
8+
TTypeAliasesDeclarationQuery()
9+
10+
predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleId, string category) {
11+
query =
12+
// `Query` instance for the `memberFunctionsRefqualified` query
13+
Declarations5Package::memberFunctionsRefqualifiedQuery() and
14+
queryId =
15+
// `@id` for the `memberFunctionsRefqualified` query
16+
"cpp/misra/member-functions-refqualified" and
17+
ruleId = "RULE-6-8-4" and
18+
category = "advisory"
19+
or
20+
query =
21+
// `Query` instance for the `typeAliasesDeclaration` query
22+
Declarations5Package::typeAliasesDeclarationQuery() and
23+
queryId =
24+
// `@id` for the `typeAliasesDeclaration` query
25+
"cpp/misra/type-aliases-declaration" and
26+
ruleId = "RULE-6-9-1" and
27+
category = "required"
28+
}
29+
30+
module Declarations5Package {
31+
Query memberFunctionsRefqualifiedQuery() {
32+
//autogenerate `Query` type
33+
result =
34+
// `Query` type for `memberFunctionsRefqualified` query
35+
TQueryCPP(TDeclarations5PackageQuery(TMemberFunctionsRefqualifiedQuery()))
36+
}
37+
38+
Query typeAliasesDeclarationQuery() {
39+
//autogenerate `Query` type
40+
result =
41+
// `Query` type for `typeAliasesDeclaration` query
42+
TQueryCPP(TDeclarations5PackageQuery(TTypeAliasesDeclarationQuery()))
43+
}
44+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import DeadCode8
3333
import DeadCode9
3434
import Declarations
3535
import Declarations1
36+
import Declarations5
3637
import ExceptionSafety
3738
import Exceptions1
3839
import Exceptions2
@@ -129,6 +130,7 @@ newtype TCPPQuery =
129130
TDeadCode9PackageQuery(DeadCode9Query q) or
130131
TDeclarationsPackageQuery(DeclarationsQuery q) or
131132
TDeclarations1PackageQuery(Declarations1Query q) or
133+
TDeclarations5PackageQuery(Declarations5Query q) or
132134
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
133135
TExceptions1PackageQuery(Exceptions1Query q) or
134136
TExceptions2PackageQuery(Exceptions2Query q) or
@@ -225,6 +227,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
225227
isDeadCode9QueryMetadata(query, queryId, ruleId, category) or
226228
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
227229
isDeclarations1QueryMetadata(query, queryId, ruleId, category) or
230+
isDeclarations5QueryMetadata(query, queryId, ruleId, category) or
228231
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
229232
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
230233
isExceptions2QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @id cpp/misra/type-aliases-declaration
3+
* @name RULE-6-9-1: The same type aliases shall be used in all declarations of the same entity
4+
* @description Using different type aliases on redeclarations can make code hard to understand and
5+
* maintain.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-6-9-1
10+
* maintainability
11+
* readability
12+
* scope/single-translation-unit
13+
* external/misra/enforcement/decidable
14+
* external/misra/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.misra
19+
20+
from DeclarationEntry decl1, DeclarationEntry decl2, TypedefType t
21+
where
22+
not isExcluded(decl1, Declarations5Package::typeAliasesDeclarationQuery()) and
23+
not isExcluded(decl2, Declarations5Package::typeAliasesDeclarationQuery()) and
24+
not decl1 = decl2 and
25+
decl1.getDeclaration() = decl2.getDeclaration() and
26+
t.getATypeNameUse() = decl1 and
27+
not t.getATypeNameUse() = decl2 and
28+
//exception cases - we dont want to disallow struct typedef name use
29+
not t.getBaseType() instanceof Struct and
30+
not t.getBaseType() instanceof Enum
31+
select decl1,
32+
"Declaration entry has a different type alias than $@ where the type alias used is '$@'.", decl2,
33+
decl2.getName(), t, t.getName()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:4:5:4:5 | definition of i | Declaration entry has a different type alias than $@ where the type alias used is '$@'. | test.cpp:5:12:5:12 | declaration of i | i | test.cpp:1:13:1:15 | INT | INT |
2+
| test.cpp:11:20:11:20 | declaration of i | Declaration entry has a different type alias than $@ where the type alias used is '$@'. | test.cpp:10:12:10:12 | declaration of i | i | test.cpp:2:7:2:11 | Index | Index |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-6-9-1/TypeAliasesDeclaration.ql
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
typedef int INT;
2+
using Index = int;
3+
4+
INT i;
5+
extern int i; // NON_COMPLIANT
6+
7+
INT j;
8+
extern INT j; // COMPLIANT
9+
10+
void g(int i);
11+
void g(Index const i); // NON_COMPLIANT
12+
13+
void h(Index i);
14+
void h(Index const i); // COMPLIANT
15+
void h(int *i); // COMPLIANT
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-6-8-4": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "advisory"
7+
},
8+
"queries": [
9+
{
10+
"description": "Member functions that return references to temporary objects (or subobjects) can lead to dangling pointers.",
11+
"kind": "problem",
12+
"name": "Member functions returning references to their object should be refqualified appropriately",
13+
"precision": "very-high",
14+
"severity": "error",
15+
"short_name": "MemberFunctionsRefqualified",
16+
"tags": [
17+
"correctness",
18+
"scope/single-translation-unit"
19+
]
20+
}
21+
],
22+
"title": "Member functions returning references to their object should be refqualified appropriately"
23+
},
24+
"RULE-6-9-1": {
25+
"properties": {
26+
"enforcement": "decidable",
27+
"obligation": "required"
28+
},
29+
"queries": [
30+
{
31+
"description": "Using different type aliases on redeclarations can make code hard to understand and maintain.",
32+
"kind": "problem",
33+
"name": "The same type aliases shall be used in all declarations of the same entity",
34+
"precision": "very-high",
35+
"severity": "warning",
36+
"short_name": "TypeAliasesDeclaration",
37+
"tags": [
38+
"maintainability",
39+
"readability",
40+
"scope/single-translation-unit"
41+
]
42+
}
43+
],
44+
"title": "The same type aliases shall be used in all declarations of the same entity"
45+
}
46+
}
47+
}

rules.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ cpp,MISRA-C++-2023,RULE-6-7-2,Yes,Required,Decidable,Single Translation Unit,Glo
867867
cpp,MISRA-C++-2023,RULE-6-8-1,Yes,Required,Undecidable,System,An object shall not be accessed outside of its lifetime,A3-8-1,ImportMisra23,Import,
868868
cpp,MISRA-C++-2023,RULE-6-8-2,Yes,Mandatory,Decidable,Single Translation Unit,A function must not return a reference or a pointer to a local variable with automatic storage duration,M7-5-1,ImportMisra23,Import,
869869
cpp,MISRA-C++-2023,RULE-6-8-3,Yes,Required,Decidable,Single Translation Unit,An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime,,Lifetime,Medium,
870-
cpp,MISRA-C++-2023,RULE-6-8-4,Yes,Advisory,Decidable,Single Translation Unit,Member functions returning references to their object should be refqualified appropriately,,Declarations2,Medium,
871-
cpp,MISRA-C++-2023,RULE-6-9-1,Yes,Required,Decidable,Single Translation Unit,The same type aliases shall be used in all declarations of the same entity,,Declarations2,Medium,
870+
cpp,MISRA-C++-2023,RULE-6-8-4,Yes,Advisory,Decidable,Single Translation Unit,Member functions returning references to their object should be refqualified appropriately,,Declarations5,Medium,
871+
cpp,MISRA-C++-2023,RULE-6-9-1,Yes,Required,Decidable,Single Translation Unit,The same type aliases shall be used in all declarations of the same entity,,Declarations5,Medium,
872872
cpp,MISRA-C++-2023,RULE-6-9-2,Yes,Advisory,Decidable,Single Translation Unit,The names of the standard signed integer types and standard unsigned integer types should not be used,A3-9-1,BannedAPIs,Easy,
873873
cpp,MISRA-C++-2023,RULE-7-0-1,Yes,Required,Decidable,Single Translation Unit,There shall be no conversion from type bool,,Conversions,Easy,
874874
cpp,MISRA-C++-2023,RULE-7-0-2,Yes,Required,Decidable,Single Translation Unit,There shall be no conversion to type bool,,Conversions,Easy,

0 commit comments

Comments
 (0)