Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [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 | #include "lifetime_annotations/lifetime.h" |
| 6 | |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 7 | #include "absl/strings/str_cat.h" |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 8 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 9 | namespace clang { |
| 10 | namespace tidy { |
| 11 | namespace lifetimes { |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 12 | |
| 13 | constexpr int INVALID_LIFETIME_ID_EMPTY = 0; |
| 14 | constexpr int INVALID_LIFETIME_ID_TOMBSTONE = 1; |
| 15 | constexpr int STATIC_LIFETIME_ID = -1; |
| 16 | constexpr int FIRST_VARIABLE_LIFETIME_ID = 2; |
| 17 | constexpr int FIRST_LOCAL_LIFETIME_ID = -2; |
| 18 | |
| 19 | std::atomic<int> Lifetime::next_variable_id_{FIRST_VARIABLE_LIFETIME_ID}; |
| 20 | |
| 21 | std::atomic<int> Lifetime::next_local_id_{FIRST_LOCAL_LIFETIME_ID}; |
| 22 | |
| 23 | Lifetime::Lifetime() : id_(INVALID_LIFETIME_ID_EMPTY) {} |
| 24 | |
| 25 | Lifetime Lifetime::CreateVariable() { return Lifetime(next_variable_id_++); } |
| 26 | |
| 27 | Lifetime Lifetime::Static() { return Lifetime(STATIC_LIFETIME_ID); } |
| 28 | |
| 29 | Lifetime Lifetime::CreateLocal() { return Lifetime(next_local_id_--); } |
| 30 | |
| 31 | bool Lifetime::IsVariable() const { |
| 32 | assert(IsValid()); |
| 33 | return id_ > 0; |
| 34 | } |
| 35 | |
| 36 | bool Lifetime::IsConstant() const { |
| 37 | assert(IsValid()); |
| 38 | return !IsVariable(); |
| 39 | } |
| 40 | |
| 41 | bool Lifetime::IsLocal() const { |
| 42 | assert(IsValid()); |
| 43 | return id_ <= FIRST_LOCAL_LIFETIME_ID; |
| 44 | } |
| 45 | |
| 46 | std::string Lifetime::DebugString() const { |
| 47 | assert(IsValid()); |
| 48 | |
| 49 | switch (id_) { |
| 50 | case INVALID_LIFETIME_ID_EMPTY: |
| 51 | return "INVALID_EMPTY"; |
| 52 | case INVALID_LIFETIME_ID_TOMBSTONE: |
| 53 | return "INVALID_TOMBSTONE"; |
| 54 | case STATIC_LIFETIME_ID: |
| 55 | return "'static"; |
| 56 | default: |
| 57 | if (id_ <= FIRST_LOCAL_LIFETIME_ID) { |
| 58 | return absl::StrCat("'local", -id_); |
| 59 | } else { |
| 60 | return absl::StrCat("'", id_); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Lifetime::Lifetime(int id) : id_(id) {} |
| 66 | |
| 67 | Lifetime Lifetime::InvalidEmpty() { |
| 68 | return Lifetime(INVALID_LIFETIME_ID_EMPTY); |
| 69 | } |
| 70 | |
| 71 | Lifetime Lifetime::InvalidTombstone() { |
| 72 | return Lifetime(INVALID_LIFETIME_ID_TOMBSTONE); |
| 73 | } |
| 74 | |
| 75 | bool Lifetime::IsValid() const { |
| 76 | return id_ != INVALID_LIFETIME_ID_EMPTY && |
| 77 | id_ != INVALID_LIFETIME_ID_TOMBSTONE; |
| 78 | } |
| 79 | |
| 80 | std::ostream& operator<<(std::ostream& os, Lifetime lifetime) { |
| 81 | return os << lifetime.DebugString(); |
| 82 | } |
| 83 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 84 | } // namespace lifetimes |
| 85 | } // namespace tidy |
| 86 | } // namespace clang |