Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isUncountedPtr should take QualType as an argument. #110213

Merged
merged 2 commits into from
Oct 10, 2024

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Sep 27, 2024

Make isUncountedPtr take QualType as an argument instead of Type*. This simplifies some code.

Make isUncountedPtr take QualType as an argument instead of Type*.
This simplifies some code.
@rniwa rniwa requested a review from haoNoQ September 27, 2024 07:14
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Sep 27, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 27, 2024

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

Make isUncountedPtr take QualType as an argument instead of Type*. This simplifies some code.


Full diff: https://github.com/llvm/llvm-project/pull/110213.diff

5 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp (+4-11)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h (+1-1)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp (+1-5)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp (+5-5)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp (+1-5)
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,

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 27, 2024

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

Changes

Make isUncountedPtr take QualType as an argument instead of Type*. This simplifies some code.


Full diff: https://github.com/llvm/llvm-project/pull/110213.diff

5 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp (+4-11)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h (+1-1)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp (+1-5)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp (+5-5)
  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp (+1-5)
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,

Copy link

github-actions bot commented Sep 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@@ -190,11 +190,7 @@ class UncountedLocalVarsChecker
if (shouldSkipVarDecl(V))
return;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these null checks may still be necessary (with QualType.isNull()).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to build the entire WebKit with this PR applied so it seems that we don't need these isNull checks in practice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

@@ -190,11 +190,7 @@ class UncountedLocalVarsChecker
if (shouldSkipVarDecl(V))
return;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

@rniwa rniwa merged commit 39a9141 into llvm:main Oct 10, 2024
6 of 8 checks passed
@rniwa
Copy link
Contributor Author

rniwa commented Oct 10, 2024

Thanks for reviews!

@rniwa rniwa deleted the make-IsUncountedPtr-take-QualType branch October 10, 2024 17:02
rniwa added a commit to rniwa/llvm-project that referenced this pull request Oct 10, 2024
Make isUncountedPtr take QualType as an argument instead of Type*. This
simplifies some code.
ericastor pushed a commit to ericastor/llvm-project that referenced this pull request Oct 10, 2024
Make isUncountedPtr take QualType as an argument instead of Type*. This
simplifies some code.
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
Make isUncountedPtr take QualType as an argument instead of Type*. This
simplifies some code.
bricknerb pushed a commit to bricknerb/llvm-project that referenced this pull request Oct 17, 2024
Make isUncountedPtr take QualType as an argument instead of Type*. This
simplifies some code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants