[PATCH] D114718: [analyzer] Implement a new checker for Strict Aliasing Rule.

Denys Petrov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 29 09:39:48 PST 2021

ASDenysPetrov created this revision.
ASDenysPetrov added reviewers: rsmith, martong, NoQ, vsavchenko, steakhal, aaron.ballman, xazax.hun, Szelethus.
ASDenysPetrov added a project: clang.
Herald added subscribers: manas, jeroen.dobbelaere, dkrupp, donat.nagy, mikhail.ramalho, a.sidorin, rnkovacs, szepet, baloghadamsoftware, mgorny.
ASDenysPetrov requested review of this revision.
Herald added a subscriber: cfe-commits.

`StrictAliasingChecker` implements checks on violation of the next paragraph of the Standard which is known as **Strict Aliasing Rule**. It operates on variable loads and stores. The checker compares the original type of the value with the type of the pointer with which the value is aliased.
The paragraph:

> C++20 7.2.1 p11 [basic.lval]:
>  If a program attempts to access the stored value of an object through a glvalue whose type is not similar to one of the following types the behavior is undefined:
> - the dynamic type of the object,
> - a type that is the signed or unsigned type corresponding to the dynamic type of the object, or
> - a char, unsigned char, or std​::​byte type.


  int x = 42;
  auto c = *((char*)&x); // The original type is `int`. The aliased type is `char`. OK. 
  auto f = *((float*)&x); // The original type is `int`. The aliased type is `float`. UB. 

  rG LLVM Github Monorepo



-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114718.390387.patch
Type: text/x-patch
Size: 12061 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211129/63c56338/attachment-0001.bin>

More information about the cfe-commits mailing list