AutosarC++19_03-A9.6.1¶
Data types used for interfacing with hardware or conforming to communication protocols shall be trivial, standard-layout and only contain members of types with defined sizes
Required inputs: IR
| Level | required |
|---|---|
| Target | design |
| Enforcement | partially automated |
Rationale
When the layout of data types is important, only those types that have a defined size shall be used (see A3-9-1, this excludes bool, wchar_t, pointers, and pointers to members). Enumeration types may be used if they have been explicitly declared with an underlying type that has a defined size.
Note: As the use of bit-fields is only allowed for interfacing with hardware or conforming to communication protocols, this restriction on types also applies to bit-fields, see A9- 6-2.
Note: The signed exact-width integer types like std::int16_t are guaranteed to have a two’s complement representation.
Example
// $Id: A9-6-1.cpp 319312 2018-05-15 08:29:17Z christof.meerwald $
#include <cstdint>
enum class E1 : std::uint8_t
{
E11,
E12,
E13
};
enum class E2 : std::int16_t
{
E21,
E22,
E23
};
enum class E3
{
E31,
E32,
E33
};
enum E4
{
E41,
E42,
E43
};
class C
{
public:
std::int32_t a : 2; // Compliant
std::uint8_t b : 2U; // Compliant
bool c : 1; // Non-compliant - the size of bool is implementation defined
char d : 2; // Non-compliant
wchar_t e : 2; // Non-compliant - the size of wchar_t is implementationdefined
E1 f1 : 2; // Compliant
E2 f2 : 2; // Compliant
E3 f3 : 2; // Non-compliant - E3 enum class does not explicitly define
// underlying type
E4 f4 : 2; // Non-compliant - E4 enum does not explicitly define underlying
// type
};
struct D
{
std::int8_t a; // Compliant
bool b; // Non-compliant - the size of bool is
// implementation defined
std::uint16_t c1 : 8; // Compliant
std::uint16_t c2 : 8; // Compliant
};
void Fn() noexcept
{
C c;
c.f1 = E1::E11;
}
See Also
- MISRA C++ 2008 [7]: A9-6-2: Bit-fields shall be either bool type or an explicitly unsigned or signed integral type
- JSF December 2005 [8]: AV Rule 154: Bit-fields shall have explicitly unsigned integral or enumeration types only
- HIC++ v4.0 [9]: 9.2.1: Declare bit-fields with an explicitly unsigned integral or enumeration type
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
member_size |
Type used for interfacing with hardware contains member of types without defined size. |
None |
False |
standard_layout |
Type used for interfacing with hardware does not have standard-layout. |
None |
False |
trivial |
Type used for interfacing with hardware is not trivial. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
check_pod_types¶
check_pod_types : bool = True
relevant_types¶
relevant_types : set[bauhaus.analysis.config.QualifiedName] = set()