[llvm-bugs] [Bug 51158] New: Bad warning behavior for empty enums in general and std::byte in particular

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 21 10:48:15 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51158

            Bug ID: 51158
           Summary: Bad warning behavior for empty enums in general and
                    std::byte in particular
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: davidfromonline at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

If an enumeration has no enumerators, it is safe to assume that it can take on
any value of the underlying type and it is being used as a strong typedef not a
list of possible values. A few clang warnings do not take this edge case into
account, which shows up in the standard library because that is how `std::byte`
is implemented.

When compiling the following translation unit with `-Wcovered-switch-default`

```
enum class byte : unsigned char {};

void f(byte b) {
        switch (b) {
                default: break;
        }
}
```

clang warns (incorrectly, in my opinion)

```
<source>:5:3: warning: default label in switch which covers all enumeration
values [-Wcovered-switch-default]
                default: break;
                ^
1 warning generated.
Compiler returned: 0
```

See it live: https://godbolt.org/z/vazPzefxr


When compiling the following translation unit with no additional flags

```
enum class byte : unsigned char {};

void f(byte b) {
        switch (b) {
                case byte(0x00): break;
        }
}
```

clang warns (incorrectly)

```
<source>:5:8: warning: case value not in enumerated type 'byte' [-Wswitch]
                case byte(0x00): break;
                     ^
1 warning generated.
Compiler returned: 0
```

See it live: https://godbolt.org/z/bKGoWMev9

It's possible to turn off the warning by changing the code slightly to

```
enum class byte : unsigned char {};

constexpr auto zero = byte(0x00);
void f(byte b) {
        switch (b) {
                case zero: break;
        }
}
```

Although making it a local constexpr variable does not turn off the warning,
which is strange.


Compiling the following translation unit with `-Wmissing-noreturn`

```
enum class byte : unsigned char {};

void f(byte b) {
        switch (b) {
        }
}
```

causes clang to incorrectly emit

```
<source>:3:16: warning: function 'f' could be declared with attribute
'noreturn' [-Wmissing-noreturn]
void f(byte b) {
               ^
1 warning generated.
Compiler returned: 0
```

See it live: https://godbolt.org/z/qYY9z75cs


The following translation unit causes clang to crash with an assertion failure
when compiled with `--Weverything -Wno-c++98-compat-pedantic
-Wno-missing-prototypes -Wno-missing-noreturn -Wno-covered-switch-default` (not
sure which warning in particular causes it)

```
enum class byte : unsigned char {};

void f(byte b) {
        switch (b) {
                default: break;
        }
}
```


```
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash
backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -g -o
/app/output.s -mllvm --x86-asm-syntax=intel -S
--gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics
-fno-crash-diagnostics -Weverything -Wno-c++98-compat-pedantic
-Wno-missing-prototypes -Wno-missing-noreturn -Wno-covered-switch-default
<source>
1.      <eof> parser at end of file
2.      <source>:3:16: parsing function body 'f'
 #0 0x000055b680a1da3f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055b680a1b900 llvm::sys::CleanupOnSignal(unsigned long)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x33da900)
 #2 0x000055b68096d2e8 CrashRecoverySignalHandler(int)
CrashRecoveryContext.cpp:0:0
 #3 0x00007f3624e143c0 __restore_rt
(/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)
 #4 0x000055b68312a113
clang::consumed::ConsumedAnalyzer::run(clang::AnalysisDeclContext&)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x5ae9113)
 #5 0x000055b683054ece
clang::sema::AnalysisBasedWarnings::IssueWarnings(clang::sema::AnalysisBasedWarnings::Policy,
clang::sema::FunctionScopeInfo*, clang::Decl const*, clang::QualType)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a13ece)
 #6 0x000055b6828cfa63
clang::Sema::PopFunctionScopeInfo(clang::sema::AnalysisBasedWarnings::Policy
const*, clang::Decl const*, clang::QualType)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x528ea63)
 #7 0x000055b682a2fd70 clang::Sema::ActOnFinishFunctionBody(clang::Decl*,
clang::Stmt*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53eed70)
 #8 0x000055b68285f377 clang::Parser::ParseFunctionStatementBody(clang::Decl*,
clang::Parser::ParseScope&)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x521e377)
 #9 0x000055b6827b94f4
clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&,
clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x51784f4)
#10 0x000055b6827e24a1 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&,
clang::DeclaratorContext, clang::SourceLocation*, clang::Parser::ForRangeInit*)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x51a14a1)
#11 0x000055b6827b4589
clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributesWithRange&,
clang::ParsingDeclSpec&, clang::AccessSpecifier)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x5173589)
#12 0x000055b6827b4c91
clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributesWithRange&,
clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.280) Parser.cpp:0:0
#13 0x000055b6827babb9
clang::Parser::ParseExternalDeclaration(clang::ParsedAttributesWithRange&,
clang::ParsingDeclSpec*)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x5179bb9)
#14 0x000055b6827bbfd9
clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, bool)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x517afd9)
#15 0x000055b6827af839 clang::ParseAST(clang::Sema&, bool, bool)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x516e839)
#16 0x000055b681956b52 clang::CodeGenAction::ExecuteAction()
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x4315b52)
#17 0x000055b68130a901 clang::FrontendAction::Execute()
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cc9901)
#18 0x000055b6812a86f2
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c676f2)
#19 0x000055b6813d6643
clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3d95643)
#20 0x000055b67e7f09ec cc1_main(llvm::ArrayRef<char const*>, char const*,
void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x11af9ec)
#21 0x000055b67e7ecb8d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&)
driver.cpp:0:0
#22 0x000055b6811557a5 void llvm::function_ref<void
()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>
>, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#23 0x000055b68096d8d3
llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x332c8d3)
#24 0x000055b68115767e
clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>
>, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >*, bool*) const
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b1667e)
#25 0x000055b68112d37a
clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&,
clang::driver::Command const*&) const
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3aec37a)
#26 0x000055b68112decf
clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&,
llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3aececf)
#27 0x000055b681137145
clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&,
llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&)
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x3af6145)
#28 0x000055b67e705d2e main
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x10c4d2e)
#29 0x00007f36248c40b3 __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x270b3)
#30 0x000055b67e7ec70a _start
(/opt/compiler-explorer/clang-trunk/bin/clang+++0x11ab70a)
clang-13: error: clang frontend command failed with exit code 139 (use -v to
see invocation)
Compiler returned: 139
```

See it live: https://godbolt.org/z/G3jYTcobf




All of these in a single file: https://godbolt.org/z/hbGGWa9qW

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210721/3c991e71/attachment-0001.html>


More information about the llvm-bugs mailing list