Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

But:

    DIV_ROUND_UP(psize, cols);
I need sleep, so I'm not going to check, but that may appear to be an intermediate value to a C compiler, and thus escape -Wunused-variable.


> that may appear to be an intermediate value to a C compiler, and thus escape -Wunused-variable.

It does indeed (I checked):

    $ cat test.c
    
    #include <stdint.h>
    
    #define     DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))
    
    struct vdev;
    typedef struct vdev vdev_t;
    
    struct vdev
    {
        uint64_t     vdev_ashift;
        void        *vdev_tsd;
        vdev_t      *vdev_top;
    };
    
    struct vdev_raidz;
    typedef struct vdev_raidz vdev_raidz_t;
    
    struct vdev_raidz
    {
        int          vd_original_width;
        int          vd_physical_width;
        int          vd_nparity;
    };
    
    extern uint64_t vdev_raidz_get_logical_width(vdev_raidz_t *, uint64_t);
    
    static uint64_t
    vdev_raidz_asize_to_psize(vdev_t *vd, uint64_t asize, uint64_t txg)
    {
        vdev_raidz_t *vdrz = vd->vdev_tsd;
        uint64_t psize;
        uint64_t ashift = vd->vdev_top->vdev_ashift;
        uint64_t cols = vdrz->vd_original_width;
        uint64_t nparity = vdrz->vd_nparity;
    
        cols = vdev_raidz_get_logical_width(vdrz, txg);
    
        psize = (asize >> ashift);
        psize -= nparity * DIV_ROUND_UP(psize, cols);
        psize <<= ashift;
    
        return (asize);
    }



    $ gcc-14 -Wall -Wextra -Wpedantic -Wunused -Wunused-variable -Wunused-but-set-variable -c test.c -o test.o
    
    test.c:28:5: warning: ‘vdev_raidz_asize_to_psize’ defined but not used [-Wunused-function]
       28 |     vdev_raidz_asize_to_psize(vdev_t *vd, uint64_t asize, uint64_t txg)
          |     ^~~~~~~~~~~~~~~~~~~~~~~~~



    $ clang-19 -Weverything -c test.c -o test.o
    
    test.c:33:31: warning: implicit conversion changes signedness: 'int' to 'uint64_t' (aka 'unsigned long') [-Wsign-conversion]
       33 |         uint64_t cols = vdrz->vd_original_width;
          |                  ~~~~   ~~~~~~^~~~~~~~~~~~~~~~~
    
    test.c:34:34: warning: implicit conversion changes signedness: 'int' to 'uint64_t' (aka 'unsigned long') [-Wsign-conversion]
       34 |         uint64_t nparity = vdrz->vd_nparity;
          |                  ~~~~~~~   ~~~~~~^~~~~~~~~~
    
    test.c:28:5: warning: unused function 'vdev_raidz_asize_to_psize' [-Wunused-function]
       28 |     vdev_raidz_asize_to_psize(vdev_t *vd, uint64_t asize, uint64_t txg)
          |     ^~~~~~~~~~~~~~~~~~~~~~~~~
    
    3 warnings generated.


(The unused-function diagnostic is expected for this test)

I would have also expected clang to catch the double assignment to cols, to be honest.


Oh, that's unfortunate :-(

I don't know these compiler internals, but how come the use of DIV_ROUND_UP() make the compilers ignore the unused state of the end variable?

I mean, after the DIV_ROUND_UP(), theres a bit shift:

    psize <<= ashift;
Only after the bit shift, nobody uses the result.

Shouldn't this be enough clues for the compiler to realize the variable is unused?


> Shouldn't this be enough clues for the compiler to realize the variable is unused?

You would think so! I can't imagine why it behaves this way.


> I can't imagine why it behaves this way.

The warning implementation must be simple minded: if the variable is ever read then it isn't "unused." The compiler need only set a flag on every read, and later scan all the flags. What you wish it did is track the lexical position of the last assignment and detect when pointless assignment occurs. This is clearly more complex and also has a higher performance cost. So for reasons of laziness and/or performance, the developers just haven't bothered.

That's what I imagine. Having dealt with C compilers for a long time now, it doesn't occur to me to expect better, so I guessed correctly that Wunused was not applicable here.


> That's what I imagine

That makes sense; thanks.


> (I checked)

Thank you.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: