Repository navigation
C-generating back end: scoped enumerations with a signed underlying type [GH #485] #456
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
Draft
71iq
wants to merge
1
commit into
edgcpp:main
Choose a base branch
from
71iq:fix/edg-28934-scoped-enum-signedness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−10
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [GH #485] | ||
| C-generating back end: scoped enumerations with a signed underlying type | ||
|
|
||
| The C-generating back end emitted a scoped enumeration as a C enum type. The | ||
| C compiler could give that enum type an unsigned representation, so a negative | ||
| value of a scoped enumeration whose underlying type is signed (int, when no | ||
| enum-base is specified) was converted to a large positive value. The | ||
| underlying integer type is now used instead, as it already was for enums with | ||
| an explicit enum-base. Bit fields of an enumeration type with a fixed | ||
| underlying type (a scoped enumeration, or one with an explicit enum-base) now | ||
| also take their signedness from that type, so a bit field of an enumeration | ||
| with underlying type bool can hold the value true. For example: | ||
|
|
||
| enum class E { ZERO = 0 }; | ||
| int main() { | ||
| E e = static_cast<E>(-1); | ||
| long widened = static_cast<long>(e); // Was 4294967295, now -1. | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/tests/regressions/.gh-485-bit-field-size.rto/default.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=130300 Test_name.c | ||
| "Test_name.c", line 8: warning: bit field cannot contain all values of the enumerated type | ||
| struct S { F f : 2; }; | ||
| ^ | ||
|
|
||
| "Test_name.c", line 10: warning: bit field cannot contain all values of the enumerated type | ||
| struct T { E e : 2; }; | ||
| ^ | ||
|
|
9 changes: 9 additions & 0 deletions
9
tests/tests/regressions/.gh-485-bit-field-size.rto/default.2.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| fe_only -DTEST_NUMBER=2 --c++20 --strict Test_name.c | ||
| "Test_name.c", line 8: warning: bit field cannot contain all values of the enumerated type | ||
| struct S { F f : 2; }; | ||
| ^ | ||
|
|
||
| "Test_name.c", line 10: warning: bit field cannot contain all values of the enumerated type | ||
| struct T { E e : 2; }; | ||
| ^ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| eccp -DTEST_NUMBER=1 --c++20 --g++ --gnu_version=130300 Test_name.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ./a.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| eccp -DTEST_NUMBER=2 --c++20 --strict Test_name.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ./a.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| //type:fp | ||
| //options_all:--c++20 | ||
| //options:--g++ --gnu_version=130300:--strict | ||
| // An enum with a fixed underlying type gives its bit fields the signedness of | ||
| // that type, whether the enum is scoped or has an explicit enum base, so both | ||
| // of these bit fields are too small to hold the value 3. | ||
| enum class F { f3 = 3 }; | ||
| struct S { F f : 2; }; | ||
| enum E : int { e3 = 3 }; | ||
| struct T { E e : 2; }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //type:rp | ||
| //options_all:--c++20 | ||
| //options:--g++ --gnu_version=130300:--strict | ||
| // Test the emitted C representation as well as the front-end type. | ||
| enum class Signed { zero = 0 }; | ||
| enum struct Empty {}; | ||
| enum class Unsigned : unsigned { zero = 0 }; | ||
| enum class Boolean : bool { zero = false, one = true }; | ||
| struct Fields { Signed s : 3; Unsigned u : 3; Boolean b : 1; }; | ||
| #ifdef __GNUC__ | ||
| static_assert(__is_same(__underlying_type(Signed), int), "signed base"); | ||
| static_assert(__is_same(__underlying_type(Empty), int), "empty base"); | ||
| #endif /* __GNUC__ */ | ||
| volatile int minus_one = -1; | ||
| int main() { | ||
| Signed s = static_cast<Signed>(minus_one); | ||
| Empty e = static_cast<Empty>(minus_one); | ||
| Fields f{static_cast<Signed>(minus_one), Unsigned::zero, Boolean::one}; | ||
| return static_cast<long>(s) != -1 || static_cast<long>(e) != -1 || | ||
| static_cast<int>(f.s) != -1 || static_cast<unsigned>(f.u) != 0 || | ||
| static_cast<int>(f.b) != 1; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.