Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

-Wimplicit-fallthrough in GCC 7

March 10, 2017
Marek Polacek
Related topics:
Developer ToolsLinuxC, C#, C++
Related products:
Developer ToolsRed Hat Enterprise Linux

Share:

    (See this article to install GCC 7 on Red Hat Enterprise Linux.)

    In C and C++, the cases of a switch statement are in fact labels, and the switch is essentially a go to that jumps to the desired label. Since labels do not change the flow of control, one case block falls through to the following case block, unless terminated by a return, a break, a no return call or similar. In the example below, "case 1" falls through to "case 2":

    switch (cond)
       {
       case 1:
         a = 1;
       case 2:
         a = 2;
         break;
       /* ... */
       }

    The switch fallthrough has been widely considered a design defect in C, a misfeature or, to use Marshall Cline's definition, evil [1]. An overwhelming majority of the time the fallthrough to the next case is not appropriate, and it's easy to forget to "break" at the end of a case, making this far too error prone. Yet GCC didn't have the ability to warn for undesirable fallthroughs. A feature request for such a warning was opened back in 2002, but it didn't get much attention for the next 14 years. But then the [[fallthrough]] attribute was approved for C++17 [2], the request gained more traction, and I decided to take on this project. Although my first attempts flopped, in the end, I was successful in getting all the pieces in place, and with help from other GCC developers, we were able to implement -Wimplicit-fallthrough. In the following article, I will attempt to describe the warning in more detail.

    The warning is enabled by -Wextra for C and C++. In the original test case above -Wimplicit-fallthrough will warn about "a = 1;" falling through to "case 2" like this:

    z.c: In function ‘f’:
    z.c:7:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
     a = 1;
     ~~^~~
    z.c:8:5: note: here
     case 2:
     ^~~~

    The warning is able to cope with various control-flow statements, nested scopes, gotos, and similar, and should only warn when appropriate. Consider the following snippet:

    switch (cond)
      {
      case 1:
        if (n > 20)
          return;
        else if (n > 10)
          {
            foo (9);
            break;
          }
        else
          foo (8); // warn here
      case 2:
      /* ... */
      }

    Here the compiler is smart enough to figure out that only the third branch can actually fall through, and the diagnostic is only given for the "foo (8);" statement. Naturally, since the warning occurs at compile-time, rather than at run-time, it's not totally foolproof since it's not possible for the warning to determine in all cases if the loop can terminate or not.

    To further reduce the rate of false positives, the warning will not warn when the last statement of a case block cannot fall through, as it happens e.g. for noreturn calls, as demonstrated in the example below:

     __attribute__((noreturn)) void die (void);
     
     switch (cond)
       {
       case -1:
         die ();
       case 0:
       /* ... */
       }

    The warning is also suppressed when a case label falls through to a case that merely breaks or returns:

    switch (cond)
     {
     case -1:
       foo ();
     default:
       break;
     }

    Since there are occasions where a switch case fallthrough is desirable, GCC provides several ways to quiet the warning that would otherwise occur. The first option is to use a null statement (";") with the fallthrough attribute. As mentioned before, C++17 provides a standard way to suppress the warning by
    using "[[fallthrough]];" instead of the non-standard way above with the GNU attribute. In C++11 and C++14 users can still use the [[...]] notation, only with the gnu:: prefix. In C++03 users will have to revert to the C way. (There is a proposal to add the [[...]] attribute syntax to C2X [3].)
    To summarize:

    switch (cond)
     {
     case 1:
       bar (1);
       __attribute__ ((fallthrough)); // C and C++03
     case 2:
       bar (2);
       [[gnu::fallthrough]]; // C++11 and C++14
     case 3:
       bar (3);
       [[fallthrough]]; // C++17 and above
     /* ... */
     }

    Of course, existing programs will be missing the smarts above to suppress the warning, and so enabling the warning would cause more harm than good. To reduce the annoyance when porting to GCC 7, we decided to recognize a wide variety of "falls through" comments. We hope that the code bases that use these comments consistently won't have to worry about turning on -Wimplicit-fallthrough.

    The range and shape of "falls through" comments accepted are contingent upon the level of the warning. (The default level is =3.)

    • -Wimplicit-fallthrough=0 disables the warning altogether.
    • -Wimplicit-fallthrough=1 treats any kind of comment as a "falls through" comment.
    • -Wimplicit-fallthrough=2 essentially accepts any comment that contains something that matches (case insensitively) "falls?[ \t-]*thr(ough|u)" regular expression.
    • -Wimplicit-fallthrough=3 case sensitively matches a wide range of regular expressions, listed in the GCC manual. E.g., all of these are accepted:
      /* Falls through. */
      /* fall-thru */
      /* Else falls through. */
      /* FALLTHRU */
      /* ... falls through ... */
      etc.
    • -Wimplicit-fallthrough=4 also, case sensitively matches a range of regular expressions but is much more strict than level =3.
    • -Wimplicit-fallthrough=5 doesn't recognize any comments.

    Note that the attributes are recognized on any level.

    For a more detailed description of the regular expressions accepted, please see the GCC manual [4].

    These "falls through" comments are meant to be used as in the example here:

    switch (cond)
     {
     case 0:
       foo (0);
       /* FALLTHRU */
     case 1:
       foo (1); 
       /* further code */
     }

    I.e., they should precede the case or default keywords. Be aware though that they might clash with macros so sometimes using the attributes will be necessary.

    References

    [1] http://www.cs.technion.ac.il/users/yechiel/c++-faq/defn-evil.html
    [2] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0188r0.pdf
    [3] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2052.pdf
    [4] https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html


    You can easily install GCC 7 on Red Hat Enterprise Linux using yum. GCC 7.3 was included in the May 2018 of Red Hat Developer Toolset (DTS). DTS provides the latest stable compilers and is part of the no-cost developer subscription.

    Last updated: October 18, 2018

    Recent Posts

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    • Leveraging Ansible Event-Driven Automation for Automatic CPU Scaling in OpenShift Virtualization

    • Python packaging for RHEL 9 & 10 using pyproject RPM macros

    • Kafka Monthly Digest: April 2025

    • How to scale smarter with OpenShift Serverless and Knative

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue

    OSZAR »