[clang] 0e246bb - [clang][analyzer] Add C++ array delete checker

2023-10-10 Thread Viktor Cseh via cfe-commits

Author: Viktor Cseh
Date: 2023-10-10T09:37:02+01:00
New Revision: 0e246bb67573799409d0085b89902a330998ddcc

URL: 
https://github.com/llvm/llvm-project/commit/0e246bb67573799409d0085b89902a330998ddcc
DIFF: 
https://github.com/llvm/llvm-project/commit/0e246bb67573799409d0085b89902a330998ddcc.diff

LOG: [clang][analyzer] Add C++ array delete checker

This checker reports cases where an array of polymorphic objects are
deleted as their base class. Deleting an array where the array's static
type is different from its dynamic type is undefined.

Since the checker is similar to DeleteWithNonVirtualDtorChecker, I
refactored that checker to support more detection types.

This checker corresponds to the SEI Cert rule EXP51-CPP: Do not delete
an array through a pointer of the incorrect type.

Differential Revision: https://reviews.llvm.org/D158156

Added: 
clang/test/Analysis/ArrayDelete.cpp

Modified: 
clang/docs/analyzer/checkers.rst
clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp
clang/test/Analysis/DeleteWithNonVirtualDtor.cpp
clang/www/analyzer/alpha_checks.html

Removed: 




diff  --git a/clang/docs/analyzer/checkers.rst 
b/clang/docs/analyzer/checkers.rst
index dbd6d7787823530..81f333e644f31c9 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1834,6 +1834,30 @@ Either the comparison is useless or there is division by 
zero.
 alpha.cplusplus
 ^^^
 
+.. _alpha-cplusplus-ArrayDelete:
+
+alpha.cplusplus.ArrayDelete (C++)
+"
+Reports destructions of arrays of polymorphic objects that are destructed as 
their base class.
+This checker corresponds to the CERT rule `EXP51-CPP: Do not delete an array 
through a pointer of the incorrect type 
`_.
+
+.. code-block:: cpp
+
+ class Base {
+   virtual ~Base() {}
+ };
+ class Derived : public Base {}
+
+ Base *create() {
+   Base *x = new Derived[10]; // note: Casting from 'Derived' to 'Base' here
+   return x;
+ }
+
+ void foo() {
+   Base *x = create();
+   delete[] x; // warn: Deleting an array of 'Derived' objects as their base 
class 'Base' is undefined
+ }
+
 .. _alpha-cplusplus-DeleteWithNonVirtualDtor:
 
 alpha.cplusplus.DeleteWithNonVirtualDtor (C++)
@@ -1842,13 +1866,17 @@ Reports destructions of polymorphic objects with a 
non-virtual destructor in the
 
 .. code-block:: cpp
 
+ class NonVirtual {};
+ class NVDerived : public NonVirtual {};
+
  NonVirtual *create() {
-   NonVirtual *x = new NVDerived(); // note: conversion from derived to base
-//   happened here
+   NonVirtual *x = new NVDerived(); // note: Casting from 'NVDerived' to
+//   'NonVirtual' here
return x;
  }
 
- void sink(NonVirtual *x) {
+ void foo() {
+   NonVirtual *x = create();
delete x; // warn: destruction of a polymorphic object with no virtual
  //   destructor
  }

diff  --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td 
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index 65c1595eb6245dd..4ca8c98af8706aa 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -758,6 +758,11 @@ def ContainerModeling : Checker<"ContainerModeling">,
   Documentation,
   Hidden;
 
+def CXXArrayDeleteChecker : Checker<"ArrayDelete">,
+  HelpText<"Reports destructions of arrays of polymorphic objects that are "
+   "destructed as their base class.">,
+  Documentation;
+
 def DeleteWithNonVirtualDtorChecker : Checker<"DeleteWithNonVirtualDtor">,
   HelpText<"Reports destructions of polymorphic objects with a non-virtual "
"destructor in their base class">,

diff  --git a/clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp
index 3c142b49ff7288d..1a1f5c530294038 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp
@@ -1,4 +1,4 @@
-//===-- DeleteWithNonVirtualDtorChecker.cpp ---*- C++ 
-*--//
+//=== CXXDeleteChecker.cpp -*- C++ 
-*--===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,17 +6,25 @@
 //
 
//===--===//
 //
-// Defines a checker for the OOP52-CPP CERT rule: Do not delete a polymorphic
-// object without a virtual destructor.
+// This file defines the following new checkers for C++ delete expressions:
 //
-// Diagnostic flags -Wnon-virtual-dt

[clang] 71ae858 - [clang][analyzer] Rename DeleteWithNonVirtualDtorChecker to CXXDeleteChecker

2023-10-10 Thread Viktor Cseh via cfe-commits

Author: Viktor Cseh
Date: 2023-10-10T09:37:02+01:00
New Revision: 71ae858c079f9b4a1a99511af57f47ffb6070920

URL: 
https://github.com/llvm/llvm-project/commit/71ae858c079f9b4a1a99511af57f47ffb6070920
DIFF: 
https://github.com/llvm/llvm-project/commit/71ae858c079f9b4a1a99511af57f47ffb6070920.diff

LOG: [clang][analyzer] Rename DeleteWithNonVirtualDtorChecker to 
CXXDeleteChecker

This rename is done in a separate commit to preserve `git blame`,
before implementing CXXArrayDeleteChecker and refactoring the file.

Differential Revision: https://reviews.llvm.org/D158156

Added: 
clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp

Modified: 
clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt

Removed: 
clang/lib/StaticAnalyzer/Checkers/DeleteWithNonVirtualDtorChecker.cpp



diff  --git a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt 
b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
index ae849f59f90d3d9..d849649c96a0d13 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
+++ b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
@@ -30,12 +30,12 @@ add_clang_library(clangStaticAnalyzerCheckers
   CloneChecker.cpp
   ContainerModeling.cpp
   ConversionChecker.cpp
+  CXXDeleteChecker.cpp
   CXXSelfAssignmentChecker.cpp
   DeadStoresChecker.cpp
   DebugCheckers.cpp
   DebugContainerModeling.cpp
   DebugIteratorModeling.cpp
-  DeleteWithNonVirtualDtorChecker.cpp
   DereferenceChecker.cpp
   DirectIvarAssignment.cpp
   DivZeroChecker.cpp

diff  --git 
a/clang/lib/StaticAnalyzer/Checkers/DeleteWithNonVirtualDtorChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp
similarity index 100%
rename from 
clang/lib/StaticAnalyzer/Checkers/DeleteWithNonVirtualDtorChecker.cpp
rename to clang/lib/StaticAnalyzer/Checkers/CXXDeleteChecker.cpp



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits