<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Misleading warning -Winvalid-offsetof"
   href="https://bugs.llvm.org/show_bug.cgi?id=45922">45922</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Misleading warning -Winvalid-offsetof
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++17
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>shachar@shemesh.biz
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following program:

<span class="quote">> #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";
> }</span >

When compiled, produces:
<span class="quote">> $ 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.</span >

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:

<span class="quote">> 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";
>                                  ^</span >

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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>