Quick Links:

bal | bbl | bdl | bsl

Component bsla_fallthrough
[Package bsla]

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

Outline
Purpose:
Provide a macro to suppress warnings on switch fall-throughs.
Macros:
BSLA_FALLTHROUGH do not warn if switch case falls through
BSLA_FALLTHROUGH_IS_ACTIVE 1 if BSLA_FALLTHROUGH is active, else 0
See also:
Component 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 to 0 if BSLA_FALLTHROUGH expands to nothing and 1 otherwise.
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:
               BSLA_FALLTHROUGH;
            }
            case 1:
Then, we see this also works on cases that don't have a {} block:
              jj -= 6;
              printf("%d\n", jj);
              BSLA_FALLTHROUGH;
            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:
                  BSLA_FALLTHROUGH;
              }
              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:
                  BSLA_FALLTHROUGH;
              }
            }
            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.