AutosarC++17_10-A9.6.1¶
Bit-fields shall be either unsigned integral, or enumeration (with underlying type of unsigned integral type)
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Explicitly declaring a bit-field unsigned prevents unexpected sign extension, overflows and implementation-defined behavior.
Note that if a bit-field has enumeration type, then the enumeration base needs to be declared of an explicitly unsigned type.
Example
// $Id: A9-6-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#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; // Non-compliant - signed integral type
std::uint8_t b : 2U; // Compliant
bool c : 1; // Non-compliant - it is implementation-defined whether bool is
// signed or unsigned
char d : 2; // Non-compliant
wchar_t e : 2; // Non-compliant
E1 f1 : 2; // Compliant
E2 f2 : 2; // Non-compliant - E2 enum class underlying type is signed
// int
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
};
void Fn() noexcept {
C c;
c.f1 = E1::E11;
}
See Also
- JSF December 2005 [7]: AV Rule 154 Bit-fields shall have explicitly unsigned integral or enumeration types only.
- HIC++ v4.0 [8]: 9.2.1 Declare bit-fields with an explicitly unsigned integral or enumeration type.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
invalid_bitfield |
Bit-fields shall be either unsigned integral, or enumeration (with underlying type of unsigned integral type). |
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
allow_bool_bitfields¶
allow_bool_bitfields : bool = False
Setting this option to true will not report bit-fields of type bool.