[clang] [analyzer] Add cplusplus.DestroyedAsWrongType checker (PR #237801)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 13:46:16 PST 2026
================
@@ -0,0 +1,366 @@
+//===- DestroyedAsWrongTypeChecker.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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines a checker that finds storage reused by placement new for an object
+// of a different type and then destroyed or deleted as the original type
+// (C++ [basic.life]p11, [expr.delete]p3).
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ExprCXX.h"
+#include "clang/Analysis/CFG.h"
+#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/CheckerManager.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+#include "llvm/Support/FormatVariadic.h"
+#include <optional>
+
+using namespace clang;
+using namespace ento;
+
+namespace {
+struct WrongDestruction {
+ QualType Placed;
+ const CXXRecordDecl *DtorClass;
+ const CXXDeleteExpr *Delete;
+
+ bool operator==(const WrongDestruction &X) const {
+ return Placed == X.Placed && DtorClass == X.DtorClass && Delete == X.Delete;
+ }
+ void Profile(llvm::FoldingSetNodeID &ID) const {
+ ID.Add(Placed);
+ ID.AddPointer(DtorClass);
+ ID.AddPointer(Delete);
+ }
+
+ std::string getReportMessage(const PrintingPolicy &PP,
+ const NamedDecl *StdCaller) const {
+ std::string Caller;
+ if (StdCaller)
+ Caller =
+ llvm::formatv(" by '{0}'", StdCaller->getQualifiedNameAsString());
+ if (Delete)
+ return llvm::formatv(
+ "Deleting an object of type '{0}' through a pointer to '{1}'{2}",
+ Placed.getAsString(PP),
+ Delete->getDestroyedType().getUnqualifiedType().getAsString(PP),
+ Caller);
+ return llvm::formatv("Destructor of '{0}' is called{1} on storage that "
+ "holds an object of type '{2}'",
+ DtorClass->getQualifiedNameAsString(), Caller,
+ Placed.getAsString(PP));
+ }
+};
+} // namespace
+
+// Storage reused by placement new -> type of the new object.
+REGISTER_MAP_WITH_PROGRAMSTATE(PlacedObjectTypes, const MemRegion *, QualType)
+// Memory from a non-placement new, which holds an original object.
+REGISTER_SET_WITH_PROGRAMSTATE(NewAllocatedObjects, const MemRegion *)
----------------
NagyDonat wrote:
At first glance `NewAllocatedObjects` looks as if it means "new (i.e. not old) allocated objects" and not "objects allocated by `operator new`". I'd suggest renaming it to `ObjectsAllocatedByNew` where it is clear that `New` is used as an operator name and not just as an adjective.
https://github.com/llvm/llvm-project/pull/237801
More information about the cfe-commits
mailing list