clang 23.0.0git
UnsafeBufferUsage.h
Go to the documentation of this file.
1//===- UnsafeBufferUsage.h --------------------------------------*- C++ -*-===//
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#ifndef LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_UNSAFEBUFFERUSAGE_H
10#define LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_UNSAFEBUFFERUSAGE_H
11
15#include "llvm/ADT/iterator_range.h"
16#include <set>
17
18namespace clang::ssaf {
19
20/// An EntityPointerLevel represents a level of the declared pointer/array
21/// type of an entity. In the fully-expanded spelling of the declared type, a
22/// EntityPointerLevel is associated with a '*' (or a '[]`) in that declaration.
23///
24/// For example, for 'int *p[10];', there are two EntityPointerLevels. One
25/// is associated with 'int *[10]' of 'p' and the other is associated with 'int
26/// *' of 'p'.
27///
28/// An EntityPointerLevel can be identified by an EntityId and an unsigned
29/// integer indicating the pointer level: '(EntityId, PointerLevel)'. An
30/// EntityPointerLevel 'P' is valid iff
31/// - 'P.EntityId' has a pointer type with at least 'P.PointerLevel' levels
32/// (This implies 'P.PointerLevel > 0');
33/// - 'P.EntityId' identifies an lvalue object and 'P.PointerLevel == 0'.
34/// The latter case represents address-of expressions.
35///
36/// For the same example 'int *p[10];', the EntityPointerLevels below are valid:
37/// '(p, 1)' is associated with 'int *[10]' of 'p';
38/// '(p, 2)' is associated with 'int *' of 'p';
39/// '(p, 0)' represents '&p'.
40class EntityPointerLevel {
41 EntityId Entity;
42 unsigned PointerLevel;
43
46
47 EntityPointerLevel(EntityId Entity, unsigned PointerLevel)
48 : Entity(Entity), PointerLevel(PointerLevel) {}
49
50public:
51 EntityId getEntity() const { return Entity; }
52 unsigned getPointerLevel() const { return PointerLevel; }
53
54 bool operator==(const EntityPointerLevel &Other) const {
55 return Entity == Other.Entity && PointerLevel == Other.PointerLevel;
56 }
57
58 bool operator!=(const EntityPointerLevel &Other) const {
59 return !(*this == Other);
60 }
61
62 bool operator<(const EntityPointerLevel &Other) const {
63 return std::tie(Entity, PointerLevel) <
64 std::tie(Other.Entity, Other.PointerLevel);
65 }
66
67 // Comparator supporting partial comparison against EntityId:
68 struct Comparator {
69 using is_transparent = void;
70 bool operator()(const EntityPointerLevel &L,
71 const EntityPointerLevel &R) const {
72 return L < R;
73 }
74 bool operator()(const EntityId &L, const EntityPointerLevel &R) const {
75 return L < R.getEntity();
76 }
77 bool operator()(const EntityPointerLevel &L, const EntityId &R) const {
78 return L.getEntity() < R;
79 }
80 };
81};
82
84 std::set<EntityPointerLevel, EntityPointerLevel::Comparator>;
85
86/// An UnsafeBufferUsageEntitySummary is an immutable set of unsafe buffers, in
87/// the form of EntityPointerLevel.
88class UnsafeBufferUsageEntitySummary final : public EntitySummary {
89 const EntityPointerLevelSet UnsafeBuffers;
90
92
93 UnsafeBufferUsageEntitySummary(EntityPointerLevelSet &&UnsafeBuffers)
94 : EntitySummary(), UnsafeBuffers(std::move(UnsafeBuffers)) {}
95
96public:
97 using const_iterator = EntityPointerLevelSet::const_iterator;
98
99 const_iterator begin() const { return UnsafeBuffers.begin(); }
100 const_iterator end() const { return UnsafeBuffers.end(); }
101
103 return UnsafeBuffers.find(V);
104 }
105
106 llvm::iterator_range<const_iterator> getSubsetOf(EntityId Entity) const {
107 return llvm::make_range(UnsafeBuffers.equal_range(Entity));
108 }
109
110 /// \return the size of the set of EntityLevelPointers, which represents the
111 /// set of unsafe buffers
112 size_t getNumUnsafeBuffers() { return UnsafeBuffers.size(); }
113
114 SummaryName getSummaryName() const override {
115 return SummaryName{"UnsafeBufferUsage"};
116 };
117};
118} // namespace clang::ssaf
119
120#endif // LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_UNSAFEBUFFERUSAGE_H
#define V(N, I)
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
An EntityPointerLevel represents a level of the declared pointer/array type of an entity.
bool operator!=(const EntityPointerLevel &Other) const
bool operator==(const EntityPointerLevel &Other) const
bool operator<(const EntityPointerLevel &Other) const
Base class for analysis-specific summary data.
Uniquely identifies an analysis summary.
Definition SummaryName.h:22
EntityPointerLevelSet::const_iterator const_iterator
llvm::iterator_range< const_iterator > getSubsetOf(EntityId Entity) const
const_iterator find(const EntityPointerLevel &V) const
std::set< EntityPointerLevel, EntityPointerLevel::Comparator > EntityPointerLevelSet
@ Other
Other implicit parameter.
Definition Decl.h:1746
bool operator()(const EntityId &L, const EntityPointerLevel &R) const
bool operator()(const EntityPointerLevel &L, const EntityPointerLevel &R) const
bool operator()(const EntityPointerLevel &L, const EntityId &R) const