1 //===-- TestType.cpp ------------------------------------------------------===// 2 // 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "gmock/gmock.h" 11 #include "gtest/gtest.h" 12 13 #include "lldb/Symbol/Type.h" 14 #include "lldb/lldb-enumerations.h" 15 #include "lldb/lldb-private-enumerations.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 using testing::ElementsAre; 20 using testing::Not; 21 22 TEST(Type, GetTypeScopeAndBasename) { 23 EXPECT_EQ(Type::GetTypeScopeAndBasename("int"), 24 (Type::ParsedName{eTypeClassAny, {}, "int"})); 25 EXPECT_EQ(Type::GetTypeScopeAndBasename("std::string"), 26 (Type::ParsedName{eTypeClassAny, {"std"}, "string"})); 27 EXPECT_EQ(Type::GetTypeScopeAndBasename("::std::string"), 28 (Type::ParsedName{eTypeClassAny, {"::", "std"}, "string"})); 29 EXPECT_EQ(Type::GetTypeScopeAndBasename("struct std::string"), 30 (Type::ParsedName{eTypeClassStruct, {"std"}, "string"})); 31 EXPECT_EQ(Type::GetTypeScopeAndBasename("std::set<int>"), 32 (Type::ParsedName{eTypeClassAny, {"std"}, "set<int>"})); 33 EXPECT_EQ( 34 Type::GetTypeScopeAndBasename("std::set<int, std::less<int>>"), 35 (Type::ParsedName{eTypeClassAny, {"std"}, "set<int, std::less<int>>"})); 36 EXPECT_EQ(Type::GetTypeScopeAndBasename("std::string::iterator"), 37 (Type::ParsedName{eTypeClassAny, {"std", "string"}, "iterator"})); 38 EXPECT_EQ(Type::GetTypeScopeAndBasename("std::set<int>::iterator"), 39 (Type::ParsedName{eTypeClassAny, {"std", "set<int>"}, "iterator"})); 40 EXPECT_EQ( 41 Type::GetTypeScopeAndBasename("std::set<int, std::less<int>>::iterator"), 42 (Type::ParsedName{ 43 eTypeClassAny, {"std", "set<int, std::less<int>>"}, "iterator"})); 44 EXPECT_EQ(Type::GetTypeScopeAndBasename( 45 "std::set<int, std::less<int>>::iterator<bool>"), 46 (Type::ParsedName{eTypeClassAny, 47 {"std", "set<int, std::less<int>>"}, 48 "iterator<bool>"})); 49 50 EXPECT_EQ(Type::GetTypeScopeAndBasename("std::"), std::nullopt); 51 EXPECT_EQ(Type::GetTypeScopeAndBasename("foo<::bar"), std::nullopt); 52 } 53 54 namespace { 55 MATCHER_P(Matches, pattern, "") { 56 TypeQuery query(pattern, TypeQueryOptions::e_none); 57 return query.ContextMatches(arg); 58 } 59 MATCHER_P(MatchesIgnoringModules, pattern, "") { 60 TypeQuery query(pattern, TypeQueryOptions::e_ignore_modules); 61 return query.ContextMatches(arg); 62 } 63 MATCHER_P(MatchesWithStrictNamespaces, pattern, "") { 64 TypeQuery query(pattern, TypeQueryOptions::e_strict_namespaces); 65 return query.ContextMatches(arg); 66 } 67 } // namespace 68 69 TEST(Type, TypeQueryFlags) { 70 TypeQuery q("foo", e_none); 71 auto get = [](const TypeQuery &q) -> std::vector<bool> { 72 return {q.GetFindOne(), q.GetExactMatch(), q.GetModuleSearch(), 73 q.GetIgnoreModules(), q.GetStrictNamespaces()}; 74 }; 75 EXPECT_THAT(get(q), ElementsAre(false, false, false, false, false)); 76 77 q.SetFindOne(true); 78 EXPECT_THAT(get(q), ElementsAre(true, false, false, false, false)); 79 80 q.SetIgnoreModules(true); 81 EXPECT_THAT(get(q), ElementsAre(true, false, false, true, false)); 82 83 q.SetStrictNamespaces(true); 84 EXPECT_THAT(get(q), ElementsAre(true, false, false, true, true)); 85 86 q.SetIgnoreModules(false); 87 EXPECT_THAT(get(q), ElementsAre(true, false, false, false, true)); 88 } 89 90 TEST(Type, CompilerContextPattern) { 91 auto make_module = [](llvm::StringRef name) { 92 return CompilerContext(CompilerContextKind::Module, ConstString(name)); 93 }; 94 auto make_class = [](llvm::StringRef name) { 95 return CompilerContext(CompilerContextKind::ClassOrStruct, 96 ConstString(name)); 97 }; 98 auto make_any_type = [](llvm::StringRef name) { 99 return CompilerContext(CompilerContextKind::AnyType, ConstString(name)); 100 }; 101 auto make_enum = [](llvm::StringRef name) { 102 return CompilerContext(CompilerContextKind::Enum, ConstString(name)); 103 }; 104 auto make_namespace = [](llvm::StringRef name) { 105 return CompilerContext(CompilerContextKind::Namespace, ConstString(name)); 106 }; 107 108 EXPECT_THAT( 109 (std::vector{make_module("A"), make_module("B"), make_class("C")}), 110 Matches( 111 std::vector{make_module("A"), make_module("B"), make_class("C")})); 112 EXPECT_THAT( 113 (std::vector{make_module("A"), make_module("B"), make_class("C")}), 114 Not(Matches(std::vector{make_class("C")}))); 115 EXPECT_THAT( 116 (std::vector{make_module("A"), make_module("B"), make_class("C")}), 117 MatchesIgnoringModules(std::vector{make_class("C")})); 118 EXPECT_THAT( 119 (std::vector{make_module("A"), make_module("B"), make_class("C")}), 120 MatchesIgnoringModules(std::vector{make_module("B"), make_class("C")})); 121 EXPECT_THAT( 122 (std::vector{make_module("A"), make_module("B"), make_class("C")}), 123 Not(MatchesIgnoringModules( 124 std::vector{make_module("A"), make_class("C")}))); 125 EXPECT_THAT((std::vector{make_module("A"), make_module("B"), make_enum("C")}), 126 Matches(std::vector{make_module("A"), make_module("B"), 127 make_any_type("C")})); 128 EXPECT_THAT( 129 (std::vector{make_module("A"), make_module("B"), make_class("C")}), 130 Matches( 131 std::vector{make_module("A"), make_module("B"), make_any_type("C")})); 132 EXPECT_THAT((std::vector{make_module("A"), make_module("B"), 133 make_namespace(""), make_class("C")}), 134 Matches(std::vector{make_module("A"), make_module("B"), 135 make_any_type("C")})); 136 EXPECT_THAT( 137 (std::vector{make_module("A"), make_module("B"), make_enum("C2")}), 138 Not(Matches(std::vector{make_module("A"), make_module("B"), 139 make_any_type("C")}))); 140 EXPECT_THAT((std::vector{make_class("C")}), 141 Matches(std::vector{make_class("C")})); 142 EXPECT_THAT((std::vector{make_namespace("NS"), make_class("C")}), 143 Not(Matches(std::vector{make_any_type("C")}))); 144 145 EXPECT_THAT((std::vector{make_namespace(""), make_class("C")}), 146 Matches(std::vector{make_class("C")})); 147 EXPECT_THAT((std::vector{make_namespace(""), make_class("C")}), 148 Not(MatchesWithStrictNamespaces(std::vector{make_class("C")}))); 149 EXPECT_THAT((std::vector{make_namespace(""), make_class("C")}), 150 Matches(std::vector{make_namespace(""), make_class("C")})); 151 EXPECT_THAT((std::vector{make_namespace(""), make_class("C")}), 152 MatchesWithStrictNamespaces( 153 std::vector{make_namespace(""), make_class("C")})); 154 EXPECT_THAT((std::vector{make_class("C")}), 155 Not(Matches(std::vector{make_namespace(""), make_class("C")}))); 156 EXPECT_THAT((std::vector{make_class("C")}), 157 Not(MatchesWithStrictNamespaces( 158 std::vector{make_namespace(""), make_class("C")}))); 159 EXPECT_THAT((std::vector{make_namespace(""), make_namespace("NS"), 160 make_namespace(""), make_class("C")}), 161 Matches(std::vector{make_namespace("NS"), make_class("C")})); 162 EXPECT_THAT( 163 (std::vector{make_namespace(""), make_namespace(""), make_namespace("NS"), 164 make_namespace(""), make_namespace(""), make_class("C")}), 165 Matches(std::vector{make_namespace("NS"), make_class("C")})); 166 EXPECT_THAT((std::vector{make_module("A"), make_namespace("NS"), 167 make_namespace(""), make_class("C")}), 168 MatchesIgnoringModules( 169 std::vector{make_namespace("NS"), make_class("C")})); 170 } 171