Skip to content

Commit

Permalink
isUncountedPtr should take QualType as an argument. (llvm#110213)
Browse files Browse the repository at this point in the history
Make isUncountedPtr take QualType as an argument instead of Type*. This
simplifies some code.
  • Loading branch information
rniwa authored and ericastor committed Oct 10, 2024
1 parent 4839e50 commit 842314f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 28 deletions.
16 changes: 4 additions & 12 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,10 @@ std::optional<bool> isUncounted(const CXXRecordDecl* Class)
return (*IsRefCountable);
}

std::optional<bool> isUncountedPtr(const Type* T)
{
assert(T);

std::optional<bool> isUncountedPtr(const QualType T) {
if (T->isPointerType() || T->isReferenceType()) {
if (auto *CXXRD = T->getPointeeCXXRecordDecl()) {
if (auto *CXXRD = T->getPointeeCXXRecordDecl())
return isUncounted(CXXRD);
}
}
return false;
}
Expand All @@ -208,12 +204,8 @@ std::optional<bool> isGetterOfRefCounted(const CXXMethodDecl* M)
// Ref<T> -> T conversion
// FIXME: Currently allowing any Ref<T> -> whatever cast.
if (isRefType(className)) {
if (auto *maybeRefToRawOperator = dyn_cast<CXXConversionDecl>(M)) {
if (auto *targetConversionType =
maybeRefToRawOperator->getConversionType().getTypePtrOrNull()) {
return isUncountedPtr(targetConversionType);
}
}
if (auto *maybeRefToRawOperator = dyn_cast<CXXConversionDecl>(M))
return isUncountedPtr(maybeRefToRawOperator->getConversionType());
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ std::optional<bool> isUncounted(const clang::CXXRecordDecl* Class);

/// \returns true if \p T is either a raw pointer or reference to an uncounted
/// class, false if not, std::nullopt if inconclusive.
std::optional<bool> isUncountedPtr(const clang::Type* T);
std::optional<bool> isUncountedPtr(const clang::QualType T);

/// \returns true if Name is a RefPtr, Ref, or its variant, false if not.
bool isRefType(const std::string &Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,8 @@ class UncountedCallArgsChecker
// continue;

QualType ArgType = (*P)->getType().getCanonicalType();
const auto *TypePtr = ArgType.getTypePtrOrNull();
if (!TypePtr)
continue; // FIXME? Should we bail?

// FIXME: more complex types (arrays, references to raw pointers, etc)
std::optional<bool> IsUncounted = isUncountedPtr(TypePtr);
std::optional<bool> IsUncounted = isUncountedPtr(ArgType);
if (!IsUncounted || !(*IsUncounted))
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class UncountedLambdaCapturesChecker
for (const LambdaCapture &C : L->captures()) {
if (C.capturesVariable()) {
ValueDecl *CapturedVar = C.getCapturedVar();
if (auto *CapturedVarType = CapturedVar->getType().getTypePtrOrNull()) {
std::optional<bool> IsUncountedPtr = isUncountedPtr(CapturedVarType);
if (IsUncountedPtr && *IsUncountedPtr) {
reportBug(C, CapturedVar, CapturedVarType);
}
QualType CapturedVarQualType = CapturedVar->getType();
if (auto *CapturedVarType = CapturedVarQualType.getTypePtrOrNull()) {
auto IsUncountedPtr = isUncountedPtr(CapturedVarQualType);
if (IsUncountedPtr && *IsUncountedPtr)
reportBug(C, CapturedVar, CapturedVarType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,7 @@ class UncountedLocalVarsChecker
if (shouldSkipVarDecl(V))
return;

const auto *ArgType = V->getType().getTypePtr();
if (!ArgType)
return;

std::optional<bool> IsUncountedPtr = isUncountedPtr(ArgType);
std::optional<bool> IsUncountedPtr = isUncountedPtr(V->getType());
if (IsUncountedPtr && *IsUncountedPtr) {
if (tryToFindPtrOrigin(
Value, /*StopAtFirstRefCountedObj=*/false,
Expand Down

0 comments on commit 842314f

Please sign in to comment.