[clang] isUncountedPtr should take QualType as an argument. (PR #110213)

Ryosuke Niwa via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 27 00:20:39 PDT 2024


https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/110213

>From 4c82195acf092fd9a92c999040f8f66947585fde Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Fri, 27 Sep 2024 00:07:12 -0700
Subject: [PATCH 1/2] isUncountedPtr should take QualType as an argument.

Make isUncountedPtr take QualType as an argument instead of Type*.
This simplifies some code.
---
 .../Checkers/WebKit/PtrTypesSemantics.cpp         | 15 ++++-----------
 .../Checkers/WebKit/PtrTypesSemantics.h           |  2 +-
 .../Checkers/WebKit/UncountedCallArgsChecker.cpp  |  6 +-----
 .../WebKit/UncountedLambdaCapturesChecker.cpp     | 10 +++++-----
 .../Checkers/WebKit/UncountedLocalVarsChecker.cpp |  6 +-----
 5 files changed, 12 insertions(+), 27 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index 54c99c3c1b37f9..38582e6d543cd4 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -165,14 +165,11 @@ std::optional<bool> isUncounted(const CXXRecordDecl* Class)
   return (*IsRefCountable);
 }
 
-std::optional<bool> isUncountedPtr(const Type* T)
+std::optional<bool> isUncountedPtr(const QualType T)
 {
-  assert(T);
-
   if (T->isPointerType() || T->isReferenceType()) {
-    if (auto *CXXRD = T->getPointeeCXXRecordDecl()) {
+    if (auto *CXXRD = T->getPointeeCXXRecordDecl())
       return isUncounted(CXXRD);
-    }
   }
   return false;
 }
@@ -196,12 +193,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;
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h
index e2d0342bebd52c..4988f604c52283 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h
@@ -53,7 +53,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);
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
index 31e9b3c4b9d412..8071b6f70f58dc 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
@@ -103,12 +103,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;
 
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp
index a226a01ec0a579..998bd4ccee07db 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp
@@ -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);
         }
       }
     }
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp
index 274da0baf2ce5c..3cc4e86b2abb1a 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp
@@ -190,11 +190,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,

>From b774f3b2e6af3c0a672a735af50c4944ec9d2243 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Fri, 27 Sep 2024 00:20:11 -0700
Subject: [PATCH 2/2] Fix formatting.

---
 .../lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index 38582e6d543cd4..c4cb8f7dfcc108 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -165,8 +165,7 @@ std::optional<bool> isUncounted(const CXXRecordDecl* Class)
   return (*IsRefCountable);
 }
 
-std::optional<bool> isUncountedPtr(const QualType T)
-{
+std::optional<bool> isUncountedPtr(const QualType T) {
   if (T->isPointerType() || T->isReferenceType()) {
     if (auto *CXXRD = T->getPointeeCXXRecordDecl())
       return isUncounted(CXXRD);
@@ -194,7 +193,7 @@ std::optional<bool> isGetterOfRefCounted(const CXXMethodDecl* M)
     // FIXME: Currently allowing any Ref<T> -> whatever cast.
     if (isRefType(className)) {
       if (auto *maybeRefToRawOperator = dyn_cast<CXXConversionDecl>(M))
-          return isUncountedPtr(maybeRefToRawOperator->getConversionType());
+        return isUncountedPtr(maybeRefToRawOperator->getConversionType());
     }
   }
   return false;



More information about the cfe-commits mailing list