llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.32k stars 12.11k forks source link

Clang accepts typed enums language extension with -std=c99 #57836

Open strimo378 opened 2 years ago

strimo378 commented 2 years ago

Hi all,

typed enums are not supported by C99 and any later C standard. It is supported by clang as a language extension of C.

See https://godbolt.org/z/4W1dMc53e as an example.

In my opinion, clang should only support typed enums in C if compiling with e.g. -std=gnu99 and not with -std=c99.

Furthermore, the documentation at https://clang.llvm.org/docs/LanguageExtensions.html#introduction is lacking information that typed enums are supported by clang in C. It is only noted that typed enums are supported for ObjectC.

Btw. GCC is not supporting typed enums in C.

llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-frontend

AaronBallman commented 2 years ago

typed enums are not supported by C99 and any later C standard.

FWIW, they are supported by C2x: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3030.htm but we've not established whether our extension matches the standard behavior yet.

In my opinion, clang should only support typed enums in C if compiling with e.g. -std=gnu99 and not with -std=c99.

It is a conforming extension in C99 mode and doesn't require us to be in GNU mode. We meet the conformance requirements by issuing an extension diagnostic when passing -pedantic to the compiler: https://godbolt.org/z/G8fWoKacY

Furthermore, the documentation at https://clang.llvm.org/docs/LanguageExtensions.html#introduction is lacking information that typed enums are supported by clang in C. It is only noted that typed enums are supported for ObjectC.

Yeah, we definitely need to update the documentation!

llvmbot commented 2 years ago

@llvm/issue-subscribers-c

llvmbot commented 2 years ago

@llvm/issue-subscribers-c2x

strimo378 commented 2 years ago

Thank you very much for your fast answer.

FWIW, they are supported by C2x: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3030.htm but we've not established whether our extension matches the standard behavior yet.

Good to know, I only checked the cppreference webpage.

It is a conforming extension in C99 mode and doesn't require us to be in GNU mode. We meet the conformance requirements by issuing an extension diagnostic when passing -pedantic to the compiler: https://godbolt.org/z/G8fWoKacY

I suggest to explain the categories of extensions (i.e. conforming extension, non-conforming, GNU extension, clang extension) at the beginning of https://clang.llvm.org/docs/LanguageExtensions.html and when they issue an warning or error. Also a hint to the "-pedantic" and "-pedantic-error" command line option would be nice.

For example, I think it not obvious that using the restrict keyword in -std=c89 causes an error (because it is a non-conforming extension) while enums with fixed underlying type only cause an warning with -std=c89 -pedantic .