================ @@ -0,0 +1,119 @@ +//===- OperatorNewDeletePointersExtractor.cpp -----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Extractor implementation for extracting from user-provided operator +// new/delete overloadings: +// 1 return entities of operator new overloads; +// 2 the parameter (optionally the 2nd) of operator new overloads +// representing the pointer to a memory area to initialize the object at; +// 3 the first parameter of operator delete overloads representing the pointer +// to a memory block to deallocate or a null pointer; +// 4 the parameter (optionally the 2nd) of operator delete overloads +// representing the pointer used as the placement parameter in the matching +// placement new. +// +//===----------------------------------------------------------------------===// + +#include "../SSAFAnalysesCommon.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/Type.h" +#include "clang/Basic/OperatorKinds.h" +#include "clang/ScalableStaticAnalysis/Analyses/OperatorNewDelete/OperatorNewDeletePointers.h" +#include "clang/ScalableStaticAnalysis/Core/TUSummary/ExtractorRegistry.h" +#include "clang/ScalableStaticAnalysis/Core/TUSummary/TUSummaryExtractor.h" +#include "llvm/ADT/StringRef.h" +#include <memory> +#include <optional> +#include <vector> + +using namespace clang; +using namespace ssaf; + +namespace { + +class OperatorNewDeletePointersExtractor final : public TUSummaryExtractor { +public: + using TUSummaryExtractor::TUSummaryExtractor; + +private: + void HandleTranslationUnit(ASTContext &Ctx) override; + + std::unique_ptr<OperatorNewDeletePointersEntitySummary> + extractEntitySummary(const std::vector<const NamedDecl *> &Decls); +}; + +void OperatorNewDeletePointersExtractor::HandleTranslationUnit( + ASTContext &Ctx) { + extractAndAddSummaries( + *this, SummaryBuilder, Ctx, + [&](const std::vector<const NamedDecl *> &Decls) { + return extractEntitySummary(Decls); + }, + OperatorNewDeletePointersEntitySummary::Name); +} + +std::unique_ptr<OperatorNewDeletePointersEntitySummary> +OperatorNewDeletePointersExtractor::extractEntitySummary( + const std::vector<const NamedDecl *> &ContributorDecls) { + auto Summary = std::make_unique<OperatorNewDeletePointersEntitySummary>(); + + for (const NamedDecl *Decl : ContributorDecls) { + auto Matcher = [&Summary, this](const DynTypedNode &Node) { + const auto *FD = Node.get<FunctionDecl>(); + + if (!FD) + return; + + OverloadedOperatorKind OO = FD->getOverloadedOperator(); + + switch (OO) { + case OO_New: + case OO_Array_New: + // Extract case 1: + if (auto Id = addEntityForReturn(FD)) + Summary->Entities.insert(*Id); + break; + case OO_Delete: + case OO_Array_Delete: + // Extract case 3; ignore ill-formed ones (first param not a pointer). + if (!FD->getNumParams() || !hasPtrOrArrType(FD->getParamDecl(0))) + return; + if (auto Id = addEntity(FD->getParamDecl(0))) + Summary->Entities.insert(*Id); + break; ---------------- steakhal wrote:
BTW are you sure `return` is the right choice to bail? We are within the `ContributorDecls` loop. I was expecting a `continue` there instead. https://github.com/llvm/llvm-project/pull/206600 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
