Main Content

MISRA C:2012 Rule 21.22

All operand arguments to any type-generic macros declared in <tgmath.h> shall have an appropriate essential type

Since R2024a

Description

Rule Definition

All operand arguments to any type-generic macros declared in <tgmath.h> shall have an appropriate essential type.

This rule comes from MISRA C™: 2012 Amendment 3.

Rationale

The type-generic macros declared in tgmath.h support only arguments of essentially signed, essentially unsigned, or essentially floating types. Arguments of other types result in undefined behavior. For more information on essential types, see MISRA C:2012 Rule 10.1.

A subset of these macros do not allow arguments of essentially complex floating types. For instance, the function atan2() does not support complex arguments. For these macros, using arguments of essentially complex floating types result in undefined behavior.

Polyspace Implementation

The rule checker reports violations if a macro declared in tgmath.h is used with arguments that have one of the following data types:

  • Essentially character.

  • Essentially boolean.

  • Essentially enum.

  • Essentially complex floating, if the macro does not support complex floating arguments.

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

In this example, the macro sqrt() from tgmath.g is invoked four times.

  • The first two invocations with essentially floating and essentially signed arguments are compliant with the rule.

  • The last two invocations with essentially character and essentially enum arguments violate the rule.

#include<tgmath.h>

float fInput, fRes;
int iInput, iRes;

char cInput, cRes;

typedef enum number {
    ZERO,
    ONE,
    TWO
} number;

number numberInput, numberRes;

void sqrtOps(void) {
    fRes = sqrt(fInput);             // Compliant
    iRes = sqrt(iInput);             // Compliant
    cRes = sqrt(cInput);             // Noncompliant
    numberRes = sqrt(numberInput);   // Noncompliant
}

Check Information

Group: Standard Libraries
Category: Mandatory
AGC Category: Mandatory

Version History

Introduced in R2024a