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"
15#include <set>
16
17namespace clang::ssaf {
18
19/// An EntityPointerLevel represents a level of the declared pointer/array
20/// type of an entity. In the fully-expanded spelling of the declared type, a
21/// EntityPointerLevel is associated with a '*' (or a '[]`) in that declaration.
22///
23/// For example, for 'int *p[10];', there are two EntityPointerLevels. One
24/// is associated with 'int *[10]' of 'p' and the other is associated with 'int
25/// *' of 'p'.
26///
27/// An EntityPointerLevel can be identified by an EntityId and an unsigned
28/// integer indicating the pointer level: '(EntityId, PointerLevel)'.
29/// An EntityPointerLevel 'P' is valid iff 'P.EntityId' has a pointer type with
30/// at least 'P.PointerLevel' levels (This implies 'P.PointerLevel > 0').
31///
32/// For the same example 'int *p[10];', the EntityPointerLevels below are valid:
33/// - '(p, 2)' is associated with the 'int *' part of the declared type of 'p';
34/// - '(p, 1)' is associated with the 'int *[10]' part of the declared type of
35/// 'p'.
36class EntityPointerLevel {
37 EntityId Entity;
38 unsigned PointerLevel;
39
40 friend class EntityPointerLevelTranslator;
41 friend EntityPointerLevel buildEntityPointerLevel(EntityId, unsigned);
42
43 EntityPointerLevel(EntityId Entity, unsigned PointerLevel)
44 : Entity(Entity), PointerLevel(PointerLevel) {}
45
46public:
47 EntityId getEntity() const { return Entity; }
48 unsigned getPointerLevel() const { return PointerLevel; }
49
50 bool operator==(const EntityPointerLevel &Other) const {
51 return std::tie(Entity, PointerLevel) ==
52 std::tie(Other.Entity, Other.PointerLevel);
53 }
54
55 bool operator!=(const EntityPointerLevel &Other) const {
56 return !(*this == Other);
57 }
58
59 bool operator<(const EntityPointerLevel &Other) const {
60 return std::tie(Entity, PointerLevel) <
61 std::tie(Other.Entity, Other.PointerLevel);
62 }
63
64 /// Compares `EntityPointerLevel`s; additionally, partially compares
65 /// `EntityPointerLevel` with `EntityId`.
66 struct Comparator {
67 using is_transparent = void;
68 bool operator()(const EntityPointerLevel &L,
69 const EntityPointerLevel &R) const {
70 return L < R;
71 }
72 bool operator()(const EntityId &L, const EntityPointerLevel &R) const {
73 return L < R.getEntity();
74 }
75 bool operator()(const EntityPointerLevel &L, const EntityId &R) const {
76 return L.getEntity() < R;
77 }
78 };
79};
80
81using EntityPointerLevelSet =
82 std::set<EntityPointerLevel, EntityPointerLevel::Comparator>;
83
84/// Translate a pointer/array type expression 'E' to a (set of)
85/// EntityPointerLevel(s) associated with the declared type of the base address
86/// of `E`. If the base address of `E` is not associated with an entity, the
87/// translation result is an empty set.
88///
89/// \param E the pointer expression to be translated
90/// \param Ctx the AST context of `E`
91/// \param AddEntity the callback provided by the caller to convert EntityNames
92/// to EntityIds.
93llvm::Expected<EntityPointerLevelSet>
94translateEntityPointerLevel(const Expr *E, ASTContext &Ctx,
95 std::function<EntityId(EntityName EN)> AddEntity);
96
97EntityPointerLevel buildEntityPointerLevel(EntityId, unsigned);
98} // namespace clang::ssaf
99#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_ENTITYPOINTERLEVEL_ENTITYPOINTERLEVEL_H
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Uniquely identifies an entity in a program.
Definition EntityName.h:28
bool operator!=(const CommonEntityInfo &LHS, const CommonEntityInfo &RHS)
Definition Types.h:153
EntityPointerLevel buildEntityPointerLevel(EntityId, unsigned)
llvm::Expected< EntityPointerLevelSet > translateEntityPointerLevel(const Expr *E, ASTContext &Ctx, std::function< EntityId(EntityName EN)> AddEntity)
An EntityPointerLevel represents 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:1761
int const char * function
Definition c++config.h:31