Main Content

MISRA C:2023 Rule 8.15

The argument of an integer constant macro shall have an appropriate form

Since R2024a

Description

Rule Definition

The argument of an integer constant macro shall have an appropriate form.

Rationale

It is undefined behavior to have multiple declarations of an object where the declarations have conflicting alignment specifications:

  • If you need to explicitly specify the alignment of an object using _Alignas, specify the same alignment for all declarations of that object.

  • If you do not need to specify the alignment of an object, do not use _Alignas in any declaration of that object.

You use explicit alignment specifications in declarations and definitions to optimize code execution by, for example, matching the memory alignment requirements of a hardware interface or of a single instruction, multiple data (SIMD) operation.

Polyspace Implementation

The coding rule checker reports a violation of this rule for multiple declarations of the same variable with conflicting alignment specifications.

Polyspace® does not check structure declarations for compliance with this rule.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

myHeader.h

#include <stdint.h>

extern _Alignas (4) int16_t var_align_match;
extern int16_t var_no_align;
extern _Alignas (float) int16_t var_align_no_align; //Noncompliant
extern _Alignas(float) int16_t var_align_type_other_type;

myFile.c

#include "myHeader.h"

_Alignas (4) int16_t var_align_match;
int16_t var_no_align;
int16_t var_align_no_align; 
_Alignas(double) int16_t var_align_type_other_type; //Noncompliant

In this example, header file myHeader.h declares some variables with explicit alignment specifications. The variables are declared again in file myFile.c which includes myHeader.h. Polyspace reports a coding rule violation for these variables with conflicting alignment specifications:

  • var_align_no_align — The variable is declared without an alignment specification in myFile.c but with an alignment specification in myHeader.h.

  • var_align_type_other_type — The variable is declared with an alignment requirement of afloat in myHeader.h and with an alignment requirement of a double in myFile.c.

In both cases, Polyspace reports the violation on the variable declaration with the stricter alignment specification.

The declarations of variables var_align_match and var_no_align are compliant with this coding rule because they are declared with the same alignment requirement in both files.

Check Information

Group: Declarations and definitions
Category: Required
AGC Category: Required

Version History

Introduced in R2024a