BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsla_fallthrough

Macros

#define BSLA_FALLTHROUGH
 

Detailed Description

Outline

Purpose

Provide a macro to suppress warnings on switch fall-throughs.

Macros

See also
bsla_annotations

Description

This component provides a preprocessor macro that suppresses compiler warnings about flow of control fall-through from one case or default of a switch statement to another. On compilers where the appropriate attribute is not supported, the macro expands to nothing.

Macro Reference

BSLA_FALLTHROUGH: This annotation should be placed in a case clause as the last statement within a flow of control that is expected to allow control to fall through instead of ending with a break, continue, or return. This will prevent compilers from warning about fall-through. The BSLA_FALLTHROUGH must be followed by a semicolon and may be nested within blocks, ifs, or elses.

BSLA_FALLTHROUGH_IS_ACTIVE: The macro BSLA_FALLTHROUGH_IS_ACTIVE is defined if BSLA_FALLTHROUGH expands to something with the desired effect; otherwise BSLA_FALLTHROUGH_IS_ACTIVE is not defined and BSLA_FALLTHROUGH expands to nothing.

Usage

This section illustrates intended use of this component.

Example 1: Suppressing Fall-Through Warnings in a switch Statement

First, we define a function:

int usageFunction(int jj)
// Demonstrate the usage of 'BSLA_FALLTHROUGH', read the specified
// 'jj'.
{
for (int ii = 0; ii < 5; ++ii) {

Then, we have a switch in the function:

switch (ii) {
case 0: {
printf("%d\n", jj - 3);

Next, we see that BSLA_FALLTHROUGH; as the last statement in a case block before falling through silences the fall-through warning from the compiler:

}
case 1:
#define BSLA_FALLTHROUGH
Definition bsla_fallthrough.h:186

Then, we see this also works on cases that don't have a {} block:

jj -= 6;
printf("%d\n", jj);
case 2: {
if (jj > 4) {
printf("%d\n", jj + 10);

Next, we see that a BSLA_FALLTHROUGH; works within an if block, provided that it's in the last statement in the flow of control before falling through:

}
else {
return 0; // RETURN
}
}
case 3: {
if (jj > 4) {
continue;
}
else {
printf("%d\n", ++jj);

Now, we see that a BSLA_FALLTHROUGH; can also occur as the last statement in an else block:

}
}
default: {
return 1; // RETURN
} break;
}
}
return -7;
}

Finally, we see that if we compile when BSLA_FALLTHROUGH_IS_ACTIVE is set, the above compiles with no warnings.

Macro Definition Documentation

◆ BSLA_FALLTHROUGH

#define BSLA_FALLTHROUGH