clang 23.0.0git
UnsafeBufferUsageExtractor.cpp
Go to the documentation of this file.
1//===- UnsafeBufferUsageExtractor.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
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorHandling.h"
24
25using namespace clang;
26using namespace ssaf;
27
28namespace clang::ssaf {
38} // namespace clang::ssaf
39
42 const NamedDecl *Contributor, ASTContext &Ctx) {
43 std::set<const Expr *> UnsafePointers;
44
45 auto MatchAction = [&UnsafePointers, &Ctx](const DynTypedNode &Node) {
46 matchUnsafePointers(Node, Ctx, UnsafePointers);
47 };
48 findMatchesIn(Contributor, MatchAction);
49
50 EntityPointerLevelSet Results;
51
52 for (const Expr *Ptr : UnsafePointers) {
54 translateEntityPointerLevel(Ptr, Ctx, [this](const EntityName &EN) {
55 return SummaryBuilder.addEntity(EN);
56 });
57
58 if (Translation) {
59 // Filter out those temporary invalid EntityPointerLevels associated
60 // with `&E` pointers. They need no transformation of entities:
61 auto FilteredTranslation = llvm::make_filter_range(
62 *Translation, [](const EntityPointerLevel &E) -> bool {
63 return E.getPointerLevel() > 0;
64 });
65 Results.insert(FilteredTranslation.begin(), FilteredTranslation.end());
66 continue;
67 }
68 return Translation.takeError();
69 }
70
71 return std::make_unique<UnsafeBufferUsageEntitySummary>(
72 UnsafeBufferUsageEntitySummary(std::move(Results)));
73}
74
76 ASTContext &Ctx) {
77 std::vector<const NamedDecl *> Contributors;
78
79 findContributors(Ctx, Contributors);
80 for (auto *CD : Contributors) {
81 auto EntitySummary = extractEntitySummary(CD, Ctx);
82
83 if (!EntitySummary)
84 llvm::reportFatalInternalError(EntitySummary.takeError());
85 assert(*EntitySummary);
86 if ((*EntitySummary)->empty())
87 continue;
88
89 auto ContributorName = getEntityName(CD);
90
91 if (!ContributorName)
92 llvm::reportFatalInternalError(makeEntityNameErr(Ctx, CD));
93
94 [[maybe_unused]] auto [Ignored, InsertionSucceeded] =
95 SummaryBuilder.addSummary(SummaryBuilder.addEntity(*ContributorName),
96 std::move(*EntitySummary));
97
98 assert(InsertionSucceeded && "duplicated contributor extraction");
99 }
100}
101
102// NOLINTNEXTLINE(misc-use-internal-linkage)
104
105static clang::ssaf::TUSummaryExtractorRegistry::Add<
108 "The TUSummaryExtractor for unsafe buffer pointers");
Defines the clang::ASTContext interface.
static TUSummaryExtractorRegistry::Add< CallGraphExtractor > RegisterExtractor(CallGraphSummary::Name, "Extracts static call-graph information")
volatile int UnsafeBufferUsageTUSummaryExtractorAnchorSource
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
A dynamically typed AST node container.
This represents one expression.
Definition Expr.h:112
This represents a decl that may have a name.
Definition Decl.h:274
Uniquely identifies an entity in a program.
Definition EntityName.h:28
Base class for analysis-specific summary data.
TUSummaryExtractor(TUSummaryBuilder &Builder)
An UnsafeBufferUsageEntitySummary contains a set of EntityPointerLevels extracted from unsafe buffer ...
static constexpr llvm::StringLiteral Name
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Expected< std::unique_ptr< UnsafeBufferUsageEntitySummary > > extractEntitySummary(const NamedDecl *Contributor, ASTContext &Ctx)
void findContributors(ASTContext &Ctx, std::vector< const NamedDecl * > &Contributors)
Find all contributors in an AST.
llvm::Error makeEntityNameErr(clang::ASTContext &Ctx, const clang::NamedDecl *D)
void findMatchesIn(const NamedDecl *Contributor, llvm::function_ref< void(const DynTypedNode &)> MatchActionRef)
Perform "MatchAction" on each Stmt and Decl belonging to the Contributor.
std::optional< EntityName > getEntityName(const Decl *D)
Maps a declaration to an EntityName.
The JSON file list parser is used to communicate input to InstallAPI.
bool matchUnsafePointers(const DynTypedNode &N, ASTContext &Ctx, std::set< const Expr * > &UnsafePointers)