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
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/ErrorHandling.h"
22
23using namespace clang;
24using namespace ssaf;
25
26namespace clang::ssaf {
36} // namespace clang::ssaf
37
40 const NamedDecl *Contributor, ASTContext &Ctx) {
41 std::set<const Expr *> UnsafePointers;
42
43 auto MatchAction = [&UnsafePointers, &Ctx](const DynTypedNode &Node) {
44 matchUnsafePointers(Node, Ctx, UnsafePointers);
45 };
46 findMatchesIn(Contributor, MatchAction);
47
48 EntityPointerLevelSet Results;
49
50 for (const Expr *Ptr : UnsafePointers) {
52 translateEntityPointerLevel(Ptr, Ctx, *this);
53
54 if (Translation) {
55 // Filter out those temporary invalid EntityPointerLevels associated
56 // with `&E` pointers. They need no transformation of entities:
57 auto FilteredTranslation = llvm::make_filter_range(
58 *Translation, [](const EntityPointerLevel &E) -> bool {
59 return E.getPointerLevel() > 0;
60 });
61 Results.insert(FilteredTranslation.begin(), FilteredTranslation.end());
62 continue;
63 }
64 return Translation.takeError();
65 }
66
67 return std::make_unique<UnsafeBufferUsageEntitySummary>(
68 UnsafeBufferUsageEntitySummary(std::move(Results)));
69}
70
72 ASTContext &Ctx) {
73 std::vector<const NamedDecl *> Contributors;
74
75 findContributors(Ctx, Contributors);
76 for (auto *CD : Contributors) {
77 auto EntitySummary = extractEntitySummary(CD, Ctx);
78
79 if (!EntitySummary)
80 llvm::reportFatalInternalError(EntitySummary.takeError());
81 assert(*EntitySummary);
82 if ((*EntitySummary)->empty())
83 continue;
84
85 auto ContributorId = addEntity(CD);
86
87 if (!ContributorId)
88 llvm::reportFatalInternalError(makeEntityNameErr(Ctx, CD));
89
90 [[maybe_unused]] auto [Ignored, InsertionSucceeded] =
91 SummaryBuilder.addSummary(*ContributorId, std::move(*EntitySummary));
92
93 assert(InsertionSucceeded && "duplicated contributor extraction");
94 }
95}
96
97// NOLINTNEXTLINE(misc-use-internal-linkage)
99
100static clang::ssaf::TUSummaryExtractorRegistry::Add<
103 "Extract 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:228
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
Base class for analysis-specific summary data.
TUSummaryExtractor(TUSummaryBuilder &Builder)
std::optional< EntityId > addEntity(const NamedDecl *D)
Creates EntityName from the Decl, registers the entity, and sets its linkage atomically.
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.
The JSON file list parser is used to communicate input to InstallAPI.
bool matchUnsafePointers(const DynTypedNode &N, ASTContext &Ctx, std::set< const Expr * > &UnsafePointers)