[clang] [WebKit Checkers] Make TrivialFunctionAnalysis recognize std::array::operator[] as trivial (PR #113377)

Rashmi Mudduluru via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 24 13:35:30 PDT 2024


https://github.com/t-rasmud updated https://github.com/llvm/llvm-project/pull/113377

>From 9e21b0f2f0968a5f8810c797913318884dcc8c7b Mon Sep 17 00:00:00 2001
From: Rashmi Mudduluru <r_mudduluru at apple.com>
Date: Tue, 22 Oct 2024 13:16:30 -0700
Subject: [PATCH 1/3] [WebKit Checkers] Make TrivialFunctionAnalysis recognize
 std::array::operator[] as trivial

TFA wasn't recognizing `__libcpp_verbose_abort` as trivial causing `std::array::operator[]` also not being recongnized as trivial.
---
 .../Checkers/WebKit/PtrTypesSemantics.cpp     |  3 +-
 .../WebKit/uncounted-obj-arg-std-array.cpp    | 33 +++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp

diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index e043806eadd6ac..71440e6d08a1c9 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -414,7 +414,8 @@ class TrivialFunctionAnalysisVisitor
         Name == "isMainThreadOrGCThread" || Name == "isMainRunLoop" ||
         Name == "isWebThread" || Name == "isUIThread" ||
         Name == "mayBeGCThread" || Name == "compilerFenceForCrash" ||
-        Name == "bitwise_cast" || Name.find("__builtin") == 0)
+        Name == "bitwise_cast" || Name.find("__builtin") == 0 ||
+        Name == "__libcpp_verbose_abort")
       return true;
 
     return IsFunctionTrivial(Callee);
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp
new file mode 100644
index 00000000000000..43c76c0b0793a8
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
+// expected-no-diagnostics
+
+#include "mock-types.h"
+
+using size_t = __typeof(sizeof(int));
+namespace std{
+template <class T, size_t N>
+class array {
+  T elements[N];
+  
+  public:
+  T& operator[](unsigned i) { return elements[i]; }
+  constexpr const T* data() const noexcept {
+     return elements;
+  }
+
+};
+}
+
+class ArrayClass {
+public:
+    typedef std::array<std::array<double, 4>, 4> Matrix;
+    double e() { return matrix[3][0]; }
+    Matrix matrix;
+};
+
+class AnotherClass {
+    Ref<ArrayClass> matrix;
+    void test() {
+      double val[] = { matrix->e(), matrix->e() };
+    }
+};

>From a7d7b50159161a4d9ac85532d38ed14fdb841195 Mon Sep 17 00:00:00 2001
From: Rashmi Mudduluru <r_mudduluru at apple.com>
Date: Thu, 24 Oct 2024 11:44:32 -0700
Subject: [PATCH 2/3] fix tests to call __libcpp_verbose_abort

---
 .../WebKit/uncounted-obj-arg-std-array.cpp    | 28 +++++++++++--------
 .../Checkers/WebKit/uncounted-obj-arg.cpp     |  7 +++++
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp
index 43c76c0b0793a8..ed28a64bba059c 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg-std-array.cpp
@@ -3,6 +3,8 @@
 
 #include "mock-types.h"
 
+void __libcpp_verbose_abort(const char *__format, ...);
+
 using size_t = __typeof(sizeof(int));
 namespace std{
 template <class T, size_t N>
@@ -10,24 +12,28 @@ class array {
   T elements[N];
   
   public:
-  T& operator[](unsigned i) { return elements[i]; }
-  constexpr const T* data() const noexcept {
-     return elements;
+  T& operator[](unsigned i) {
+    if (i >= N) {
+      __libcpp_verbose_abort("%s", "aborting");
+    }
+    return elements[i];
   }
-
 };
 }
 
 class ArrayClass {
 public:
-    typedef std::array<std::array<double, 4>, 4> Matrix;
-    double e() { return matrix[3][0]; }
-    Matrix matrix;
+  void ref() const;
+  void deref() const;
+  typedef std::array<std::array<double, 4>, 4> Matrix;
+  double e() { return matrix[3][0]; }
+  Matrix matrix;
 };
 
 class AnotherClass {
-    Ref<ArrayClass> matrix;
-    void test() {
-      double val[] = { matrix->e(), matrix->e() };
-    }
+  RefPtr<ArrayClass> matrix;
+  void test() {
+    double val = { matrix->e()};
+  }
 };
+
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
index 10da776f81575c..2fe818a629cb82 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
@@ -238,6 +238,8 @@ class SomeType : public BaseType {
   using BaseType::BaseType;
 };
 
+void __libcpp_verbose_abort(const char *__format, ...);
+
 class RefCounted {
 public:
   void ref() const;
@@ -452,6 +454,10 @@ class RefCounted {
   unsigned nonTrivial23() { return DerivedNumber("123").value(); }
   SomeType nonTrivial24() { return SomeType("123"); }
 
+  void call_libcpp_verbose_abort() {
+      __libcpp_verbose_abort("%s", "aborting");
+    }
+
   static unsigned s_v;
   unsigned v { 0 };
   Number* number { nullptr };
@@ -620,6 +626,7 @@ class UnrelatedClass {
     // expected-warning at -1{{Call argument for 'this' parameter is uncounted and unsafe}}
     getFieldTrivial().nonTrivial24();
     // expected-warning at -1{{Call argument for 'this' parameter is uncounted and unsafe}}
+    getFieldTrivial().call_libcpp_verbose_abort(); // no-warning
   }
 
   void setField(RefCounted*);

>From 209612dfef0289ebd11245adc907cf36b03b56b3 Mon Sep 17 00:00:00 2001
From: Rashmi Mudduluru <r_mudduluru at apple.com>
Date: Thu, 24 Oct 2024 13:35:15 -0700
Subject: [PATCH 3/3] fix test

---
 .../test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp  | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
index 2fe818a629cb82..e1dacdd9e25b6d 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
@@ -363,6 +363,9 @@ class RefCounted {
   void trivial62() { WTFReportBacktrace(); }
   SomeType trivial63() { return SomeType(0); }
   SomeType trivial64() { return SomeType(); }
+  void trivial65() {
+    __libcpp_verbose_abort("%s", "aborting");
+  }
 
   static RefCounted& singleton() {
     static RefCounted s_RefCounted;
@@ -454,10 +457,6 @@ class RefCounted {
   unsigned nonTrivial23() { return DerivedNumber("123").value(); }
   SomeType nonTrivial24() { return SomeType("123"); }
 
-  void call_libcpp_verbose_abort() {
-      __libcpp_verbose_abort("%s", "aborting");
-    }
-
   static unsigned s_v;
   unsigned v { 0 };
   Number* number { nullptr };
@@ -550,6 +549,7 @@ class UnrelatedClass {
     getFieldTrivial().trivial62(); // no-warning
     getFieldTrivial().trivial63(); // no-warning
     getFieldTrivial().trivial64(); // no-warning
+    getFieldTrivial().trivial65(); // no-warning
 
     RefCounted::singleton().trivial18(); // no-warning
     RefCounted::singleton().someFunction(); // no-warning
@@ -626,7 +626,6 @@ class UnrelatedClass {
     // expected-warning at -1{{Call argument for 'this' parameter is uncounted and unsafe}}
     getFieldTrivial().nonTrivial24();
     // expected-warning at -1{{Call argument for 'this' parameter is uncounted and unsafe}}
-    getFieldTrivial().call_libcpp_verbose_abort(); // no-warning
   }
 
   void setField(RefCounted*);



More information about the cfe-commits mailing list