Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enum-ptr-to-record.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enum-ptr-to-record.cpp @@ -0,0 +1,122 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "enums.h" +#include "records.h" + +template +T *create() { return new T; } + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } + +#define CHECKS \ + CHECK(x, EmptyClass) \ + CHECK(x, StructInt) \ + CHECK(x, Base) \ + CHECK(x, AnotherBase) \ + CHECK(x, Derived) \ + CHECK(x, MostDerived) \ + CHECK(x, EmptyUnion) \ + CHECK(x, UnionUchar) \ + CHECK(x, UnionInt) \ + CHECK(x, UnionClass) + +void std_byte_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void std_OtherByte_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnum_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumChar_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumInt_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Enum_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumChar_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumUChar_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumInt_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void E_Dummy_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void std_byte_test(std::byte *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Enum_test(Enum *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumChar_test(EnumChar *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumInt_test(EnumInt *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enum-var-to-record.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enum-var-to-record.cpp @@ -0,0 +1,119 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "enums.h" +#include "records.h" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } + +#define CHECKS \ + CHECK(x, EmptyClass) \ + CHECK(x, StructInt) \ + CHECK(x, Base) \ + CHECK(x, AnotherBase) \ + CHECK(x, Derived) \ + CHECK(x, MostDerived) \ + CHECK(x, EmptyUnion) \ + CHECK(x, UnionUchar) \ + CHECK(x, UnionInt) \ + CHECK(x, UnionClass) + +void std_byte_test() { + std::byte x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void std_OtherByte_test() { + std::OtherByte x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnum_test() { + ClassEnum x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumChar_test() { + ClassEnumChar x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + ClassEnumUChar x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumInt_test() { + ClassEnumInt x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Enum_test() { + Enum x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumChar_test() { + EnumChar x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumUChar_test() { + EnumUChar x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumInt_test() { + EnumInt x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void E_Dummy_test() { + decltype(E_Dummy) x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void std_byte_test(std::byte x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Enum_test(Enum x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumChar_test(EnumChar x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EnumInt_test(EnumInt x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/multi-casts.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/multi-casts.cpp @@ -0,0 +1,132 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "enums.h" +#include "records.h" + +#define CAST_SEQ(orig, type1, type2, alias) \ + { \ + orig a{}; \ + type1 *b = reinterpret_cast(&a); \ + type2 *c = reinterpret_cast(b); \ + alias *d = reinterpret_cast(c); \ + alias e = *d; \ + } + +#define CHECK_TWO(orig, type1, type2, alias) \ + CAST_SEQ(orig, type1, type2, alias) \ + CAST_SEQ(orig, type2, type1, alias) + +// NOTE: This macros produces 2 `warnings` and 2 `notes`. +// And that's OK. They are not related to needed ones. +#define CHECK_ALL(orig, alias) \ + CHECK_TWO(orig, unsigned char, int, alias) \ + CHECK_TWO(orig, signed char, short, alias) \ + CHECK_TWO(orig, char, unsigned long, alias) \ + CHECK_TWO(orig, long long, std::byte, alias) \ + CHECK_TWO(orig, double, Enum, alias) \ + CHECK_TWO(orig, float, ClassEnumInt, alias) \ + CHECK_TWO(orig, unsigned int, StructInt, alias) \ + CHECK_TWO(orig, bool, UnionInt, alias) \ + CHECK_TWO(orig, Enum, ClassEnumUChar, alias) \ + CHECK_TWO(orig, EnumInt, std::OtherByte, alias) \ + CHECK_TWO(orig, ClassEnum, StructInt, alias) \ + CHECK_TWO(orig, std::byte, MostDerived, alias) \ + CHECK_TWO(orig, EmptyClass, UnionUchar, alias) \ + CHECK_TWO(orig, Base, Derived, alias) \ + CHECK_TWO(orig, Base, AnotherBase, alias) \ + CHECK_TWO(orig, EmptyUnion, StructInt, alias) + +#define CHECK_ALL_PASS(orig) \ + CHECK_ALL(orig, char) \ + CHECK_ALL(orig, unsigned char) \ + CHECK_ALL(orig, std::byte) \ + CHECK_ALL(orig, orig) + +#define CHECK_SCALAR(orig) \ + CHECK_ALL(orig, bool) \ + CHECK_ALL(orig, signed char) \ + CHECK_ALL(orig, double) + +#define CHECK_ENUM(orig) \ + CHECK_ALL(orig, std::OtherByte) \ + CHECK_ALL(orig, ClassEnumInt) \ + CHECK_ALL(orig, decltype(E_Dummy)) + +#define CHECK_RECORD(orig) \ + CHECK_ALL(orig, StructInt) \ + CHECK_ALL(orig, MostDerived) \ + CHECK_ALL(orig, UnionUchar) + +void scalar_pass_test() { + CHECK_ALL_PASS(bool) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(char) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(short) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(long long) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(signed char) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(unsigned char) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(unsigned int) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(long double) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} +} + +void scalar_warn_test() { + CHECK_SCALAR(float) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_SCALAR(short) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_SCALAR(long long) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(unsigned char) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(unsigned int) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(unsigned long) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_RECORD(char) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_RECORD(long double) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_RECORD(unsigned short) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} +} + +void enum_pass_test() { + CHECK_ALL_PASS(std::byte) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(std::OtherByte) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(ClassEnum) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(ClassEnumChar) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(ClassEnumUChar) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(ClassEnumInt) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(Enum) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(EnumChar) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(EnumUChar) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(EnumInt) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(decltype(E_Dummy)) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} +} + +void enum_warn_test() { + CHECK_SCALAR(std::byte) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_SCALAR(std::OtherByte) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_SCALAR(ClassEnum) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(ClassEnumChar) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(ClassEnumUChar) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(EnumInt) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_RECORD(Enum) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_RECORD(ClassEnumInt) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_RECORD(decltype(E_Dummy)) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} +} + +void record_pass_test() { + CHECK_ALL_PASS(EmptyClass) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(StructInt) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(Base) // expected-warning 18{{reinterpret_cast}} expected-note 18{{static_cast}} + CHECK_ALL_PASS(AnotherBase) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(Derived) // expected-warning 18{{reinterpret_cast}} expected-note 18{{static_cast}} + CHECK_ALL_PASS(MostDerived) // expected-warning 18{{reinterpret_cast}} expected-note 18{{static_cast}} + CHECK_ALL_PASS(EmptyUnion) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(UnionUchar) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(UnionInt) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_ALL_PASS(UnionClass) // expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} +} + +void record_warn_test() { + CHECK_SCALAR(EmptyClass) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_SCALAR(Base) // expected-warning 96{{Undefined behavior}} expected-warning 12{{reinterpret_cast}} expected-note 12{{static_cast}} + CHECK_SCALAR(AnotherBase) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(Derived) // expected-warning 96{{Undefined behavior}} expected-warning 12{{reinterpret_cast}} expected-note 12{{static_cast}} + CHECK_ENUM(EmptyUnion) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_ENUM(UnionUchar) // expected-warning 96{{Undefined behavior}} expected-warning 6{{reinterpret_cast}} expected-note 6{{static_cast}} + CHECK_RECORD(UnionInt) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} + CHECK_RECORD(UnionClass) // expected-warning 96{{Undefined behavior}} expected-warning 8{{reinterpret_cast}} expected-note 8{{static_cast}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/record-ptr-to-enum.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/record-ptr-to-enum.cpp @@ -0,0 +1,113 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "enums.h" +#include "records.h" + +template +T *create(); + +#define CHECK(var, type) \ + { auto y = *(type *)var; } + +#define CHECKS \ + CHECK(x, std::OtherByte) \ + CHECK(x, ClassEnum) \ + CHECK(x, ClassEnumChar) \ + CHECK(x, ClassEnumUChar) \ + CHECK(x, ClassEnumInt) \ + CHECK(x, Enum) \ + CHECK(x, EnumChar) \ + CHECK(x, EnumUChar) \ + CHECK(x, EnumInt) \ + CHECK(x, decltype(E_Dummy)) + +void EmptyClass_test() { + EmptyClass *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void StructInt_test() { + StructInt *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Base_test() { + Base *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Derived_test() { + Derived *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void StructInt_test(StructInt *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Base_test(Base *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Derived_test(Derived *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void MostDerived_test(MostDerived *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionInt_test(UnionInt *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionClass_test(UnionClass *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/record-ptr-to-record.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/record-ptr-to-record.cpp @@ -0,0 +1,280 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "records.h" + +template +T *create(); + +#define CHECK(var, type) \ + { auto y = *reinterpret_cast(var); } + +void EmptyClass_test() { + auto *x = create(); + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void StructInt_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Base_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void AnotherBase_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Derived_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void MostDerived_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void EmptyUnion_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionUchar_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionInt_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionClass_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // no-warning +} + +void EmptyClass_test(EmptyClass *x) { + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void StructInt_test(StructInt *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Base_test(Base *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Derived_test(Derived *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void MostDerived_test(MostDerived *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionInt_test(UnionInt *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionClass_test(UnionClass *x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // no-warning +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/record-ptr-to-scalar.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/record-ptr-to-scalar.cpp @@ -0,0 +1,139 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "records.h" + +template +T *create(); + +#define CHECK(var, type) \ + { auto y = *(type *)var; } + +#define CHECKS_NO_WARN \ + CHECK(x, char) \ + CHECK(x, unsigned char) + +#define CHECKS \ + CHECK(x, bool) \ + CHECK(x, short) \ + CHECK(x, int) \ + CHECK(x, long) \ + CHECK(x, long long) \ + CHECK(x, signed char) \ + CHECK(x, unsigned short) \ + CHECK(x, unsigned int) \ + CHECK(x, unsigned long) \ + CHECK(x, unsigned long long) \ + CHECK(x, float) \ + CHECK(x, double) \ + CHECK(x, long double) + +void EmptyClass_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void StructInt_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Base_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void AnotherBase_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Derived_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void MostDerived_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void EmptyUnion_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionUchar_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionInt_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionClass_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void StructInt_test(StructInt *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Base_test(Base *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Derived_test(Derived *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void MostDerived_test(MostDerived *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionInt_test(UnionInt *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionClass_test(UnionClass *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/record-var-to-enum.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/record-var-to-enum.cpp @@ -0,0 +1,110 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "enums.h" +#include "records.h" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } + +#define CHECKS \ + CHECK(x, std::OtherByte) \ + CHECK(x, ClassEnum) \ + CHECK(x, ClassEnumChar) \ + CHECK(x, ClassEnumUChar) \ + CHECK(x, ClassEnumInt) \ + CHECK(x, Enum) \ + CHECK(x, EnumChar) \ + CHECK(x, EnumUChar) \ + CHECK(x, EnumInt) \ + CHECK(x, decltype(E_Dummy)) + +void EmptyClass_test() { + EmptyClass x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void StructInt_test() { + StructInt x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Base_test() { + Base x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Derived_test() { + Derived x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass x{}; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void StructInt_test(StructInt x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Base_test(Base x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void Derived_test(Derived x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void MostDerived_test(MostDerived x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionInt_test(UnionInt x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void UnionClass_test(UnionClass x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/record-var-to-record.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/record-var-to-record.cpp @@ -0,0 +1,277 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "records.h" + +#define CHECK(var, type) \ + { auto y = *reinterpret_cast(&var); } + +void EmptyClass_test() { + EmptyClass x{}; + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void StructInt_test() { + StructInt x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Base_test() { + Base x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Derived_test() { + Derived x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass x{}; + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // no-warning +} + +void EmptyClass_test(EmptyClass x) { + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void StructInt_test(StructInt x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Base_test(Base x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void Derived_test(Derived x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void MostDerived_test(MostDerived x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} expected-warning {{reinterpret_cast}} expected-note {{static_cast}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionInt_test(UnionInt x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning {{Undefined behavior}} +} + +void UnionClass_test(UnionClass x) { + CHECK(x, EmptyClass) // expected-warning {{Undefined behavior}} + CHECK(x, StructInt) // expected-warning {{Undefined behavior}} + CHECK(x, Base) // expected-warning {{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning {{Undefined behavior}} + CHECK(x, Derived) // expected-warning {{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning {{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning {{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning {{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning {{Undefined behavior}} + CHECK(x, UnionClass) // no-warning +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/record-var-to-scalar.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/record-var-to-scalar.cpp @@ -0,0 +1,136 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "records.h" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } + +#define CHECKS_NO_WARN \ + CHECK(x, char) \ + CHECK(x, unsigned char) + +#define CHECKS \ + CHECK(x, bool) \ + CHECK(x, short) \ + CHECK(x, int) \ + CHECK(x, long) \ + CHECK(x, long long) \ + CHECK(x, signed char) \ + CHECK(x, unsigned short) \ + CHECK(x, unsigned int) \ + CHECK(x, unsigned long) \ + CHECK(x, unsigned long long) \ + CHECK(x, float) \ + CHECK(x, double) \ + CHECK(x, long double) + +void EmptyClass_test() { + EmptyClass x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void StructInt_test() { + StructInt x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Base_test() { + Base x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Derived_test() { + Derived x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void StructInt_test(StructInt x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Base_test(Base x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void Derived_test(Derived x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void MostDerived_test(MostDerived x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionInt_test(UnionInt x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} + +void UnionClass_test(UnionClass x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 13{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/records.h =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/records.h @@ -0,0 +1,33 @@ +#pragma once +class EmptyClass {}; +struct StructInt { + int x; +}; +class Base { +public: + int x; +}; +class AnotherBase { +public: + int y; +}; +class Derived : public AnotherBase, public Base { +public: + int z; +}; +class MostDerived : public Derived { +public: + int w; +}; +union EmptyUnion { +}; +union UnionUchar { + unsigned char x; + char y; +}; +union UnionInt { + int x; +}; +union UnionClass { + MostDerived md; +}; Index: clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-ptr-to-record.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-ptr-to-record.cpp @@ -0,0 +1,153 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "records.h" + +template +T *create(); + +#define CHECK(var, type) \ + { auto y = *(type *)var; } + +#define CHECKS \ + CHECK(x, EmptyClass) \ + CHECK(x, StructInt) \ + CHECK(x, Base) \ + CHECK(x, AnotherBase) \ + CHECK(x, Derived) \ + CHECK(x, MostDerived) \ + CHECK(x, EmptyUnion) \ + CHECK(x, UnionUchar) \ + CHECK(x, UnionInt) \ + CHECK(x, UnionClass) + +void bool_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void char_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void short_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void int_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_long_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void schar_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uchar_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ushort_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uint_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_long_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void float_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void double_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_double_test() { + auto *x = create(); + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void bool_test(bool *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void char_test(char *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void short_test(short *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void int_test(int *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_test(long *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_long_test(long long *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uchar_test(unsigned char *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ushort_test(unsigned short *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uint_test(unsigned int *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_test(unsigned long *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void float_test(float *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void double_test(double *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_double_test(long double *x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-var-to-record.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-var-to-record.cpp @@ -0,0 +1,150 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "records.h" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } + +#define CHECKS \ + CHECK(x, EmptyClass) \ + CHECK(x, StructInt) \ + CHECK(x, Base) \ + CHECK(x, AnotherBase) \ + CHECK(x, Derived) \ + CHECK(x, MostDerived) \ + CHECK(x, EmptyUnion) \ + CHECK(x, UnionUchar) \ + CHECK(x, UnionInt) \ + CHECK(x, UnionClass) + +void bool_test() { + bool x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void char_test() { + char x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void short_test() { + short x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void int_test() { + int x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_test() { + long x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_long_test() { + long long x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void schar_test() { + signed char x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uchar_test() { + unsigned char x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ushort_test() { + unsigned short x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uint_test() { + unsigned int x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_test() { + unsigned long x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_long_test() { + unsigned long long x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void float_test() { + float x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void double_test() { + double x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_double_test() { + long double x = 42; + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void bool_test(bool x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void char_test(char x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void short_test(short x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void int_test(int x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_test(long x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_long_test(long long x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uchar_test(unsigned char x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ushort_test(unsigned short x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void uint_test(unsigned int x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_test(unsigned long x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void float_test(float x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void double_test(double x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} + +void long_double_test(long double x) { + CHECKS // expected-warning 10{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/various-indirection.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/various-indirection.cpp @@ -0,0 +1,83 @@ +// RUN: %clang_cc1 -std=c++20 -analyze -analyzer-config eagerly-assume=false -analyzer-checker=debug.ExprInspection,alpha.core.StrictAliasing -verify %s +// NOTE: -relaxed-aliasing flag disables StrictAliasing checker. + +#include "enums.h" +#include "records.h" + +#define CAST(var, type) (reinterpret_cast(var)) +#define DEREF(var, type) *CAST(var, type) +#define SUBSCRIPT(var, type) \ + CAST(var, type) \ + [0] +#define DEREF_ADD(var, type) *(CAST(var, type) + 0) + +#define CHECK_DEREF(var, type) \ + { auto y = DEREF(var, type); } \ + { DEREF(var, type) = type{}; } + +#define CHECK_SUBSCRIPT(var, type) \ + { auto y = SUBSCRIPT(var, type); } \ + { SUBSCRIPT(var, type) = type{}; } + +#define CHECK_DEREF_ADD(var, type) \ + { auto y = DEREF_ADD(var, type); } \ + { DEREF_ADD(var, type) = type{}; } + +#define CHECK_MEMBER_ARROW(var, type) \ + { auto y = CAST(var, type)->x; } \ + { CAST(var, type)->x = decltype(CAST(var, type)->x){}; } + +#define CHECK_MEMBER_DOT(var, type) \ + { auto y = (*CAST(var, type)).x; } \ + { (*CAST(var, type)).x = decltype((*CAST(var, type)).x){}; } + +#define CHECK_SCALAR_NO_WARN(type) \ + { \ + type x{}; \ + CHECK_DEREF(&x, type) \ + CHECK_SUBSCRIPT(&x, type) \ + CHECK_DEREF_ADD(&x, type) \ + } + +#define CHECK_RECORD_NO_WARN(type) \ + { \ + CHECK_SCALAR_NO_WARN(type) \ + type x{}; \ + CHECK_MEMBER_ARROW(&x, type) \ + CHECK_MEMBER_DOT(&x, type) \ + } + +#define CHECK_NOOP_NO_WARN(type1, type2) \ + { \ + type1 x{}; \ + DEREF(&x, type2); \ + SUBSCRIPT(&x, type2); \ + DEREF_ADD(&x, type2); \ + } + +void test_store_load() { + CHECK_SCALAR_NO_WARN(bool) // no-warning + CHECK_SCALAR_NO_WARN(char) // no-warning + CHECK_SCALAR_NO_WARN(int) // no-warning + CHECK_SCALAR_NO_WARN(double) // no-warning + CHECK_SCALAR_NO_WARN(ClassEnum) // no-warning + CHECK_SCALAR_NO_WARN(Enum) // no-warning + + CHECK_RECORD_NO_WARN(StructInt) // no-warning + CHECK_RECORD_NO_WARN(Base) // no-warning + CHECK_RECORD_NO_WARN(MostDerived) // no-warning + CHECK_RECORD_NO_WARN(UnionUchar) // no-warning +} + +// TODO: All should not warn. +void test_noop() { + CHECK_NOOP_NO_WARN(bool, MostDerived) // expected-warning 3{{Undefined behavior}} + CHECK_NOOP_NO_WARN(char, int) // expected-warning 3{{Undefined behavior}} + CHECK_NOOP_NO_WARN(double, ClassEnum) // expected-warning 3{{Undefined behavior}} + CHECK_NOOP_NO_WARN(ClassEnum, StructInt) // expected-warning 3{{Undefined behavior}} + CHECK_NOOP_NO_WARN(Enum, UnionUchar) // expected-warning 3{{Undefined behavior}} + CHECK_NOOP_NO_WARN(StructInt, std::byte) // no-warning + CHECK_NOOP_NO_WARN(AnotherBase, float) // expected-warning 3{{Undefined behavior}} + CHECK_NOOP_NO_WARN(MostDerived, unsigned char) // no-warning + CHECK_NOOP_NO_WARN(UnionUchar, Base) // expected-warning 3{{Undefined behavior}} +}