https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87950

            Bug ID: 87950
           Summary: GCC warns about reaching end of non-void function when
                    all switch is completely handled
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vlovich at gmail dot com
  Target Milestone: ---

Noticed this for a while. If a function has a single switch statement that
handles all enum values & returns a value GCC will warn about the function not
returning a value whereas clang does not.  GCC requires an explicit
__builtin_unreachable() annotation after the switch. Maybe it's an intentional
disagreement over how to interpret the spec? AFAICT via objdump the generated
assembly is identical between clang & GCC even when compiled with
optimizations.

> cat test.c
enum Enum {
  A,
  B,
};

int CoverMyBases(enum Enum x) {
        switch (x) {
                case A:
                        return 1;
                case B:
                        return 0;
        }
}

int main(int argc, const char **argv) {
        CoverMyBases(A);
        CoverMyBases(B);
        return 0;
}

> gcc-8 -Wall test.c
test.c: In function 'CoverMyBases':
test.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

> clang -Wall test.c
> gcc-8 --version
gcc-8 (Homebrew GCC 8.2.0) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> clang --version
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

This applies to both C & C++.

Reply via email to