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

>From 525500d81eb51a4678ae3c5cb3c1ea067f53a552 Mon Sep 17 00:00:00 2001
From: Fady Farag <[email protected]>
Date: Thu, 12 Nov 2026 17:23:36 -0600
Subject: [PATCH] [WebKit checkers] Make the triviality of mutually recursive
 functions independent of call order

Previously, when `TrivialFunctionAnalysis` reached a function or
statement that was still being analyzed, it assumed it was trivial, and
anything that finished on top of it was cached as trivial right away.
That is correct when the one still being analyzed turns out to be
trivial, but when it later hit something non-trivial, only the entries
still on the stack were marked non-trivial, so a function that called
back into it stayed cached as trivial. This caused a missing warning
that depended on which function was called first. This makes everything
analyzed on top of a pending entry share its result, so they become
non-trivial together if it fails and final together when it finishes.

Co-authored with Ryosuke Niwa (@rniwa) and Claude.
---
 .../Checkers/WebKit/PtrTypesSemantics.cpp     | 84 +++++++++++++------
 .../Checkers/WebKit/PtrTypesSemantics.h       |  8 +-
 .../Checkers/WebKit/uncounted-obj-arg.cpp     | 62 ++++++++++++++
 3 files changed, 125 insertions(+), 29 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp 
b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index 9cc782985c834..abb9afbab58d9 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -726,6 +726,20 @@ bool isSingleton(const NamedDecl *F) {
   return Name == "singleton" || Name.ends_with("Singleton");
 }
 
+struct TrivialFunctionAnalysis::Triviality {
+  bool IsTrivial = true;
+  // Whether it's still being analyzed. Only meaningful when not merged.
+  bool IsPending = true;
+  // The result of a mutually recursive caller, which this one now shares.
+  std::shared_ptr<Triviality> MergedInto;
+
+  static std::shared_ptr<Triviality> resolve(std::shared_ptr<Triviality> T) {
+    while (T->MergedInto)
+      T = T->MergedInto;
+    return T;
+  }
+};
+
 // We only care about statements so let's use the simple
 // (non-recursive) visitor.
 class TrivialFunctionAnalysisVisitor
@@ -748,28 +762,38 @@ class TrivialFunctionAnalysisVisitor
 
   template <typename StmtOrDecl, typename CheckFunction>
   bool WithCachedResult(const StmtOrDecl *S, CheckFunction Function) {
-    auto CacheIt = Cache.find(S);
-    if (CacheIt != Cache.end() && canUseCachedResult(CacheIt->second))
-      return CacheIt->second;
-
-    // Treat a recursive statement to be trivial until proven otherwise.
-    auto [RecursiveIt, IsNew] = RecursiveFn.insert(std::make_pair(S, true));
-    if (!IsNew)
-      return RecursiveIt->second;
-
-    bool Result = Function();
-
-    if (!Result) {
-      for (auto &It : RecursiveFn)
-        It.second = false;
+    if (auto CacheIt = Cache.find(S); CacheIt != Cache.end()) {
+      auto Cached = Triviality::resolve(CacheIt->second);
+      if (Cached->IsPending) {
+        // Treat a recursive statement to be trivial until proven otherwise.
+        // Everything being analyzed on top of it relies on this assumption,
+        // so they share its result from now on. e.g. when analyzing f, which
+        // calls g and then something non-trivial, g which calls f back is
+        // only trivial if f is. Keep going until reaching the statement itself
+        // since merging a group doesn't merge the ones underneath it.
+        for (auto &Current : llvm::reverse(Analyzing)) {
+          if (Current == Cached)
+            break;
+          auto Root = Triviality::resolve(Current);
+          if (Root != Cached)
+            Root->MergedInto = Cached;
+        }
+        return Cached->IsTrivial;
+      }
+      if (canUseCachedResult(Cached->IsTrivial))
+        return Cached->IsTrivial;
     }
-    RecursiveIt = RecursiveFn.find(S);
-    assert(RecursiveIt != RecursiveFn.end());
-    Result = RecursiveIt->second;
-    RecursiveFn.erase(RecursiveIt);
-    Cache[S] = Result;
 
-    return Result;
+    auto Result = std::make_shared<Triviality>();
+    Cache[S] = Result;
+    Analyzing.push_back(Result);
+    if (!Function()) {
+      for (auto &Current : Analyzing)
+        Triviality::resolve(Current)->IsTrivial = false;
+    }
+    Analyzing.pop_back();
+    Result->IsPending = false;
+    return Triviality::resolve(Result)->IsTrivial;
   }
 
   static bool isTrivialType(QualType Ty) {
@@ -924,6 +948,7 @@ class TrivialFunctionAnalysisVisitor
 
 public:
   using CacheTy = TrivialFunctionAnalysis::CacheTy;
+  using Triviality = TrivialFunctionAnalysis::Triviality;
 
   TrivialFunctionAnalysisVisitor(CacheTy &Cache,
                                  NonTrivialityReason *Reason = nullptr)
@@ -1049,11 +1074,14 @@ class TrivialFunctionAnalysisVisitor
   }
 
   bool IsStatementTrivial(const Stmt *S) {
-    auto CacheIt = Cache.find(S);
-    if (CacheIt != Cache.end() && canUseCachedResult(CacheIt->second))
-      return CacheIt->second;
+    if (auto CacheIt = Cache.find(S); CacheIt != Cache.end()) {
+      bool Cached = Triviality::resolve(CacheIt->second)->IsTrivial;
+      if (canUseCachedResult(Cached))
+        return Cached;
+    }
     bool Result = Visit(S);
-    Cache[S] = Result;
+    Cache[S] = std::make_shared<Triviality>(
+        Triviality{Result, /*IsPending=*/false, nullptr});
     return Result;
   }
 
@@ -1542,9 +1570,11 @@ class TrivialFunctionAnalysisVisitor
 
 private:
   CacheTy &Cache;
-  CacheTy FieldCtorCache;
-  CacheTy FieldDtorCache;
-  CacheTy RecursiveFn;
+  llvm::DenseMap<const CXXRecordDecl *, bool> FieldCtorCache;
+  llvm::DenseMap<const CXXRecordDecl *, bool> FieldDtorCache;
+  // Results of the statements and functions currently being analyzed,
+  // outermost first.
+  SmallVector<std::shared_ptr<Triviality>> Analyzing;
   const Stmt **OffendingStmt;
   SmallVectorImpl<NonTrivialityReason::Frame> *CallStack;
   unsigned ActiveDepth = 0;
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h 
b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h
index e6ef9ba984637..e299a868107f4 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h
@@ -14,6 +14,7 @@
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/PointerUnion.h"
 #include "llvm/ADT/SmallVector.h"
+#include <memory>
 #include <optional>
 #include <string>
 
@@ -275,8 +276,11 @@ class TrivialFunctionAnalysis {
 private:
   friend class TrivialFunctionAnalysisVisitor;
 
-  using CacheTy =
-      llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt *>, bool>;
+  /// The triviality of a function or a statement, which mutually recursive
+  /// functions share until the outermost one has been fully analyzed.
+  struct Triviality;
+  using CacheTy = llvm::DenseMap<llvm::PointerUnion<const Decl *, const Stmt 
*>,
+                                 std::shared_ptr<Triviality>>;
   mutable CacheTy TheCache{};
 
   static bool isTrivialImpl(const Decl *D, CacheTy &Cache);
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp 
b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
index 4434439501f47..3fc25b6743d4a 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
@@ -732,6 +732,68 @@ void test2() {
   someFunction(*object());
 }
 
+namespace mutual_recursion_query_order {
+
+RefCounted* provide();
+void unknownFunction();
+void first(RefCounted*);
+void second(RefCounted*);
+void loopFirst(RefCounted*);
+void loopSecond(RefCounted*);
+void loopThird(RefCounted*);
+void outer(RefCounted*);
+void inner(RefCounted*);
+void innerLoop(RefCounted*);
+void sibling(RefCounted*);
+void top(RefCounted*);
+void middle(RefCounted*);
+void bottom(RefCounted*);
+
+void test() {
+  first(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::first') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+  second(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::second') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+  loopFirst(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::loopFirst') is a raw pointer to RefPtr-capable 
type 'RefCounted'}}
+  loopSecond(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::loopSecond') is a raw pointer to RefPtr-capable 
type 'RefCounted'}}
+  loopThird(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::loopThird') is a raw pointer to RefPtr-capable 
type 'RefCounted'}}
+  outer(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::outer') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+  inner(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::inner') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+  innerLoop(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::innerLoop') is a raw pointer to RefPtr-capable 
type 'RefCounted'}}
+  sibling(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::sibling') is a raw pointer to RefPtr-capable 
type 'RefCounted'}}
+  top(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::top') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+  middle(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::middle') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+  bottom(provide());
+  // expected-warning@-1{{Function argument 'provide()' (to 
'mutual_recursion_query_order::bottom') is a raw pointer to RefPtr-capable type 
'RefCounted'}}
+}
+
+void first(RefCounted* obj) { second(obj); unknownFunction(); }
+void second(RefCounted* obj) { first(obj); }
+
+void loopFirst(RefCounted* obj) { loopSecond(obj); unknownFunction(); }
+void loopSecond(RefCounted* obj) { loopThird(obj); }
+void loopThird(RefCounted* obj) { loopFirst(obj); }
+
+void outer(RefCounted* obj) { inner(obj); sibling(obj); unknownFunction(); }
+void inner(RefCounted* obj) { innerLoop(obj); outer(obj); }
+void innerLoop(RefCounted* obj) { inner(obj); }
+void sibling(RefCounted* obj) { innerLoop(obj); }
+
+void top(RefCounted* obj) { middle(obj); unknownFunction(); }
+void middle(RefCounted* obj) { bottom(obj); }
+void bottom(RefCounted* obj) { { bottom(obj); top(obj); } }
+
+}
+
 void system_header() {
   callMethod<RefCountable>(object);
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to