llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

`__builtin_offsetof` in clang does not support qualified member name

Open Ming-3364 opened this issue 2 years ago • 2 comments

Consider the following code:

#include <cstddef>
struct bs{int b;};
struct tst:bs{
    char b;
};

auto off=offsetof(tst,bs::b);

This is rejected by clang with the following diagnostic:

<source>:7:25: error: expected ')'
    7 | auto off=offsetof(tst,bs::b);
      |                         ^
<source>:7:10: note: to match this '('
    7 | auto off=offsetof(tst,bs::b);
      |          ^
/opt/compiler-explorer/clang-trunk-20240803/lib/clang/20/include/__stddef_offsetof.h:16:42: note: expanded from macro 'offsetof'
   16 | #define offsetof(t, d) __builtin_offsetof(t, d)

In fact, calling the__builtin_offsetof function with a qualified member name as its second parameter is always rejected by clang. This seems unreasonable, as using qualified name is necessary in some special cases. For example, in the test above, b and bs::b do not actually refer to the same variable.

Additionally, the error message provided by clang is also somewhat confusing.

https://godbolt.org/z/favKoPaTv

Ming-3364 avatar Aug 03 '24 15:08 Ming-3364

@llvm/issue-subscribers-clang-frontend

Author: Raki (Ming-3364)

Consider the following code:
#include &lt;cstddef&gt;
struct bs{int b;};
struct tst:bs{
    char b;
};

auto off=offsetof(tst,bs::b);

This is rejected by clang with the following diagnostic:

&lt;source&gt;:7:25: error: expected ')'
    7 | auto off=offsetof(tst,bs::b);
      |                         ^
&lt;source&gt;:7:10: note: to match this '('
    7 | auto off=offsetof(tst,bs::b);
      |          ^
/opt/compiler-explorer/clang-trunk-20240803/lib/clang/20/include/__stddef_offsetof.h:16:42: note: expanded from macro 'offsetof'
   16 | #define offsetof(t, d) __builtin_offsetof(t, d)

In fact, calling the__builtin_offsetof function with a qualified member name as its second parameter is always rejected by clang. This seems unreasonable, as using qualified name is necessary in some special cases. For example, in the test above, b and bs::b do not actually refer to the same variable.

Additionally, the error message provided by clang is also somewhat confusing.

https://godbolt.org/z/favKoPaTv

llvmbot avatar Aug 03 '24 15:08 llvmbot

Duplicate of #64154

MitalAshok avatar Aug 03 '24 17:08 MitalAshok