]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/Symbol/TestClangASTContext.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / unittests / Symbol / TestClangASTContext.cpp
1 //===-- TestClangASTContext.cpp ---------------------------------------*- C++ -*-===//
2
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "gtest/gtest.h"
12
13 #include "lldb/Host/HostInfo.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/ClangUtil.h"
16 #include "lldb/Symbol/Declaration.h"
17 #include "lldb/Symbol/GoASTContext.h"
18
19 using namespace clang;
20 using namespace lldb;
21 using namespace lldb_private;
22
23 class TestClangASTContext : public testing::Test
24 {
25 public:
26     static void
27     SetUpTestCase()
28     {
29         HostInfo::Initialize();
30     }
31
32     static void
33     TearDownTestCase()
34     {
35         HostInfo::Terminate();
36     }
37
38     virtual void
39     SetUp() override
40     {
41         std::string triple = HostInfo::GetTargetTriple();
42         m_ast.reset(new ClangASTContext(triple.c_str()));
43     }
44
45     virtual void
46     TearDown() override
47     {
48         m_ast.reset();
49     }
50
51 protected:
52     std::unique_ptr<ClangASTContext> m_ast;
53
54     QualType
55     GetBasicQualType(BasicType type) const
56     {
57         return ClangUtil::GetQualType(m_ast->GetBasicTypeFromAST(type));
58     }
59
60     QualType
61     GetBasicQualType(const char *name) const
62     {
63         return ClangUtil::GetQualType(m_ast->GetBuiltinTypeByName(ConstString(name)));
64     }
65 };
66
67 TEST_F(TestClangASTContext, TestGetBasicTypeFromEnum)
68 {
69     clang::ASTContext *context = m_ast->getASTContext();
70
71     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeBool), context->BoolTy));
72     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeChar), context->CharTy));
73     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeChar16), context->Char16Ty));
74     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeChar32), context->Char32Ty));
75     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeDouble), context->DoubleTy));
76     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeDoubleComplex), context->DoubleComplexTy));
77     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeFloat), context->FloatTy));
78     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeFloatComplex), context->FloatComplexTy));
79     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeHalf), context->HalfTy));
80     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeInt), context->IntTy));
81     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeInt128), context->Int128Ty));
82     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeLong), context->LongTy));
83     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeLongDouble), context->LongDoubleTy));
84     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeLongDoubleComplex), context->LongDoubleComplexTy));
85     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeLongLong), context->LongLongTy));
86     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeNullPtr), context->NullPtrTy));
87     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeObjCClass), context->getObjCClassType()));
88     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeObjCID), context->getObjCIdType()));
89     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeObjCSel), context->getObjCSelType()));
90     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeShort), context->ShortTy));
91     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeSignedChar), context->SignedCharTy));
92     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeUnsignedChar), context->UnsignedCharTy));
93     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeUnsignedInt), context->UnsignedIntTy));
94     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeUnsignedInt128), context->UnsignedInt128Ty));
95     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeUnsignedLong), context->UnsignedLongTy));
96     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeUnsignedLongLong), context->UnsignedLongLongTy));
97     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeUnsignedShort), context->UnsignedShortTy));
98     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeVoid), context->VoidTy));
99     EXPECT_TRUE(context->hasSameType(GetBasicQualType(eBasicTypeWChar), context->WCharTy));
100 }
101
102 TEST_F(TestClangASTContext, TestGetBasicTypeFromName)
103 {
104     EXPECT_EQ(GetBasicQualType(eBasicTypeChar), GetBasicQualType("char"));
105     EXPECT_EQ(GetBasicQualType(eBasicTypeSignedChar), GetBasicQualType("signed char"));
106     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedChar), GetBasicQualType("unsigned char"));
107     EXPECT_EQ(GetBasicQualType(eBasicTypeWChar), GetBasicQualType("wchar_t"));
108     EXPECT_EQ(GetBasicQualType(eBasicTypeSignedWChar), GetBasicQualType("signed wchar_t"));
109     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedWChar), GetBasicQualType("unsigned wchar_t"));
110     EXPECT_EQ(GetBasicQualType(eBasicTypeShort), GetBasicQualType("short"));
111     EXPECT_EQ(GetBasicQualType(eBasicTypeShort), GetBasicQualType("short int"));
112     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedShort), GetBasicQualType("unsigned short"));
113     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedShort), GetBasicQualType("unsigned short int"));
114     EXPECT_EQ(GetBasicQualType(eBasicTypeInt), GetBasicQualType("int"));
115     EXPECT_EQ(GetBasicQualType(eBasicTypeInt), GetBasicQualType("signed int"));
116     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedInt), GetBasicQualType("unsigned int"));
117     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedInt), GetBasicQualType("unsigned"));
118     EXPECT_EQ(GetBasicQualType(eBasicTypeLong), GetBasicQualType("long"));
119     EXPECT_EQ(GetBasicQualType(eBasicTypeLong), GetBasicQualType("long int"));
120     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLong), GetBasicQualType("unsigned long"));
121     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLong), GetBasicQualType("unsigned long int"));
122     EXPECT_EQ(GetBasicQualType(eBasicTypeLongLong), GetBasicQualType("long long"));
123     EXPECT_EQ(GetBasicQualType(eBasicTypeLongLong), GetBasicQualType("long long int"));
124     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLongLong), GetBasicQualType("unsigned long long"));
125     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLongLong), GetBasicQualType("unsigned long long int"));
126     EXPECT_EQ(GetBasicQualType(eBasicTypeInt128), GetBasicQualType("__int128_t"));
127     EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedInt128), GetBasicQualType("__uint128_t"));
128     EXPECT_EQ(GetBasicQualType(eBasicTypeVoid), GetBasicQualType("void"));
129     EXPECT_EQ(GetBasicQualType(eBasicTypeBool), GetBasicQualType("bool"));
130     EXPECT_EQ(GetBasicQualType(eBasicTypeFloat), GetBasicQualType("float"));
131     EXPECT_EQ(GetBasicQualType(eBasicTypeDouble), GetBasicQualType("double"));
132     EXPECT_EQ(GetBasicQualType(eBasicTypeLongDouble), GetBasicQualType("long double"));
133     EXPECT_EQ(GetBasicQualType(eBasicTypeObjCID), GetBasicQualType("id"));
134     EXPECT_EQ(GetBasicQualType(eBasicTypeObjCSel), GetBasicQualType("SEL"));
135     EXPECT_EQ(GetBasicQualType(eBasicTypeNullPtr), GetBasicQualType("nullptr"));
136 }
137
138 void
139 VerifyEncodingAndBitSize(clang::ASTContext *context, lldb::Encoding encoding, int bit_size)
140 {
141     CompilerType type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(context, encoding, bit_size);
142     EXPECT_TRUE(type.IsValid());
143
144     QualType qtype = ClangUtil::GetQualType(type);
145     EXPECT_FALSE(qtype.isNull());
146     if (qtype.isNull())
147         return;
148
149     uint64_t actual_size = context->getTypeSize(qtype);
150     EXPECT_EQ(bit_size, actual_size);
151
152     const clang::Type *type_ptr = qtype.getTypePtr();
153     EXPECT_NE(nullptr, type_ptr);
154     if (!type_ptr)
155         return;
156
157     EXPECT_TRUE(type_ptr->isBuiltinType());
158     if (encoding == eEncodingSint)
159         EXPECT_TRUE(type_ptr->isSignedIntegerType());
160     else if (encoding == eEncodingUint)
161         EXPECT_TRUE(type_ptr->isUnsignedIntegerType());
162     else if (encoding == eEncodingIEEE754)
163         EXPECT_TRUE(type_ptr->isFloatingType());
164 }
165
166 TEST_F(TestClangASTContext, TestBuiltinTypeForEncodingAndBitSize)
167 {
168     clang::ASTContext *context = m_ast->getASTContext();
169
170     // Make sure we can get types of every possible size in every possible encoding.
171     // We can't make any guarantee about which specific type we get, because the standard
172     // isn't that specific.  We only need to make sure the compiler hands us some type that
173     // is both a builtin type and matches the requested bit size.
174     VerifyEncodingAndBitSize(context, eEncodingSint, 8);
175     VerifyEncodingAndBitSize(context, eEncodingSint, 16);
176     VerifyEncodingAndBitSize(context, eEncodingSint, 32);
177     VerifyEncodingAndBitSize(context, eEncodingSint, 64);
178     VerifyEncodingAndBitSize(context, eEncodingSint, 128);
179
180     VerifyEncodingAndBitSize(context, eEncodingUint, 8);
181     VerifyEncodingAndBitSize(context, eEncodingUint, 16);
182     VerifyEncodingAndBitSize(context, eEncodingUint, 32);
183     VerifyEncodingAndBitSize(context, eEncodingUint, 64);
184     VerifyEncodingAndBitSize(context, eEncodingUint, 128);
185
186     VerifyEncodingAndBitSize(context, eEncodingIEEE754, 32);
187     VerifyEncodingAndBitSize(context, eEncodingIEEE754, 64);
188 }
189
190 TEST_F(TestClangASTContext, TestIsClangType)
191 {
192     clang::ASTContext *context = m_ast->getASTContext();
193     lldb::opaque_compiler_type_t bool_ctype = ClangASTContext::GetOpaqueCompilerType(context, lldb::eBasicTypeBool);
194     CompilerType bool_type(m_ast.get(), bool_ctype);
195     CompilerType record_type = m_ast->CreateRecordType(nullptr, lldb::eAccessPublic, "FooRecord", clang::TTK_Struct,
196                                                        lldb::eLanguageTypeC_plus_plus, nullptr);
197     // Clang builtin type and record type should pass
198     EXPECT_TRUE(ClangUtil::IsClangType(bool_type));
199     EXPECT_TRUE(ClangUtil::IsClangType(record_type));
200
201     // Default constructed type should fail
202     EXPECT_FALSE(ClangUtil::IsClangType(CompilerType()));
203
204     // Go type should fail
205     GoASTContext go_ast;
206     CompilerType go_type(&go_ast, bool_ctype);
207     EXPECT_FALSE(ClangUtil::IsClangType(go_type));
208 }
209
210 TEST_F(TestClangASTContext, TestRemoveFastQualifiers)
211 {
212     CompilerType record_type = m_ast->CreateRecordType(nullptr, lldb::eAccessPublic, "FooRecord", clang::TTK_Struct,
213                                                        lldb::eLanguageTypeC_plus_plus, nullptr);
214     QualType qt;
215
216     qt = ClangUtil::GetQualType(record_type);
217     EXPECT_EQ(0, qt.getLocalFastQualifiers());
218     record_type = record_type.AddConstModifier();
219     record_type = record_type.AddVolatileModifier();
220     record_type = record_type.AddRestrictModifier();
221     qt = ClangUtil::GetQualType(record_type);
222     EXPECT_NE(0, qt.getLocalFastQualifiers());
223     record_type = ClangUtil::RemoveFastQualifiers(record_type);
224     qt = ClangUtil::GetQualType(record_type);
225     EXPECT_EQ(0, qt.getLocalFastQualifiers());
226 }
227
228 TEST_F(TestClangASTContext, TestConvertAccessTypeToAccessSpecifier)
229 {
230     EXPECT_EQ(AS_none, ClangASTContext::ConvertAccessTypeToAccessSpecifier(eAccessNone));
231     EXPECT_EQ(AS_none, ClangASTContext::ConvertAccessTypeToAccessSpecifier(eAccessPackage));
232     EXPECT_EQ(AS_public, ClangASTContext::ConvertAccessTypeToAccessSpecifier(eAccessPublic));
233     EXPECT_EQ(AS_private, ClangASTContext::ConvertAccessTypeToAccessSpecifier(eAccessPrivate));
234     EXPECT_EQ(AS_protected, ClangASTContext::ConvertAccessTypeToAccessSpecifier(eAccessProtected));
235 }
236
237 TEST_F(TestClangASTContext, TestUnifyAccessSpecifiers)
238 {
239     // Unifying two of the same type should return the same type
240     EXPECT_EQ(AS_public, ClangASTContext::UnifyAccessSpecifiers(AS_public, AS_public));
241     EXPECT_EQ(AS_private, ClangASTContext::UnifyAccessSpecifiers(AS_private, AS_private));
242     EXPECT_EQ(AS_protected, ClangASTContext::UnifyAccessSpecifiers(AS_protected, AS_protected));
243
244     // Otherwise the result should be the strictest of the two.
245     EXPECT_EQ(AS_private, ClangASTContext::UnifyAccessSpecifiers(AS_private, AS_public));
246     EXPECT_EQ(AS_private, ClangASTContext::UnifyAccessSpecifiers(AS_private, AS_protected));
247     EXPECT_EQ(AS_private, ClangASTContext::UnifyAccessSpecifiers(AS_public, AS_private));
248     EXPECT_EQ(AS_private, ClangASTContext::UnifyAccessSpecifiers(AS_protected, AS_private));
249     EXPECT_EQ(AS_protected, ClangASTContext::UnifyAccessSpecifiers(AS_protected, AS_public));
250     EXPECT_EQ(AS_protected, ClangASTContext::UnifyAccessSpecifiers(AS_public, AS_protected));
251
252     // None is stricter than everything (by convention)
253     EXPECT_EQ(AS_none, ClangASTContext::UnifyAccessSpecifiers(AS_none, AS_public));
254     EXPECT_EQ(AS_none, ClangASTContext::UnifyAccessSpecifiers(AS_none, AS_protected));
255     EXPECT_EQ(AS_none, ClangASTContext::UnifyAccessSpecifiers(AS_none, AS_private));
256     EXPECT_EQ(AS_none, ClangASTContext::UnifyAccessSpecifiers(AS_public, AS_none));
257     EXPECT_EQ(AS_none, ClangASTContext::UnifyAccessSpecifiers(AS_protected, AS_none));
258     EXPECT_EQ(AS_none, ClangASTContext::UnifyAccessSpecifiers(AS_private, AS_none));
259 }
260
261 TEST_F(TestClangASTContext, TestRecordHasFields)
262 {
263     CompilerType int_type = ClangASTContext::GetBasicType(m_ast->getASTContext(), eBasicTypeInt);
264
265     // Test that a record with no fields returns false
266     CompilerType empty_base = m_ast->CreateRecordType(nullptr, lldb::eAccessPublic, "EmptyBase", clang::TTK_Struct,
267                                                       lldb::eLanguageTypeC_plus_plus, nullptr);
268     ClangASTContext::StartTagDeclarationDefinition(empty_base);
269     ClangASTContext::CompleteTagDeclarationDefinition(empty_base);
270
271     RecordDecl *empty_base_decl = ClangASTContext::GetAsRecordDecl(empty_base);
272     EXPECT_NE(nullptr, empty_base_decl);
273     EXPECT_FALSE(ClangASTContext::RecordHasFields(empty_base_decl));
274
275     // Test that a record with direct fields returns true
276     CompilerType non_empty_base = m_ast->CreateRecordType(nullptr, lldb::eAccessPublic, "NonEmptyBase",
277                                                           clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
278     ClangASTContext::StartTagDeclarationDefinition(non_empty_base);
279     FieldDecl *non_empty_base_field_decl =
280         m_ast->AddFieldToRecordType(non_empty_base, "MyField", int_type, eAccessPublic, 0);
281     ClangASTContext::CompleteTagDeclarationDefinition(non_empty_base);
282     RecordDecl *non_empty_base_decl = ClangASTContext::GetAsRecordDecl(non_empty_base);
283     EXPECT_NE(nullptr, non_empty_base_decl);
284     EXPECT_NE(nullptr, non_empty_base_field_decl);
285     EXPECT_TRUE(ClangASTContext::RecordHasFields(non_empty_base_decl));
286
287     // Test that a record with no direct fields, but fields in a base returns true
288     CompilerType empty_derived = m_ast->CreateRecordType(nullptr, lldb::eAccessPublic, "EmptyDerived",
289                                                          clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
290     ClangASTContext::StartTagDeclarationDefinition(empty_derived);
291     CXXBaseSpecifier *non_empty_base_spec =
292         m_ast->CreateBaseClassSpecifier(non_empty_base.GetOpaqueQualType(), lldb::eAccessPublic, false, false);
293     bool result = m_ast->SetBaseClassesForClassType(empty_derived.GetOpaqueQualType(), &non_empty_base_spec, 1);
294     ClangASTContext::CompleteTagDeclarationDefinition(empty_derived);
295     EXPECT_TRUE(result);
296     CXXRecordDecl *empty_derived_non_empty_base_cxx_decl = m_ast->GetAsCXXRecordDecl(empty_derived.GetOpaqueQualType());
297     RecordDecl *empty_derived_non_empty_base_decl = ClangASTContext::GetAsRecordDecl(empty_derived);
298     EXPECT_EQ(1, ClangASTContext::GetNumBaseClasses(empty_derived_non_empty_base_cxx_decl, false));
299     EXPECT_TRUE(ClangASTContext::RecordHasFields(empty_derived_non_empty_base_decl));
300
301     // Test that a record with no direct fields, but fields in a virtual base returns true
302     CompilerType empty_derived2 = m_ast->CreateRecordType(nullptr, lldb::eAccessPublic, "EmptyDerived2",
303                                                           clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
304     ClangASTContext::StartTagDeclarationDefinition(empty_derived2);
305     CXXBaseSpecifier *non_empty_vbase_spec =
306         m_ast->CreateBaseClassSpecifier(non_empty_base.GetOpaqueQualType(), lldb::eAccessPublic, true, false);
307     result = m_ast->SetBaseClassesForClassType(empty_derived2.GetOpaqueQualType(), &non_empty_vbase_spec, 1);
308     ClangASTContext::CompleteTagDeclarationDefinition(empty_derived2);
309     EXPECT_TRUE(result);
310     CXXRecordDecl *empty_derived_non_empty_vbase_cxx_decl =
311         m_ast->GetAsCXXRecordDecl(empty_derived2.GetOpaqueQualType());
312     RecordDecl *empty_derived_non_empty_vbase_decl = ClangASTContext::GetAsRecordDecl(empty_derived2);
313     EXPECT_EQ(1, ClangASTContext::GetNumBaseClasses(empty_derived_non_empty_vbase_cxx_decl, false));
314     EXPECT_TRUE(ClangASTContext::RecordHasFields(empty_derived_non_empty_vbase_decl));
315 }