Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 1 | // 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 Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame^] | 8 | #include <cstddef> |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 9 | #include <initializer_list> |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame^] | 10 | #include <ostream> |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include "lifetime_analysis/object.h" |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 14 | #include "llvm/ADT/SmallSet.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace lifetimes { |
| 19 | |
| 20 | // A set of `Object`s. |
| 21 | class ObjectSet { |
| 22 | public: |
Martin Brænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 23 | using const_iterator = llvm::SmallSet<const Object*, 2>::const_iterator; |
| 24 | using value_type = const Object*; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 25 | |
| 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ænne | b891677 | 2022-07-01 02:28:09 -0700 | [diff] [blame] | 34 | ObjectSet(std::initializer_list<const Object*> objects) { |
| 35 | for (const Object* object : objects) { |
Martin Brænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 36 | objects_.insert(object); |
Martin Brænne | b891677 | 2022-07-01 02:28:09 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 39 | |
| 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ænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 52 | bool Contains(const Object* object) const { |
| 53 | return objects_.contains(object); |
| 54 | } |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 55 | |
| 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ænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 59 | for (const Object* object : other) { |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 60 | 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 Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 75 | // 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 Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 85 | // Adds `object` to this object set. |
Martin Brænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 86 | void Add(const Object* object) { objects_.insert(object); } |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 87 | |
| 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ænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 104 | llvm::SmallSet<const Object*, 2> objects_; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace lifetimes |
| 108 | } // namespace tidy |
| 109 | } // namespace clang |
| 110 | |
| 111 | #endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_SET_H_ |