clang 23.0.0git
OperatorNewDeletePointersExtractor.cpp
Go to the documentation of this file.
1//===- OperatorNewDeletePointersExtractor.cpp -----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Extractor implementation for extracting from user-provided operator
10// new/delete overloadings:
11// 1 return entities of operator new overloads;
12// 2 the parameter (optionally the 2nd) of operator new overloads
13// representing the pointer to a memory area to initialize the object at;
14// 3 the first parameter of operator delete overloads representing the pointer
15// to a memory block to deallocate or a null pointer;
16// 4 the parameter (optionally the 2nd) of operator delete overloads
17// representing the pointer used as the placement parameter in the matching
18// placement new.
19//
20//===----------------------------------------------------------------------===//
21
24#include "clang/AST/Decl.h"
25#include "clang/AST/DeclCXX.h"
26#include "clang/AST/Type.h"
31#include "llvm/ADT/StringRef.h"
32#include <memory>
33#include <optional>
34#include <vector>
35
36using namespace clang;
37using namespace ssaf;
38
39namespace {
40
41class OperatorNewDeletePointersExtractor final : public TUSummaryExtractor {
42public:
44
45private:
46 void HandleTranslationUnit(ASTContext &Ctx) override;
47
48 std::unique_ptr<OperatorNewDeletePointersEntitySummary>
49 extractEntitySummary(const std::vector<const NamedDecl *> &Decls);
50};
51
52void OperatorNewDeletePointersExtractor::HandleTranslationUnit(
53 ASTContext &Ctx) {
55 *this, SummaryBuilder, Ctx,
56 [&](const std::vector<const NamedDecl *> &Decls) {
57 return extractEntitySummary(Decls);
58 },
60}
61
62std::unique_ptr<OperatorNewDeletePointersEntitySummary>
63OperatorNewDeletePointersExtractor::extractEntitySummary(
64 const std::vector<const NamedDecl *> &ContributorDecls) {
65 auto Summary = std::make_unique<OperatorNewDeletePointersEntitySummary>();
66 auto Matcher = [&Summary, this](const DynTypedNode &Node) {
67 const auto *FD = Node.get<FunctionDecl>();
68
69 if (!FD)
70 return;
71
72 OverloadedOperatorKind OO = FD->getOverloadedOperator();
73
74 switch (OO) {
75 case OO_New:
76 case OO_Array_New:
77 // Extract case 1:
78 if (auto Id = addEntityForReturn(FD))
79 Summary->Entities.insert(*Id);
80 break;
81 case OO_Delete:
82 case OO_Array_Delete:
83 // Extract case 3; ignore ill-formed ones (first param not a pointer).
84 if (!FD->getNumParams() || !hasPtrOrArrType(FD->getParamDecl(0)))
85 return;
86 if (auto Id = addEntity(FD->getParamDecl(0)))
87 Summary->Entities.insert(*Id);
88 break;
89 default:
90 return;
91 };
92 // Extract case 2 & 4: only `operator new(size_t, void*)` and
93 // `operator delete(void*, void*)` are standard-defined with a void* 2nd
94 // param; for user-defined 3+ param overloads the 2nd param type is
95 // unconstrained, so we conservatively skip them.
96 if (FD->getNumParams() == 2 && hasPtrOrArrType(FD->getParamDecl(1))) {
97 if (auto Id = addEntity(FD->getParamDecl(1)))
98 Summary->Entities.insert(*Id);
99 }
100 };
101
102 for (const NamedDecl *Decl : ContributorDecls)
103 findMatchesIn(Decl, Matcher);
104 return Summary;
105}
106
107} // namespace
108
109namespace clang::ssaf {
110// NOLINTNEXTLINE(misc-use-internal-linkage)
112} // namespace clang::ssaf
113
114static TUSummaryExtractorRegistry::Add<OperatorNewDeletePointersExtractor>
117 "Extract pointer entities in operator new/delete overloads that must "
118 "have a 'void*' type");
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines an enumeration for C++ overloaded operators.
static TUSummaryExtractorRegistry::Add< OperatorNewDeletePointersExtractor > RegisterOperatorNewDeletePointersExtractor(OperatorNewDeletePointersEntitySummary::Name, "Extract pointer entities in operator new/delete overloads that must " "have a 'void*' type")
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
TUSummaryExtractor(TUSummaryBuilder &Builder)
DynTypedNode DynTypedNode
void extractAndAddSummaries(TUSummaryExtractor &Extractor, TUSummaryBuilder &Builder, ASTContext &Ctx, ExtractorFnT ExtractFn, llvm::StringRef ExtractorName="")
The standard contributor-summary extraction procedure:
bool hasPtrOrArrType(const Expr *E)
void findMatchesIn(const NamedDecl *Contributor, llvm::function_ref< void(const DynTypedNode &)> MatchActionRef)
Perform "MatchAction" on each Stmt and Decl belonging to the Contributor.
volatile int OperatorNewDeletePointersExtractorAnchorSource
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.