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 "gtest/gtest.h" |
11 | |
12 | #include "lldb/Symbol/Type.h" |
13 | |
14 | using namespace lldb; |
15 | using namespace lldb_private; |
16 | |
17 | namespace { |
18 | void TestGetTypeScopeAndBasenameHelper(const char *full_type, |
19 | bool expected_is_scoped, |
20 | const char *expected_scope, |
21 | const char *expected_name) { |
22 | llvm::StringRef scope, name; |
23 | lldb::TypeClass type_class; |
24 | bool is_scoped = |
25 | Type::GetTypeScopeAndBasename(name: full_type, scope, basename&: name, type_class); |
26 | EXPECT_EQ(is_scoped, expected_is_scoped); |
27 | if (expected_is_scoped) { |
28 | EXPECT_EQ(scope, expected_scope); |
29 | EXPECT_EQ(name, expected_name); |
30 | } |
31 | } |
32 | } |
33 | |
34 | TEST(Type, GetTypeScopeAndBasename) { |
35 | TestGetTypeScopeAndBasenameHelper(full_type: "int" , expected_is_scoped: false, expected_scope: "" , expected_name: "" ); |
36 | TestGetTypeScopeAndBasenameHelper(full_type: "std::string" , expected_is_scoped: true, expected_scope: "std::" , expected_name: "string" ); |
37 | TestGetTypeScopeAndBasenameHelper(full_type: "std::set<int>" , expected_is_scoped: true, expected_scope: "std::" , expected_name: "set<int>" ); |
38 | TestGetTypeScopeAndBasenameHelper(full_type: "std::set<int, std::less<int>>" , expected_is_scoped: true, |
39 | expected_scope: "std::" , expected_name: "set<int, std::less<int>>" ); |
40 | TestGetTypeScopeAndBasenameHelper(full_type: "std::string::iterator" , expected_is_scoped: true, |
41 | expected_scope: "std::string::" , expected_name: "iterator" ); |
42 | TestGetTypeScopeAndBasenameHelper(full_type: "std::set<int>::iterator" , expected_is_scoped: true, |
43 | expected_scope: "std::set<int>::" , expected_name: "iterator" ); |
44 | TestGetTypeScopeAndBasenameHelper( |
45 | full_type: "std::set<int, std::less<int>>::iterator" , expected_is_scoped: true, |
46 | expected_scope: "std::set<int, std::less<int>>::" , expected_name: "iterator" ); |
47 | TestGetTypeScopeAndBasenameHelper( |
48 | full_type: "std::set<int, std::less<int>>::iterator<bool>" , expected_is_scoped: true, |
49 | expected_scope: "std::set<int, std::less<int>>::" , expected_name: "iterator<bool>" ); |
50 | } |
51 | |
52 | TEST(Type, CompilerContextPattern) { |
53 | std::vector<CompilerContext> mms = { |
54 | {CompilerContextKind::Module, ConstString("A" )}, |
55 | {CompilerContextKind::Module, ConstString("B" )}, |
56 | {CompilerContextKind::Struct, ConstString("S" )}}; |
57 | EXPECT_TRUE(contextMatches(mms, mms)); |
58 | std::vector<CompilerContext> mmc = { |
59 | {CompilerContextKind::Module, ConstString("A" )}, |
60 | {CompilerContextKind::Module, ConstString("B" )}, |
61 | {CompilerContextKind::Class, ConstString("S" )}}; |
62 | EXPECT_FALSE(contextMatches(mms, mmc)); |
63 | std::vector<CompilerContext> ms = { |
64 | {CompilerContextKind::Module, ConstString("A" )}, |
65 | {CompilerContextKind::Struct, ConstString("S" )}}; |
66 | std::vector<CompilerContext> mas = { |
67 | {CompilerContextKind::Module, ConstString("A" )}, |
68 | {CompilerContextKind::AnyModule, ConstString("*" )}, |
69 | {CompilerContextKind::Struct, ConstString("S" )}}; |
70 | EXPECT_TRUE(contextMatches(mms, mas)); |
71 | EXPECT_TRUE(contextMatches(ms, mas)); |
72 | EXPECT_FALSE(contextMatches(mas, ms)); |
73 | std::vector<CompilerContext> mmms = { |
74 | {CompilerContextKind::Module, ConstString("A" )}, |
75 | {CompilerContextKind::Module, ConstString("B" )}, |
76 | {CompilerContextKind::Module, ConstString("C" )}, |
77 | {CompilerContextKind::Struct, ConstString("S" )}}; |
78 | EXPECT_TRUE(contextMatches(mmms, mas)); |
79 | std::vector<CompilerContext> mme = { |
80 | {CompilerContextKind::Module, ConstString("A" )}, |
81 | {CompilerContextKind::Module, ConstString("B" )}, |
82 | {CompilerContextKind::Enum, ConstString("S" )}}; |
83 | std::vector<CompilerContext> mma = { |
84 | {CompilerContextKind::Module, ConstString("A" )}, |
85 | {CompilerContextKind::Module, ConstString("B" )}, |
86 | {CompilerContextKind::AnyType, ConstString("S" )}}; |
87 | EXPECT_TRUE(contextMatches(mme, mma)); |
88 | EXPECT_TRUE(contextMatches(mms, mma)); |
89 | std::vector<CompilerContext> mme2 = { |
90 | {CompilerContextKind::Module, ConstString("A" )}, |
91 | {CompilerContextKind::Module, ConstString("B" )}, |
92 | {CompilerContextKind::Enum, ConstString("S2" )}}; |
93 | EXPECT_FALSE(contextMatches(mme2, mma)); |
94 | } |
95 | |