Index: clang/include/clang/StaticAnalyzer/Checkers/Checkers.td =================================================================== --- clang/include/clang/StaticAnalyzer/Checkers/Checkers.td +++ clang/include/clang/StaticAnalyzer/Checkers/Checkers.td @@ -298,6 +298,12 @@ Dependencies<[PthreadLockBase]>, Documentation; +def StrictAliasingChecker : Checker<"StrictAliasing">, + HelpText<"Check conformity with Strict Alising Rule. Check an access to the " + "stored value through a glvalue whose type is not allowed by " + "the Standard. ([basic.lval])">, + Documentation; + } // end "alpha.core" //===----------------------------------------------------------------------===// Index: clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt =================================================================== --- clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt +++ clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt @@ -105,6 +105,7 @@ StdLibraryFunctionsChecker.cpp STLAlgorithmModeling.cpp StreamChecker.cpp + StrictAliasingChecker.cpp StringChecker.cpp Taint.cpp TaintTesterChecker.cpp Index: clang/lib/StaticAnalyzer/Checkers/StrictAliasingChecker.cpp =================================================================== --- /dev/null +++ clang/lib/StaticAnalyzer/Checkers/StrictAliasingChecker.cpp @@ -0,0 +1,214 @@ +//===- StrictAliasingChecker - ... ------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// StrictAliasingChecker implements checks on violation of the next paragraph of +// the Standard which is known as `Strict Aliasing Rule`. +// 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. +// +// NOTE: The checker operates only when strict-aliasing is enabled with +// corresponding compiler flag. +// +// NOTE: There are differences in strict aliasing rules between C, C++ +// Standards. Now we only impelement checks since C++20 Standard (there were +// changes applied in C++20 http://wg21.link/cwg2051). +// +// TODO: Support C++98/11/14/17. Shall we? +// TODO: Support C. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/Attr.h" +#include "clang/AST/ExprCXX.h" +#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" + +using namespace clang; +using namespace ento; + +namespace { + +class AccessInferrer { + QualType From; + QualType To; + ASTContext &Ctx; + +public: + // Check whether the given types submit to the Strict Aliasing Rule. + // + // NOTE: User must provide canonical and unqualified QualType's for the + // correct result. + static bool canAccess(QualType From, QualType To, ASTContext &Ctx) { + // NOTE: Despite the function name `isCanonical()`, it also check whether + // the type is unqualified. (See implementation) + assert(From.isCanonical() && "The type shall be an unqualified canonical."); + assert(To.isCanonical() && "The type shall be an unqualified canonical."); + AccessInferrer AI(From, To, Ctx); + return AI.canAccessImpl(); + } + +private: + AccessInferrer(QualType From, QualType To, ASTContext &Ctx) + : From(From), To(To), Ctx(Ctx) {} + bool canAccessImpl() { + return isSameDynamic() || isCharOrByte() || isOppositeSign(); + } + // - the dynamic type of the object + bool isSameDynamic() { return From == To; } + // - a char, unsigned char, or std::byte type. + bool isCharOrByte() { + return To == Ctx.CharTy || To == Ctx.UnsignedCharTy || To->isStdByteType(); + } + // - a type that is the signed or unsigned type corresponding to the dynamic + // type of the object + bool isOppositeSign() { + // `char` type is a separate type from `signed char` and `unsigned char`. + // Thus, it can has two corresponding types with signedness. Here we only + // check for `signed char` because we already checked for `unsigned char` + // and `char` in `isCharOrByte()`. + assert(!isCharOrByte() && + "The check for `isCharOrByte()` should be invoked first."); + if (From == Ctx.CharTy && To == Ctx.SignedCharTy) + return true; + + QualType OppositeSignTy; + + auto IsToUnsignedType = false; + auto IsToSignedType = false; + if (const auto *BT = dyn_cast(To)) { + BuiltinType::Kind Kind = BT->getKind(); + IsToUnsignedType = + ((Kind >= BuiltinType::UShort) && (Kind <= BuiltinType::UInt128)) || + (Kind == BuiltinType::UChar); + IsToSignedType = + ((Kind >= BuiltinType::Short) && (Kind <= BuiltinType::Int128)) || + (Kind == BuiltinType::SChar); + } + + if (IsToUnsignedType) + OppositeSignTy = Ctx.getCorrespondingSignedType(To); + else if (IsToSignedType) + OppositeSignTy = Ctx.getCorrespondingUnsignedType(To); + + return From == OppositeSignTy; + } +}; + +class StrictAliasingChecker : public Checker, + check::PreStmt, + check::PreStmt> { + BugType BT{this, "Strict Aliasing Rule", + "Access Violation through unallowed type."}; + +public: + void checkPreStmt(const UnaryOperator *UO, CheckerContext &C) const { + // Handle indirection operator '*' only. + if (UO->getOpcode() == UO_Deref) + checkAccess(UO->getSubExpr(), UO->getType(), C); + } + void checkPreStmt(const ArraySubscriptExpr *ASE, CheckerContext &C) const { + checkAccess(ASE->getBase(), ASE->getType(), C); + } + void checkPreStmt(const MemberExpr *ME, CheckerContext &C) const { + const Expr *B = ME->getBase()->IgnoreParenBaseCasts(); + checkAccess(B, B->getType()->getPointeeType(), C); + } + +private: + void checkAccess(const Expr *OrigEx, QualType AliasedTy, + CheckerContext &C) const { + AliasedTy = getCanonicalUnqualifiedType(AliasedTy); + // TODO: Handle this case in a proper way, if any. + if (AliasedTy.isNull()) + return; + + const QualType OrigTy = getOriginalType(OrigEx, C); + // TODO: Handle this case in a proper way, if any. + if (OrigTy.isNull()) + return; + + if (!AccessInferrer::canAccess(OrigTy, AliasedTy, C.getASTContext())) + reportBug(C, OrigTy, AliasedTy); + } + + // FIXME: Probably, we should have such function in QualType class. + // Existing `T->getCanonicalTypeUnqualified()` does not return unqualified + // type which, apparently, is expected. + QualType getCanonicalUnqualifiedType(QualType T) const { + if (!T.isNull()) { + T = T->getCanonicalTypeUnqualified(); + T.removeLocalFastQualifiers(); + } + return T; + } + + QualType getOriginalType(const Expr *OrigEx, CheckerContext &C) const { + QualType T; + + SVal V = C.getSVal(OrigEx->IgnoreParens()); + if (V.isUnknownOrUndef()) + return T; + + if (auto MRV = V.getAs()) { + // Unwrap ElementRegion and CXXBaseObjectRegion nesting to get the base + // region. + const MemRegion *Base = MRV->getRegion()->StripCasts(true); + // Get type of the original declaration. + if (const auto *VR = dyn_cast(Base)) + T = VR->getValueType(); + else if (const auto *SR = dyn_cast(Base)) + T = SR->getSymbol()->getType()->getPointeeType(); + // TODO: Support other regions in a proper way if would need. + } + + // Get type from other types. + // NOTE: This is very inaccurate but it's better than NULL though. + if (T.isNull()) + T = V.getType(C.getASTContext()); + + return getCanonicalUnqualifiedType(T); + } + + void reportBug(CheckerContext &C, QualType From, QualType To) const { + SmallString<256> Buf; + llvm::raw_svector_ostream OS(Buf); + OS << "Undefined behavior. Attempting to access the stored value of type "; + OS << "'" << From.getAsString() << "'"; + OS << " through unallowed type "; + OS << "'" << To.getAsString() << "'."; + + ExplodedNode *Node = C.generateNonFatalErrorNode(); + auto Report = std::make_unique(BT, OS.str(), Node); + C.emitReport(std::move(Report)); + } +}; +} // end anonymous namespace + +//===----------------------------------------------------------------------===// +// Registration. +//===----------------------------------------------------------------------===// + +void ento::registerStrictAliasingChecker(CheckerManager &CM) { + CM.registerChecker(); +} + +bool ento::shouldRegisterStrictAliasingChecker(const CheckerManager &CM) { + const LangOptions &LO = CM.getLangOpts(); + const bool IsStrictAliasing = !CM.getCodeGenOpts().RelaxedAliasing; + // Support from C++20 for now. + // TODO: Support C++98/11/14/17. Shall we? + // TODO: Support C. + return IsStrictAliasing && (LO.CPlusPlus20 || LO.CPlusPlus2b); +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/cv.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/cv.cpp @@ -0,0 +1,21 @@ +// 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. + +#define CHECK_CV(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = *(const type *)&var; } \ + { auto y = *(const volatile type *)&var; } + +void cv_test() { + int x = 42; + CHECK_CV(x, char) // no-warning + CHECK_CV(x, short) // expected-warning 3{{Undefined behavior}} + + const int cx = 42; + CHECK_CV(cx, char) // no-warning + CHECK_CV(cx, short) // expected-warning 3{{Undefined behavior}} + + const volatile int cvx = 42; + CHECK_CV(cvx, char) // no-warning + CHECK_CV(cvx, short) // expected-warning 3{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enam-ptr-to-scalar.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enam-ptr-to-scalar.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 "enums.h" + +template +T *create() { return new T; } + +#define CHECK(var, type) \ + { auto y = *(type *)var; } \ + { auto y = ((type *)var)[0]; } \ + { auto y = *(((type *)var) + 0); } \ + { auto y = *(type *)(&(var)[0]); } + +#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 std_byte_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void std_OtherByte_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnum_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumChar_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumInt_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Enum_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumChar_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumUChar_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumInt_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void E_Dummy_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void std_byte_test(std::byte *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Enum_test(Enum *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumChar_test(EnumChar *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumInt_test(EnumInt *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enam-var-to-scalar.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enam-var-to-scalar.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 "enums.h" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 std_byte_test() { + std::byte x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void std_OtherByte_test() { + std::OtherByte x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnum_test() { + ClassEnum x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumChar_test() { + ClassEnumChar x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + ClassEnumUChar x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumInt_test() { + ClassEnumInt x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Enum_test() { + Enum x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumChar_test() { + EnumChar x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumUChar_test() { + EnumUChar x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumInt_test() { + EnumInt x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void E_Dummy_test() { + decltype(E_Dummy) x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void std_byte_test(std::byte x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Enum_test(Enum x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumChar_test(EnumChar x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EnumInt_test(EnumInt x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enum-ptr-to-enum.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enum-ptr-to-enum.cpp @@ -0,0 +1,332 @@ +// 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" + +template +T *create() { return new T{}; } + +#define CHECK(var, type) \ + { auto y = *(type *)var; } \ + { auto y = ((type *)var)[0]; } \ + { auto y = *(((type *)var) + 0); } \ + { auto y = *(type *)(&(var)[0]); } + +void std_byte_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void std_OtherByte_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // no-warning + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnum_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // no-warning + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumChar_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // no-warning + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // no-warning + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumInt_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // no-warning + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void Enum_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // no-warning + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumChar_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // no-warning + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumUChar_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // no-warning + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumInt_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // no-warning + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void E_Dummy_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // no-warning +} + +void std_byte_test(std::byte *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // no-warning + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // no-warning + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // no-warning + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // no-warning + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // no-warning + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void Enum_test(Enum *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // no-warning + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumChar_test(EnumChar *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // no-warning + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // no-warning + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumInt_test(EnumInt *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // no-warning + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) *x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // no-warning +} 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,125 @@ +// 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; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 40{{Undefined behavior}} +} + +void std_OtherByte_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnum_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumChar_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumInt_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Enum_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumChar_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumUChar_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumInt_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void E_Dummy_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void std_byte_test(std::byte *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Enum_test(Enum *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumChar_test(EnumChar *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumInt_test(EnumInt *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enum-var-to-enum.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enum-var-to-enum.cpp @@ -0,0 +1,329 @@ +// 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" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +void std_byte_test() { + std::byte x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void std_OtherByte_test() { + std::OtherByte x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // no-warning + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnum_test() { + ClassEnum x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // no-warning + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumChar_test() { + ClassEnumChar x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // no-warning + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + ClassEnumUChar x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // no-warning + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumInt_test() { + ClassEnumInt x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // no-warning + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void Enum_test() { + Enum x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // no-warning + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumChar_test() { + EnumChar x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // no-warning + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumUChar_test() { + EnumUChar x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // no-warning + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumInt_test() { + EnumInt x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // no-warning + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void E_Dummy_test() { + decltype(E_Dummy) x{}; + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // no-warning +} + +void std_byte_test(std::byte x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // no-warning + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // no-warning + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // no-warning + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // no-warning + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // no-warning + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void Enum_test(Enum x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // no-warning + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumChar_test(EnumChar x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // no-warning + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // no-warning + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void EnumInt_test(EnumInt x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // no-warning + CHECK(x, decltype(E_Dummy)) // expected-warning 4{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) x) { + CHECK(x, std::byte) // no-warning + CHECK(x, std::OtherByte) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnum) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, ClassEnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Enum) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumUChar) // expected-warning 4{{Undefined behavior}} + CHECK(x, EnumInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, decltype(E_Dummy)) // no-warning +} 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,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" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 40{{Undefined behavior}} +} + +void std_OtherByte_test() { + std::OtherByte x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnum_test() { + ClassEnum x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumChar_test() { + ClassEnumChar x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumUChar_test() { + ClassEnumUChar x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumInt_test() { + ClassEnumInt x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Enum_test() { + Enum x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumChar_test() { + EnumChar x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumUChar_test() { + EnumUChar x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumInt_test() { + EnumInt x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void E_Dummy_test() { + decltype(E_Dummy) x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void std_byte_test(std::byte x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void std_OtherByte_test(std::OtherByte x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnum_test(ClassEnum x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumChar_test(ClassEnumChar x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumUChar_test(ClassEnumUChar x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ClassEnumInt_test(ClassEnumInt x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Enum_test(Enum x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumChar_test(EnumChar x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumUChar_test(EnumUChar x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EnumInt_test(EnumInt x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void E_Dummy_test(decltype(E_Dummy) x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/enums.h =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/enums.h @@ -0,0 +1,14 @@ +#pragma once +namespace std { +enum class byte : unsigned char {}; +enum class OtherByte : unsigned char {}; +}; // namespace std +enum class ClassEnum {}; +enum class ClassEnumChar : char {}; +enum class ClassEnumUChar : unsigned char {}; +enum class ClassEnumInt : int {}; +enum Enum {}; +enum EnumChar : char {}; +enum EnumUChar : unsigned char {}; +enum EnumInt : int {}; +enum { E_Dummy }; 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,116 @@ +// 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; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 40{{Undefined behavior}} +} + +void StructInt_test() { + StructInt *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Base_test() { + Base *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Derived_test() { + Derived *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void StructInt_test(StructInt *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Base_test(Base *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Derived_test(Derived *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void MostDerived_test(MostDerived *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionInt_test(UnionInt *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionClass_test(UnionClass *x) { + CHECKS // expected-warning 40{{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,283 @@ +// 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); } \ + { auto y = reinterpret_cast(var)[0]; } \ + { auto y = *((reinterpret_cast(var)) + 0); } \ + { auto y = *reinterpret_cast(&(var)[0]); } + +void EmptyClass_test() { + auto *x = create(); + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void StructInt_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Base_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void AnotherBase_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Derived_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void MostDerived_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void EmptyUnion_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionUchar_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionInt_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionClass_test() { + auto *x = create(); + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // no-warning +} + +void EmptyClass_test(EmptyClass *x) { + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void StructInt_test(StructInt *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Base_test(Base *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Derived_test(Derived *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void MostDerived_test(MostDerived *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionInt_test(UnionInt *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionClass_test(UnionClass *x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{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,142 @@ +// 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; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 52{{Undefined behavior}} +} + +void StructInt_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Base_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void AnotherBase_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Derived_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void MostDerived_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EmptyUnion_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionUchar_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionInt_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionClass_test() { + auto *x = create(); + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void StructInt_test(StructInt *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Base_test(Base *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Derived_test(Derived *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void MostDerived_test(MostDerived *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionInt_test(UnionInt *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionClass_test(UnionClass *x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{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,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" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 40{{Undefined behavior}} +} + +void StructInt_test() { + StructInt x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Base_test() { + Base x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Derived_test() { + Derived x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass x{}; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void StructInt_test(StructInt x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Base_test(Base x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void Derived_test(Derived x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void MostDerived_test(MostDerived x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionInt_test(UnionInt x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void UnionClass_test(UnionClass x) { + CHECKS // expected-warning 40{{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,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" + +#define CHECK(var, type) \ + { auto y = *reinterpret_cast(&var); } \ + { auto y = reinterpret_cast(&var)[0]; } \ + { auto y = *((reinterpret_cast(&var)) + 0); } \ + { auto y = *reinterpret_cast(&(&var)[0]); } + +void EmptyClass_test() { + EmptyClass x{}; + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void StructInt_test() { + StructInt x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Base_test() { + Base x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Derived_test() { + Derived x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass x{}; + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // no-warning +} + +void EmptyClass_test(EmptyClass x) { + CHECK(x, EmptyClass) // no-warning + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void StructInt_test(StructInt x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // no-warning + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Base_test(Base x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // no-warning + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // no-warning + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void Derived_test(Derived x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // no-warning + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void MostDerived_test(MostDerived x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} expected-warning 4{{reinterpret_cast}} expected-note 4{{static_cast}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // no-warning + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // no-warning + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // no-warning + CHECK(x, UnionInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionInt_test(UnionInt x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // no-warning + CHECK(x, UnionClass) // expected-warning 4{{Undefined behavior}} +} + +void UnionClass_test(UnionClass x) { + CHECK(x, EmptyClass) // expected-warning 4{{Undefined behavior}} + CHECK(x, StructInt) // expected-warning 4{{Undefined behavior}} + CHECK(x, Base) // expected-warning 4{{Undefined behavior}} + CHECK(x, AnotherBase) // expected-warning 4{{Undefined behavior}} + CHECK(x, Derived) // expected-warning 4{{Undefined behavior}} + CHECK(x, MostDerived) // expected-warning 4{{Undefined behavior}} + CHECK(x, EmptyUnion) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionUchar) // expected-warning 4{{Undefined behavior}} + CHECK(x, UnionInt) // expected-warning 4{{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,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" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 52{{Undefined behavior}} +} + +void StructInt_test() { + StructInt x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Base_test() { + Base x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void AnotherBase_test() { + AnotherBase x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Derived_test() { + Derived x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void MostDerived_test() { + MostDerived x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EmptyUnion_test() { + EmptyUnion x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionUchar_test() { + UnionUchar x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionInt_test() { + UnionInt x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionClass_test() { + UnionClass x{}; + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EmptyClass_test(EmptyClass x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void StructInt_test(StructInt x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Base_test(Base x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void AnotherBase_test(AnotherBase x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void Derived_test(Derived x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void MostDerived_test(MostDerived x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void EmptyUnion_test(EmptyUnion x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionUchar_test(UnionUchar x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionInt_test(UnionInt x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{Undefined behavior}} +} + +void UnionClass_test(UnionClass x) { + CHECKS_NO_WARN // no-warning + CHECKS // expected-warning 52{{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-enum.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-ptr-to-enum.cpp @@ -0,0 +1,185 @@ +// 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" + +template +T *create(); + +#define CHECK(var, type) \ + { auto y = *(type *)var; } \ + { auto y = ((type *)var)[0]; } \ + { auto y = *(((type *)var) + 0); } \ + { auto y = *(type *)(&(var)[0]); } + +#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 bool_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void char_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void schar_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test() { + auto *x = create(); + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void bool_test(bool *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void char_test(char *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test(short *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test(int *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test(long *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test(long long *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test(unsigned char *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test(unsigned short *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test(unsigned int *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test(unsigned long *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test(float *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test(double *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test(long double *x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} 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,156 @@ +// 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; } \ + { auto y = ((type *)var)[0]; } \ + { auto y = *(((type *)var) + 0); } \ + { auto y = *(type *)(&(var)[0]); } + +#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 40{{Undefined behavior}} +} + +void char_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void schar_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test() { + auto *x = create(); + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void bool_test(bool *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void char_test(char *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test(short *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test(int *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test(long *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test(long long *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test(unsigned char *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test(unsigned short *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test(unsigned int *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test(unsigned long *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test(float *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test(double *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test(long double *x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-ptr-to-scalar.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-ptr-to-scalar.cpp @@ -0,0 +1,564 @@ +// 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. + +template +T *create(); + +#define CHECK(var, type) \ + { auto y = *(type *)var; } \ + { auto y = ((type *)var)[0]; } \ + { auto y = *(((type *)var) + 0); } \ + { auto y = *(type *)(&(var)[0]); } + +void bool_test() { + auto *x = create(); + CHECK(x, bool) // no-warning + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void char_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void short_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void int_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_long_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void schar_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uchar_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ushort_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uint_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_long_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void char_test(char *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void short_test(short *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} +} + +void int_test(int *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_test(long *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_long_test(long long *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void schar_test(signed char *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uchar_test(unsigned char *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} +} + +void ushort_test(unsigned short *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uint_test(unsigned int *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_test(unsigned long *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long *x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void float_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // no-warning + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void double_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // no-warning + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_double_test() { + auto *x = create(); + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // no-warning +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-var-to-enum.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-var-to-enum.cpp @@ -0,0 +1,182 @@ +// 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" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 bool_test() { + bool x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void char_test() { + char x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test() { + short x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test() { + int x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test() { + long x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test() { + long long x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void schar_test() { + signed char x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test() { + unsigned char x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test() { + unsigned short x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test() { + unsigned int x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test() { + unsigned long x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test() { + unsigned long long x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test() { + float x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test() { + double x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test() { + long double x = 42; + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void bool_test(bool x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void char_test(char x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test(short x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test(int x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test(long x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test(long long x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test(unsigned char x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test(unsigned short x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test(unsigned int x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test(unsigned long x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test(float x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test(double x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test(long double x) { + CHECK(x, std::byte) // no-warning + CHECKS // expected-warning 40{{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,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" + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +#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 40{{Undefined behavior}} +} + +void char_test() { + char x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test() { + short x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test() { + int x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test() { + long x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test() { + long long x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void schar_test() { + signed char x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test() { + unsigned char x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test() { + unsigned short x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test() { + unsigned int x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test() { + unsigned long x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test() { + unsigned long long x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test() { + float x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test() { + double x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test() { + long double x = 42; + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void bool_test(bool x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void char_test(char x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void short_test(short x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void int_test(int x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_test(long x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_long_test(long long x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uchar_test(unsigned char x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ushort_test(unsigned short x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void uint_test(unsigned int x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_test(unsigned long x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void float_test(float x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void double_test(double x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} + +void long_double_test(long double x) { + CHECKS // expected-warning 40{{Undefined behavior}} +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-var-to-scalar.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/scalar-var-to-scalar.cpp @@ -0,0 +1,653 @@ +// 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. + +#define CHECK(var, type) \ + { auto y = *(type *)&var; } \ + { auto y = ((type *)&var)[0]; } \ + { auto y = *(((type *)&var) + 0); } \ + { auto y = *(type *)(&(&var)[0]); } + +void bool_test() { + bool x = true; + CHECK(x, bool) // no-warning + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void char_test() { + char x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void short_test() { + short x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void int_test() { + int x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_test() { + long x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_long_test() { + long long x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void schar_test() { + signed char x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uchar_test() { + unsigned char x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ushort_test() { + unsigned short x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uint_test() { + unsigned int x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_test() { + unsigned long x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_long_test() { + unsigned long long x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void float_test() { + float x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // no-warning + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void double_test() { + double x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // no-warning + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_double_test() { + long double x = 42; + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // no-warning +} + +void bool_test(bool x) { + CHECK(x, bool) // no-warning + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void char_test(char x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void short_test(short x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void int_test(int x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_test(long x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_long_test(long long x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void schar_test(signed char x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uchar_test(unsigned char x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // no-warning + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ushort_test(unsigned short x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // no-warning + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // no-warning + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void uint_test(unsigned int x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // no-warning + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // no-warning + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_test(unsigned long x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // no-warning + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // no-warning + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void ulong_long_test(unsigned long long x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // no-warning + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // no-warning + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void float_test(float x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // no-warning + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void double_test(double x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // no-warning + CHECK(x, long double) // expected-warning 4{{Undefined behavior}} +} + +void long_double_test(long double x) { + CHECK(x, bool) // expected-warning 4{{Undefined behavior}} + + CHECK(x, char) // no-warning + CHECK(x, short) // expected-warning 4{{Undefined behavior}} + CHECK(x, int) // expected-warning 4{{Undefined behavior}} + CHECK(x, long) // expected-warning 4{{Undefined behavior}} + CHECK(x, long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, signed char) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned char) // no-warning + CHECK(x, unsigned short) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned int) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long) // expected-warning 4{{Undefined behavior}} + CHECK(x, unsigned long long) // expected-warning 4{{Undefined behavior}} + + CHECK(x, float) // expected-warning 4{{Undefined behavior}} + CHECK(x, double) // expected-warning 4{{Undefined behavior}} + CHECK(x, long double) // no-warning +} Index: clang/test/Analysis/Checkers/StrictAliasingChecker/typedef.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/StrictAliasingChecker/typedef.cpp @@ -0,0 +1,18 @@ +// 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. + +typedef int MyInt; +typedef MyInt MyAnotherInt; +typedef char MyChar; +typedef short MyShort; +typedef MyChar *MyCharPtr; +typedef MyShort *MyShortPtr; +typedef MyInt *MyIntPtr; + +MyAnotherInt x = 42; + +void typedef_test() { + MyChar y1 = *(MyCharPtr)&x; // no-warning + MyShort y2 = *(MyShortPtr)&x; // expected-warning{{Undefined behavior}} + MyInt y3 = *(MyIntPtr)&x; // no-warning +}