[clang-tools-extra] [clang-tidy] Improve documentation of bugprone-pointer-arithmetic-on-polymorphic-object (PR #108324)

Mainak Sil via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 20:27:42 PDT 2024


https://github.com/MainakSil updated https://github.com/llvm/llvm-project/pull/108324

>From edb6664fbdafb5127d141fde633779865a90dde1 Mon Sep 17 00:00:00 2001
From: Mainak Sil <mainaksil0 at gmail.com>
Date: Thu, 12 Sep 2024 08:58:15 +0530
Subject: [PATCH 1/4] Improve documentation for
 bugprone-pointer-arithmetic-on-polymorphic-object

---
 ...inter-arithmetic-on-polymorphic-object.rst | 38 ++++++++++++++-----
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
index 1884acd5f12b3e..4f926808f9e408 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
@@ -19,20 +19,33 @@ Example:
 .. code-block:: c++
 
   struct Base {
-    virtual void ~Base();
+  virtual ~Base();
+  int i;
   };
 
   struct Derived : public Base {};
 
   void foo() {
-    Base *b = new Derived[10];
+  Base *b = new Derived[10];
 
-    b += 1;
-    // warning: pointer arithmetic on class that declares a virtual function can
-    // result in undefined behavior if the dynamic type differs from the
-    // pointer type
+  b += 1;
+  // warning: pointer arithmetic on class that declares a virtual function can
+  // result in undefined behavior if the dynamic type differs from the
+  // pointer type
 
-    delete[] static_cast<Derived*>(b);
+  delete[] static_cast<Derived*>(b);
+  }
+
+  // Another example showing array access with polymorphic objects.
+  int bar(const Derived d[]) {
+    return d[1].i; // warning due to pointer arithmetic on polymorphic object
+  }
+
+  // Making Derived final suppresses the warning
+  struct FinalDerived final : public Base {};
+
+  int baz(const FinalDerived d[]) {
+    return d[1].i; // no warning as FinalDerived is final
   }
 
 Options
@@ -50,14 +63,19 @@ Options
     void bar() {
       Base *b = new Base[10];
       b += 1; // warning, as Base declares a virtual destructor
-  
+
       delete[] b;
-  
+
       Derived *d = new Derived[10]; // Derived overrides the destructor, and
                                     // declares no other virtual functions
       d += 1; // warning only if IgnoreVirtualDeclarationsOnly is set to false
-  
+
       delete[] d;
+
+      FinalDerived *f = new FinalDerived[10];
+      f += 1; // no warning, FinalDerived is final and cannot be further derived
+
+      delete[] f;
     }
 
 References

>From c6832232a55d39bc98562537b9a357378a007227 Mon Sep 17 00:00:00 2001
From: Mainak Sil <145980094+MainakSil at users.noreply.github.com>
Date: Thu, 12 Sep 2024 19:32:25 +0530
Subject: [PATCH 2/4] Update example to remove new/delete and focus on pointer
 arithmetic

---
 ...inter-arithmetic-on-polymorphic-object.rst | 28 ++++++++++---------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
index 4f926808f9e408..cff997d5ada2aa 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
@@ -19,21 +19,23 @@ Example:
 .. code-block:: c++
 
   struct Base {
-  virtual ~Base();
-  int i;
+    virtual ~Base();
+    int i;
   };
-
+  
   struct Derived : public Base {};
-
-  void foo() {
-  Base *b = new Derived[10];
-
-  b += 1;
-  // warning: pointer arithmetic on class that declares a virtual function can
-  // result in undefined behavior if the dynamic type differs from the
-  // pointer type
-
-  delete[] static_cast<Derived*>(b);
+  
+  // Function that takes a pointer and performs pointer arithmetic
+  void foo(Base* b) {
+    b += 1;
+    // warning: pointer arithmetic on class that declares a virtual function can
+    // result in undefined behavior if the dynamic type differs from the
+    // pointer type
+  }
+  
+  void bar() {
+    Derived d[10];  // Array of derived objects
+    foo(d);         // Passing Derived pointer to foo(), which performs arithmetic
   }
 
   // Another example showing array access with polymorphic objects.

>From 96fa8165375bf7a5c249c1e7c43a49ce67fd183e Mon Sep 17 00:00:00 2001
From: Mainak Sil <145980094+MainakSil at users.noreply.github.com>
Date: Thu, 12 Sep 2024 22:58:11 +0530
Subject: [PATCH 3/4] Address maintainer feedback: remove redundant comments,
 new/delete, and final example

---
 ...inter-arithmetic-on-polymorphic-object.rst | 27 +------------------
 1 file changed, 1 insertion(+), 26 deletions(-)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
index cff997d5ada2aa..81556b4537cb65 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
@@ -22,30 +22,18 @@ Example:
     virtual ~Base();
     int i;
   };
-  
   struct Derived : public Base {};
-  
-  // Function that takes a pointer and performs pointer arithmetic
   void foo(Base* b) {
     b += 1;
     // warning: pointer arithmetic on class that declares a virtual function can
     // result in undefined behavior if the dynamic type differs from the
     // pointer type
   }
-  
-  void bar() {
-    Derived d[10];  // Array of derived objects
-    foo(d);         // Passing Derived pointer to foo(), which performs arithmetic
-  }
-
-  // Another example showing array access with polymorphic objects.
   int bar(const Derived d[]) {
     return d[1].i; // warning due to pointer arithmetic on polymorphic object
   }
-
   // Making Derived final suppresses the warning
   struct FinalDerived final : public Base {};
-
   int baz(const FinalDerived d[]) {
     return d[1].i; // no warning as FinalDerived is final
   }
@@ -62,22 +50,9 @@ Options
 
   .. code-block:: c++
   
-    void bar() {
-      Base *b = new Base[10];
+    void bar(Base b[], Derived d[]) {
       b += 1; // warning, as Base declares a virtual destructor
-
-      delete[] b;
-
-      Derived *d = new Derived[10]; // Derived overrides the destructor, and
-                                    // declares no other virtual functions
       d += 1; // warning only if IgnoreVirtualDeclarationsOnly is set to false
-
-      delete[] d;
-
-      FinalDerived *f = new FinalDerived[10];
-      f += 1; // no warning, FinalDerived is final and cannot be further derived
-
-      delete[] f;
     }
 
 References

>From 6605d3b12ec52b32cae4a1e94256b5016021bebb Mon Sep 17 00:00:00 2001
From: Mainak Sil <145980094+MainakSil at users.noreply.github.com>
Date: Fri, 13 Sep 2024 08:57:33 +0530
Subject: [PATCH 4/4] Address maintainer feedback: whitespace

---
 .../bugprone/pointer-arithmetic-on-polymorphic-object.rst    | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
index 81556b4537cb65..95509ef3c724de 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
@@ -22,18 +22,23 @@ Example:
     virtual ~Base();
     int i;
   };
+
   struct Derived : public Base {};
+
   void foo(Base* b) {
     b += 1;
     // warning: pointer arithmetic on class that declares a virtual function can
     // result in undefined behavior if the dynamic type differs from the
     // pointer type
   }
+
   int bar(const Derived d[]) {
     return d[1].i; // warning due to pointer arithmetic on polymorphic object
   }
+
   // Making Derived final suppresses the warning
   struct FinalDerived final : public Base {};
+
   int baz(const FinalDerived d[]) {
     return d[1].i; // no warning as FinalDerived is final
   }



More information about the cfe-commits mailing list