clang 23.0.0git
EntityPointerLevel.h
Go to the documentation of this file.
1//===- EntityPointerLevel.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_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_ENTITYPOINTERLEVEL_ENTITYPOINTERLEVEL_H
10#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_ENTITYPOINTERLEVEL_ENTITYPOINTERLEVEL_H
11
12#include "clang/AST/Expr.h"
14#include <set>
15
16namespace clang::ssaf {
18
19/// An EntityPointerLevel is associated with a level of the declared
20/// pointer/array type of an entity. In the fully-expanded spelling of the
21/// declared type, a EntityPointerLevel is associated with a '*' (or a '[]`) in
22/// that declaration.
23///
24/// For example, for 'int *p[10];', there are two EntityPointerLevels.
25/// One is associated with 'int *[10]' of 'p' and the other is associated with
26/// 'int *' of 'p'.
27///
28/// An EntityPointerLevel can be identified by an EntityId and an unsigned
29/// integer indicating the pointer level: '(EntityId, PointerLevel)'.
30/// An EntityPointerLevel 'P' is valid iff 'P.EntityId' has a pointer type with
31/// at least 'P.PointerLevel' levels (This implies 'P.PointerLevel > 0').
32///
33/// For the same example 'int *p[10];', the EntityPointerLevels below are valid:
34/// - '(p, 2)' is associated with the 'int *' part of the declared type of 'p';
35/// - '(p, 1)' is associated with the 'int *[10]' part of the declared type of
36/// 'p'.
37class EntityPointerLevel {
38 EntityId Entity;
39 unsigned PointerLevel;
40
41 friend class EntityPointerLevelTranslator;
42 // For unittests:
43 friend EntityPointerLevel buildEntityPointerLevel(EntityId, unsigned);
44
45 explicit EntityPointerLevel(std::pair<EntityId, unsigned> Pair)
46 : Entity(Pair.first), PointerLevel(Pair.second) {}
47
48public:
49 EntityId getEntity() const { return Entity; }
50 unsigned getPointerLevel() const { return PointerLevel; }
51
52 bool operator==(const EntityPointerLevel &Other) const {
53 return std::tie(Entity, PointerLevel) ==
54 std::tie(Other.Entity, Other.PointerLevel);
55 }
56
57 bool operator!=(const EntityPointerLevel &Other) const {
58 return !(*this == Other);
59 }
60
61 bool operator<(const EntityPointerLevel &Other) const {
62 return std::tie(Entity, PointerLevel) <
63 std::tie(Other.Entity, Other.PointerLevel);
64 }
65
66 /// Compares `EntityPointerLevel`s; additionally, partially compares
67 /// `EntityPointerLevel` with `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
83using EntityPointerLevelSet =
84 std::set<EntityPointerLevel, EntityPointerLevel::Comparator>;
85
86/// Translate a pointer/array type expression 'E' to a (set of)
87/// EntityPointerLevel(s) associated with the declared type of the base address
88/// of `E`. If the base address of `E` is not associated with an entity, the
89/// translation result is an empty set.
90///
91/// \param E the pointer expression to be translated
92/// \param Ctx the AST context of `E`
93/// \param AddEntity the callback provided by the caller to convert EntityNames
94/// to EntityIds.
95llvm::Expected<EntityPointerLevelSet>
96translateEntityPointerLevel(const Expr *E, ASTContext &Ctx,
97 TUSummaryExtractor &Extractor);
98
99/// Creates a `EntityPointerLevel` from a pair of an EntityId and a pointer
100/// level:
101EntityPointerLevel buildEntityPointerLevel(EntityId, unsigned);
102
103/// Create an EntityPointerLevel (EPL) from a NamedDecl of a pointer/array type.
104///
105/// \param ND the NamedDecl of a pointer/array type.
106/// \param AddEntity the callback provided by the caller to convert EntityNames
107/// to EntityIds.
108/// \param IsFunRet true iff the created EPL is associated with the return type
109/// of a function entity.
110llvm::Expected<EntityPointerLevel>
111createEntityPointerLevel(const NamedDecl *ND, TUSummaryExtractor &Extractor,
112 bool IsFunRet = false);
113
114/// Creates a new EntityPointerLevel (EPL) from `E` by incrementing `E`'s
115/// pointer level.
116/// \return the EPL that is associated with the pointee (or array element) type
117/// of `E`'s associated pointer/array type of the same entity.
118EntityPointerLevel incrementPointerLevel(const EntityPointerLevel &E);
119} // namespace clang::ssaf
120
121#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_ENTITYPOINTERLEVEL_ENTITYPOINTERLEVEL_H
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
bool operator!=(const CommonEntityInfo &LHS, const CommonEntityInfo &RHS)
Definition Types.h:153
EntityPointerLevel incrementPointerLevel(const EntityPointerLevel &E)
An EntityPointerLevel is associated with a level of the declared pointer/array type of an entity.
bool operator==(const ValueType &a, const ValueType &b)
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
@ Other
Other implicit parameter.
Definition Decl.h:1763