[llvm-bugs] [Bug 45922] New: Misleading warning -Winvalid-offsetof

via llvm-bugs llvm-bugs at lists.llvm.org
Wed May 13 23:50:12 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45922

            Bug ID: 45922
           Summary: Misleading warning -Winvalid-offsetof
           Product: clang
           Version: 8.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++17
          Assignee: unassignedclangbugs at nondot.org
          Reporter: shachar at shemesh.biz
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Consider the following program:

> #include <iostream>
> 
> struct Base {
>     int foo;
> };
> 
> struct Child : public Base {
>     int bar;
> };
> 
> int main() {
>     std::cout<<"foo "<<offsetof(Child, foo)<<" bar "<<offsetof(Child, bar)<<"\n";
> }

When compiled, produces:
> $ clang++-8 -std=c++17 -o so so.cpp
> so.cpp:12:24: warning: offset of on non-standard-layout type 'Child' [-Winvalid-offsetof]
>     std::cout<<"foo "<<offsetof(Child, foo)<<" bar "<<offsetof(Child, bar)<<"\n";
>                        ^               ~~~
> /usr/include/clang/8.0.0/include/stddef.h:120:24: note: expanded from macro 'offsetof'
> #define offsetof(t, d) __builtin_offsetof(t, d)
>                        ^                     ~
> so.cpp:12:55: warning: offset of on non-standard-layout type 'Child' [-Winvalid-offsetof]
>     std::cout<<"foo "<<offsetof(Child, foo)<<" bar "<<offsetof(Child, bar)<<"\n";
>                                                       ^               ~~~
> /usr/include/clang/8.0.0/include/stddef.h:120:24: note: expanded from macro 'offsetof'
> #define offsetof(t, d) __builtin_offsetof(t, d)
>                        ^                     ~
> 2 warnings generated.

The C++ specs claim that taking an offset from a non-standard layout struct is
conditionally supported. The warning says this is what I am doing, but does not
mention any obvious consequences (i.e. - so what?), and the warning's option,
invalid-offset, suggests this won't work at all.

I suggest rephrasing the warning to say that this case is "non-portable". Even
gcc's horrific:

> so.cpp:12:33: warning: offsetof within non-standard-layout type ‘Child’ is conditionally-supported [-Winvalid-offsetof]
>      std::cout<<"foo "<<offsetof(Child, foo)<<" bar "<<offsetof(Child, bar)<<"\n";
>                                  ^

while sending me on a standard diving expedition to see what "conditionally
supported" means, is better than clang, that simply doesn't tell me where I
stand.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200514/6c68ae21/attachment.html>


More information about the llvm-bugs mailing list