blob: 3da954f41500e0e6f434946ab7dee7df73198187 [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
8#include <initializer_list>
9#include <string>
10
11#include "lifetime_analysis/object.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/SmallSet.h"
14
15namespace clang {
16namespace tidy {
17namespace lifetimes {
18
19// A set of `Object`s.
20class ObjectSet {
21 public:
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070022 using const_iterator = llvm::SmallSet<const Object*, 2>::const_iterator;
23 using value_type = const Object*;
Luca Versari99fddff2022-05-25 10:22:32 -070024
25 ObjectSet() = default;
26
27 ObjectSet(const ObjectSet&) = default;
28 ObjectSet(ObjectSet&&) = default;
29 ObjectSet& operator=(const ObjectSet&) = default;
30 ObjectSet& operator=(ObjectSet&&) = default;
31
32 // Initializes the object set with `objects`.
Martin Brænneb8916772022-07-01 02:28:09 -070033 ObjectSet(std::initializer_list<const Object*> objects) {
34 for (const Object* object : objects) {
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070035 objects_.insert(object);
Martin Brænneb8916772022-07-01 02:28:09 -070036 }
37 }
Luca Versari99fddff2022-05-25 10:22:32 -070038
39 // Returns a human-readable string representation of the object set.
40 std::string DebugString() const;
41
42 const_iterator begin() const { return objects_.begin(); }
43
44 const_iterator end() const { return objects_.end(); }
45
46 bool empty() const { return objects_.empty(); }
47
48 size_t size() const { return objects_.size(); }
49
50 // Returns whether this set contains `object`.
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070051 bool Contains(const Object* object) const {
52 return objects_.contains(object);
53 }
Luca Versari99fddff2022-05-25 10:22:32 -070054
55 // Returns whether this set contains all objects in `other`, i.e. whether
56 // this set is a superset of `other`.
57 bool Contains(const ObjectSet& other) const {
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070058 for (const Object* object : other) {
Luca Versari99fddff2022-05-25 10:22:32 -070059 if (!Contains(object)) {
60 return false;
61 }
62 }
63 return true;
64 }
65
66 // Returns a `ObjectSet` containing the union of the pointees from this
67 // `ObjectSet` and `other`.
68 ObjectSet Union(const ObjectSet& other) const {
69 ObjectSet result = *this;
70 result.Add(other);
71 return result;
72 }
73
Luca Versari53a2f582023-01-16 10:09:29 -080074 // Returns a `ObjectSet` containing the intersection of the pointees from this
75 // `ObjectSet` and `other`.
76 ObjectSet Intersection(const ObjectSet& other) const {
77 ObjectSet result;
78 for (const Object* obj : *this) {
79 if (other.Contains(obj)) result.Add(obj);
80 }
81 return result;
82 }
83
Luca Versari99fddff2022-05-25 10:22:32 -070084 // Adds `object` to this object set.
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070085 void Add(const Object* object) { objects_.insert(object); }
Luca Versari99fddff2022-05-25 10:22:32 -070086
87 // Adds the `other` objects to this object set.
88 void Add(const ObjectSet& other) {
89 objects_.insert(other.objects_.begin(), other.objects_.end());
90 }
91
92 bool operator==(const ObjectSet& other) const {
93 return objects_ == other.objects_;
94 }
95 bool operator!=(const ObjectSet& other) const { return !(*this == other); }
96
97 private:
98 friend std::ostream& operator<<(std::ostream& os,
99 const ObjectSet& object_set) {
100 return os << object_set.DebugString();
101 }
102
Martin Brænne4d8cdfd2022-07-01 06:16:36 -0700103 llvm::SmallSet<const Object*, 2> objects_;
Luca Versari99fddff2022-05-25 10:22:32 -0700104};
105
106} // namespace lifetimes
107} // namespace tidy
108} // namespace clang
109
110#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_SET_H_