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

Support for cv-qualifiers ("const" etc.) #134

Closed
yallop opened this issue Mar 18, 2014 · 10 comments
Closed

Support for cv-qualifiers ("const" etc.) #134

yallop opened this issue Mar 18, 2014 · 10 comments

Comments

@yallop
Copy link
Owner

yallop commented Mar 18, 2014

There's currently no support for const, volatile, etc., so bindings to those functions generate code with incorrect types.

@aantron
Copy link
Contributor

aantron commented Oct 13, 2018

This would be quite useful, especially if Ctypes.string then became a view of const char* rather than char*.

@kit-ty-kate
Copy link
Contributor

This is an issue for 4.10 as String_val now returns const char* instead of char*. This breaks sodium for instance:

#=== ERROR while compiling sodium.0.6.0 =======================================#
# context              2.0.5 | linux/x86_64 | ocaml-variants.4.10.0+trunk | file:///home/opam/opam-repository
# path                 ~/.opam/4.10.0+trunk/.opam-switch/build/sodium.0.6.0
# command              /usr/bin/make
# exit-code            2
# env-file             ~/.opam/log/sodium-6-45397e.env
# output-file          ~/.opam/log/sodium-6-45397e.out
### output ###
# [...]
# lib/sodium_stubs.c:947:17: note: in expansion of macro 'CTYPES_PTR_OF_OCAML_STRING'
#   947 |    char* x801 = CTYPES_PTR_OF_OCAML_STRING(x800);
#       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
# /home/opam/.opam/4.10.0+trunk/lib/ctypes/ctypes_cstubs_internals.h:20:3: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
#    20 |   (String_val(Field(s, 1)) + Long_val(Field(s, 0)))
#       |   ^
# lib/sodium_stubs.c:950:17: note: in expansion of macro 'CTYPES_PTR_OF_OCAML_STRING'
#   950 |    char* x806 = CTYPES_PTR_OF_OCAML_STRING(x797);
#       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
# cc1: all warnings being treated as errors
# Command exited with code 2.
# make: *** [Makefile:13: all] Error 10

Granted they use -Werror but this generates 900 lines of warnings when compiled

@yallop
Copy link
Owner Author

yallop commented Dec 23, 2019

Thanks for the report! The issue in sodium arises from the use of ocaml_bytes; it's fixed by #622.

@aantron
Copy link
Contributor

aantron commented Mar 30, 2020

There continues to be an issue with warnings on 4.10, with ocaml_string. For example:

c_generated_functions.c: In function ‘luv_stub_96_uv_pipe_connect’:
/mnt/c/Users/antron/code/luv/luv/_opam/lib/ctypes/ctypes_cstubs_internals.h:20:3: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   (String_val(Field(s, 1)) + Long_val(Field(s, 0)))
   ^
c_generated_functions.c:655:17: note: in expansion of macro ‘CTYPES_PTR_OF_OCAML_STRING’    char* x480 = CTYPES_PTR_OF_OCAML_STRING(x475);

The binding is generated from this:

    let connect =
      foreign "uv_pipe_connect"
        (ptr Types.Stream.Connect_request.t @->
         ptr t @->
         ocaml_string @->
         Stream.Connect_request.trampoline @->
          returning void)

The generated code:

value luv_stub_96_uv_pipe_connect(value x477, value x476, value x475,
                                  value x474)
{
   uv_connect_t* x478 = CTYPES_ADDR_OF_FATPTR(x477);
   uv_pipe_t* x479 = CTYPES_ADDR_OF_FATPTR(x476);
   char* x480 = CTYPES_PTR_OF_OCAML_STRING(x475);
   void(* x481)(uv_connect_t*, int) = CTYPES_ADDR_OF_FATPTR(x474);
   uv_pipe_connect(x478, x479, x480, x481);
   return Val_unit;
}

It appears that char* x480 should be const char* x480.

@yallop
Copy link
Owner Author

yallop commented Apr 30, 2020

@aantron: #646 will eliminate these warnings.

@aantron
Copy link
Contributor

aantron commented Apr 30, 2020

Great, thanks!

@tjammer
Copy link

tjammer commented May 20, 2024

With gcc14, -Wincompatible-pointer-types is now an error instead of a warning, see https://gcc.gnu.org/gcc-14/porting_to.html#warnings-as-errors.

@yallop
Copy link
Owner Author

yallop commented Aug 8, 2024

#780 makes it possible to generate const-correct code, which should help to address issues with gcc14.

@yallop
Copy link
Owner Author

yallop commented Aug 10, 2024

This would be quite useful, especially if Ctypes.string then became a view of const char* rather than char*.

That's a good idea, implemented in #782.

@yallop
Copy link
Owner Author

yallop commented Aug 12, 2024

const and volatile are available in ctypes 0.23.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants