Skip to content

Commit 907d5bb

Browse files
Merge branch 'develop-test-integerSizes'. Close #471.
**Description** The code generated by `copilot-c99` during tests produces warnings during compilation. The reason is that some constants included in the code are too big and the compiler is making assumptions about their necessary types. If those constants are annotated, their intention will be made clearer. **Type** - Bug: test produce warnings during compilation. **Additional context** None. **Requester** - Scott Talbert (Debian Developer & Debian Haskell Group) **Method to check presence of bug** Compiling `copilot-c99`'s tests can produce warnings: ``` main.c:9:23: warning: integer constant is so large that it is unsigned 9 | uint64_t input_s[] = {10613134964919841005}; | ^~~~~~~~~~~~~~~~~~~~ ``` **Expected result** The above tests should compile without warnings. **Solution implemented** Modify the [`CShow` instances for different types](https://github.com/Copilot-Language/copilot/blob/cfc565bcf52dd359eb7ee768549459402c1785df/copilot-c99/tests/Test/Copilot/Compile/C99.hs#L884-L916) so that constants are printed with the necessary annotations. Instead of hard-coding the suffixes, which produces code that might not be portable, we use macros defined in stdint.h to wrap the values. This customizes the suffixes for the architecture for which the C code produced is being compiled. **Further notes** - Tests are randomly generated, so there's no guarantee that code with such warnings will be produced by a specific run. This issue is therefore not always replicable unless we fix the number generator seed. - We have confirmed that the solution implemented works by asking the original user to check on their side that compilation now completes without those warnings.
2 parents 60c4484 + f06f903 commit 907d5bb

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

copilot-c99/CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
2023-12-14
1+
2023-12-17
22
* Change return type of main generated for tests. (#468)
3+
* Print constants in tests using portable suffixes. (#471).
34

45
2023-11-07
56
* Version bump (3.17). (#466)

copilot-c99/tests/Test/Copilot/Compile/C99.hs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -886,28 +886,52 @@ class CShow s where
886886
cshow :: s -> String
887887

888888
instance CShow Int8 where
889-
cshow = show
889+
-- Use a macro to ensure that any necessary suffixes are added to the number.
890+
-- We choose this macro instead of specifically adding a suffix for reasons
891+
-- of portability.
892+
cshow x = "INT8_C(" ++ show x ++ ")"
890893

891894
instance CShow Int16 where
892-
cshow = show
895+
-- Use a macro to ensure that any necessary suffixes are added to the number.
896+
-- We choose this macro instead of specifically adding a suffix for reasons
897+
-- of portability.
898+
cshow x = "INT16_C(" ++ show x ++ ")"
893899

894900
instance CShow Int32 where
895-
cshow = show
901+
-- Use a macro to ensure that any necessary suffixes are added to the number.
902+
-- We choose this macro instead of specifically adding a suffix for reasons
903+
-- of portability.
904+
cshow x = "INT32_C(" ++ show x ++ ")"
896905

897906
instance CShow Int64 where
898-
cshow = show
907+
-- Use a macro to ensure that any necessary suffixes are added to the number.
908+
-- We choose this macro instead of specifically adding a suffix for reasons
909+
-- of portability.
910+
cshow x = "INT64_C(" ++ show x ++ ")"
899911

900912
instance CShow Word8 where
901-
cshow = show
913+
-- Use a macro to ensure that any necessary suffixes are added to the number.
914+
-- We choose this macro instead of specifically adding a suffix for reasons
915+
-- of portability.
916+
cshow x = "UINT8_C(" ++ show x ++ ")"
902917

903918
instance CShow Word16 where
904-
cshow = show
919+
-- Use a macro to ensure that any necessary suffixes are added to the number.
920+
-- We choose this macro instead of specifically adding a suffix for reasons
921+
-- of portability.
922+
cshow x = "UINT16_C(" ++ show x ++ ")"
905923

906924
instance CShow Word32 where
907-
cshow = show
925+
-- Use a macro to ensure that any necessary suffixes are added to the number.
926+
-- We choose this macro instead of specifically adding a suffix for reasons
927+
-- of portability.
928+
cshow x = "UINT32_C(" ++ show x ++ ")"
908929

909930
instance CShow Word64 where
910-
cshow = show
931+
-- Use a macro to ensure that any necessary suffixes are added to the number.
932+
-- We choose this macro instead of specifically adding a suffix for reasons
933+
-- of portability.
934+
cshow x = "UINT64_C(" ++ show x ++ ")"
911935

912936
instance CShow Float where
913937
cshow = show

0 commit comments

Comments
 (0)