Skip to content

Commit

Permalink
Fixes for type-conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier-varez committed Aug 3, 2022
1 parent 2749af7 commit fbce89e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 16 additions & 2 deletions clang-tools-extra/clang-tidy/daedalean/TypeConversionsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ QualType getPointee(QualType type) {
return llvm::cast<BlockPointerType>(type)->getPointeeType();
}
assert(false && "Not a valid pointer type!");
return type;
}

bool hasPointerRepresentation(QualType type) {
Expand Down Expand Up @@ -216,7 +217,8 @@ void TypeConversionsCheck::handleImplicitCast(clang::ASTContext *context,
return;
}

if (sourceType->isNullPtrType() && destType->isPointerType()) {
if (sourceType->isNullPtrType() &&
(destType->isPointerType() || destType->isMemberPointerType())) {
// Casting from nullptr to a pointer type is always allowed
return;
}
Expand All @@ -226,7 +228,7 @@ void TypeConversionsCheck::handleImplicitCast(clang::ASTContext *context,
return;
}

if (sourceType->isBuiltinType()) {
if (llvm::isa<BuiltinType>(sourceType)) {
const BuiltinType *Builtin = llvm::dyn_cast<BuiltinType>(sourceType);
if ((Builtin->getKind() == BuiltinType::Kind::BuiltinFn) &&
destType->isFunctionPointerType()) {
Expand Down Expand Up @@ -265,6 +267,18 @@ void TypeConversionsCheck::handleImplicitCast(clang::ASTContext *context,
return;
}

if (hasPointerRepresentation(sourceType) &&
hasPointerRepresentation(destType)) {
const CXXRecordDecl *sourceRecord =
getPointee(sourceType)->getAsCXXRecordDecl();
const CXXRecordDecl *destRecord =
getPointee(destType)->getAsCXXRecordDecl();
if (sourceRecord && destRecord && sourceRecord->isDerivedFrom(destRecord)) {
// Cast pointer or reference from child to base MAY be implicit.
return;
}
}

diag(location, "Type conversions MUST be explicit");
diag(location, "Implicit conversion from %1 to %0", DiagnosticIDs::Note)
<< destType << sourceType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ void testImplicitCastFromDerivedPtrToBase() {

// Implicit B to A is fine
A &NonConstA{Obj};
A *NonConstAPtr{&Obj};
static_cast<void>(NonConstA);
static_cast<void>(NonConstAPtr);
}

void testImplicitCastAddingQualifiers() {
Expand Down

0 comments on commit fbce89e

Please sign in to comment.