clang 22.0.0git
SemaCast.cpp File Reference
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTStructuralEquivalence.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 "clang/Sema/SemaHLSL.h"
#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/SemaRISCV.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include <set>

Go to the source code of this file.

Enumerations

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

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, SourceRange OpRange, 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType 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, CastOperation::OpRangeType range, Expr *src, QualType destType, bool listInitialization)
 Try to diagnose a failed overloaded cast.
static void diagnoseBadCast (Sema &S, unsigned msg, CastType castType, CastOperation::OpRangeType 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, CastOperation::OpRangeType OpRange)
 Check that a reinterpret_cast<DestType>(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, CastOperation::OpRangeType 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.

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)

CT_Addrspace 

addrspace_cast

Definition at line 49 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 36 of file SemaCast.cpp.

Function Documentation

◆ argTypeIsABIEquivalent()

bool argTypeIsABIEquivalent ( QualType SrcType,
QualType DestType,
ASTContext & Context )
static

◆ CastsAwayConstness()

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
CheckCVRWhether to check for const/volatile/restrict qualifiers.
CheckObjCLifetimeWhether to check Objective-C lifetime qualifiers.

Definition at line 717 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()

◆ checkIntToPointerCast()

◆ diagnoseBadCast()

void diagnoseBadCast ( Sema & S,
unsigned msg,
CastType castType,
CastOperation::OpRangeType opRange,
Expr * src,
QualType destType,
bool listInitialization )
static

◆ DiagnoseBadFunctionCast()

◆ DiagnoseCallingConvCast()

◆ DiagnoseCastOfObjCSEL()

◆ DiagnoseCastQual()

◆ DiagnoseReinterpretUpDownCast()

void DiagnoseReinterpretUpDownCast ( Sema & Self,
const Expr * SrcExpr,
QualType DestType,
CastOperation::OpRangeType OpRange )
static

◆ fixOverloadedReinterpretCastExpr()

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

Definition at line 2255 of file SemaCast.cpp.

References clang::Expr::getValueKindForType(), clang::Self, and clang::VK_PRValue.

Referenced by TryReinterpretCast().

◆ getCastAwayConstnessCastKind()

TryCastResult getCastAwayConstnessCastKind ( CastAwayConstnessKind CACK,
unsigned & DiagID )
static

Definition at line 813 of file SemaCast.cpp.

References TC_Extension, and TC_Failed.

Referenced by TryReinterpretCast().

◆ IsAddressSpaceConversion()

bool IsAddressSpaceConversion ( QualType SrcType,
QualType DestType )
static

◆ isValidCast()

bool isValidCast ( TryCastResult TCR)
static

Definition at line 45 of file SemaCast.cpp.

References TC_Extension, and TC_Success.

◆ TryAddressSpaceCast()

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

◆ TryConstCast()

◆ tryDiagnoseOverloadedCast()

◆ TryLValueToRValueCast()

◆ TryReinterpretCast()

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

◆ TryStaticCast()

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

◆ TryStaticDowncast()

◆ TryStaticImplicitCast()

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

◆ TryStaticMemberPointerUpcast()

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

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

References clang::Sema::Ambiguous, clang::cast(), clang::Sema::DifferentPointee, clang::ActionResult< PtrTy, Compress >::get(), clang::Type::getAs(), clang::CXXMethodDecl::getParent(), clang::Expr::getType(), clang::Sema::Inaccessible, clang::ActionResult< PtrTy, Compress >::isUsable(), clang::Sema::NotDerived, clang::Self, clang::Sema::Success, TC_Failed, TC_NotApplicable, TC_Success, clang::Sema::Upcast, and clang::Sema::Virtual.

Referenced by TryStaticCast().

◆ TryStaticPointerDowncast()

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

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

Definition at line 1690 of file SemaCast.cpp.

References clang::Type::getAs(), clang::PointerType::getPointeeType(), clang::Self, TC_NotApplicable, and TryStaticDowncast().

Referenced by TryStaticCast().

◆ TryStaticReferenceDowncast()

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

◆ unwrapCastAwayConstnessLevel()

CastAwayConstnessKind unwrapCastAwayConstnessLevel ( ASTContext & Context,
QualType & T1,
QualType & T2 )
static

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

References clang::QualType::getCVRQualifiers(), clang::Type::getPointeeType(), clang::Type::isReferenceType(), None, clang::T, and clang::QualType::withCVRQualifiers().

Referenced by CastsAwayConstness().