[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / Symbol / TestType.cpp
blobe3bb2cf6e69e2a70a5bc624566386d077db108f7
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 //===----------------------------------------------------------------------===//
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
13 #include "lldb/Symbol/Type.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-private-enumerations.h"
17 using namespace lldb;
18 using namespace lldb_private;
19 using testing::ElementsAre;
20 using testing::Not;
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>"}));
50 EXPECT_EQ(Type::GetTypeScopeAndBasename("std::"), std::nullopt);
51 EXPECT_EQ(Type::GetTypeScopeAndBasename("foo<::bar"), std::nullopt);
54 namespace {
55 MATCHER_P(Matches, pattern, "") {
56 TypeQuery query(pattern, TypeQueryOptions::e_none);
57 return query.ContextMatches(arg);
59 MATCHER_P(MatchesIgnoringModules, pattern, "") {
60 TypeQuery query(pattern, TypeQueryOptions::e_ignore_modules);
61 return query.ContextMatches(arg);
63 MATCHER_P(MatchesWithStrictNamespaces, pattern, "") {
64 TypeQuery query(pattern, TypeQueryOptions::e_strict_namespaces);
65 return query.ContextMatches(arg);
67 } // namespace
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()};
75 EXPECT_THAT(get(q), ElementsAre(false, false, false, false, false));
77 q.SetFindOne(true);
78 EXPECT_THAT(get(q), ElementsAre(true, false, false, false, false));
80 q.SetIgnoreModules(true);
81 EXPECT_THAT(get(q), ElementsAre(true, false, false, true, false));
83 q.SetStrictNamespaces(true);
84 EXPECT_THAT(get(q), ElementsAre(true, false, false, true, true));
86 q.SetIgnoreModules(false);
87 EXPECT_THAT(get(q), ElementsAre(true, false, false, false, true));
90 TEST(Type, CompilerContextPattern) {
91 auto make_module = [](llvm::StringRef name) {
92 return CompilerContext(CompilerContextKind::Module, ConstString(name));
94 auto make_class = [](llvm::StringRef name) {
95 return CompilerContext(CompilerContextKind::ClassOrStruct,
96 ConstString(name));
98 auto make_any_type = [](llvm::StringRef name) {
99 return CompilerContext(CompilerContextKind::AnyType, ConstString(name));
101 auto make_enum = [](llvm::StringRef name) {
102 return CompilerContext(CompilerContextKind::Enum, ConstString(name));
104 auto make_namespace = [](llvm::StringRef name) {
105 return CompilerContext(CompilerContextKind::Namespace, ConstString(name));
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")})));
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")}));