Googler | 68f2115 | 2021-11-30 11:45:33 +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_symbol_table.h" |
| 6 | |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 7 | #include <optional> |
| 8 | |
Luca Versari | c21d92f | 2022-05-25 00:56:30 -0700 | [diff] [blame] | 9 | #include "gtest/gtest.h" |
Dmitri Gribenko | a087d23 | 2023-07-10 08:03:46 -0700 | [diff] [blame] | 10 | #include "lifetime_annotations/lifetime.h" |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 11 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 12 | namespace clang { |
| 13 | namespace tidy { |
| 14 | namespace lifetimes { |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | TEST(LifetimeSymbolTableTest, Static) { |
| 18 | LifetimeSymbolTable table; |
| 19 | EXPECT_EQ(table.LookupName("static"), Lifetime::Static()); |
| 20 | EXPECT_EQ(table.LookupNameAndMaybeDeclare("static"), Lifetime::Static()); |
| 21 | } |
| 22 | |
| 23 | TEST(LifetimeSymbolTableTest, LookupName) { |
| 24 | LifetimeSymbolTable table; |
| 25 | |
| 26 | EXPECT_EQ(table.LookupName("a"), std::nullopt); |
| 27 | Lifetime a = table.LookupNameAndMaybeDeclare("a"); |
| 28 | EXPECT_EQ(table.LookupName("a"), a); |
| 29 | EXPECT_EQ(table.LookupNameAndMaybeDeclare("a"), a); |
| 30 | |
| 31 | EXPECT_EQ(table.LookupName("b"), std::nullopt); |
| 32 | Lifetime b = table.LookupNameAndMaybeDeclare("b"); |
| 33 | EXPECT_NE(a, b); |
| 34 | EXPECT_EQ(table.LookupName("b"), b); |
| 35 | EXPECT_EQ(table.LookupNameAndMaybeDeclare("b"), b); |
| 36 | } |
| 37 | |
Googler | 7055729 | 2021-11-30 11:46:02 +0000 | [diff] [blame] | 38 | TEST(LifetimeSymbolTableTest, LookupLifetime) { |
| 39 | LifetimeSymbolTable table; |
| 40 | |
| 41 | Lifetime a = table.LookupNameAndMaybeDeclare("a"); |
| 42 | Lifetime b = table.LookupNameAndMaybeDeclare("b"); |
| 43 | |
| 44 | EXPECT_EQ(table.LookupLifetime(a), "a"); |
| 45 | EXPECT_EQ(table.LookupLifetime(b), "b"); |
| 46 | } |
| 47 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 48 | TEST(LifetimeSymbolTableTest, RebindLifetime) { |
| 49 | LifetimeSymbolTable table; |
| 50 | |
| 51 | Lifetime a = table.LookupNameAndMaybeDeclare("a"); |
| 52 | Lifetime b = Lifetime::CreateVariable(); |
| 53 | |
| 54 | EXPECT_EQ(table.LookupLifetime(a), "a"); |
| 55 | |
| 56 | table.Rebind("a", b); |
| 57 | EXPECT_EQ(table.LookupName("a"), b); |
| 58 | EXPECT_EQ(table.LookupLifetime(a), std::nullopt); |
| 59 | EXPECT_EQ(table.LookupLifetime(b), "a"); |
| 60 | } |
| 61 | |
Googler | 352ec04 | 2021-11-30 11:46:23 +0000 | [diff] [blame] | 62 | TEST(LifetimeSymbolTableTest, LookupLifetimeAndMaybeDeclare) { |
| 63 | { |
| 64 | LifetimeSymbolTable table; |
| 65 | |
| 66 | table.LookupNameAndMaybeDeclare("a"); |
| 67 | table.LookupNameAndMaybeDeclare("c"); |
| 68 | |
| 69 | EXPECT_EQ(table.LookupLifetimeAndMaybeDeclare(Lifetime::CreateVariable()), |
| 70 | "b"); |
| 71 | EXPECT_EQ(table.LookupLifetimeAndMaybeDeclare(Lifetime::CreateVariable()), |
| 72 | "d"); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | LifetimeSymbolTable table; |
| 77 | for (int i = 0; i < 26; ++i) { |
| 78 | table.LookupLifetimeAndMaybeDeclare(Lifetime::CreateVariable()); |
| 79 | } |
| 80 | EXPECT_EQ(table.LookupLifetimeAndMaybeDeclare(Lifetime::CreateVariable()), |
| 81 | "aa"); |
| 82 | EXPECT_EQ(table.LookupLifetimeAndMaybeDeclare(Lifetime::CreateVariable()), |
| 83 | "ab"); |
| 84 | } |
| 85 | } |
| 86 | |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 87 | } // namespace |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 88 | } // namespace lifetimes |
| 89 | } // namespace tidy |
| 90 | } // namespace clang |