Skip to content

Commit 89485d5

Browse files
committed
Conversions: Swap some queries around
Rule 7.0.4 is now in the Conversions package. Rules 8.x in the Conversions package are now moved to Conversions2. This is to avoid having too many complex queries in one package, and because 7.0.4 is more closely related to the other 7.x rules.
1 parent f1502d6 commit 89485d5

File tree

3 files changed

+169
-110
lines changed

3 files changed

+169
-110
lines changed

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

Lines changed: 18 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ newtype ConversionsQuery =
77
TNoConversionFromBoolQuery() or
88
TNoImplicitBoolConversionQuery() or
99
TNoCharacterNumericalValueQuery() or
10+
TInappropriateBitwiseOrShiftOperandsQuery() or
1011
TNoSignednessChangeFromPromotionQuery() or
1112
TNumericAssignmentTypeMismatchQuery() or
12-
TFunctionPointerConversionContextQuery() or
13-
TVirtualBaseClassCastToDerivedQuery() or
14-
TNoCStyleOrFunctionalCastsQuery() or
15-
TIntToPointerCastProhibitedQuery() or
16-
TNoPointerToIntegralCastQuery() or
17-
TPointerToIntegralCastQuery() or
18-
TNoStandaloneTypeCastExpressionQuery()
13+
TFunctionPointerConversionContextQuery()
1914

2015
predicate isConversionsQueryMetadata(Query query, string queryId, string ruleId, string category) {
2116
query =
@@ -45,6 +40,15 @@ predicate isConversionsQueryMetadata(Query query, string queryId, string ruleId,
4540
ruleId = "RULE-7-0-3" and
4641
category = "required"
4742
or
43+
query =
44+
// `Query` instance for the `inappropriateBitwiseOrShiftOperands` query
45+
ConversionsPackage::inappropriateBitwiseOrShiftOperandsQuery() and
46+
queryId =
47+
// `@id` for the `inappropriateBitwiseOrShiftOperands` query
48+
"cpp/misra/inappropriate-bitwise-or-shift-operands" and
49+
ruleId = "RULE-7-0-4" and
50+
category = "required"
51+
or
4852
query =
4953
// `Query` instance for the `noSignednessChangeFromPromotion` query
5054
ConversionsPackage::noSignednessChangeFromPromotionQuery() and
@@ -71,60 +75,6 @@ predicate isConversionsQueryMetadata(Query query, string queryId, string ruleId,
7175
"cpp/misra/function-pointer-conversion-context" and
7276
ruleId = "RULE-7-11-3" and
7377
category = "required"
74-
or
75-
query =
76-
// `Query` instance for the `virtualBaseClassCastToDerived` query
77-
ConversionsPackage::virtualBaseClassCastToDerivedQuery() and
78-
queryId =
79-
// `@id` for the `virtualBaseClassCastToDerived` query
80-
"cpp/misra/virtual-base-class-cast-to-derived" and
81-
ruleId = "RULE-8-2-1" and
82-
category = "required"
83-
or
84-
query =
85-
// `Query` instance for the `noCStyleOrFunctionalCasts` query
86-
ConversionsPackage::noCStyleOrFunctionalCastsQuery() and
87-
queryId =
88-
// `@id` for the `noCStyleOrFunctionalCasts` query
89-
"cpp/misra/no-c-style-or-functional-casts" and
90-
ruleId = "RULE-8-2-2" and
91-
category = "required"
92-
or
93-
query =
94-
// `Query` instance for the `intToPointerCastProhibited` query
95-
ConversionsPackage::intToPointerCastProhibitedQuery() and
96-
queryId =
97-
// `@id` for the `intToPointerCastProhibited` query
98-
"cpp/misra/int-to-pointer-cast-prohibited" and
99-
ruleId = "RULE-8-2-6" and
100-
category = "required"
101-
or
102-
query =
103-
// `Query` instance for the `noPointerToIntegralCast` query
104-
ConversionsPackage::noPointerToIntegralCastQuery() and
105-
queryId =
106-
// `@id` for the `noPointerToIntegralCast` query
107-
"cpp/misra/no-pointer-to-integral-cast" and
108-
ruleId = "RULE-8-2-7" and
109-
category = "advisory"
110-
or
111-
query =
112-
// `Query` instance for the `pointerToIntegralCast` query
113-
ConversionsPackage::pointerToIntegralCastQuery() and
114-
queryId =
115-
// `@id` for the `pointerToIntegralCast` query
116-
"cpp/misra/pointer-to-integral-cast" and
117-
ruleId = "RULE-8-2-8" and
118-
category = "required"
119-
or
120-
query =
121-
// `Query` instance for the `noStandaloneTypeCastExpression` query
122-
ConversionsPackage::noStandaloneTypeCastExpressionQuery() and
123-
queryId =
124-
// `@id` for the `noStandaloneTypeCastExpression` query
125-
"cpp/misra/no-standalone-type-cast-expression" and
126-
ruleId = "RULE-9-2-1" and
127-
category = "required"
12878
}
12979

13080
module ConversionsPackage {
@@ -149,6 +99,13 @@ module ConversionsPackage {
14999
TQueryCPP(TConversionsPackageQuery(TNoCharacterNumericalValueQuery()))
150100
}
151101

102+
Query inappropriateBitwiseOrShiftOperandsQuery() {
103+
//autogenerate `Query` type
104+
result =
105+
// `Query` type for `inappropriateBitwiseOrShiftOperands` query
106+
TQueryCPP(TConversionsPackageQuery(TInappropriateBitwiseOrShiftOperandsQuery()))
107+
}
108+
152109
Query noSignednessChangeFromPromotionQuery() {
153110
//autogenerate `Query` type
154111
result =
@@ -169,46 +126,4 @@ module ConversionsPackage {
169126
// `Query` type for `functionPointerConversionContext` query
170127
TQueryCPP(TConversionsPackageQuery(TFunctionPointerConversionContextQuery()))
171128
}
172-
173-
Query virtualBaseClassCastToDerivedQuery() {
174-
//autogenerate `Query` type
175-
result =
176-
// `Query` type for `virtualBaseClassCastToDerived` query
177-
TQueryCPP(TConversionsPackageQuery(TVirtualBaseClassCastToDerivedQuery()))
178-
}
179-
180-
Query noCStyleOrFunctionalCastsQuery() {
181-
//autogenerate `Query` type
182-
result =
183-
// `Query` type for `noCStyleOrFunctionalCasts` query
184-
TQueryCPP(TConversionsPackageQuery(TNoCStyleOrFunctionalCastsQuery()))
185-
}
186-
187-
Query intToPointerCastProhibitedQuery() {
188-
//autogenerate `Query` type
189-
result =
190-
// `Query` type for `intToPointerCastProhibited` query
191-
TQueryCPP(TConversionsPackageQuery(TIntToPointerCastProhibitedQuery()))
192-
}
193-
194-
Query noPointerToIntegralCastQuery() {
195-
//autogenerate `Query` type
196-
result =
197-
// `Query` type for `noPointerToIntegralCast` query
198-
TQueryCPP(TConversionsPackageQuery(TNoPointerToIntegralCastQuery()))
199-
}
200-
201-
Query pointerToIntegralCastQuery() {
202-
//autogenerate `Query` type
203-
result =
204-
// `Query` type for `pointerToIntegralCast` query
205-
TQueryCPP(TConversionsPackageQuery(TPointerToIntegralCastQuery()))
206-
}
207-
208-
Query noStandaloneTypeCastExpressionQuery() {
209-
//autogenerate `Query` type
210-
result =
211-
// `Query` type for `noStandaloneTypeCastExpression` query
212-
TQueryCPP(TConversionsPackageQuery(TNoStandaloneTypeCastExpressionQuery()))
213-
}
214129
}

rule_packages/cpp/Conversions.json

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-7-0-1": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "required"
7+
},
8+
"queries": [
9+
{
10+
"description": "Converting a bool type (implicitly or explicitly) to another type can lead to unintended behavior and code obfuscation, particularly when using bitwise operators instead of logical operators.",
11+
"kind": "problem",
12+
"name": "There shall be no conversion from type bool",
13+
"precision": "very-high",
14+
"severity": "error",
15+
"short_name": "NoConversionFromBool",
16+
"tags": [
17+
"scope/single-translation-unit"
18+
]
19+
}
20+
],
21+
"title": "There shall be no conversion from type bool"
22+
},
23+
"RULE-7-0-2": {
24+
"properties": {
25+
"enforcement": "decidable",
26+
"obligation": "required"
27+
},
28+
"queries": [
29+
{
30+
"description": "Implicit and contextual conversions to bool from fundamental types, unscoped enums, or pointers may lead to unintended behavior, except for specific cases like pointer checks and explicit operator bool conversions.",
31+
"kind": "problem",
32+
"name": "There shall be no conversion to type bool",
33+
"precision": "very-high",
34+
"severity": "error",
35+
"short_name": "NoImplicitBoolConversion",
36+
"tags": [
37+
"scope/single-translation-unit"
38+
]
39+
}
40+
],
41+
"title": "There shall be no conversion to type bool"
42+
},
43+
"RULE-7-0-3": {
44+
"properties": {
45+
"enforcement": "decidable",
46+
"obligation": "required"
47+
},
48+
"queries": [
49+
{
50+
"description": "Using the numerical value of a character type may lead to inconsistent behavior due to encoding dependencies and should be avoided in favor of safer C++ Standard Library functions.",
51+
"kind": "problem",
52+
"name": "The numerical value of a character shall not be used",
53+
"precision": "very-high",
54+
"severity": "error",
55+
"short_name": "NoCharacterNumericalValue",
56+
"tags": [
57+
"scope/single-translation-unit"
58+
]
59+
}
60+
],
61+
"title": "The numerical value of a character shall not be used"
62+
},
63+
"RULE-7-0-4": {
64+
"properties": {
65+
"enforcement": "decidable",
66+
"obligation": "required"
67+
},
68+
"queries": [
69+
{
70+
"description": "Bitwise and shift operators should only be applied to operands of appropriate types and values to avoid implementation-defined or undefined behavior.",
71+
"kind": "problem",
72+
"name": "The operands of bitwise operators and shift operators shall be appropriate",
73+
"precision": "very-high",
74+
"severity": "error",
75+
"short_name": "InappropriateBitwiseOrShiftOperands",
76+
"tags": [
77+
"scope/single-translation-unit"
78+
]
79+
}
80+
],
81+
"title": "The operands of bitwise operators and shift operators shall be appropriate"
82+
},
83+
"RULE-7-0-5": {
84+
"properties": {
85+
"enforcement": "decidable",
86+
"obligation": "required"
87+
},
88+
"queries": [
89+
{
90+
"description": "Integral promotion and usual arithmetic conversions that change operand signedness or type category may cause unexpected behavior or undefined behavior when operations overflow.",
91+
"kind": "problem",
92+
"name": "Integral promotion and the usual arithmetic conversions shall not change the signedness or the type",
93+
"precision": "very-high",
94+
"severity": "error",
95+
"short_name": "NoSignednessChangeFromPromotion",
96+
"tags": [
97+
"scope/single-translation-unit"
98+
]
99+
}
100+
],
101+
"title": "Integral promotion and the usual arithmetic conversions shall not change the signedness or the type category of an operand"
102+
},
103+
"RULE-7-0-6": {
104+
"properties": {
105+
"enforcement": "decidable",
106+
"obligation": "required"
107+
},
108+
"queries": [
109+
{
110+
"description": "Assignment between numeric types with different sizes, signedness, or type categories can lead to unexpected information loss, undefined behavior, or silent overload resolution changes.",
111+
"kind": "problem",
112+
"name": "Assignment between numeric types shall be appropriate",
113+
"precision": "high",
114+
"severity": "error",
115+
"short_name": "NumericAssignmentTypeMismatch",
116+
"tags": [
117+
"scope/single-translation-unit"
118+
]
119+
}
120+
],
121+
"title": "Assignment between numeric types shall be appropriate"
122+
},
123+
"RULE-7-11-3": {
124+
"properties": {
125+
"enforcement": "decidable",
126+
"obligation": "required"
127+
},
128+
"queries": [
129+
{
130+
"description": "Converting a function type to a pointer-to-function type outside of static_cast or assignment to a pointer-to-function object creates ambiguous behavior and potential unintended effects.",
131+
"kind": "problem",
132+
"name": "A conversion from function type to pointer-to-function type shall only occur in appropriate contexts",
133+
"precision": "very-high",
134+
"severity": "error",
135+
"short_name": "FunctionPointerConversionContext",
136+
"tags": [
137+
"scope/single-translation-unit"
138+
]
139+
}
140+
],
141+
"title": "A conversion from function type to pointer-to-function type shall only occur in appropriate contexts"
142+
}
143+
}
144+
}

rules.csv

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ cpp,MISRA-C++-2023,RULE-6-9-2,Yes,Advisory,Decidable,Single Translation Unit,The
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,
875875
cpp,MISRA-C++-2023,RULE-7-0-3,Yes,Required,Decidable,Single Translation Unit,The numerical value of a character shall not be used,M5-0-11,Conversions,Medium,
876-
cpp,MISRA-C++-2023,RULE-7-0-4,Yes,Required,Decidable,Single Translation Unit,The operands of bitwise operators and shift operators shall be appropriate,RULE-10-1,Preconditions,Medium,
876+
cpp,MISRA-C++-2023,RULE-7-0-4,Yes,Required,Decidable,Single Translation Unit,The operands of bitwise operators and shift operators shall be appropriate,RULE-10-1,Conversions,Medium,
877877
cpp,MISRA-C++-2023,RULE-7-0-5,Yes,Required,Decidable,Single Translation Unit,Integral promotion and the usual arithmetic conversions shall not change the signedness or the type category of an operand,"M5-0-4,M5-0-9,INT31-C",Conversions,Medium,
878878
cpp,MISRA-C++-2023,RULE-7-0-6,Yes,Required,Decidable,Single Translation Unit,Assignment between numeric types shall be appropriate,,Conversions,Hard,
879879
cpp,MISRA-C++-2023,RULE-7-11-1,Yes,Required,Decidable,Single Translation Unit,nullptr shall be the only form of the null-pointer-constant,A4-10-1,ImportMisra23,Import,
@@ -882,14 +882,14 @@ cpp,MISRA-C++-2023,RULE-7-11-3,Yes,Required,Decidable,Single Translation Unit,A
882882
cpp,MISRA-C++-2023,RULE-8-0-1,Yes,Advisory,Decidable,Single Translation Unit,Parentheses should be used to make the meaning of an expression appropriately explicit,M5-0-2,Expressions2,Medium,
883883
cpp,MISRA-C++-2023,RULE-8-1-1,Yes,Required,Decidable,Single Translation Unit,A non-transient lambda shall not implicitly capture this,,Expressions2,Easy,
884884
cpp,MISRA-C++-2023,RULE-8-1-2,Yes,Advisory,Decidable,Single Translation Unit,Variables should be captured explicitly in a non-transient lambda,A5-1-2,Expressions2,Easy,
885-
cpp,MISRA-C++-2023,RULE-8-2-1,Yes,Required,Decidable,Single Translation Unit,A virtual base class shall only be cast to a derived class by means of dynamic_cast,,Conversions,Easy,
886-
cpp,MISRA-C++-2023,RULE-8-2-2,Yes,Required,Decidable,Single Translation Unit,C-style casts and functional notation casts shall not be used,A5-2-2,Conversions,Easy,
885+
cpp,MISRA-C++-2023,RULE-8-2-1,Yes,Required,Decidable,Single Translation Unit,A virtual base class shall only be cast to a derived class by means of dynamic_cast,,Conversions2,Easy,
886+
cpp,MISRA-C++-2023,RULE-8-2-2,Yes,Required,Decidable,Single Translation Unit,C-style casts and functional notation casts shall not be used,A5-2-2,Conversions2,Easy,
887887
cpp,MISRA-C++-2023,RULE-8-2-3,Yes,Required,Decidable,Single Translation Unit,A cast shall not remove any const or volatile qualification from the type accessed via a pointer or by reference,A5-2-3,ImportMisra23,Import,
888888
cpp,MISRA-C++-2023,RULE-8-2-4,Yes,Required,Decidable,Single Translation Unit,Casts shall not be performed between a pointer to function and any other type,M5-2-6,ImportMisra23,Import,
889889
cpp,MISRA-C++-2023,RULE-8-2-5,Yes,Required,Decidable,Single Translation Unit,reinterpret_cast shall not be used,A5-2-4,ImportMisra23,Import,
890-
cpp,MISRA-C++-2023,RULE-8-2-6,Yes,Required,Decidable,Single Translation Unit,"An object with integral, enumerated, or pointer to void type shall not be cast to a pointer type","RULE-11-6, INT36-C",Conversions,Easy,
891-
cpp,MISRA-C++-2023,RULE-8-2-7,Yes,Advisory,Decidable,Single Translation Unit,A cast should not convert a pointer type to an integral type,"RULE-11-6, INT36-C",Conversions,Easy,
892-
cpp,MISRA-C++-2023,RULE-8-2-8,Yes,Required,Decidable,Single Translation Unit,An object pointer type shall not be cast to an integral type other than std::uintptr_t or std::intptr_t,"RULE-11-6, INT36-C",Conversions,Easy,
890+
cpp,MISRA-C++-2023,RULE-8-2-6,Yes,Required,Decidable,Single Translation Unit,"An object with integral, enumerated, or pointer to void type shall not be cast to a pointer type","RULE-11-6, INT36-C",Conversions2,Easy,
891+
cpp,MISRA-C++-2023,RULE-8-2-7,Yes,Advisory,Decidable,Single Translation Unit,A cast should not convert a pointer type to an integral type,"RULE-11-6, INT36-C",Conversions2,Easy,
892+
cpp,MISRA-C++-2023,RULE-8-2-8,Yes,Required,Decidable,Single Translation Unit,An object pointer type shall not be cast to an integral type other than std::uintptr_t or std::intptr_t,"RULE-11-6, INT36-C",Conversions2,Easy,
893893
cpp,MISRA-C++-2023,RULE-8-2-9,Yes,Required,Decidable,Single Translation Unit,The operand to typeid shall not be an expression of polymorphic class type,,Preconditions,Easy,
894894
cpp,MISRA-C++-2023,RULE-8-2-10,Yes,Required,Undecidable,System,"Functions shall not call themselves, either directly or indirectly",A7-5-2,ImportMisra23,Import,
895895
cpp,MISRA-C++-2023,RULE-8-2-11,Yes,Required,Decidable,Single Translation Unit,An argument passed via ellipsis shall have an appropriate type,,Preconditions,Easy,
@@ -903,7 +903,7 @@ cpp,MISRA-C++-2023,RULE-8-18-1,Yes,Mandatory,Undecidable,System,An object or sub
903903
cpp,MISRA-C++-2023,RULE-8-18-2,Yes,Advisory,Decidable,Single Translation Unit,The result of an assignment operator should not be used,RULE-13-4,ImportMisra23,Import,
904904
cpp,MISRA-C++-2023,RULE-8-19-1,Yes,Advisory,Decidable,Single Translation Unit,The comma operator should not be used,M5-18-1,ImportMisra23,Import,
905905
cpp,MISRA-C++-2023,RULE-8-20-1,Yes,Advisory,Decidable,Single Translation Unit,An unsigned arithmetic operation with constant operands should not wrap,INT30-C,ImportMisra23,Import,
906-
cpp,MISRA-C++-2023,RULE-9-2-1,Yes,Required,Decidable,Single Translation Unit,An explicit type conversion shall not be an expression statement,DCL53-CPP,Conversions,Easy,
906+
cpp,MISRA-C++-2023,RULE-9-2-1,Yes,Required,Decidable,Single Translation Unit,An explicit type conversion shall not be an expression statement,DCL53-CPP,Conversions2,Easy,
907907
cpp,MISRA-C++-2023,RULE-9-3-1,Yes,Required,Decidable,Single Translation Unit,The body of an iteration-statement or a selection-statement shall be a compound-statement,RULE-15-6,ImportMisra23,Import,
908908
cpp,MISRA-C++-2023,RULE-9-4-1,Yes,Required,Decidable,Single Translation Unit,All if ... else if constructs shall be terminated with an else statement,RULE-15-7,ImportMisra23,Import,
909909
cpp,MISRA-C++-2023,RULE-9-4-2,Yes,Required,Decidable,Single Translation Unit,The structure of a switch statement shall be appropriate,"RULE-16-1, RULE-16-2,RULE-16-3,RULE-16-4,RULE-16-5,RULE-16-6,RULE-16-7",Statements,Medium,

0 commit comments

Comments
 (0)