[llvm-dev] GCC 5 and -Wstrict-aliasing in JSON.h

Leslie Zhai via llvm-dev llvm-dev at lists.llvm.org
Wed Aug 8 19:09:07 PDT 2018


Hi Kim,

Thanks for your report!

My GCC 4.9.3 is *not* able to reproduce the issue with reduced testcase[1]

But it is able to reproduce the issue by compiling LLVM with GCC 
toolchain[2]


In file included from /home/loongson/llvm/lib/Support/JSON.cpp:10:0:
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = bool]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:393:23:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
      return *reinterpret_cast<T *>(Union.buffer);
                                                ^
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = double]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:398:25:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = long int]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:400:26:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = std::basic_string<char>]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:418:46:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = llvm::StringRef]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:420:34:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = llvm::json::Object]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:424:62:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = llvm::json::Array]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:430:60:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
In file included from /home/loongson/llvm/lib/TableGen/JSONBackend.cpp:20:0:
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = bool]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:393:23:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
      return *reinterpret_cast<T *>(Union.buffer);
                                                ^
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = double]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:398:25:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = long int]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:400:26:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = std::basic_string<char>]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:418:46:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = llvm::StringRef]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:420:34:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = llvm::json::Object]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:424:62:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]
/home/loongson/llvm/include/llvm/Support/JSON.h: In instantiation of ‘T& 
llvm::json::Value::as() const [with T = llvm::json::Array]’:
/home/loongson/llvm/include/llvm/Support/JSON.h:430:60:   required from here
/home/loongson/llvm/include/llvm/Support/JSON.h:455:47: warning: 
dereferencing type-punned pointer will break strict-aliasing rules 
[-Wstrict-aliasing]


Hi GCC developers,

Thanks for your patch to gcc-7-branch!

Could you test to compile LLVM with GCC old versions 4/5/6? Does it need 
to backport your patch to GCC old version?


1.
$ cat punning.cpp
template<unsigned _Len, unsigned _Align>
struct aligned_storage
{
   union type
     {
       unsigned char __data[_Len];
       struct __attribute__((__aligned__((_Align)))) { } __align;
     };
};

aligned_storage<sizeof(int), alignof(int)>::type storage;

int main()
{
   *reinterpret_cast<int*>(&storage) = 42;
}

2.
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/mips64el-redhat-linux/4.9.3/lto-wrapper
Target: mips64el-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man 
--infodir=/usr/share/info 
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap 
--enable-shared --enable-threads=posix --enable-checking=release 
--enable-multilib --with-system-zlib --enable-__cxa_atexit 
--disable-libunwind-exceptions --enable-gnu-unique-object 
--enable-linker-build-id --with-arch=loongson3a 
--enable-languages=c,c++,objc,obj-c++,fortran,go,lto --enable-plugin 
--disable-libgcj 
--with-isl=/builddir/build/BUILD/gcc-4.9.3/obj-mips64el-redhat-linux/isl-install 
--with-cloog=/builddir/build/BUILD/gcc-4.9.3/obj-mips64el-redhat-linux/cloog-install 
--enable-gnu-indirect-function --with-long-double-128 
--build=mips64el-redhat-linux
Thread model: posix
gcc version 4.9.3 20150626 (Red Hat 4.9.3-8) (GCC)


在 2018年08月09日 04:31, Kim Gräsman via llvm-dev 写道:
> Hello,
>
> For the IWYU project, we have a buildbot on Ubuntu 16.04 and its
> bundled GCC (which I think is some GCC 5 variant).
>
> We're getting a number of -Wstrict-aliasing warnings from JSON.h on this line:
> https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Support/JSON.h#L455
>
> I'm not sure if GCC has a point here but GCC 7.2 does not complain, so
> I'm going to guess no.
>
> Would you consider patches to disable -Wstrict-aliasing wholesale for
> GCC 5 and older?
But it is *no* warning when bootstrap with LLVM toolchain, and Sam 
should test it before code review https://reviews.llvm.org/D45753

>
> - Kim
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

-- 
Regards,
Leslie Zhai




More information about the llvm-dev mailing list