blob: 61d3922a52294df9b481c61736d97a59b3306ede [file] [log] [blame]
Luca Versari99fddff2022-05-25 10:22:32 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_SET_H_
6#define DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_SET_H_
7
Dmitri Gribenkoa087d232023-07-10 08:03:46 -07008#include <cstddef>
Luca Versari99fddff2022-05-25 10:22:32 -07009#include <initializer_list>
Dmitri Gribenkoa087d232023-07-10 08:03:46 -070010#include <ostream>
Luca Versari99fddff2022-05-25 10:22:32 -070011#include <string>
12
13#include "lifetime_analysis/object.h"
Luca Versari99fddff2022-05-25 10:22:32 -070014#include "llvm/ADT/SmallSet.h"
15
16namespace clang {
17namespace tidy {
18namespace lifetimes {
19
20// A set of `Object`s.
21class ObjectSet {
22 public:
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070023 using const_iterator = llvm::SmallSet<const Object*, 2>::const_iterator;
24 using value_type = const Object*;
Luca Versari99fddff2022-05-25 10:22:32 -070025
26 ObjectSet() = default;
27
28 ObjectSet(const ObjectSet&) = default;
29 ObjectSet(ObjectSet&&) = default;
30 ObjectSet& operator=(const ObjectSet&) = default;
31 ObjectSet& operator=(ObjectSet&&) = default;
32
33 // Initializes the object set with `objects`.
Martin Brænneb8916772022-07-01 02:28:09 -070034 ObjectSet(std::initializer_list<const Object*> objects) {
35 for (const Object* object : objects) {
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070036 objects_.insert(object);
Martin Brænneb8916772022-07-01 02:28:09 -070037 }
38 }
Luca Versari99fddff2022-05-25 10:22:32 -070039
40 // Returns a human-readable string representation of the object set.
41 std::string DebugString() const;
42
43 const_iterator begin() const { return objects_.begin(); }
44
45 const_iterator end() const { return objects_.end(); }
46
47 bool empty() const { return objects_.empty(); }
48
49 size_t size() const { return objects_.size(); }
50
51 // Returns whether this set contains `object`.
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070052 bool Contains(const Object* object) const {
53 return objects_.contains(object);
54 }
Luca Versari99fddff2022-05-25 10:22:32 -070055
56 // Returns whether this set contains all objects in `other`, i.e. whether
57 // this set is a superset of `other`.
58 bool Contains(const ObjectSet& other) const {
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070059 for (const Object* object : other) {
Luca Versari99fddff2022-05-25 10:22:32 -070060 if (!Contains(object)) {
61 return false;
62 }
63 }
64 return true;
65 }
66
67 // Returns a `ObjectSet` containing the union of the pointees from this
68 // `ObjectSet` and `other`.
69 ObjectSet Union(const ObjectSet& other) const {
70 ObjectSet result = *this;
71 result.Add(other);
72 return result;
73 }
74
Luca Versari53a2f582023-01-16 10:09:29 -080075 // Returns a `ObjectSet` containing the intersection of the pointees from this
76 // `ObjectSet` and `other`.
77 ObjectSet Intersection(const ObjectSet& other) const {
78 ObjectSet result;
79 for (const Object* obj : *this) {
80 if (other.Contains(obj)) result.Add(obj);
81 }
82 return result;
83 }
84
Luca Versari99fddff2022-05-25 10:22:32 -070085 // Adds `object` to this object set.
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070086 void Add(const Object* object) { objects_.insert(object); }
Luca Versari99fddff2022-05-25 10:22:32 -070087
88 // Adds the `other` objects to this object set.
89 void Add(const ObjectSet& other) {
90 objects_.insert(other.objects_.begin(), other.objects_.end());
91 }
92
93 bool operator==(const ObjectSet& other) const {
94 return objects_ == other.objects_;
95 }
96 bool operator!=(const ObjectSet& other) const { return !(*this == other); }
97
98 private:
99 friend std::ostream& operator<<(std::ostream& os,
100 const ObjectSet& object_set) {
101 return os << object_set.DebugString();
102 }
103
Martin Brænne4d8cdfd2022-07-01 06:16:36 -0700104 llvm::SmallSet<const Object*, 2> objects_;
Luca Versari99fddff2022-05-25 10:22:32 -0700105};
106
107} // namespace lifetimes
108} // namespace tidy
109} // namespace clang
110
111#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_SET_H_