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 | |
| 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 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace lifetimes { |
| 18 | |
| 19 | // A set of `Object`s. |
| 20 | class ObjectSet { |
| 21 | public: |
Martin Brænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 22 | using const_iterator = llvm::SmallSet<const Object*, 2>::const_iterator; |
| 23 | using value_type = const Object*; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 24 | |
| 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ænne | b891677 | 2022-07-01 02:28:09 -0700 | [diff] [blame] | 33 | ObjectSet(std::initializer_list<const Object*> objects) { |
| 34 | for (const Object* object : objects) { |
Martin Brænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 35 | objects_.insert(object); |
Martin Brænne | b891677 | 2022-07-01 02:28:09 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 38 | |
| 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ænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 51 | bool Contains(const Object* object) const { |
| 52 | return objects_.contains(object); |
| 53 | } |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 54 | |
| 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ænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 58 | for (const Object* object : other) { |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 59 | 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 Versari | 53a2f58 | 2023-01-16 10:09:29 -0800 | [diff] [blame] | 74 | // 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 Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 84 | // Adds `object` to this object set. |
Martin Brænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 85 | void Add(const Object* object) { objects_.insert(object); } |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 86 | |
| 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ænne | 4d8cdfd | 2022-07-01 06:16:36 -0700 | [diff] [blame] | 103 | llvm::SmallSet<const Object*, 2> objects_; |
Luca Versari | 99fddff | 2022-05-25 10:22:32 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace lifetimes |
| 107 | } // namespace tidy |
| 108 | } // namespace clang |
| 109 | |
| 110 | #endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_SET_H_ |