Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][enum_overflow]Clang incorrectly thinks that enumerators of enum does not fit in the largest integer type #120350

Closed
zhaojiangkun-1 opened this issue Dec 18, 2024 · 2 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer diverges-from:gcc Does the clang frontend diverge from gcc on this issue question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@zhaojiangkun-1
Copy link

zhaojiangkun-1 commented Dec 18, 2024

Test case:

enum E {
  e1 = 0xffffffffffffffff,
  e2, e3
} e = e3;

#define SA(I,X) int a##I[(X)? 1 : -1]

SA(1, sizeof(E) == sizeof(__int128));

Backtrace:

warning: incremented enumerator value 18446744073709551616 is not representable in the largest integer type [-Wenum-too-large]
  e2, e3
  ^
./ext1/int128-3.C:11:1: error: 'a1' declared as an array with a negative size
SA(1, sizeof(E) == sizeof(__int128));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./ext1/int128-3.C:9:26: note: expanded from macro 'SA'
#define SA(I,X) int a##I[(X)? 1 : -1]
                         ^~~~~~~~~~~
1 warning and 1 error generated.

It can be compiled in gcc, but an error is reported in clang.

https://godbolt.org/z/4Pdxs5sEs

This problem has been fixed before, but is it not compatible with this yet?
#24667

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Dec 18, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" diverges-from:gcc Does the clang frontend diverge from gcc on this issue and removed clang Clang issues not falling into any other category labels Dec 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (zhaojiangkun-1)

Test case: ``` enum E { e1 = 0xffffffffffffffff, e2, e3 } e = e3;

#define SA(I,X) int a##I[(X)? 1 : -1]

SA(1, sizeof(E) == sizeof(__int128));

Backtrace:

warning: incremented enumerator value 18446744073709551616 is not representable in the largest integer type [-Wenum-too-large]
e2, e3
^
./ext1/int128-3.C:11:1: error: 'a1' declared as an array with a negative size
SA(1, sizeof(E) == sizeof(__int128));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./ext1/int128-3.C:9:26: note: expanded from macro 'SA'
#define SA(I,X) int a##I[(X)? 1 : -1]
^~~~~~~~~~~
1 warning and 1 error generated.

It can be compiled in gcc, but an error is reported in clang.

https://godbolt.org/z/4Pdxs5sEs

This problem has been fixed before, but is it not compatible with this yet?
https://github.com/llvm/llvm-project/issues/24667

</details>

@hstk30-hw hstk30-hw added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Dec 18, 2024
@hstk30-hw
Copy link
Contributor

C99 6.7.2.2p2

The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int

C99 6.7.2.2p4

Each enumerated type shall be compatible with char, a signed integer type, or an
unsigned integer type. The choice of type is implementation-defined,108) but shall be
capable of representing the values of all the members of the enumeration.

// If we're not in C++, diagnose the overflow of enumerator values,
// which in C99 means that the enumerator value is not representable in
// an int (C99 6.7.2.2p2). However C23 permits enumerator values that
// are representable in some larger integral type and we allow it in
// older language modes as an extension.
// Exclude fixed enumerators since they are diagnosed with an error for
// this case.
if (!getLangOpts().CPlusPlus && !T.isNull() && !Enum->isFixed())
Diag(IdLoc, getLangOpts().C23
? diag::warn_c17_compat_enum_value_not_int
: diag::ext_c23_enum_value_not_int)
<< 1 << toString(EnumVal, 10) << 1;

@EugeneZelenko EugeneZelenko added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer diverges-from:gcc Does the clang frontend diverge from gcc on this issue question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants