Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should there be a diagnostic from GCC compiler for this ill-formed C++ code involving [[fallthrough]] attribute?

I was testing C++17 features on GCC compiler version 7.1.0. This is related to the fallthrough attribute and the following example (live example) is adapted from online CPP reference here

#include "iostream"
using namespace std;

int f(int n) {

  switch (n) {
    case 1:
    case 2:
      n = n + 20;
     [[fallthrough]];
    case 3: // no warning on fallthrough      
      n = n + 30;
    case 4: // compiler may warn on fallthrough      
      [[fallthrough]]; // ill­formed, not before a case label
      //n = n + 40;  //commented out to test if compiler will warn.
  }
  return n;
}    

int main()
{
    cout << f(1) << endl;
    cout << f(2) << endl;
    cout << f(3) << endl;
    cout << f(4) << endl;
    return 0;
}

The last [[fallthrough]] (for case 4:) is ill-formed.

The question on "What is the C++ compiler required to do with ill-formed programs according to the Standard?" here has the top answer stating that:

So to sum it up: if an ill-formed program contains a diagnosable violation for which the Standard does not explicitly specify "no diagnostic required", conforming implementations should emit a diagnostic.

So, I looked up the standard (N4713) to see if it stated that there was no diagnostic was required for this issue. I was not able to find any such statement.

Interestingly, after all this, when I added the following statement after the last [[fallthrough]]

n = n + 40;

the compiler warns (live example):

warning: attribute 'fallthrough' not preceding a case label or default label

So, two questions here:

  1. Has the compiler missed out on emitting a diagnostic, or am I missing something here?
  2. If it is a compiler issue, is it serious enough to be reported?
like image 504
P.W Avatar asked Aug 23 '18 10:08

P.W


People also ask

What is __ attribute __ (( Fallthrough ))?

The fallthrough attribute with a null statement serves as a fallthrough statement. It hints to the compiler that a statement that falls through to another case label, or user-defined label in a switch statement is intentional and thus the -Wimplicit-fallthrough warning must not trigger.

How do I turn off warnings in GCC?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

Which flags would you pass to your C++ compiler so it warns you about uninitialized variables?

Warn about uninitialized variables that are initialized with themselves. Note this option can only be used with the -Wuninitialized option. This warning is enabled by -Wall in C++. This option controls warnings when a declaration does not specify a type.

How do I enable warnings in GCC?

This warning is enabled by -Wall . Warn when a #pragma directive is encountered which is not understood by GCC. If this command line option is used, warnings will even be issued for unknown pragmas in system header files. This is not the case if the warnings were only enabled by the -Wall command line option.


1 Answers

  1. If it is a compiler issue, is it serious enough to be reported?

Yes, conformance bugs are important bugs, developers rely on compilers conforming to the standard (compiler may have modes that don't require strict conformance though i.e. gcc requires -pedantic to obtain all diagnostics required by the standard) What priority a bug gets is a different story but merely documenting the bug and having the compiler team acknowledge it as a bug can be a huge help to future developers who run into the bug.

  1. Has the compiler missed out on emitting a diagnostic, or am I missing something here?

Yes, this is ill-formed as per [dcl.attr.fallthrough#]p1:

... The next statement that would be executed after a fallthrough statement shall be a labeled statement whose label is a case label or default label for the same switch statement. The program is ill-formed if there is no such statement.

and the compiler is required to issue at least a diagnostic as per [intro.compliance]p2.2:

If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this document as “conditionally-supported” when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.

like image 130
Shafik Yaghmour Avatar answered Oct 07 '22 01:10

Shafik Yaghmour