goblint / cil

C Intermediate Language
https://goblint.github.io/cil/
Other
136 stars 20 forks source link

First argument to ‘__builtin_choose_expr’ not a constant #117

Closed icmccorm closed 1 year ago

icmccorm commented 2 years ago

When compiling libgit2 using cilly, I've encountered the following error:

/home/.../libgit2/src/libgit2/config_file.c:481:20: error: first argument to ‘__builtin_choose_expr’ not a constant
  481 |         config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent);
      |                    ^~~~~~~~~~~~~~~~~~~~~

This error does not occur when using only gcc. This is occurring on Ubuntu 22.04.1 LTS with gcc 11.2.

sim642 commented 2 years ago

I guess GCC is doing more constant-folding to evaluate the condition than CIL does. Looking at the preprocessed source, where GIT_CONTAINER_OF, etc are expanded should reveal what the conditional expression is.

michael-schwarz commented 1 year ago

The corresponding expression appears to be fairly complex.

# define GIT_CONTAINER_OF(ptr, type, member) \
    __builtin_choose_expr( \
        __builtin_offsetof(type, member) == 0 && \
        __builtin_types_compatible_p(__typeof__(&((type *) 0)->member), __typeof__(ptr)), \
        ((type *) (ptr)), \
        (void)0)

We don't have the resources to investigate this further at the moment and make sure CIL's constant folding is up-to-date with recent versions of GCC, and such issues can usually be avoided with a little editing of the input files.

I'm closing this for now, though we would of course welcome patches to fix issues such as this one if someone has the resources to dive deeper here.