Main Content

AUTOSAR C++14 Rule 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

Description

Rule Definition

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.

Rationale

When interfacing with hardware or when conforming to communication protocols, the layout of the data type you use must be consistent and standardized. For these tasks, use data types with layout and size that are not dependent on your hardware or software.

Types with well defined sizes are listed in the header <cstdint>. Examples of such data type includes types such as int16_8 and int32_t. This list excludes types such as bool, wchar_t, and pointers. Avoid using excluded types when interfacing with hardware or when conforming to communication protocols.

Using enumerations are allowed only when the underlying data types of the enum has a well defined size. Using bitfields for these tasks are allowed only when the underlying type has a well defined size,

Polyspace Implementation

Polyspace® raises a violation of this rule if you use a data type other than these two in a bit field:

  • Enumeration

  • Unsigned integral type

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>
enum class E1: std::uint8_t {
	E11,
	E12,
	E13
};
enum class E2{
	E21,
	E22,
	E23
};
class hwInterface{
public:
	std::int32_t A:2; //Noncompliant- signed integer
	std::uint8_t B:2U; //Compliant 
	E1 field1:2; //Compliant

	char D:2;//Noncompliant
	E2 field2:2; //Compliant
};

In this example, Polyspace flags the use of any types other than enums and unsigned integers in bitfields. For instance, the use of std::int32_t and char is flagged. Polyspace does not flag the use of std::uint8_t or enums such as E1 or E2.

Check Information

Group: Classes
Category: Required, Partially automated

Version History

Introduced in R2019a