clang  6.0.0svn
Enumerations | Functions
SemaCast.cpp File Reference
#include "clang/Sema/SemaInternal.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/RecordLayout.h"
#include "clang/Basic/PartialDiagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Initialization.h"
#include "llvm/ADT/SmallVector.h"
#include <set>
Include dependency graph for SemaCast.cpp:

Go to the source code of this file.

Enumerations

enum  TryCastResult { TC_NotApplicable, TC_Success, TC_Failed }
 
enum  CastType {
  CT_Const, CT_Static, CT_Reinterpret, CT_Dynamic,
  CT_CStyle, CT_Functional
}
 

Functions

static void DiagnoseCastQual (Sema &Self, const ExprResult &SrcExpr, QualType DestType)
 DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either const, volatile or both. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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: More...
 
static TryCastResult TryStaticImplicitCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, Sema::CheckedConversionKind CCK, SourceRange OpRange, unsigned &msg, CastKind &Kind, bool ListInitialization)
 TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 is valid: More...
 
static TryCastResult TryStaticCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, Sema::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. More...
 
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. More...
 
static TryCastResult TryReinterpretCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, bool CStyle, SourceRange OpRange, 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. More...
 
static void diagnoseBadCast (Sema &S, unsigned msg, CastType castType, SourceRange opRange, Expr *src, QualType destType, bool listInitialization)
 Diagnose a failed cast. More...
 
static bool UnwrapDissimilarPointerTypes (QualType &T1, QualType &T2)
 UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, this removes one level of indirection from both types, provided that they're the same kind of pointer (plain or to-member). More...
 
static bool CastsAwayConstness (Sema &Self, QualType SrcType, QualType DestType, bool CheckCVR, bool CheckObjCLifetime, QualType *TheOffendingSrcType=nullptr, QualType *TheOffendingDestType=nullptr, Qualifiers *CastAwayQualifiers=nullptr)
 CastsAwayConstness - Check if the pointer conversion from SrcType to DestType casts away constness as defined in C++ 5.2.11p8ff. More...
 
static void DiagnoseReinterpretUpDownCast (Sema &Self, const Expr *SrcExpr, QualType DestType, SourceRange OpRange)
 Check that a reinterpret_cast<DestType>(SrcExpr) is not used as upcast or downcast between respective pointers or references. More...
 
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. More...
 
static void checkIntToPointerCast (bool CStyle, SourceLocation Loc, 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. More...
 

Enumeration Type Documentation

◆ CastType

enum 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)

Definition at line 40 of file SemaCast.cpp.

◆ TryCastResult

Enumerator
TC_NotApplicable 

The cast method is not applicable.

TC_Success 

The cast method is appropriate and successful.

TC_Failed 

The cast method is appropriate, but failed.

A diagnostic has been emitted.

Definition at line 33 of file SemaCast.cpp.

Function Documentation

◆ CastsAwayConstness()

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

CastsAwayConstness - Check if the pointer conversion from SrcType to DestType casts away constness as defined in C++ 5.2.11p8ff.

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

Parameters
CheckCVRWhether to check for const/volatile/restrict qualifiers.
CheckObjCLifetimeWhether to check Objective-C lifetime qualifiers.

Definition at line 517 of file SemaCast.cpp.

References clang::Qualifiers::compatiblyIncludesObjCLifetime(), clang::Sema::Context, clang::ASTContext::getCanonicalType(), clang::Qualifiers::getCVRQualifiers(), clang::ASTContext::getLangOpts(), clang::ASTContext::getUnqualifiedArrayType(), clang::Type::isAnyPointerType(), clang::Type::isBlockPointerType(), clang::Type::isLValueReferenceType(), clang::Type::isMemberPointerType(), clang::Type::isObjCObjectType(), clang::Qualifiers::removeConst(), clang::Qualifiers::setCVRQualifiers(), UnwrapDissimilarPointerTypes(), and clang::ASTContext::VoidTy.

Referenced by DiagnoseCastQual(), and TryReinterpretCast().

◆ checkIntToPointerCast()

static void checkIntToPointerCast ( bool  CStyle,
SourceLocation  Loc,
const Expr SrcExpr,
QualType  DestType,
Sema Self 
)
static

◆ diagnoseBadCast()

static void diagnoseBadCast ( Sema S,
unsigned  msg,
CastType  castType,
SourceRange  opRange,
Expr src,
QualType  destType,
bool  listInitialization 
)
static

Diagnose a failed cast.

Definition at line 389 of file SemaCast.cpp.

Referenced by TryReinterpretCast().

◆ DiagnoseBadFunctionCast()

static void DiagnoseBadFunctionCast ( Sema Self,
const ExprResult SrcExpr,
QualType  DestType 
)
static

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 2351 of file SemaCast.cpp.

References clang::Sema::AA_Casting, clang::LangOptions::allowsNonTrivialObjCLifetimeQualifiers(), clang::VectorType::AltiVecVector, clang::Sema::CCK_CStyleCast, clang::Sema::CheckExtVectorCast(), checkIntToPointerCast(), clang::Sema::CheckObjCARCUnavailableWeakConversion(), clang::Sema::checkUnknownAnyCast(), clang::Sema::CheckVectorCast(), clang::Qualifiers::compatiblyIncludesObjCLifetime(), clang::Sema::Context, clang::Sema::DefaultFunctionArrayLvalueConversion(), clang::Sema::Diag(), DiagnoseCallingConvCast(), DiagnoseCastOfObjCSEL(), clang::Sema::Diags, clang::ExprError(), clang::Sema::FixOverloadedFunctionReference(), clang::ActionResult< PtrTy, CompressInvalid >::get(), clang::Type::getAs(), clang::SourceRange::getBegin(), clang::RecordType::getDecl(), clang::Sema::getLangOpts(), clang::Sema::getOpenCLOptions(), clang::PointerType::getPointeeType(), clang::QualType::getQualifiers(), clang::CastExpr::getTargetFieldForToUnionCast(), clang::QualType::getUnqualifiedType(), clang::ASTContext::hasSameUnqualifiedType(), clang::Sema::IgnoredValueConversions(), clang::PointerType::isAddressSpaceOverlapping(), clang::Type::isAnyPointerType(), clang::Type::isArithmeticType(), clang::Type::isBlockPointerType(), clang::Type::isBooleanType(), clang::Type::isComplexIntegerType(), clang::Type::isComplexType(), clang::OpenCLOptions::isEnabled(), clang::Type::isEnumeralType(), clang::Type::isEventT(), clang::Type::isExtVectorType(), clang::Type::isFloatingType(), clang::Type::isHalfType(), clang::DiagnosticsEngine::isIgnored(), clang::Type::isIntegerType(), clang::Type::isIntegralType(), clang::ActionResult< PtrTy, CompressInvalid >::isInvalid(), clang::Type::isObjCLifetimeType(), clang::Type::isPlaceholderType(), clang::Type::isPointerType(), clang::Type::isRealFloatingType(), clang::Type::isScalarType(), clang::TagDecl::isUnion(), clang::ActionResult< PtrTy, CompressInvalid >::isUsable(), clang::Type::isVectorType(), clang::Type::isVoidType(), clang::ASTContext::OverloadTy, clang::Sema::PrepareScalarCast(), clang::Sema::prepareVectorSplat(), clang::Sema::RequireCompleteType(), and clang::Sema::ResolveAddressOfOverloadedFunction().

◆ DiagnoseCallingConvCast()

static void DiagnoseCallingConvCast ( Sema Self,
const ExprResult SrcExpr,
QualType  DstType,
SourceRange  OpRange 
)
static

◆ DiagnoseCastOfObjCSEL()

static void DiagnoseCastOfObjCSEL ( Sema Self,
const ExprResult SrcExpr,
QualType  DestType 
)
static

◆ DiagnoseCastQual()

static void DiagnoseCastQual ( Sema Self,
const ExprResult SrcExpr,
QualType  DestType 
)
static

◆ DiagnoseReinterpretUpDownCast()

static void DiagnoseReinterpretUpDownCast ( Sema Self,
const Expr SrcExpr,
QualType  DestType,
SourceRange  OpRange 
)
static

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

Definition at line 785 of file SemaCast.cpp.

References clang::Type::getAsCXXRecordDecl(), clang::Type::getPointeeCXXRecordDecl(), clang::Expr::getType(), clang::TagDecl::isCompleteDefinition(), clang::CXXRecordDecl::isDerivedFrom(), and clang::Decl::isInvalidDecl().

◆ fixOverloadedReinterpretCastExpr()

static bool fixOverloadedReinterpretCastExpr ( Sema Self,
QualType  DestType,
ExprResult Result 
)
static

Definition at line 1889 of file SemaCast.cpp.

Referenced by TryReinterpretCast().

◆ TryConstCast()

static TryCastResult TryConstCast ( Sema Self,
ExprResult SrcExpr,
QualType  DestType,
bool  CStyle,
unsigned &  msg 
)
static

◆ tryDiagnoseOverloadedCast()

static bool tryDiagnoseOverloadedCast ( Sema S,
CastType  CT,
SourceRange  range,
Expr src,
QualType  destType,
bool  listInitialization 
)
static

Try to diagnose a failed overloaded cast.

Returns true if diagnostics were emitted.

Definition at line 313 of file SemaCast.cpp.

◆ TryLValueToRValueCast()

TryCastResult TryLValueToRValueCast ( Sema Self,
Expr SrcExpr,
QualType  DestType,
bool  CStyle,
CastKind Kind,
CXXCastPath BasePath,
unsigned &  msg 
)
static

◆ TryReinterpretCast()

static TryCastResult TryReinterpretCast ( Sema Self,
ExprResult SrcExpr,
QualType  DestType,
bool  CStyle,
SourceRange  OpRange,
unsigned &  msg,
CastKind Kind 
)
static

Definition at line 1916 of file SemaCast.cpp.

References clang::LangOptions::allowsNonTrivialObjCLifetimeQualifiers(), clang::VectorType::AltiVecVector, clang::Sema::areLaxCompatibleVectorTypes(), CastsAwayConstness(), clang::Sema::CCK_CStyleCast, clang::Sema::CCK_FunctionalCast, clang::Sema::CheckCompatibleReinterpretCast(), checkIntToPointerCast(), clang::Sema::checkUnknownAnyCast(), clang::Sema::Context, CT_CStyle, CT_Functional, clang::Sema::DefaultFunctionArrayLvalueConversion(), clang::Sema::Diag(), diagnoseBadCast(), DiagnoseCallingConvCast(), DiagnoseCastOfObjCSEL(), clang::ExprError(), clang::OverloadExpr::find(), fixOverloadedReinterpretCastExpr(), clang::ActionResult< PtrTy, CompressInvalid >::get(), clang::Type::getAs(), clang::SourceRange::getBegin(), clang::ASTContext::getCanonicalType(), clang::TargetInfo::getCXXABI(), clang::Sema::getLangOpts(), clang::OverloadExpr::getName(), clang::ASTContext::getPointerType(), clang::OverloadExpr::getQualifierLoc(), clang::NestedNameSpecifierLoc::getSourceRange(), clang::ASTContext::getTargetInfo(), clang::ASTContext::getTypeSize(), clang::Sema::IgnoredValueConversions(), clang::Type::isAnyPointerType(), clang::Type::isBlockPointerType(), clang::Type::isBooleanType(), clang::Sema::isCompleteType(), clang::Type::isDependentType(), clang::Type::isFunctionPointerType(), clang::Type::isIntegralOrEnumerationType(), clang::Type::isIntegralType(), clang::ActionResult< PtrTy, CompressInvalid >::isInvalid(), clang::MemberPointerType::isMemberFunctionPointer(), clang::Type::isMemberPointerType(), clang::TargetCXXABI::isMicrosoft(), clang::Type::isNullPtrType(), clang::Type::isObjCObjectPointerType(), clang::Type::isRecordType(), clang::ActionResult< PtrTy, CompressInvalid >::isUsable(), clang::Type::isVectorType(), clang::Type::isVoidType(), clang::Sema::NoteAllOverloadCandidates(), clang::OK_BitField, clang::OK_ObjCProperty, clang::OK_ObjCSubscript, clang::OK_Ordinary, clang::OK_VectorComponent, clang::ASTContext::OverloadTy, clang::Sema::PrepareCastToObjCObjectPointer(), clang::Sema::prepareVectorSplat(), clang::Sema::ResolveAddressOfOverloadedFunction(), clang::Sema::ResolveAndFixSingleFunctionTemplateSpecialization(), clang::Result, TC_Failed, TC_NotApplicable, TC_Success, TryConstCast(), TryStaticCast(), and clang::VK_RValue.

◆ TryStaticCast()

static TryCastResult TryStaticCast ( Sema Self,
ExprResult SrcExpr,
QualType  DestType,
Sema::CheckedConversionKind  CCK,
SourceRange  OpRange,
unsigned &  msg,
CastKind Kind,
CXXCastPath BasePath,
bool  ListInitialization 
)
static

◆ TryStaticDowncast()

TryCastResult TryStaticDowncast ( Sema Self,
CanQualType  SrcType,
CanQualType  DestType,
bool  CStyle,
SourceRange  OpRange,
QualType  OrigSrcType,
QualType  OrigDestType,
unsigned &  msg,
CastKind Kind,
CXXCastPath BasePath 
)
static

◆ TryStaticImplicitCast()

TryCastResult TryStaticImplicitCast ( Sema Self,
ExprResult SrcExpr,
QualType  DestType,
Sema::CheckedConversionKind  CCK,
SourceRange  OpRange,
unsigned &  msg,
CastKind Kind,
bool  ListInitialization 
)
static

◆ TryStaticMemberPointerUpcast()

TryCastResult TryStaticMemberPointerUpcast ( Sema Self,
ExprResult SrcExpr,
QualType  SrcType,
QualType  DestType,
bool  CStyle,
SourceRange  OpRange,
unsigned &  msg,
CastKind Kind,
CXXCastPath BasePath 
)
static

◆ TryStaticPointerDowncast()

TryCastResult TryStaticPointerDowncast ( Sema Self,
QualType  SrcType,
QualType  DestType,
bool  CStyle,
SourceRange  OpRange,
unsigned &  msg,
CastKind Kind,
CXXCastPath BasePath 
)
static

Tests whether a conversion according to C++ 5.2.9p8 is valid.

Definition at line 1269 of file SemaCast.cpp.

References clang::Sema::Context, clang::Type::getAs(), clang::ASTContext::getCanonicalType(), clang::PointerType::getPointeeType(), TC_NotApplicable, and TryStaticDowncast().

Referenced by TryStaticCast().

◆ TryStaticReferenceDowncast()

TryCastResult TryStaticReferenceDowncast ( Sema Self,
Expr SrcExpr,
QualType  DestType,
bool  CStyle,
SourceRange  OpRange,
unsigned &  msg,
CastKind Kind,
CXXCastPath BasePath 
)
static

◆ UnwrapDissimilarPointerTypes()

static bool UnwrapDissimilarPointerTypes ( QualType T1,
QualType T2 
)
static

UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, this removes one level of indirection from both types, provided that they're the same kind of pointer (plain or to-member).

Unlike the Sema function, this one doesn't care if the two pointers-to-member don't point into the same class. This is because CastsAwayConstness doesn't care. And additionally, it handles C++ references. If both the types are references, then their pointee types are returned, else if only one of them is reference, it's pointee type is returned, and the other type is returned as-is.

Definition at line 437 of file SemaCast.cpp.

References clang::Type::getAs(), clang::Type::getPointeeType(), clang::PointerType::getPointeeType(), clang::BlockPointerType::getPointeeType(), clang::ReferenceType::getPointeeType(), clang::MemberPointerType::getPointeeType(), and clang::ObjCObjectPointerType::getPointeeType().

Referenced by CastsAwayConstness().