Skip to content

[alpha.webkit.ForwardDeclChecker] Ignore forward declared struct. #133804

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

Merged
merged 3 commits into from
Apr 4, 2025

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Mar 31, 2025

There are some system libraries such as sqlite3 which forward declare a struct then use a pointer to that forward declared type in various APIs. Ignore these types ForwardDeclChecker like other pointer types.

There are some system libraries such as sqlite3 which forward declare a struct
then use a pointer to that forward declared type in various APIs.
Ignore these types ForwardDeclChecker like other pointer types.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Mar 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 31, 2025

@llvm/pr-subscribers-clang

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

Author: Ryosuke Niwa (rniwa)

Changes

There are some system libraries such as sqlite3 which forward declare a struct then use a pointer to that forward declared type in various APIs. Ignore these types ForwardDeclChecker like other pointer types.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp (+5-5)
  • (modified) clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm (+4)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
index a524593b0119b..399ae9e0a6874 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
@@ -108,17 +108,16 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
     RTC.visitTypedef(TD);
     auto QT = TD->getUnderlyingType().getCanonicalType();
     if (BR->getSourceManager().isInSystemHeader(TD->getBeginLoc())) {
-      if (auto *Type = QT.getTypePtrOrNull(); Type && QT->isPointerType())
+      if (auto *Type = QT.getTypePtrOrNull())
         SystemTypes.insert(Type);
     }
   }
 
   bool isUnknownType(QualType QT) const {
-    auto *Type = QT.getTypePtrOrNull();
-    if (!Type)
-      return false;
     auto *CanonicalType = QT.getCanonicalType().getTypePtrOrNull();
-    auto PointeeQT = Type->getPointeeType();
+    if (!CanonicalType)
+      return false;
+    auto PointeeQT = CanonicalType->getPointeeType();
     auto *PointeeType = PointeeQT.getTypePtrOrNull();
     if (!PointeeType)
       return false;
@@ -128,6 +127,7 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
     auto Name = R->getName();
     return !R->hasDefinition() && !RTC.isUnretained(QT) &&
            !SystemTypes.contains(CanonicalType) &&
+           !SystemTypes.contains(PointeeType) &&
            !Name.starts_with("Opaque") && Name != "_NSZone";
   }
 
diff --git a/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm b/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm
index 151cbe2affa92..64100d60c4867 100644
--- a/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm
+++ b/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm
@@ -25,6 +25,8 @@
 
 Obj* provide_obj_ptr();
 void receive_obj_ptr(Obj* p = nullptr);
+sqlite3* open_db();
+void close_db(sqlite3*);
 
 Obj* ptr(Obj* arg) {
   receive_obj_ptr(provide_obj_ptr());
@@ -34,6 +36,8 @@
   receive_obj_ptr(arg);
   receive_obj_ptr(nullptr);
   receive_obj_ptr();
+  auto* db = open_db();
+  close_db(db);
   return obj;
 }
 

Copy link

github-actions bot commented Mar 31, 2025

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

@rniwa rniwa requested a review from t-rasmud March 31, 2025 21:42
Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

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

LGTM!

@rniwa
Copy link
Contributor Author

rniwa commented Apr 4, 2025

Thank you!

@rniwa rniwa merged commit d8fd665 into llvm:main Apr 4, 2025
11 checks passed
@rniwa rniwa deleted the fix-webkit-forward-decl-struct branch April 4, 2025 19:04
rniwa added a commit to rniwa/llvm-project that referenced this pull request Apr 5, 2025
…vm#133804)

There are some system libraries such as sqlite3 which forward declare a
struct then use a pointer to that forward declared type in various APIs.
Ignore these types ForwardDeclChecker like other pointer types.
rniwa added a commit to rniwa/llvm-project that referenced this pull request Apr 22, 2025
…vm#133804)

There are some system libraries such as sqlite3 which forward declare a
struct then use a pointer to that forward declared type in various APIs.
Ignore these types ForwardDeclChecker like other pointer types.
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