diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Sema/SemaCast.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Sema/SemaCast.cpp | 146 |
1 files changed, 101 insertions, 45 deletions
diff --git a/contrib/llvm/tools/clang/lib/Sema/SemaCast.cpp b/contrib/llvm/tools/clang/lib/Sema/SemaCast.cpp index 6222e4c..d603101 100644 --- a/contrib/llvm/tools/clang/lib/Sema/SemaCast.cpp +++ b/contrib/llvm/tools/clang/lib/Sema/SemaCast.cpp @@ -120,12 +120,12 @@ namespace { Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); } - void checkObjCARCConversion(Sema::CheckedConversionKind CCK) { - assert(Self.getLangOpts().ObjCAutoRefCount); + void checkObjCConversion(Sema::CheckedConversionKind CCK) { + assert(Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()); Expr *src = SrcExpr.get(); - if (Self.CheckObjCARCConversion(OpRange, DestType, src, CCK) == - Sema::ACR_unbridged) + if (Self.CheckObjCConversion(OpRange, DestType, src, CCK) == + Sema::ACR_unbridged) IsARCUnbridgedCast = true; SrcExpr = src; } @@ -143,6 +143,9 @@ namespace { }; } +static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr, + QualType DestType); + // The Try functions attempt a specific way of casting. If they succeed, they // return TC_Success. If their way of casting is not appropriate for the given // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic @@ -427,6 +430,10 @@ static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType, /// 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. static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) { const PointerType *T1PtrType = T1->getAs<PointerType>(), *T2PtrType = T2->getAs<PointerType>(); @@ -475,6 +482,26 @@ static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) { return true; } + const LValueReferenceType *T1RefType = T1->getAs<LValueReferenceType>(), + *T2RefType = T2->getAs<LValueReferenceType>(); + if (T1RefType && T2RefType) { + T1 = T1RefType->getPointeeType(); + T2 = T2RefType->getPointeeType(); + return true; + } + + if (T1RefType) { + T1 = T1RefType->getPointeeType(); + // T2 = T2; + return true; + } + + if (T2RefType) { + // T1 = T1; + T2 = T2RefType->getPointeeType(); + return true; + } + return false; } @@ -503,11 +530,13 @@ CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, // the rules are non-trivial. So first we construct Tcv *...cv* as described // in C++ 5.2.11p8. assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || - SrcType->isBlockPointerType()) && + SrcType->isBlockPointerType() || + DestType->isLValueReferenceType()) && "Source type is not pointer or pointer to member."); assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() || - DestType->isBlockPointerType()) && - "Destination type is not pointer or pointer to member."); + DestType->isBlockPointerType() || + DestType->isLValueReferenceType()) && + "Destination type is not pointer or pointer to member, or reference."); QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), UnwrappedDestType = Self.Context.getCanonicalType(DestType); @@ -523,7 +552,14 @@ CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, Qualifiers SrcQuals, DestQuals; Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals); Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals); - + + // We do not meaningfully track object const-ness of Objective-C object + // types. Remove const from the source type if either the source or + // the destination is an Objective-C object type. + if (UnwrappedSrcType->isObjCObjectType() || + UnwrappedDestType->isObjCObjectType()) + SrcQuals.removeConst(); + Qualifiers RetainedSrcQuals, RetainedDestQuals; if (CheckCVR) { RetainedSrcQuals.setCVRQualifiers(SrcQuals.getCVRQualifiers()); @@ -871,8 +907,8 @@ void CastOperation::CheckReinterpretCast() { } SrcExpr = ExprError(); } else if (tcr == TC_Success) { - if (Self.getLangOpts().ObjCAutoRefCount) - checkObjCARCConversion(Sema::CCK_OtherCast); + if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) + checkObjCConversion(Sema::CCK_OtherCast); DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange); } } @@ -935,8 +971,8 @@ void CastOperation::CheckStaticCast() { } else if (tcr == TC_Success) { if (Kind == CK_BitCast) checkCastAlign(); - if (Self.getLangOpts().ObjCAutoRefCount) - checkObjCARCConversion(Sema::CCK_OtherCast); + if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) + checkObjCConversion(Sema::CCK_OtherCast); } else if (Kind == CK_BitCast) { checkCastAlign(); } @@ -1763,13 +1799,12 @@ static void DiagnoseCallingConvCast(Sema &Self, const ExprResult &SrcExpr, if (!DRE) return; auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl()); - const FunctionDecl *Definition; - if (!FD || !FD->hasBody(Definition)) + if (!FD) return; // Only warn if we are casting from the default convention to a non-default // convention. This can happen when the programmer forgot to apply the calling - // convention to the function definition and then inserted this cast to + // convention to the function declaration and then inserted this cast to // satisfy the type system. CallingConv DefaultCC = Self.getASTContext().getDefaultCallingConvention( FD->isVariadic(), FD->isCXXInstanceMember()); @@ -1792,7 +1827,7 @@ static void DiagnoseCallingConvCast(Sema &Self, const ExprResult &SrcExpr, // whose address was taken. Try to use the latest macro for the convention. // For example, users probably want to write "WINAPI" instead of "__stdcall" // to match the Windows header declarations. - SourceLocation NameLoc = Definition->getNameInfo().getLoc(); + SourceLocation NameLoc = FD->getFirstDecl()->getNameInfo().getLoc(); Preprocessor &PP = Self.getPreprocessor(); SmallVector<TokenValue, 6> AttrTokens; SmallString<64> CCAttrText; @@ -1872,7 +1907,8 @@ static bool fixOverloadedReinterpretCastExpr(Sema &Self, QualType DestType, // No guarantees that ResolveAndFixSingleFunctionTemplateSpecialization // preserves Result. Result = E; - if (!Self.resolveAndFixAddressOfOnlyViableOverloadCandidate(Result)) + if (!Self.resolveAndFixAddressOfOnlyViableOverloadCandidate( + Result, /*DoFunctionPointerConversion=*/true)) return false; return Result.isUsable(); } @@ -2177,6 +2213,8 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle, bool ListInitialization) { + assert(Self.getLangOpts().CPlusPlus); + // Handle placeholders. if (isPlaceholder()) { // C-style casts can resolve __unknown_any types. @@ -2273,8 +2311,9 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle, } } - if (Self.getLangOpts().ObjCAutoRefCount && tcr == TC_Success) - checkObjCARCConversion(CCK); + if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && + tcr == TC_Success) + checkObjCConversion(CCK); if (tcr != TC_Success && msg != 0) { if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { @@ -2540,12 +2579,13 @@ void CastOperation::CheckCStyleCast() { } // ARC imposes extra restrictions on casts. - if (Self.getLangOpts().ObjCAutoRefCount) { - checkObjCARCConversion(Sema::CCK_CStyleCast); + if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) { + checkObjCConversion(Sema::CCK_CStyleCast); if (SrcExpr.isInvalid()) return; - - if (const PointerType *CastPtr = DestType->getAs<PointerType>()) { + + const PointerType *CastPtr = DestType->getAs<PointerType>(); + if (Self.getLangOpts().ObjCAutoRefCount && CastPtr) { if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) { Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); @@ -2578,30 +2618,42 @@ void CastOperation::CheckCStyleCast() { if (Kind == CK_BitCast) checkCastAlign(); +} + +/// DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either +/// const, volatile or both. +static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr, + QualType DestType) { + if (SrcExpr.isInvalid()) + return; + + QualType SrcType = SrcExpr.get()->getType(); + if (!((SrcType->isAnyPointerType() && DestType->isAnyPointerType()) || + DestType->isLValueReferenceType())) + return; - // -Wcast-qual QualType TheOffendingSrcType, TheOffendingDestType; Qualifiers CastAwayQualifiers; - if (SrcType->isAnyPointerType() && DestType->isAnyPointerType() && - CastsAwayConstness(Self, SrcType, DestType, true, false, - &TheOffendingSrcType, &TheOffendingDestType, - &CastAwayQualifiers)) { - int qualifiers = -1; - if (CastAwayQualifiers.hasConst() && CastAwayQualifiers.hasVolatile()) { - qualifiers = 0; - } else if (CastAwayQualifiers.hasConst()) { - qualifiers = 1; - } else if (CastAwayQualifiers.hasVolatile()) { - qualifiers = 2; - } - // This is a variant of int **x; const int **y = (const int **)x; - if (qualifiers == -1) - Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual2) << - SrcType << DestType; - else - Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual) << - TheOffendingSrcType << TheOffendingDestType << qualifiers; - } + if (!CastsAwayConstness(Self, SrcType, DestType, true, false, + &TheOffendingSrcType, &TheOffendingDestType, + &CastAwayQualifiers)) + return; + + int qualifiers = -1; + if (CastAwayQualifiers.hasConst() && CastAwayQualifiers.hasVolatile()) { + qualifiers = 0; + } else if (CastAwayQualifiers.hasConst()) { + qualifiers = 1; + } else if (CastAwayQualifiers.hasVolatile()) { + qualifiers = 2; + } + // This is a variant of int **x; const int **y = (const int **)x; + if (qualifiers == -1) + Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual2) + << SrcType << DestType; + else + Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual) + << TheOffendingSrcType << TheOffendingDestType << qualifiers; } ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc, @@ -2622,17 +2674,21 @@ ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc, if (Op.SrcExpr.isInvalid()) return ExprError(); + // -Wcast-qual + DiagnoseCastQual(Op.Self, Op.SrcExpr, Op.DestType); + return Op.complete(CStyleCastExpr::Create(Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(), &Op.BasePath, CastTypeInfo, LPLoc, RPLoc)); } ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo, + QualType Type, SourceLocation LPLoc, Expr *CastExpr, SourceLocation RPLoc) { assert(LPLoc.isValid() && "List-initialization shouldn't get here."); - CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); + CastOperation Op(*this, Type, CastExpr); Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getLocEnd()); |