Skip to content

Commit 0ca65d2

Browse files
Merge pull request #1094 from github/jeongsoolee09/MISRA-C++-2023-Banned856
Add Banned8, Banned5, and Banned6
2 parents fd9c1d7 + a8dda3d commit 0ca65d2

35 files changed

+809
-58
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `A9-6-2` - `BitFieldsShallBeUsedOnlyWhenInterfacingToHardwareOrConformingToCommunicationProtocols.ql`:
2+
- Shorten the name to `BitFieldsShouldNotBeDeclaredAutosarCpp`, where the name shared query it imports is `BitFieldsShouldNotBeDeclared`.
3+
- Tag `"portability"` is added.
4+
- Alert message now includes single quotes around union name.

cpp/autosar/src/rules/A9-5-1/UnionsUsed.ql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @id cpp/autosar/unions-used
33
* @name A9-5-1: Unions shall not be used
4-
* @description Unions shall not be used. Tagged untions can be used if std::variant is not
4+
* @description Unions shall not be used. Tagged unions can be used if 'std::variant' is not
55
* available.
66
* @kind problem
77
* @precision very-high
@@ -34,8 +34,9 @@ class TaggedUnion extends UserType {
3434
}
3535
}
3636

37-
from Union u
37+
from Union u, string message
3838
where
3939
not isExcluded(u, BannedSyntaxPackage::unionsUsedQuery()) and
40-
not u.getParentScope() instanceof TaggedUnion
41-
select u, u.getName() + " is not a tagged union."
40+
not u.getParentScope() instanceof TaggedUnion and
41+
message = "'" + u.getName() + "' is not a tagged union."
42+
select u, message

cpp/autosar/src/rules/A9-6-2/BitFieldsShallBeUsedOnlyWhenInterfacingToHardwareOrConformingToCommunicationProtocols.ql

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id cpp/autosar/bit-fields-should-not-be-declared-autosar-cpp
3+
* @name A9-6-2: Bit-fields shall be used only when interfacing to hardware or conforming to communication protocols
4+
* @description The usage of bit-fields increases code complexity and certain aspects of bit-field
5+
* manipulation can be error prone and implementation defined. Hence a bit-field usage
6+
* is reserved only when interfacing to hardware or conformance to communication
7+
* protocols.
8+
* @kind problem
9+
* @precision very-high
10+
* @problem.severity recommendation
11+
* @tags external/autosar/id/a9-6-2
12+
* maintainability
13+
* portability
14+
* external/autosar/allocated-target/design
15+
* external/autosar/enforcement/partially-automated
16+
* external/autosar/obligation/required
17+
*/
18+
19+
import cpp
20+
import codingstandards.cpp.autosar
21+
import codingstandards.cpp.rules.bitfieldsshouldnotbedeclared.BitFieldsShouldNotBeDeclared
22+
23+
module BitFieldsShouldNotBeDeclaredAutosarCppConfig implements BitFieldsShouldNotBeDeclaredConfigSig
24+
{
25+
Query getQuery() { result = RepresentationPackage::bitFieldsShouldNotBeDeclaredAutosarCppQuery() }
26+
}
27+
28+
import BitFieldsShouldNotBeDeclared<BitFieldsShouldNotBeDeclaredAutosarCppConfig>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| test.cpp:14:9:14:9 | u | u is not a tagged union. |
2-
| test.cpp:21:7:21:7 | u | u is not a tagged union. |
1+
| test.cpp:14:9:14:9 | u | 'u' is not a tagged union. |
2+
| test.cpp:21:7:21:7 | u | 'u' is not a tagged union. |

cpp/autosar/test/rules/A9-6-2/BitFieldsShallBeUsedOnlyWhenInterfacingToHardwareOrConformingToCommunicationProtocols.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

cpp/autosar/test/rules/A9-6-2/BitFieldsShallBeUsedOnlyWhenInterfacingToHardwareOrConformingToCommunicationProtocols.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/bitfieldsshouldnotbedeclared/BitFieldsShouldNotBeDeclared.ql
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
import cpp as default
2+
3+
/*
4+
* Implementations of the C/C++ Fixed Width Types from cstdint.
5+
*
6+
* TODO: Deprecate once this is available in the CodeQL standard library.
7+
*/
8+
9+
/**
10+
* A parent class representing C/C++ a typedef'd `UserType` such as `int8_t`.
11+
*/
12+
abstract private class IntegralUnderlyingUserType extends default::UserType {
13+
IntegralUnderlyingUserType() { this.getUnderlyingType() instanceof default::IntegralType }
14+
}
15+
16+
abstract private class TFixedWidthIntegralType extends IntegralUnderlyingUserType { }
17+
18+
/**
19+
* A C/C++ fixed-width numeric type, such as `int8_t`.
20+
*/
21+
class FixedWidthIntegralType extends TFixedWidthIntegralType {
22+
FixedWidthIntegralType() { this instanceof TFixedWidthIntegralType }
23+
}
24+
25+
abstract private class TMinimumWidthIntegralType extends IntegralUnderlyingUserType { }
26+
27+
/**
28+
* A C/C++ minimum-width numeric type, such as `int_least8_t`.
29+
*/
30+
class MinimumWidthIntegralType extends TMinimumWidthIntegralType {
31+
MinimumWidthIntegralType() { this instanceof TMinimumWidthIntegralType }
32+
}
33+
34+
abstract private class TFastestMinimumWidthIntegralType extends IntegralUnderlyingUserType { }
35+
36+
/**
37+
* A C/C++ minimum-width numeric type, representing the fastest integer type with a
38+
* width of at least `N` such as `int_fast8_t`.
39+
*/
40+
class FastestMinimumWidthIntegralType extends TFastestMinimumWidthIntegralType {
41+
FastestMinimumWidthIntegralType() { this instanceof TFastestMinimumWidthIntegralType }
42+
}
43+
44+
/**
45+
* An enum type based on a fixed-width integer type. For instance, `enum e: uint8_t = { a, b };`
46+
*/
47+
class FixedWidthEnumType extends default::UserType {
48+
FixedWidthEnumType() {
49+
this.(default::Enum).getExplicitUnderlyingType() instanceof FixedWidthIntegralType
50+
}
51+
}
52+
53+
/**
54+
* The C/C++ `int8_t` type.
55+
*/
56+
class Int8_t extends TFixedWidthIntegralType {
57+
Int8_t() { this.hasGlobalOrStdName("int8_t") }
58+
59+
override string getAPrimaryQlClass() { result = "Int8_t" }
60+
}
61+
62+
/**
63+
* The C/C++ `int16_t` type.
64+
*/
65+
class Int16_t extends TFixedWidthIntegralType {
66+
Int16_t() { this.hasGlobalOrStdName("int16_t") }
67+
68+
override string getAPrimaryQlClass() { result = "Int16_t" }
69+
}
70+
71+
/**
72+
* The C/C++ `int32_t` type.
73+
*/
74+
class Int32_t extends TFixedWidthIntegralType {
75+
Int32_t() { this.hasGlobalOrStdName("int32_t") }
76+
77+
override string getAPrimaryQlClass() { result = "Int32_t" }
78+
}
79+
80+
/**
81+
* The C/C++ `int64_t` type.
82+
*/
83+
class Int64_t extends TFixedWidthIntegralType {
84+
Int64_t() { this.hasGlobalOrStdName("int64_t") }
85+
86+
override string getAPrimaryQlClass() { result = "Int64_t" }
87+
}
88+
89+
/**
90+
* The C/C++ `uint8_t` type.
91+
*/
92+
class UInt8_t extends TFixedWidthIntegralType {
93+
UInt8_t() { this.hasGlobalOrStdName("uint8_t") }
94+
95+
override string getAPrimaryQlClass() { result = "UInt8_t" }
96+
}
97+
98+
/**
99+
* The C/C++ `uint16_t` type.
100+
*/
101+
class UInt16_t extends TFixedWidthIntegralType {
102+
UInt16_t() { this.hasGlobalOrStdName("uint16_t") }
103+
104+
override string getAPrimaryQlClass() { result = "UInt16_t" }
105+
}
106+
107+
/**
108+
* The C/C++ `uint32_t` type.
109+
*/
110+
class UInt32_t extends TFixedWidthIntegralType {
111+
UInt32_t() { this.hasGlobalOrStdName("uint32_t") }
112+
113+
override string getAPrimaryQlClass() { result = "UInt32_t" }
114+
}
115+
116+
/**
117+
* The C/C++ `uint64_t` type.
118+
*/
119+
class UInt64_t extends TFixedWidthIntegralType {
120+
UInt64_t() { this.hasGlobalOrStdName("uint64_t") }
121+
122+
override string getAPrimaryQlClass() { result = "UInt64_t" }
123+
}
124+
125+
/**
126+
* The C/C++ `int_least8_t` type.
127+
*/
128+
class Int_least8_t extends TMinimumWidthIntegralType {
129+
Int_least8_t() { this.hasGlobalOrStdName("int_least8_t") }
130+
131+
override string getAPrimaryQlClass() { result = "Int_least8_t" }
132+
}
133+
134+
/**
135+
* The C/C++ `int_least16_t` type.
136+
*/
137+
class Int_least16_t extends TMinimumWidthIntegralType {
138+
Int_least16_t() { this.hasGlobalOrStdName("int_least16_t") }
139+
140+
override string getAPrimaryQlClass() { result = "Int_least16_t" }
141+
}
142+
143+
/**
144+
* The C/C++ `int_least32_t` type.
145+
*/
146+
class Int_least32_t extends TMinimumWidthIntegralType {
147+
Int_least32_t() { this.hasGlobalOrStdName("int_least32_t") }
148+
149+
override string getAPrimaryQlClass() { result = "Int_least32_t" }
150+
}
151+
152+
/**
153+
* The C/C++ `int_least64_t` type.
154+
*/
155+
class Int_least64_t extends TMinimumWidthIntegralType {
156+
Int_least64_t() { this.hasGlobalOrStdName("int_least64_t") }
157+
158+
override string getAPrimaryQlClass() { result = "Int_least64_t" }
159+
}
160+
161+
/**
162+
* The C/C++ `uint_least8_t` type.
163+
*/
164+
class UInt_least8_t extends TMinimumWidthIntegralType {
165+
UInt_least8_t() { this.hasGlobalOrStdName("uint_least8_t") }
166+
167+
override string getAPrimaryQlClass() { result = "UInt_least8_t" }
168+
}
169+
170+
/**
171+
* The C/C++ `uint_least16_t` type.
172+
*/
173+
class UInt_least16_t extends TMinimumWidthIntegralType {
174+
UInt_least16_t() { this.hasGlobalOrStdName("uint_least16_t") }
175+
176+
override string getAPrimaryQlClass() { result = "UInt_least16_t" }
177+
}
178+
179+
/**
180+
* The C/C++ `uint_least32_t` type.
181+
*/
182+
class UInt_least32_t extends TMinimumWidthIntegralType {
183+
UInt_least32_t() { this.hasGlobalOrStdName("uint_least32_t") }
184+
185+
override string getAPrimaryQlClass() { result = "UInt_least32_t" }
186+
}
187+
188+
/**
189+
* The C/C++ `uint_least64_t` type.
190+
*/
191+
class UInt_least64_t extends TMinimumWidthIntegralType {
192+
UInt_least64_t() { this.hasGlobalOrStdName("uint_least64_t") }
193+
194+
override string getAPrimaryQlClass() { result = "UInt_least64_t" }
195+
}
196+
197+
/**
198+
* The C/C++ `int_fast8_t` type.
199+
*/
200+
class Int_fast8_t extends TFastestMinimumWidthIntegralType {
201+
Int_fast8_t() { this.hasGlobalOrStdName("int_fast8_t") }
202+
203+
override string getAPrimaryQlClass() { result = "Int_fast8_t" }
204+
}
205+
206+
/**
207+
* The C/C++ `int_fast16_t` type.
208+
*/
209+
class Int_fast16_t extends TFastestMinimumWidthIntegralType {
210+
Int_fast16_t() { this.hasGlobalOrStdName("int_fast16_t") }
211+
212+
override string getAPrimaryQlClass() { result = "Int_fast16_t" }
213+
}
214+
215+
/**
216+
* The C/C++ `int_fast32_t` type.
217+
*/
218+
class Int_fast32_t extends TFastestMinimumWidthIntegralType {
219+
Int_fast32_t() { this.hasGlobalOrStdName("int_fast32_t") }
220+
221+
override string getAPrimaryQlClass() { result = "Int_fast32_t" }
222+
}
223+
224+
/**
225+
* The C/C++ `int_fast64_t` type.
226+
*/
227+
class Int_fast64_t extends TFastestMinimumWidthIntegralType {
228+
Int_fast64_t() { this.hasGlobalOrStdName("int_fast64_t") }
229+
230+
override string getAPrimaryQlClass() { result = "Int_fast64_t" }
231+
}
232+
233+
/**
234+
* The C/C++ `uint_fast8_t` type.
235+
*/
236+
class UInt_fast8_t extends TFastestMinimumWidthIntegralType {
237+
UInt_fast8_t() { this.hasGlobalOrStdName("uint_fast8_t") }
238+
239+
override string getAPrimaryQlClass() { result = "UInt_fast8_t" }
240+
}
241+
242+
/**
243+
* The C/C++ `uint_fast16_t` type.
244+
*/
245+
class UInt_fast16_t extends TFastestMinimumWidthIntegralType {
246+
UInt_fast16_t() { this.hasGlobalOrStdName("uint_fast16_t") }
247+
248+
override string getAPrimaryQlClass() { result = "UInt_fast16_t" }
249+
}
250+
251+
/**
252+
* The C/C++ `uint_fast32_t` type.
253+
*/
254+
class UInt_fast32_t extends TFastestMinimumWidthIntegralType {
255+
UInt_fast32_t() { this.hasGlobalOrStdName("uint_fast32_t") }
256+
257+
override string getAPrimaryQlClass() { result = "UInt_fast32_t" }
258+
}
259+
260+
/**
261+
* The C/C++ `uint_fast64_t` type.
262+
*/
263+
class UInt_fast64_t extends TFastestMinimumWidthIntegralType {
264+
UInt_fast64_t() { this.hasGlobalOrStdName("uint_fast64_t") }
265+
266+
override string getAPrimaryQlClass() { result = "UInt_fast64_t" }
267+
}
268+
269+
/**
270+
* Type that models a type that is either a pointer or a reference type.
271+
*/
272+
class PointerOrReferenceType extends default::DerivedType {
273+
PointerOrReferenceType() {
274+
this instanceof default::PointerType or
275+
this instanceof default::ReferenceType
276+
}
277+
}
278+
279+
/**
280+
* Type that models a char type that is explicitly signed or unsigned.
281+
*/
282+
class ExplictlySignedOrUnsignedCharType extends default::CharType {
283+
ExplictlySignedOrUnsignedCharType() {
284+
isExplicitlySigned() or
285+
isExplicitlyUnsigned()
286+
}
287+
}

cpp/autosar/src/codingstandards/cpp/HardwareOrProtocolInterface.qll renamed to cpp/common/src/codingstandards/cpp/HardwareOrProtocolInterface.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import codingstandards.cpp.autosar
1+
import cpp
22
import codingstandards.cpp.CommonTypes as CommonTypes
33

44
abstract class HardwareOrProtocolInterfaceClass extends Class { }

0 commit comments

Comments
 (0)