3

I have been playing around a bit with the [[noreturn]] attribute, which I'm trying to get a handle of and make use of (I understand [[noreturn]] is a C++11 standard attribute and __attribute__((noreturn)) is a GCC/Clang extension). As part of this, I enabled the Clang warning -Wmissing-noreturn.

> clang++ -v
Ubuntu clang version 3.7.1-svn253742-1~exp1 (branches/release_37) (based on LLVM 3.7.1)
Target: x86_64-pc-linux-gnu
Thread model: posix

foo.cpp:

enum bar
{
  A = 1,
  B,
  C
};

void foo()
{
  switch (bar())
  {
    case A:
    case B:
    case C:
    default:
      break;
  }
}

int main()
{
  foo();
  return 0;
}

Then compile:

> clang++ foo.cpp -o foo -Wmissing-noreturn -std=c++14
foo.cpp:9:1: warning: function 'foo' could be declared with attribute 'noreturn'
      [-Wmissing-noreturn]
{
^
1 warning generated.

It appears to me that it would return! What's going on here? Is this a compiler bug?

If you remove the "= 1" from A, then it compiles fine without a warning.

If I do make the foo() function [[noreturn]] void foo(), then it does crash with a segmentation fault.

Jetski S-type
  • 1,138
  • 2
  • 16
  • 32
  • 4
    Looks like a clang bug to me. – Vaughn Cato Apr 01 '16 at 01:35
  • I can reproduce with Apple (XCode) `clang++` (version info `Apple LLVM version 7.3.0 (clang-703.0.29)` `Target: x86_64-apple-darwin15.4.0` `Thread model: posix`). I'm not sure I can interpret it yet — what does the default constructor for an `enum` return? (Dinking with your code, it appears that `bar()` returns 0 — which is interesting. But doesn't mean that the function doesn't return.) – Jonathan Leffler Apr 01 '16 at 01:38
  • @JonathanLeffler - good question. If I make it "A = 0", it doesn't have the warning. I was playing around with an enum class before, which behaves the same. Perhaps it always initialises it to 0 by default, which is invalid, and becomes a trap rather than dropping to default? Or undefined behaviour? – Jetski S-type Apr 01 '16 at 01:45
  • 1
    Superficially, the `default:` clause should catch the value 0 too, if the return from the enumeration constructor generating a value that isn't in the range of the enumeration isn't a problem. Given that `A = 0` in the enumeration alters things, it's going to take analysis of the wording, but at best the error message is misleading, I think. I'd support @VaughnCato's analysis provisionally: a bug in `clang++`. —— FYI: C11 has `_Noreturn` as a keyword and `` to provide a macro `#define noreturn _Noreturn`. But it's not enclosed in `[[noreturn]]` in C11. That's wholly tangential. – Jonathan Leffler Apr 01 '16 at 01:50

2 Answers2

-1

Ok sorry for delete the last answer. It was wrong. Ok noreturn meann that the function never end. and the segementation fould is because bar is not a variable

Aron Wolf
  • 58
  • 1
  • 8
  • The attribute `noreturn` (however it is decorated) means that when this function is called, the flow of control will never return from the function. That typically means that it, in turn, calls a function such as `exit()` or `_exit()` or something similar that stops the program. In the sample code, the only function called from within `foo()` is `bar()` — and that is a default constructor for the `enum bar { … };` type. It is not immediately obvious why that would not return — and if it does, then the function `foo()` would return, so the `noreturn` attribute would be inappropriate. – Jonathan Leffler Apr 01 '16 at 06:26
-3

Just to give a follow up to an old question: clang is correct, even if a little pedantic.

Indeed the attribute noreturn is applicable to your foo() function, since it doesn't return anything. The fact that clang of 2016 gets confused with the slight variation in the enumeration that is a problem.

But we should forgive clang for this pecadillo, for C++11 grammar is not what could be called very simple.

  • What do you mean? Please explain how foo() does not return to the caller! https://stackoverflow.com/questions/10538291/what-is-the-point-of-noreturn – Jetski S-type Jan 14 '19 at 22:22
  • Additionally, how is Clang correct if doing the suggested [[noreturn]] causes it to crash with a segfault?? – Jetski S-type Jan 14 '19 at 22:25