clang: lib/Sema/SemaCast.cpp File Reference (original) (raw)

Go to the source code of this file.

Functions
static bool isValidCast (TryCastResult TCR)
static void DiagnoseCastQual (Sema &Self, const ExprResult &SrcExpr, QualType DestType)
DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either const, volatile or both.
static TryCastResult TryLValueToRValueCast (Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, CastKind &Kind, CXXCastPath &BasePath, unsigned &msg)
Tests whether a conversion according to N2844 is valid.
static TryCastResult TryStaticReferenceDowncast (Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, SourceRange OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath)
Tests whether a conversion according to C++ 5.2.9p5 is valid.
static TryCastResult TryStaticPointerDowncast (Sema &Self, QualType SrcType, QualType DestType, bool CStyle, SourceRange OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath)
Tests whether a conversion according to C++ 5.2.9p8 is valid.
static TryCastResult TryStaticDowncast (Sema &Self, CanQualType SrcType, CanQualType DestType, bool CStyle, SourceRange OpRange, QualType OrigSrcType, QualType OrigDestType, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath)
TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and TryStaticPointerDowncast.
static TryCastResult TryStaticMemberPointerUpcast (Sema &Self, ExprResult &SrcExpr, QualType SrcType, QualType DestType, bool CStyle, SourceRange OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath)
TryStaticMemberPointerUpcast - Tests whether a conversion according to C++ 5.2.9p9 is valid:
static TryCastResult TryStaticImplicitCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, CheckedConversionKind CCK, SourceRange OpRange, unsigned &msg, CastKind &Kind, bool ListInitialization)
TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 is valid:
static TryCastResult TryStaticCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, CheckedConversionKind CCK, SourceRange OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath, bool ListInitialization)
TryStaticCast - Check if a static cast can be performed, and do so if possible.
static TryCastResult TryConstCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, bool CStyle, unsigned &msg)
TryConstCast - See if a const_cast from source to destination is allowed, and perform it if it is.
static TryCastResult TryReinterpretCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, bool CStyle, SourceRange OpRange, unsigned &msg, CastKind &Kind)
static TryCastResult TryAddressSpaceCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, bool CStyle, unsigned &msg, CastKind &Kind)
static bool tryDiagnoseOverloadedCast (Sema &S, CastType CT, SourceRange range, Expr *src, QualType destType, bool listInitialization)
Try to diagnose a failed overloaded cast.
static void diagnoseBadCast (Sema &S, unsigned msg, CastType castType, SourceRange opRange, Expr *src, QualType destType, bool listInitialization)
Diagnose a failed cast.
static CastAwayConstnessKind unwrapCastAwayConstnessLevel (ASTContext &Context, QualType &T1, QualType &T2)
Unwrap one level of types for CastsAwayConstness.
static CastAwayConstnessKind CastsAwayConstness (Sema &Self, QualType SrcType, QualType DestType, bool CheckCVR, bool CheckObjCLifetime, QualType *TheOffendingSrcType=nullptr, QualType *TheOffendingDestType=nullptr, Qualifiers *CastAwayQualifiers=nullptr)
Check if the pointer conversion from SrcType to DestType casts away constness as defined in C++ [expr.const.cast].
static TryCastResult getCastAwayConstnessCastKind (CastAwayConstnessKind CACK, unsigned &DiagID)
static void DiagnoseReinterpretUpDownCast (Sema &Self, const Expr *SrcExpr, QualType DestType, SourceRange OpRange)
Check that a reinterpret_cast(SrcExpr) is not used as upcast or downcast between respective pointers or references.
static bool argTypeIsABIEquivalent (QualType SrcType, QualType DestType, ASTContext &Context)
static unsigned int checkCastFunctionType (Sema &Self, const ExprResult &SrcExpr, QualType DestType)
static bool IsAddressSpaceConversion (QualType SrcType, QualType DestType)
static void DiagnoseCastOfObjCSEL (Sema &Self, const ExprResult &SrcExpr, QualType DestType)
static void DiagnoseCallingConvCast (Sema &Self, const ExprResult &SrcExpr, QualType DstType, SourceRange OpRange)
Diagnose casts that change the calling convention of a pointer to a function defined in the current TU.
static void checkIntToPointerCast (bool CStyle, const SourceRange &OpRange, const Expr *SrcExpr, QualType DestType, Sema &Self)
static bool fixOverloadedReinterpretCastExpr (Sema &Self, QualType DestType, ExprResult &Result)
static void DiagnoseBadFunctionCast (Sema &Self, const ExprResult &SrcExpr, QualType DestType)
DiagnoseBadFunctionCast - Warn whenever a function call is cast to a non-matching type.

CastType

Enumerator
CT_Const const_cast
CT_Static static_cast
CT_Reinterpret reinterpret_cast
CT_Dynamic dynamic_cast
CT_CStyle (Type)expr
CT_Functional Type(expr)
CT_Addrspace addrspace_cast

Definition at line 48 of file SemaCast.cpp.

TryCastResult

Enumerator
TC_NotApplicable The cast method is not applicable.
TC_Success The cast method is appropriate and successful.
TC_Extension The cast method is appropriate and accepted as a language extension.
TC_Failed The cast method is appropriate, but failed. A diagnostic has been emitted.

Definition at line 35 of file SemaCast.cpp.

argTypeIsABIEquivalent()

CastsAwayConstness()

static CastAwayConstnessKind CastsAwayConstness ( Sema & Self, QualType SrcType, QualType DestType, bool CheckCVR, bool CheckObjCLifetime, QualType * TheOffendingSrcType = nullptr, QualType * TheOffendingDestType = nullptr, Qualifiers * CastAwayQualifiers = nullptr ) static

Check if the pointer conversion from SrcType to DestType casts away constness as defined in C++ [expr.const.cast].

This is used by the cast checkers. Both arguments must denote pointer (possibly to member) types.

Parameters

CheckCVR Whether to check for const/volatile/restrict qualifiers.
CheckObjCLifetime Whether to check Objective-C lifetime qualifiers.

Definition at line 674 of file SemaCast.cpp.

References clang::Qualifiers::compatiblyIncludesObjCLifetime(), clang::Qualifiers::fromCVRMask(), clang::Qualifiers::getCVRQualifiers(), clang::Qualifiers::hasConst(), clang::Type::isAnyPointerType(), clang::Type::isBlockPointerType(), clang::Type::isMemberPointerType(), clang::Type::isObjCObjectType(), clang::Type::isReferenceType(), clang::Qualifiers::removeConst(), clang::Self, and unwrapCastAwayConstnessLevel().

Referenced by DiagnoseCastQual(), and TryReinterpretCast().

checkCastFunctionType()

Definition at line 1108 of file SemaCast.cpp.

References argTypeIsABIEquivalent(), clang::Type::castAs(), clang::ActionResult< PtrTy, Compress >::get(), clang::Type::getAs(), clang::Expr::getExprLoc(), clang::QualType::getNonReferenceType(), clang::Type::getPointeeType(), clang::FunctionType::getReturnType(), clang::Expr::getType(), clang::Type::isBlockPointerType(), clang::Type::isFunctionNoProtoType(), clang::Type::isFunctionPointerType(), clang::Type::isFunctionReferenceType(), clang::Type::isFunctionType(), clang::Type::isMemberFunctionPointerType(), clang::Type::isVoidType(), clang::Self, and clang::T.

checkIntToPointerCast()

diagnoseBadCast()

DiagnoseBadFunctionCast()

DiagnoseBadFunctionCast - Warn whenever a function call is cast to a non-matching type.

Such as enum function call to int, int call to pointer; etc. Cast to 'void' is an exception.

Definition at line 2893 of file SemaCast.cpp.

References clang::ActionResult< PtrTy, Compress >::get(), clang::Expr::getExprLoc(), clang::Stmt::getSourceRange(), clang::Expr::getType(), clang::QualType::getUnqualifiedType(), clang::Type::isAnyPointerType(), clang::Type::isBlockPointerType(), clang::Type::isBooleanType(), clang::Type::isComplexIntegerType(), clang::Type::isComplexType(), clang::Type::isEnumeralType(), clang::Type::isFixedPointType(), clang::Type::isIntegerType(), clang::Type::isRealFloatingType(), clang::Type::isVoidType(), and clang::Self.

DiagnoseCallingConvCast()

Diagnose casts that change the calling convention of a pointer to a function defined in the current TU.

Definition at line 2119 of file SemaCast.cpp.

References clang::Type::castAs(), clang::FixItHint::CreateInsertion(), clang::ActionResult< PtrTy, Compress >::get(), clang::SourceRange::getBegin(), clang::FunctionType::getCallConv(), clang::Preprocessor::getIdentifierInfo(), clang::Preprocessor::getLastMacroWithSpelling(), clang::FunctionType::getNameForCallConv(), getPointeeType(), clang::IdentifierInfo::getTokenID(), clang::Expr::getType(), clang::Expr::IgnoreParenImpCasts(), clang::Type::isFunctionPointerType(), clang::IdentifierInfo::isKeyword(), and clang::Self.

Referenced by TryReinterpretCast().

DiagnoseCastOfObjCSEL()

DiagnoseCastQual()

DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either const, volatile or both.

Definition at line 3315 of file SemaCast.cpp.

References CastsAwayConstness(), clang::ActionResult< PtrTy, Compress >::get(), clang::Stmt::getBeginLoc(), clang::Expr::getType(), clang::Qualifiers::hasConst(), clang::Qualifiers::hasVolatile(), clang::Type::isAnyPointerType(), clang::ActionResult< PtrTy, Compress >::isInvalid(), clang::Type::isLValueReferenceType(), and clang::Self.

Referenced by clang::Sema::BuildCStyleCastExpr(), and clang::Sema::BuildCXXFunctionalCastExpr().

DiagnoseReinterpretUpDownCast()

Check that a reinterpret_cast(SrcExpr) is not used as upcast or downcast between respective pointers or references.

Definition at line 1002 of file SemaCast.cpp.

References clang::CXXBasePaths::begin(), clang::Class, clang::FixItHint::CreateReplacement(), E, clang::CXXBasePaths::end(), clang::Type::getAsCXXRecordDecl(), clang::ASTRecordLayout::getBaseClassOffset(), clang::SourceRange::getBegin(), clang::Type::getPointeeCXXRecordDecl(), clang::Expr::getType(), int, clang::TagDecl::isCompleteDefinition(), clang::CXXRecordDecl::isDerivedFrom(), clang::Decl::isInvalidDecl(), Path, clang::Self, clang::VirtualBase, and clang::CharUnits::Zero().

fixOverloadedReinterpretCastExpr()

getCastAwayConstnessCastKind()

static TryCastResult getCastAwayConstnessCastKind ( CastAwayConstnessKind CACK, unsigned & DiagID ) static

IsAddressSpaceConversion()

isValidCast()

TryAddressSpaceCast()

TryConstCast()

TryConstCast - See if a const_cast from source to destination is allowed, and perform it if it is.

Definition at line 1947 of file SemaCast.cpp.

References clang::ActionResult< PtrTy, Compress >::get(), clang::Type::getAs(), clang::Expr::getType(), clang::Type::isFunctionPointerType(), clang::Expr::isLValue(), clang::Type::isMemberFunctionPointerType(), clang::Type::isMemberPointerType(), clang::Type::isObjCObjectPointerType(), clang::Type::isPointerType(), clang::Expr::isPRValue(), clang::Type::isRecordType(), clang::Expr::refersToBitField(), clang::Self, TC_NotApplicable, and TC_Success.

tryDiagnoseOverloadedCast()

Try to diagnose a failed overloaded cast.

Returns true if diagnostics were emitted.

Definition at line 416 of file SemaCast.cpp.

References clang::OverloadCandidateSet::BestViableFunction(), clang::InitializationKind::CreateCast(), clang::InitializationKind::CreateCStyleCast(), clang::InitializationKind::CreateFunctionalCast(), CT_Addrspace, CT_Const, CT_CStyle, CT_Dynamic, CT_Functional, CT_Reinterpret, CT_Static, clang::OverloadCandidateSet::empty(), clang::InitializationSequence::Failed(), clang::InitializationSequence::FK_ConstructorOverloadFailed, clang::InitializationSequence::FK_ParenthesizedListInitFailed, clang::InitializationSequence::FK_UserConversionOverloadFailed, clang::InitializationSequence::getFailedCandidateSet(), clang::InitializationSequence::getFailedOverloadResult(), clang::InitializationSequence::getFailureKind(), clang::Stmt::getSourceRange(), clang::StringLiteral::getString(), clang::Expr::getType(), clang::InitializedEntity::InitializeTemporary(), clang::Type::isArrayType(), clang::Type::isRecordType(), clang::OverloadCandidateSet::NoteCandidates(), clang::OCD_AllCandidates, clang::OCD_AmbiguousCandidates, clang::OCD_ViableCandidates, clang::OR_Ambiguous, clang::OR_Deleted, clang::OR_No_Viable_Function, clang::OR_Success, and clang::SemaBase::PDiag().

Referenced by diagnoseBadCast().

TryLValueToRValueCast()

Tests whether a conversion according to N2844 is valid.

Definition at line 1533 of file SemaCast.cpp.

References clang::Type::getAs(), clang::Stmt::getBeginLoc(), clang::ReferenceType::getPointeeType(), clang::Expr::getType(), clang::QualType::getUnqualifiedType(), clang::Expr::isGLValue(), clang::Expr::isLValue(), clang::Sema::Ref_Compatible, clang::Sema::Ref_Incompatible, clang::Self, TC_Failed, TC_NotApplicable, and TC_Success.

Referenced by TryStaticCast().

TryReinterpretCast()

Definition at line 2261 of file SemaCast.cpp.

References CastsAwayConstness(), checkIntToPointerCast(), Diag(), DiagnoseCallingConvCast(), DiagnoseCastOfObjCSEL(), clang::ExprError(), fixOverloadedReinterpretCastExpr(), clang::ActionResult< PtrTy, Compress >::get(), clang::QualType::getAddressSpace(), clang::Type::getAs(), clang::SourceRange::getBegin(), getCastAwayConstnessCastKind(), clang::Expr::getObjectKind(), clang::Type::getPointeeType(), clang::QualType::getQualifiers(), clang::Stmt::getSourceRange(), clang::Expr::getType(), IsAddressSpaceConversion(), clang::Qualifiers::isAddressSpaceSupersetOf(), clang::Type::isAnyPointerType(), clang::Type::isBlockPointerType(), clang::Type::isBooleanType(), clang::Type::isExtVectorType(), clang::Type::isFunctionPointerType(), clang::Expr::isGLValue(), clang::Type::isIntegralOrEnumerationType(), clang::Type::isIntegralType(), clang::MemberPointerType::isMemberFunctionPointer(), clang::Type::isMemberPointerType(), clang::QualType::isNull(), clang::Type::isNullPtrType(), clang::Type::isObjCObjectPointerType(), clang::Type::isPointerType(), clang::ActionResult< PtrTy, Compress >::isUsable(), clang::Type::isVectorType(), clang::Type::isVoidPointerType(), clang::OK_BitField, clang::OK_MatrixComponent, clang::OK_ObjCProperty, clang::OK_ObjCSubscript, clang::OK_Ordinary, clang::OK_VectorComponent, clang::Result, clang::Self, TC_Failed, TC_NotApplicable, and TC_Success.

TryStaticCast()

TryStaticCast - Check if a static cast can be performed, and do so if possible.

If CStyle, ignore access restrictions on hierarchy casting and casting away constness.

Definition at line 1321 of file SemaCast.cpp.

References clang::Type::castAs(), clang::Qualifiers::compatiblyIncludes(), clang::CStyleCast, clang::Enum, clang::ExprError(), clang::FunctionalCast, clang::ActionResult< PtrTy, Compress >::get(), clang::Type::getAs(), clang::SourceRange::getBegin(), clang::Type::getPointeeType(), clang::PointerType::getPointeeType(), clang::QualType::getQualifiers(), clang::Expr::getType(), IsAddressSpaceConversion(), clang::Type::isBlockPointerType(), clang::Type::isBooleanType(), clang::Type::isEnumeralType(), clang::Type::isFunctionType(), clang::Type::isIncompleteOrObjectType(), clang::Type::isIntegralOrEnumerationType(), clang::Type::isIntegralType(), clang::ActionResult< PtrTy, Compress >::isInvalid(), clang::Type::isMatrixType(), clang::Type::isObjCObjectPointerType(), clang::Type::isRealFloatingType(), clang::Type::isVoidType(), clang::Qualifiers::removeObjCGCAttr(), clang::Qualifiers::removeObjCLifetime(), clang::Self, TC_Failed, TC_NotApplicable, TC_Success, TryLValueToRValueCast(), TryStaticImplicitCast(), TryStaticMemberPointerUpcast(), TryStaticPointerDowncast(), and TryStaticReferenceDowncast().

TryStaticDowncast()

TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and TryStaticPointerDowncast.

Tests whether a static downcast from SrcType to DestType is possible and allowed.

Definition at line 1660 of file SemaCast.cpp.

References clang::Sema::AR_accessible, clang::Sema::AR_delayed, clang::Sema::AR_dependent, clang::Sema::AR_inaccessible, clang::CanQual< T >::getAs(), clang::QualType::getAsString(), clang::SourceRange::getBegin(), clang::CanQual< T >::getUnqualifiedType(), clang::QualType::getUnqualifiedType(), clang::CanQual< T >::isAtLeastAsQualifiedAs(), Path, clang::Self, TC_Failed, TC_NotApplicable, TC_Success, and clang::VirtualBase.

Referenced by TryStaticPointerDowncast(), and TryStaticReferenceDowncast().

TryStaticImplicitCast()

TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 is valid:

An expression e can be explicitly converted to a type T using a static_cast if the declaration "T t(e);" is well-formed [...].

Definition at line 1890 of file SemaCast.cpp.

References clang::InitializationKind::CreateCast(), clang::InitializationKind::CreateCStyleCast(), clang::InitializationKind::CreateFunctionalCast(), clang::CStyleCast, clang::InitializationSequence::Failed(), clang::FunctionalCast, clang::ActionResult< PtrTy, Compress >::get(), clang::SourceRange::getBegin(), clang::InitializedEntity::InitializeTemporary(), clang::InitializationSequence::isConstructorInitialization(), clang::Type::isRecordType(), clang::Type::isReferenceType(), clang::InitializationSequence::Perform(), clang::Result, clang::Self, TC_Failed, TC_NotApplicable, and TC_Success.

Referenced by TryStaticCast().

TryStaticMemberPointerUpcast()

TryStaticMemberPointerUpcast - Tests whether a conversion according to C++ 5.2.9p9 is valid:

An rvalue of type "pointer to member of D of type cv1 T" can be converted to an rvalue of type "pointer to member of B of type cv2 T", where B is a base class of D [...].

Definition at line 1773 of file SemaCast.cpp.

References clang::Sema::AR_accessible, clang::Sema::AR_delayed, clang::Sema::AR_dependent, clang::Sema::AR_inaccessible, clang::ActionResult< PtrTy, Compress >::get(), clang::Type::getAs(), clang::SourceRange::getBegin(), clang::MemberPointerType::getClass(), clang::CXXMethodDecl::getParent(), clang::MemberPointerType::getPointeeType(), clang::Expr::getType(), clang::ActionResult< PtrTy, Compress >::isUsable(), clang::Self, TC_Failed, TC_NotApplicable, and TC_Success.

Referenced by TryStaticCast().

TryStaticPointerDowncast()

TryStaticReferenceDowncast()

unwrapCastAwayConstnessLevel()

Unwrap one level of types for CastsAwayConstness.

Like Sema::UnwrapSimilarTypes, this removes one level of indirection from both types, provided that they're both pointer-like or array-like. Unlike the Sema function, doesn't care if the unwrapped pieces are related.

This function may remove additional levels as necessary for correctness: the resulting T1 is unwrapped sufficiently that it is never an array type, so that its qualifiers can be directly compared to those of T2 (which will have the combined set of qualifiers from all indermediate levels of T2), as (effectively) required by [expr.const.cast]p7 replacing T1's qualifiers with those from T2.

Definition at line 596 of file SemaCast.cpp.

References clang::ASTContext::getAsArrayType(), clang::QualType::getCVRQualifiers(), clang::Type::getPointeeType(), clang::Type::isAnyPointerType(), clang::Type::isBlockPointerType(), clang::Type::isConstantArrayType(), clang::Type::isIncompleteArrayType(), clang::Type::isMemberPointerType(), clang::Type::isReferenceType(), clang::None, clang::T, clang::ASTContext::UnwrapSimilarArrayTypes(), clang::ASTContext::UnwrapSimilarTypes(), and clang::QualType::withCVRQualifiers().

Referenced by CastsAwayConstness().