Remove value semantics from Object.
PiperOrigin-RevId: 458455243
diff --git a/lifetime_analysis/BUILD b/lifetime_analysis/BUILD
index 79d32d1..6c90377 100644
--- a/lifetime_analysis/BUILD
+++ b/lifetime_analysis/BUILD
@@ -99,6 +99,7 @@
hdrs = ["object.h"],
deps = [
"@absl//absl/strings",
+ "@absl//absl/strings:str_format",
"//lifetime_annotations:lifetime",
"@llvm-project//clang:ast",
"@llvm-project//llvm:Support",
diff --git a/lifetime_analysis/object.cc b/lifetime_analysis/object.cc
index 251f951..538ffc7 100644
--- a/lifetime_analysis/object.cc
+++ b/lifetime_analysis/object.cc
@@ -7,6 +7,7 @@
#include <string>
#include "absl/strings/str_cat.h"
+#include "absl/strings/str_format.h"
#include "lifetime_annotations/lifetime.h"
#include "clang/AST/Decl.h"
@@ -14,16 +15,8 @@
namespace tidy {
namespace lifetimes {
-constexpr int INVALID_OBJECT_ID_EMPTY = 0;
-constexpr int INVALID_OBJECT_ID_TOMBSTONE = 1;
-constexpr int FIRST_OBJECT_ID = 2;
-
-std::atomic<int> Object::next_id_{FIRST_OBJECT_ID};
-
-Object::Object() : id_(INVALID_OBJECT_ID_EMPTY) {}
-
Object::Object(Lifetime lifetime, clang::QualType type)
- : Object(next_id_++, lifetime, type) {
+ : lifetime_(lifetime), type_(type), func_(nullptr) {
assert(!type.isNull());
}
@@ -33,36 +26,11 @@
}
std::string Object::DebugString() const {
- assert(IsValid());
-
- switch (id_) {
- case INVALID_OBJECT_ID_EMPTY:
- return "INVALID_EMPTY";
- case INVALID_OBJECT_ID_TOMBSTONE:
- return "INVALID_TOMBSTONE";
- default: {
- std::string result = absl::StrCat("p", id_, " ", lifetime_.DebugString());
- if (!type_.isNull()) {
- absl::StrAppend(&result, " (", type_.getAsString(), ")");
- }
- return result;
- }
+ std::string result = absl::StrFormat("p%p %s", this, lifetime_.DebugString());
+ if (!type_.isNull()) {
+ absl::StrAppend(&result, " (", type_.getAsString(), ")");
}
-}
-
-Object::Object(int id, Lifetime lifetime, clang::QualType type)
- : id_(id), lifetime_(lifetime), type_(type), func_(nullptr) {}
-
-Object Object::InvalidEmpty() {
- return Object(INVALID_OBJECT_ID_EMPTY, Lifetime(), clang::QualType());
-}
-
-Object Object::InvalidTombstone() {
- return Object(INVALID_OBJECT_ID_TOMBSTONE, Lifetime(), clang::QualType());
-}
-
-bool Object::IsValid() const {
- return id_ != INVALID_OBJECT_ID_EMPTY && id_ != INVALID_OBJECT_ID_TOMBSTONE;
+ return result;
}
std::ostream& operator<<(std::ostream& os, Object object) {
diff --git a/lifetime_analysis/object.h b/lifetime_analysis/object.h
index 84ed8ab..57685e2 100644
--- a/lifetime_analysis/object.h
+++ b/lifetime_analysis/object.h
@@ -22,15 +22,8 @@
// lifetime, but two equal objects always have the same lifetime.
class Object {
public:
- // Creates an invalid object.
- //
- // This is provided because containers need default constructors. It is not
- // legal to perform any operations on an invalid object except to copy or
- // delete it.
- //
- // Use one of the ObjectRepository::CreateObject...() functions to create a
- // valid object (or, in tests, use one of the constructors below).
- Object();
+ Object(Object&&) = delete;
+ Object& operator=(Object&&) = delete;
// Creates an object with the given lifetime and type.
// This constructor should only be used in tests. Outside of tests, use
@@ -56,26 +49,10 @@
// Returns the function that this object represents, if any.
const clang::FunctionDecl* GetFunc() const { return func_; }
- bool operator==(Object other) const { return id_ == other.id_; }
-
- bool operator!=(Object other) const { return !(*this == other); }
-
private:
- Object(int id, Lifetime lifetime, clang::QualType type);
-
- bool IsValid() const;
-
- static Object InvalidEmpty();
- static Object InvalidTombstone();
-
- friend class llvm::DenseMapInfo<Object>;
- friend class std::less<Object>;
-
- int id_;
Lifetime lifetime_;
clang::QualType type_;
const clang::FunctionDecl* func_;
- static std::atomic<int> next_id_;
};
std::ostream& operator<<(std::ostream& os, Object object);
@@ -84,40 +61,4 @@
} // namespace tidy
} // namespace clang
-namespace llvm {
-
-template <>
-struct DenseMapInfo<clang::tidy::lifetimes::Object> {
- static clang::tidy::lifetimes::Object getEmptyKey() {
- return clang::tidy::lifetimes::Object::InvalidEmpty();
- }
-
- static clang::tidy::lifetimes::Object getTombstoneKey() {
- return clang::tidy::lifetimes::Object::InvalidTombstone();
- }
-
- static unsigned getHashValue(clang::tidy::lifetimes::Object object) {
- return llvm::hash_value(object.id_);
- }
-
- static bool isEqual(clang::tidy::lifetimes::Object lhs,
- clang::tidy::lifetimes::Object rhs) {
- return lhs == rhs;
- }
-};
-
-} // namespace llvm
-
-namespace std {
-
-template <>
-struct less<clang::tidy::lifetimes::Object> {
- bool operator()(const clang::tidy::lifetimes::Object& p1,
- const clang::tidy::lifetimes::Object& p2) const {
- return p1.id_ < p2.id_;
- }
-};
-
-} // namespace std
-
#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_OBJECT_H_