Debian Codegraph @

llvm-project/lldb/unittests/Symbol/TestTypeSystemClang.cpp

raw
1 //===-- TestTypeSystemClang.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
10 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
11 #include "TestingSupport/SubsystemRAII.h"
12 #include "TestingSupport/Symbol/ClangTestUtils.h"
13 #include "lldb/Core/Declaration.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "gtest/gtest.h"
20
21 using namespace clang;
22 using namespace lldb;
23 using namespace lldb_private;
24
25 class TestTypeSystemClang : public testing::Test {
26 public:
27 SubsystemRAII<FileSystem, HostInfo> subsystems;
28
29 void SetUp() override {
30 m_holder =
31 std::make_unique<clang_utils::TypeSystemClangHolder>("test ASTContext");
32 m_ast = m_holder->GetAST();
33 }
34
35 void TearDown() override {
36 m_ast = nullptr;
37 m_holder.reset();
38 }
39
40 protected:
41
42 TypeSystemClang *m_ast = nullptr;
43 std::unique_ptr<clang_utils::TypeSystemClangHolder> m_holder;
44
45 QualType GetBasicQualType(BasicType type) const {
46 return ClangUtil::GetQualType(m_ast->GetBasicTypeFromAST(type));
47 }
48
49 QualType GetBasicQualType(const char *name) const {
50 return ClangUtil::GetQualType(
51 m_ast->GetBuiltinTypeByName(ConstString(name)));
52 }
53 };
54
55 TEST_F(TestTypeSystemClang, TestGetBasicTypeFromEnum) {
56 clang::ASTContext &context = m_ast->getASTContext();
57
58 EXPECT_TRUE(
59 context.hasSameType(GetBasicQualType(eBasicTypeBool), context.BoolTy));
60 EXPECT_TRUE(
61 context.hasSameType(GetBasicQualType(eBasicTypeChar), context.CharTy));
62 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeChar8),
63 context.Char8Ty));
64 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeChar16),
65 context.Char16Ty));
66 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeChar32),
67 context.Char32Ty));
68 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeDouble),
69 context.DoubleTy));
70 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeDoubleComplex),
71 context.getComplexType(context.DoubleTy)));
72 EXPECT_TRUE(
73 context.hasSameType(GetBasicQualType(eBasicTypeFloat), context.FloatTy));
74 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeFloatComplex),
75 context.getComplexType(context.FloatTy)));
76 EXPECT_TRUE(
77 context.hasSameType(GetBasicQualType(eBasicTypeHalf), context.HalfTy));
78 EXPECT_TRUE(
79 context.hasSameType(GetBasicQualType(eBasicTypeInt), context.IntTy));
80 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeInt128),
81 context.Int128Ty));
82 EXPECT_TRUE(
83 context.hasSameType(GetBasicQualType(eBasicTypeLong), context.LongTy));
84 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeLongDouble),
85 context.LongDoubleTy));
86 EXPECT_TRUE(
87 context.hasSameType(GetBasicQualType(eBasicTypeLongDoubleComplex),
88 context.getComplexType(context.LongDoubleTy)));
89 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeLongLong),
90 context.LongLongTy));
91 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeNullPtr),
92 context.NullPtrTy));
93 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeObjCClass),
94 context.getObjCClassType()));
95 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeObjCID),
96 context.getObjCIdType()));
97 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeObjCSel),
98 context.getObjCSelType()));
99 EXPECT_TRUE(
100 context.hasSameType(GetBasicQualType(eBasicTypeShort), context.ShortTy));
101 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeSignedChar),
102 context.SignedCharTy));
103 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeUnsignedChar),
104 context.UnsignedCharTy));
105 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeUnsignedInt),
106 context.UnsignedIntTy));
107 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeUnsignedInt128),
108 context.UnsignedInt128Ty));
109 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeUnsignedLong),
110 context.UnsignedLongTy));
111 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeUnsignedLongLong),
112 context.UnsignedLongLongTy));
113 EXPECT_TRUE(context.hasSameType(GetBasicQualType(eBasicTypeUnsignedShort),
114 context.UnsignedShortTy));
115 EXPECT_TRUE(
116 context.hasSameType(GetBasicQualType(eBasicTypeVoid), context.VoidTy));
117 EXPECT_TRUE(
118 context.hasSameType(GetBasicQualType(eBasicTypeWChar), context.WCharTy));
119 }
120
121 TEST_F(TestTypeSystemClang, TestGetBasicTypeFromName) {
122 EXPECT_EQ(GetBasicQualType(eBasicTypeChar), GetBasicQualType("char"));
123 EXPECT_EQ(GetBasicQualType(eBasicTypeSignedChar),
124 GetBasicQualType("signed char"));
125 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedChar),
126 GetBasicQualType("unsigned char"));
127 EXPECT_EQ(GetBasicQualType(eBasicTypeWChar), GetBasicQualType("wchar_t"));
128 EXPECT_EQ(GetBasicQualType(eBasicTypeSignedWChar),
129 GetBasicQualType("signed wchar_t"));
130 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedWChar),
131 GetBasicQualType("unsigned wchar_t"));
132 EXPECT_EQ(GetBasicQualType(eBasicTypeShort), GetBasicQualType("short"));
133 EXPECT_EQ(GetBasicQualType(eBasicTypeShort), GetBasicQualType("short int"));
134 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedShort),
135 GetBasicQualType("unsigned short"));
136 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedShort),
137 GetBasicQualType("unsigned short int"));
138 EXPECT_EQ(GetBasicQualType(eBasicTypeInt), GetBasicQualType("int"));
139 EXPECT_EQ(GetBasicQualType(eBasicTypeInt), GetBasicQualType("signed int"));
140 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedInt),
141 GetBasicQualType("unsigned int"));
142 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedInt),
143 GetBasicQualType("unsigned"));
144 EXPECT_EQ(GetBasicQualType(eBasicTypeLong), GetBasicQualType("long"));
145 EXPECT_EQ(GetBasicQualType(eBasicTypeLong), GetBasicQualType("long int"));
146 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLong),
147 GetBasicQualType("unsigned long"));
148 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLong),
149 GetBasicQualType("unsigned long int"));
150 EXPECT_EQ(GetBasicQualType(eBasicTypeLongLong),
151 GetBasicQualType("long long"));
152 EXPECT_EQ(GetBasicQualType(eBasicTypeLongLong),
153 GetBasicQualType("long long int"));
154 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLongLong),
155 GetBasicQualType("unsigned long long"));
156 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedLongLong),
157 GetBasicQualType("unsigned long long int"));
158 EXPECT_EQ(GetBasicQualType(eBasicTypeInt128), GetBasicQualType("__int128_t"));
159 EXPECT_EQ(GetBasicQualType(eBasicTypeUnsignedInt128),
160 GetBasicQualType("__uint128_t"));
161 EXPECT_EQ(GetBasicQualType(eBasicTypeVoid), GetBasicQualType("void"));
162 EXPECT_EQ(GetBasicQualType(eBasicTypeBool), GetBasicQualType("bool"));
163 EXPECT_EQ(GetBasicQualType(eBasicTypeFloat), GetBasicQualType("float"));
164 EXPECT_EQ(GetBasicQualType(eBasicTypeDouble), GetBasicQualType("double"));
165 EXPECT_EQ(GetBasicQualType(eBasicTypeLongDouble),
166 GetBasicQualType("long double"));
167 EXPECT_EQ(GetBasicQualType(eBasicTypeObjCID), GetBasicQualType("id"));
168 EXPECT_EQ(GetBasicQualType(eBasicTypeObjCSel), GetBasicQualType("SEL"));
169 EXPECT_EQ(GetBasicQualType(eBasicTypeNullPtr), GetBasicQualType("nullptr"));
170 }
171
172 void VerifyEncodingAndBitSize(TypeSystemClang &clang_context,
173 lldb::Encoding encoding, unsigned int bit_size) {
174 clang::ASTContext &context = clang_context.getASTContext();
175
176 CompilerType type =
177 clang_context.GetBuiltinTypeForEncodingAndBitSize(encoding, bit_size);
178 EXPECT_TRUE(type.IsValid());
179
180 QualType qtype = ClangUtil::GetQualType(type);
181 EXPECT_FALSE(qtype.isNull());
182 if (qtype.isNull())
183 return;
184
185 uint64_t actual_size = context.getTypeSize(qtype);
186 EXPECT_EQ(bit_size, actual_size);
187
188 const clang::Type *type_ptr = qtype.getTypePtr();
189 EXPECT_NE(nullptr, type_ptr);
190 if (!type_ptr)
191 return;
192
193 EXPECT_TRUE(type_ptr->isBuiltinType());
194 switch (encoding) {
195 case eEncodingSint:
196 EXPECT_TRUE(type_ptr->isSignedIntegerType());
197 break;
198 case eEncodingUint:
199 EXPECT_TRUE(type_ptr->isUnsignedIntegerType());
200 break;
201 case eEncodingIEEE754:
202 EXPECT_TRUE(type_ptr->isFloatingType());
203 break;
204 default:
205 FAIL() << "Unexpected encoding";
206 break;
207 }
208 }
209
210 TEST_F(TestTypeSystemClang, TestBuiltinTypeForEncodingAndBitSize) {
211 // Make sure we can get types of every possible size in every possible
212 // encoding.
213 // We can't make any guarantee about which specific type we get, because the
214 // standard
215 // isn't that specific. We only need to make sure the compiler hands us some
216 // type that
217 // is both a builtin type and matches the requested bit size.
218 VerifyEncodingAndBitSize(*m_ast, eEncodingSint, 8);
219 VerifyEncodingAndBitSize(*m_ast, eEncodingSint, 16);
220 VerifyEncodingAndBitSize(*m_ast, eEncodingSint, 32);
221 VerifyEncodingAndBitSize(*m_ast, eEncodingSint, 64);
222 VerifyEncodingAndBitSize(*m_ast, eEncodingSint, 128);
223
224 VerifyEncodingAndBitSize(*m_ast, eEncodingUint, 8);
225 VerifyEncodingAndBitSize(*m_ast, eEncodingUint, 16);
226 VerifyEncodingAndBitSize(*m_ast, eEncodingUint, 32);
227 VerifyEncodingAndBitSize(*m_ast, eEncodingUint, 64);
228 VerifyEncodingAndBitSize(*m_ast, eEncodingUint, 128);
229
230 VerifyEncodingAndBitSize(*m_ast, eEncodingIEEE754, 32);
231 VerifyEncodingAndBitSize(*m_ast, eEncodingIEEE754, 64);
232 }
233
234 TEST_F(TestTypeSystemClang, TestDisplayName) {
235 TypeSystemClang ast("some name", llvm::Triple());
236 EXPECT_EQ("some name", ast.getDisplayName());
237 }
238
239 TEST_F(TestTypeSystemClang, TestDisplayNameEmpty) {
240 TypeSystemClang ast("", llvm::Triple());
241 EXPECT_EQ("", ast.getDisplayName());
242 }
243
244 TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeInvalid) {
245 EXPECT_FALSE(m_ast->GetEnumerationIntegerType(CompilerType()).IsValid());
246 }
247
248 TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeUnexpectedType) {
249 CompilerType int_type = m_ast->GetBasicType(lldb::eBasicTypeInt);
250 CompilerType t = m_ast->GetEnumerationIntegerType(int_type);
251 EXPECT_FALSE(t.IsValid());
252 }
253
254 TEST_F(TestTypeSystemClang, TestGetEnumIntegerTypeBasicTypes) {
255 // All possible underlying integer types of enums.
256 const std::vector<lldb::BasicType> types_to_test = {
257 eBasicTypeInt, eBasicTypeUnsignedInt, eBasicTypeLong,
258 eBasicTypeUnsignedLong, eBasicTypeLongLong, eBasicTypeUnsignedLongLong,
259 };
260
261 for (bool scoped : {true, false}) {
262 SCOPED_TRACE("scoped: " + std::to_string(scoped));
263 for (lldb::BasicType basic_type : types_to_test) {
264 SCOPED_TRACE(std::to_string(basic_type));
265
266 auto holder =
267 std::make_unique<clang_utils::TypeSystemClangHolder>("enum_ast");
268 auto &ast = *holder->GetAST();
269
270 CompilerType basic_compiler_type = ast.GetBasicType(basic_type);
271 EXPECT_TRUE(basic_compiler_type.IsValid());
272
273 CompilerType enum_type = ast.CreateEnumerationType(
274 "my_enum", ast.GetTranslationUnitDecl(), OptionalClangModuleID(),
275 Declaration(), basic_compiler_type, scoped);
276
277 CompilerType t = ast.GetEnumerationIntegerType(enum_type);
278 // Check that the type we put in at the start is found again.
279 EXPECT_EQ(basic_compiler_type.GetTypeName(), t.GetTypeName());
280 }
281 }
282 }
283
284 TEST_F(TestTypeSystemClang, TestOwningModule) {
285 auto holder =
286 std::make_unique<clang_utils::TypeSystemClangHolder>("module_ast");
287 auto &ast = *holder->GetAST();
288 CompilerType basic_compiler_type = ast.GetBasicType(BasicType::eBasicTypeInt);
289 CompilerType enum_type = ast.CreateEnumerationType(
290 "my_enum", ast.GetTranslationUnitDecl(), OptionalClangModuleID(100),
291 Declaration(), basic_compiler_type, false);
292 auto *ed = TypeSystemClang::GetAsEnumDecl(enum_type);
293 EXPECT_FALSE(!ed);
294 EXPECT_EQ(ed->getOwningModuleID(), 100u);
295
296 CompilerType record_type = ast.CreateRecordType(
297 nullptr, OptionalClangModuleID(200), lldb::eAccessPublic, "FooRecord",
298 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
299 auto *rd = TypeSystemClang::GetAsRecordDecl(record_type);
300 EXPECT_FALSE(!rd);
301 EXPECT_EQ(rd->getOwningModuleID(), 200u);
302
303 CompilerType class_type =
304 ast.CreateObjCClass("objc_class", ast.GetTranslationUnitDecl(),
305 OptionalClangModuleID(300), false, false);
306 auto *cd = TypeSystemClang::GetAsObjCInterfaceDecl(class_type);
307 EXPECT_FALSE(!cd);
308 EXPECT_EQ(cd->getOwningModuleID(), 300u);
309 }
310
311 TEST_F(TestTypeSystemClang, TestIsClangType) {
312 clang::ASTContext &context = m_ast->getASTContext();
313 lldb::opaque_compiler_type_t bool_ctype =
314 TypeSystemClang::GetOpaqueCompilerType(&context, lldb::eBasicTypeBool);
315 CompilerType bool_type(m_ast->weak_from_this(), bool_ctype);
316 CompilerType record_type = m_ast->CreateRecordType(
317 nullptr, OptionalClangModuleID(100), lldb::eAccessPublic, "FooRecord",
318 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
319 // Clang builtin type and record type should pass
320 EXPECT_TRUE(ClangUtil::IsClangType(bool_type));
321 EXPECT_TRUE(ClangUtil::IsClangType(record_type));
322
323 // Default constructed type should fail
324 EXPECT_FALSE(ClangUtil::IsClangType(CompilerType()));
325 }
326
327 TEST_F(TestTypeSystemClang, TestRemoveFastQualifiers) {
328 CompilerType record_type = m_ast->CreateRecordType(
329 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "FooRecord",
330 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
331 QualType qt;
332
333 qt = ClangUtil::GetQualType(record_type);
334 EXPECT_EQ(0u, qt.getLocalFastQualifiers());
335 record_type = record_type.AddConstModifier();
336 record_type = record_type.AddVolatileModifier();
337 record_type = record_type.AddRestrictModifier();
338 qt = ClangUtil::GetQualType(record_type);
339 EXPECT_NE(0u, qt.getLocalFastQualifiers());
340 record_type = ClangUtil::RemoveFastQualifiers(record_type);
341 qt = ClangUtil::GetQualType(record_type);
342 EXPECT_EQ(0u, qt.getLocalFastQualifiers());
343 }
344
345 TEST_F(TestTypeSystemClang, TestConvertAccessTypeToAccessSpecifier) {
346 EXPECT_EQ(AS_none,
347 TypeSystemClang::ConvertAccessTypeToAccessSpecifier(eAccessNone));
348 EXPECT_EQ(AS_none, TypeSystemClang::ConvertAccessTypeToAccessSpecifier(
349 eAccessPackage));
350 EXPECT_EQ(AS_public,
351 TypeSystemClang::ConvertAccessTypeToAccessSpecifier(eAccessPublic));
352 EXPECT_EQ(AS_private, TypeSystemClang::ConvertAccessTypeToAccessSpecifier(
353 eAccessPrivate));
354 EXPECT_EQ(AS_protected, TypeSystemClang::ConvertAccessTypeToAccessSpecifier(
355 eAccessProtected));
356 }
357
358 TEST_F(TestTypeSystemClang, TestUnifyAccessSpecifiers) {
359 // Unifying two of the same type should return the same type
360 EXPECT_EQ(AS_public,
361 TypeSystemClang::UnifyAccessSpecifiers(AS_public, AS_public));
362 EXPECT_EQ(AS_private,
363 TypeSystemClang::UnifyAccessSpecifiers(AS_private, AS_private));
364 EXPECT_EQ(AS_protected,
365 TypeSystemClang::UnifyAccessSpecifiers(AS_protected, AS_protected));
366
367 // Otherwise the result should be the strictest of the two.
368 EXPECT_EQ(AS_private,
369 TypeSystemClang::UnifyAccessSpecifiers(AS_private, AS_public));
370 EXPECT_EQ(AS_private,
371 TypeSystemClang::UnifyAccessSpecifiers(AS_private, AS_protected));
372 EXPECT_EQ(AS_private,
373 TypeSystemClang::UnifyAccessSpecifiers(AS_public, AS_private));
374 EXPECT_EQ(AS_private,
375 TypeSystemClang::UnifyAccessSpecifiers(AS_protected, AS_private));
376 EXPECT_EQ(AS_protected,
377 TypeSystemClang::UnifyAccessSpecifiers(AS_protected, AS_public));
378 EXPECT_EQ(AS_protected,
379 TypeSystemClang::UnifyAccessSpecifiers(AS_public, AS_protected));
380
381 // None is stricter than everything (by convention)
382 EXPECT_EQ(AS_none,
383 TypeSystemClang::UnifyAccessSpecifiers(AS_none, AS_public));
384 EXPECT_EQ(AS_none,
385 TypeSystemClang::UnifyAccessSpecifiers(AS_none, AS_protected));
386 EXPECT_EQ(AS_none,
387 TypeSystemClang::UnifyAccessSpecifiers(AS_none, AS_private));
388 EXPECT_EQ(AS_none,
389 TypeSystemClang::UnifyAccessSpecifiers(AS_public, AS_none));
390 EXPECT_EQ(AS_none,
391 TypeSystemClang::UnifyAccessSpecifiers(AS_protected, AS_none));
392 EXPECT_EQ(AS_none,
393 TypeSystemClang::UnifyAccessSpecifiers(AS_private, AS_none));
394 }
395
396 TEST_F(TestTypeSystemClang, TestRecordHasFields) {
397 CompilerType int_type = m_ast->GetBasicType(eBasicTypeInt);
398
399 // Test that a record with no fields returns false
400 CompilerType empty_base = m_ast->CreateRecordType(
401 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "EmptyBase",
402 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
403 TypeSystemClang::StartTagDeclarationDefinition(empty_base);
404 TypeSystemClang::CompleteTagDeclarationDefinition(empty_base);
405
406 RecordDecl *empty_base_decl = TypeSystemClang::GetAsRecordDecl(empty_base);
407 EXPECT_NE(nullptr, empty_base_decl);
408 EXPECT_FALSE(m_ast->RecordHasFields(empty_base_decl));
409
410 // Test that a record with direct fields returns true
411 CompilerType non_empty_base = m_ast->CreateRecordType(
412 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "NonEmptyBase",
413 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
414 TypeSystemClang::StartTagDeclarationDefinition(non_empty_base);
415 FieldDecl *non_empty_base_field_decl = m_ast->AddFieldToRecordType(
416 non_empty_base, "MyField", int_type, eAccessPublic, 0);
417 TypeSystemClang::CompleteTagDeclarationDefinition(non_empty_base);
418 RecordDecl *non_empty_base_decl =
419 TypeSystemClang::GetAsRecordDecl(non_empty_base);
420 EXPECT_NE(nullptr, non_empty_base_decl);
421 EXPECT_NE(nullptr, non_empty_base_field_decl);
422 EXPECT_TRUE(m_ast->RecordHasFields(non_empty_base_decl));
423
424 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases;
425
426 // Test that a record with no direct fields, but fields in a base returns true
427 CompilerType empty_derived = m_ast->CreateRecordType(
428 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "EmptyDerived",
429 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
430 TypeSystemClang::StartTagDeclarationDefinition(empty_derived);
431 std::unique_ptr<clang::CXXBaseSpecifier> non_empty_base_spec =
432 m_ast->CreateBaseClassSpecifier(non_empty_base.GetOpaqueQualType(),
433 lldb::eAccessPublic, false, false);
434 bases.push_back(std::move(non_empty_base_spec));
435 bool result = m_ast->TransferBaseClasses(empty_derived.GetOpaqueQualType(),
436 std::move(bases));
437 TypeSystemClang::CompleteTagDeclarationDefinition(empty_derived);
438 EXPECT_TRUE(result);
439 CXXRecordDecl *empty_derived_non_empty_base_cxx_decl =
440 m_ast->GetAsCXXRecordDecl(empty_derived.GetOpaqueQualType());
441 RecordDecl *empty_derived_non_empty_base_decl =
442 TypeSystemClang::GetAsRecordDecl(empty_derived);
443 EXPECT_EQ(1u, m_ast->GetNumBaseClasses(
444 empty_derived_non_empty_base_cxx_decl, false));
445 EXPECT_TRUE(m_ast->RecordHasFields(empty_derived_non_empty_base_decl));
446
447 // Test that a record with no direct fields, but fields in a virtual base
448 // returns true
449 CompilerType empty_derived2 = m_ast->CreateRecordType(
450 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "EmptyDerived2",
451 clang::TTK_Struct, lldb::eLanguageTypeC_plus_plus, nullptr);
452 TypeSystemClang::StartTagDeclarationDefinition(empty_derived2);
453 std::unique_ptr<CXXBaseSpecifier> non_empty_vbase_spec =
454 m_ast->CreateBaseClassSpecifier(non_empty_base.GetOpaqueQualType(),
455 lldb::eAccessPublic, true, false);
456 bases.push_back(std::move(non_empty_vbase_spec));
457 result = m_ast->TransferBaseClasses(empty_derived2.GetOpaqueQualType(),
458 std::move(bases));
459 TypeSystemClang::CompleteTagDeclarationDefinition(empty_derived2);
460 EXPECT_TRUE(result);
461 CXXRecordDecl *empty_derived_non_empty_vbase_cxx_decl =
462 m_ast->GetAsCXXRecordDecl(empty_derived2.GetOpaqueQualType());
463 RecordDecl *empty_derived_non_empty_vbase_decl =
464 TypeSystemClang::GetAsRecordDecl(empty_derived2);
465 EXPECT_EQ(1u, m_ast->GetNumBaseClasses(
466 empty_derived_non_empty_vbase_cxx_decl, false));
467 EXPECT_TRUE(
468 m_ast->RecordHasFields(empty_derived_non_empty_vbase_decl));
469 }
470
471 TEST_F(TestTypeSystemClang, TemplateArguments) {
472 TypeSystemClang::TemplateParameterInfos infos;
473 infos.names.push_back("T");
474 infos.args.push_back(TemplateArgument(m_ast->getASTContext().IntTy));
475 infos.names.push_back("I");
476 llvm::APSInt arg(llvm::APInt(8, 47));
477 infos.args.push_back(TemplateArgument(m_ast->getASTContext(), arg,
478 m_ast->getASTContext().IntTy));
479
480 // template<typename T, int I> struct foo;
481 ClassTemplateDecl *decl = m_ast->CreateClassTemplateDecl(
482 m_ast->GetTranslationUnitDecl(), OptionalClangModuleID(), eAccessPublic,
483 "foo", TTK_Struct, infos);
484 ASSERT_NE(decl, nullptr);
485
486 // foo<int, 47>
487 ClassTemplateSpecializationDecl *spec_decl =
488 m_ast->CreateClassTemplateSpecializationDecl(
489 m_ast->GetTranslationUnitDecl(), OptionalClangModuleID(), decl,
490 TTK_Struct, infos);
491 ASSERT_NE(spec_decl, nullptr);
492 CompilerType type = m_ast->CreateClassTemplateSpecializationType(spec_decl);
493 ASSERT_TRUE(type);
494 m_ast->StartTagDeclarationDefinition(type);
495 m_ast->CompleteTagDeclarationDefinition(type);
496
497 // typedef foo<int, 47> foo_def;
498 CompilerType typedef_type = type.CreateTypedef(
499 "foo_def", m_ast->CreateDeclContext(m_ast->GetTranslationUnitDecl()), 0);
500
501 CompilerType auto_type(
502 m_ast->weak_from_this(),
503 m_ast->getASTContext()
504 .getAutoType(ClangUtil::GetCanonicalQualType(typedef_type),
505 clang::AutoTypeKeyword::Auto, false)
506 .getAsOpaquePtr());
507
508 CompilerType int_type(m_ast->weak_from_this(),
509 m_ast->getASTContext().IntTy.getAsOpaquePtr());
510 for (CompilerType t : {type, typedef_type, auto_type}) {
511 SCOPED_TRACE(t.GetTypeName().AsCString());
512
513 const bool expand_pack = false;
514 EXPECT_EQ(
515 m_ast->GetTemplateArgumentKind(t.GetOpaqueQualType(), 0, expand_pack),
516 eTemplateArgumentKindType);
517 EXPECT_EQ(
518 m_ast->GetTypeTemplateArgument(t.GetOpaqueQualType(), 0, expand_pack),
519 int_type);
520 EXPECT_EQ(std::nullopt, m_ast->GetIntegralTemplateArgument(
521 t.GetOpaqueQualType(), 0, expand_pack));
522
523 EXPECT_EQ(
524 m_ast->GetTemplateArgumentKind(t.GetOpaqueQualType(), 1, expand_pack),
525 eTemplateArgumentKindIntegral);
526 EXPECT_EQ(
527 m_ast->GetTypeTemplateArgument(t.GetOpaqueQualType(), 1, expand_pack),
528 CompilerType());
529 auto result = m_ast->GetIntegralTemplateArgument(t.GetOpaqueQualType(), 1,
530 expand_pack);
531 ASSERT_NE(std::nullopt, result);
532 EXPECT_EQ(arg, result->value);
533 EXPECT_EQ(int_type, result->type);
534 }
535 }
536
537 class TestCreateClassTemplateDecl : public TestTypeSystemClang {
538 protected:
539 /// The class templates created so far by the Expect* functions below.
540 llvm::DenseSet<ClassTemplateDecl *> m_created_templates;
541
542 /// Utility function for creating a class template.
543 ClassTemplateDecl *
544 CreateClassTemplate(const TypeSystemClang::TemplateParameterInfos &infos) {
545 ClassTemplateDecl *decl = m_ast->CreateClassTemplateDecl(
546 m_ast->GetTranslationUnitDecl(), OptionalClangModuleID(), eAccessPublic,
547 "foo", TTK_Struct, infos);
548 return decl;
549 }
550
551 /// Creates a new class template with the given template parameters.
552 /// Asserts that a new ClassTemplateDecl is created.
553 /// \param description The gtest scope string that should describe the input.
554 /// \param infos The template parameters that the class template should have.
555 /// \returns The created ClassTemplateDecl.
556 ClassTemplateDecl *
557 ExpectNewTemplate(std::string description,
558 const TypeSystemClang::TemplateParameterInfos &infos) {
559 SCOPED_TRACE(description);
560 ClassTemplateDecl *first_template = CreateClassTemplate(infos);
561 // A new template should have been created.
562 EXPECT_FALSE(m_created_templates.contains(first_template))
563 << "Didn't create new class template but reused this existing decl:\n"
564 << ClangUtil::DumpDecl(first_template);
565 m_created_templates.insert(first_template);
566
567 // Creating a new template with the same arguments should always return
568 // the template created above.
569 ClassTemplateDecl *second_template = CreateClassTemplate(infos);
570 EXPECT_EQ(first_template, second_template)
571 << "Second attempt to create class template didn't reuse first decl:\n"
572 << ClangUtil::DumpDecl(first_template) << "\nInstead created/reused:\n"
573 << ClangUtil::DumpDecl(second_template);
574 return first_template;
575 }
576
577 /// Tries to create a new class template but asserts that an existing class
578 /// template in the current AST is reused (in contract so a new class
579 /// template being created).
580 /// \param description The gtest scope string that should describe the input.
581 /// \param infos The template parameters that the class template should have.
582 void
583 ExpectReusedTemplate(std::string description,
584 const TypeSystemClang::TemplateParameterInfos &infos,
585 ClassTemplateDecl *expected) {
586 SCOPED_TRACE(description);
587 ClassTemplateDecl *td = CreateClassTemplate(infos);
588 EXPECT_EQ(td, expected)
589 << "Created/reused class template is:\n"
590 << ClangUtil::DumpDecl(td) << "\nExpected to reuse:\n"
591 << ClangUtil::DumpDecl(expected);
592 }
593 };
594
595 TEST_F(TestCreateClassTemplateDecl, FindExistingTemplates) {
596 // This tests the logic in TypeSystemClang::CreateClassTemplateDecl that
597 // decides whether an existing ClassTemplateDecl in the AST can be reused.
598 // The behaviour should follow the C++ rules for redeclaring templates
599 // (e.g., parameter names can be changed/omitted.)
600
601 // This describes a class template *instantiation* from which we will infer
602 // the structure of the class template.
603 TypeSystemClang::TemplateParameterInfos infos;
604
605 // Test an empty template parameter list: <>
606 ExpectNewTemplate("<>", infos);
607
608 // Test that <typename T> with T = int creates a new template.
609 infos.names = {"T"};
610 infos.args = {TemplateArgument(m_ast->getASTContext().IntTy)};
611 ClassTemplateDecl *single_type_arg = ExpectNewTemplate("<typename T>", infos);
612
613 // Test that changing the parameter name doesn't create a new class template.
614 infos.names = {"A"};
615 ExpectReusedTemplate("<typename A> (A = int)", infos, single_type_arg);
616
617 // Test that changing the used type doesn't create a new class template.
618 infos.args = {TemplateArgument(m_ast->getASTContext().FloatTy)};
619 ExpectReusedTemplate("<typename A> (A = float)", infos, single_type_arg);
620
621 // Test that <typename A, signed char I> creates a new template with A = int
622 // and I = 47;
623 infos.names.push_back("I");
624 infos.args.push_back(TemplateArgument(m_ast->getASTContext(),
625 llvm::APSInt(llvm::APInt(8, 47)),
626 m_ast->getASTContext().SignedCharTy));
627 ClassTemplateDecl *type_and_char_value =
628 ExpectNewTemplate("<typename A, signed char I> (I = 47)", infos);
629
630 // Change the value of the I parameter to 123. The previously created
631 // class template should still be reused.
632 infos.args.pop_back();
633 infos.args.push_back(TemplateArgument(m_ast->getASTContext(),
634 llvm::APSInt(llvm::APInt(8, 123)),
635 m_ast->getASTContext().SignedCharTy));
636 ExpectReusedTemplate("<typename A, signed char I> (I = 123)", infos,
637 type_and_char_value);
638
639 // Change the type of the I parameter to int so we have <typename A, int I>.
640 // The class template from above can't be reused.
641 infos.args.pop_back();
642 infos.args.push_back(TemplateArgument(m_ast->getASTContext(),
643 llvm::APSInt(llvm::APInt(32, 47)),
644 m_ast->getASTContext().IntTy));
645 ExpectNewTemplate("<typename A, int I> (I = 123)", infos);
646
647 // Test a second type parameter will also cause a new template to be created.
648 // We now have <typename A, int I, typename B>.
649 infos.names.push_back("B");
650 infos.args.push_back(TemplateArgument(m_ast->getASTContext().IntTy));
651 ClassTemplateDecl *type_and_char_value_and_type =
652 ExpectNewTemplate("<typename A, int I, typename B>", infos);
653
654 // Remove all the names from the parameters which shouldn't influence the
655 // way the templates get merged.
656 infos.names = {"", "", ""};
657 ExpectReusedTemplate("<typename, int, typename>", infos,
658 type_and_char_value_and_type);
659 }
660
661 TEST_F(TestCreateClassTemplateDecl, FindExistingTemplatesWithParameterPack) {
662 // The same as FindExistingTemplates but for templates with parameter packs.
663
664 TypeSystemClang::TemplateParameterInfos infos;
665 infos.packed_args =
666 std::make_unique<TypeSystemClang::TemplateParameterInfos>();
667 infos.packed_args->names = {"", ""};
668 infos.packed_args->args = {TemplateArgument(m_ast->getASTContext().IntTy),
669 TemplateArgument(m_ast->getASTContext().IntTy)};
670 ClassTemplateDecl *type_pack =
671 ExpectNewTemplate("<typename ...> (int, int)", infos);
672
673 // Special case: An instantiation for a parameter pack with no values fits
674 // to whatever class template we find. There isn't enough information to
675 // do an actual comparison here.
676 infos.packed_args =
677 std::make_unique<TypeSystemClang::TemplateParameterInfos>();
678 ExpectReusedTemplate("<...> (no values in pack)", infos, type_pack);
679
680 // Change the type content of pack type values.
681 infos.packed_args->names = {"", ""};
682 infos.packed_args->args = {TemplateArgument(m_ast->getASTContext().IntTy),
683 TemplateArgument(m_ast->getASTContext().LongTy)};
684 ExpectReusedTemplate("<typename ...> (int, long)", infos, type_pack);
685
686 // Change the number of pack values.
687 infos.packed_args->args = {TemplateArgument(m_ast->getASTContext().IntTy)};
688 ExpectReusedTemplate("<typename ...> (int)", infos, type_pack);
689
690 // The names of the pack values shouldn't matter.
691 infos.packed_args->names = {"A", "B"};
692 ExpectReusedTemplate("<typename ...> (int)", infos, type_pack);
693
694 // Changing the kind of template argument will create a new template.
695 infos.packed_args->args = {TemplateArgument(m_ast->getASTContext(),
696 llvm::APSInt(llvm::APInt(32, 1)),
697 m_ast->getASTContext().IntTy)};
698 ClassTemplateDecl *int_pack = ExpectNewTemplate("<int ...> (int = 1)", infos);
699
700 // Changing the value of integral parameters will not create a new template.
701 infos.packed_args->args = {TemplateArgument(
702 m_ast->getASTContext(), llvm::APSInt(llvm::APInt(32, 123)),
703 m_ast->getASTContext().IntTy)};
704 ExpectReusedTemplate("<int ...> (int = 123)", infos, int_pack);
705
706 // Changing the integral type will create a new template.
707 infos.packed_args->args = {TemplateArgument(m_ast->getASTContext(),
708 llvm::APSInt(llvm::APInt(64, 1)),
709 m_ast->getASTContext().LongTy)};
710 ExpectNewTemplate("<long ...> (long = 1)", infos);
711
712 // Prependinding a non-pack parameter will create a new template.
713 infos.names = {"T"};
714 infos.args = {TemplateArgument(m_ast->getASTContext().IntTy)};
715 ExpectNewTemplate("<typename T, long...> (T = int, long = 1)", infos);
716 }
717
718 TEST_F(TestTypeSystemClang, OnlyPackName) {
719 TypeSystemClang::TemplateParameterInfos infos;
720 infos.pack_name = "A";
721 EXPECT_FALSE(infos.IsValid());
722 }
723
724 static QualType makeConstInt(clang::ASTContext &ctxt) {
725 QualType result(ctxt.IntTy);
726 result.addConst();
727 return result;
728 }
729
730 TEST_F(TestTypeSystemClang, TestGetTypeClassDeclType) {
731 clang::ASTContext &ctxt = m_ast->getASTContext();
732 auto *nullptr_expr = new (ctxt) CXXNullPtrLiteralExpr(ctxt.NullPtrTy, SourceLocation());
733 QualType t = ctxt.getDecltypeType(nullptr_expr, makeConstInt(ctxt));
734 EXPECT_EQ(lldb::eTypeClassBuiltin, m_ast->GetTypeClass(t.getAsOpaquePtr()));
735 }
736
737 TEST_F(TestTypeSystemClang, TestGetTypeClassTypeOf) {
738 clang::ASTContext &ctxt = m_ast->getASTContext();
739 QualType t = ctxt.getTypeOfType(makeConstInt(ctxt), TypeOfKind::Qualified);
740 EXPECT_EQ(lldb::eTypeClassBuiltin, m_ast->GetTypeClass(t.getAsOpaquePtr()));
741 }
742
743 TEST_F(TestTypeSystemClang, TestGetTypeClassTypeOfExpr) {
744 clang::ASTContext &ctxt = m_ast->getASTContext();
745 auto *nullptr_expr = new (ctxt) CXXNullPtrLiteralExpr(ctxt.NullPtrTy, SourceLocation());
746 QualType t = ctxt.getTypeOfExprType(nullptr_expr, TypeOfKind::Qualified);
747 EXPECT_EQ(lldb::eTypeClassBuiltin, m_ast->GetTypeClass(t.getAsOpaquePtr()));
748 }
749
750 TEST_F(TestTypeSystemClang, TestGetTypeClassNested) {
751 clang::ASTContext &ctxt = m_ast->getASTContext();
752 QualType t_base =
753 ctxt.getTypeOfType(makeConstInt(ctxt), TypeOfKind::Qualified);
754 QualType t = ctxt.getTypeOfType(t_base, TypeOfKind::Qualified);
755 EXPECT_EQ(lldb::eTypeClassBuiltin, m_ast->GetTypeClass(t.getAsOpaquePtr()));
756 }
757
758 TEST_F(TestTypeSystemClang, TestFunctionTemplateConstruction) {
759 // Tests creating a function template.
760
761 CompilerType int_type = m_ast->GetBasicType(lldb::eBasicTypeInt);
762 clang::TranslationUnitDecl *TU = m_ast->GetTranslationUnitDecl();
763
764 // Prepare the declarations/types we need for the template.
765 CompilerType clang_type =
766 m_ast->CreateFunctionType(int_type, nullptr, 0U, false, 0U);
767 FunctionDecl *func = m_ast->CreateFunctionDeclaration(
768 TU, OptionalClangModuleID(), "foo", clang_type, StorageClass::SC_None,
769 false);
770 TypeSystemClang::TemplateParameterInfos empty_params;
771
772 // Create the actual function template.
773 clang::FunctionTemplateDecl *func_template =
774 m_ast->CreateFunctionTemplateDecl(TU, OptionalClangModuleID(), func,
775 empty_params);
776
777 EXPECT_EQ(TU, func_template->getDeclContext());
778 EXPECT_EQ("foo", func_template->getName());
779 EXPECT_EQ(clang::AccessSpecifier::AS_none, func_template->getAccess());
780 }
781
782 TEST_F(TestTypeSystemClang, TestFunctionTemplateInRecordConstruction) {
783 // Tests creating a function template inside a record.
784
785 CompilerType int_type = m_ast->GetBasicType(lldb::eBasicTypeInt);
786 clang::TranslationUnitDecl *TU = m_ast->GetTranslationUnitDecl();
787
788 // Create a record we can put the function template int.
789 CompilerType record_type =
790 clang_utils::createRecordWithField(*m_ast, "record", int_type, "field");
791 clang::TagDecl *record = ClangUtil::GetAsTagDecl(record_type);
792
793 // Prepare the declarations/types we need for the template.
794 CompilerType clang_type =
795 m_ast->CreateFunctionType(int_type, nullptr, 0U, false, 0U);
796 // We create the FunctionDecl for the template in the TU DeclContext because:
797 // 1. FunctionDecls can't be in a Record (only CXXMethodDecls can).
798 // 2. It is mirroring the behavior of DWARFASTParserClang::ParseSubroutine.
799 FunctionDecl *func = m_ast->CreateFunctionDeclaration(
800 TU, OptionalClangModuleID(), "foo", clang_type, StorageClass::SC_None,
801 false);
802 TypeSystemClang::TemplateParameterInfos empty_params;
803
804 // Create the actual function template.
805 clang::FunctionTemplateDecl *func_template =
806 m_ast->CreateFunctionTemplateDecl(record, OptionalClangModuleID(), func,
807 empty_params);
808
809 EXPECT_EQ(record, func_template->getDeclContext());
810 EXPECT_EQ("foo", func_template->getName());
811 EXPECT_EQ(clang::AccessSpecifier::AS_public, func_template->getAccess());
812 }
813
814 TEST_F(TestTypeSystemClang, TestDeletingImplicitCopyCstrDueToMoveCStr) {
815 // We need to simulate this behavior in our AST that we construct as we don't
816 // have a Sema instance that can do this for us:
817 // C++11 [class.copy]p7, p18:
818 // If the class definition declares a move constructor or move assignment
819 // operator, an implicitly declared copy constructor or copy assignment
820 // operator is defined as deleted.
821
822 // Create a record and start defining it.
823 llvm::StringRef class_name = "S";
824 CompilerType t = clang_utils::createRecord(*m_ast, class_name);
825 m_ast->StartTagDeclarationDefinition(t);
826
827 // Create a move constructor that will delete the implicit copy constructor.
828 CompilerType return_type = m_ast->GetBasicType(lldb::eBasicTypeVoid);
829 CompilerType param_type = t.GetRValueReferenceType();
830 CompilerType function_type =
831 m_ast->CreateFunctionType(return_type, &param_type, /*num_params*/ 1,
832 /*variadic=*/false, /*quals*/ 0U);
833 bool is_virtual = false;
834 bool is_static = false;
835 bool is_inline = false;
836 bool is_explicit = true;
837 bool is_attr_used = false;
838 bool is_artificial = false;
839 m_ast->AddMethodToCXXRecordType(
840 t.GetOpaqueQualType(), class_name, nullptr, function_type,
841 lldb::AccessType::eAccessPublic, is_virtual, is_static, is_inline,
842 is_explicit, is_attr_used, is_artificial);
843
844 // Complete the definition and check the created record.
845 m_ast->CompleteTagDeclarationDefinition(t);
846 auto *record = llvm::cast<CXXRecordDecl>(ClangUtil::GetAsTagDecl(t));
847 // We can't call defaultedCopyConstructorIsDeleted() as this requires that
848 // the Decl passes through Sema which will actually compute this field.
849 // Instead we check that there is no copy constructor declared by the user
850 // which only leaves a non-deleted defaulted copy constructor as an option
851 // that our record will have no simple copy constructor.
852 EXPECT_FALSE(record->hasUserDeclaredCopyConstructor());
853 EXPECT_FALSE(record->hasSimpleCopyConstructor());
854 }
855
856 TEST_F(TestTypeSystemClang, TestNotDeletingUserCopyCstrDueToMoveCStr) {
857 // Tests that we don't delete the a user-defined copy constructor when
858 // a move constructor is provided.
859 // See also the TestDeletingImplicitCopyCstrDueToMoveCStr test.
860 llvm::StringRef class_name = "S";
861 CompilerType t = clang_utils::createRecord(*m_ast, class_name);
862 m_ast->StartTagDeclarationDefinition(t);
863
864 CompilerType return_type = m_ast->GetBasicType(lldb::eBasicTypeVoid);
865 bool is_virtual = false;
866 bool is_static = false;
867 bool is_inline = false;
868 bool is_explicit = true;
869 bool is_attr_used = false;
870 bool is_artificial = false;
871 // Create a move constructor.
872 {
873 CompilerType param_type = t.GetRValueReferenceType();
874 CompilerType function_type =
875 m_ast->CreateFunctionType(return_type, &param_type, /*num_params*/ 1,
876 /*variadic=*/false, /*quals*/ 0U);
877 m_ast->AddMethodToCXXRecordType(
878 t.GetOpaqueQualType(), class_name, nullptr, function_type,
879 lldb::AccessType::eAccessPublic, is_virtual, is_static, is_inline,
880 is_explicit, is_attr_used, is_artificial);
881 }
882 // Create a copy constructor.
883 {
884 CompilerType param_type = t.GetLValueReferenceType().AddConstModifier();
885 CompilerType function_type =
886 m_ast->CreateFunctionType(return_type, &param_type, /*num_params*/ 1,
887 /*variadic=*/false, /*quals*/ 0U);
888 m_ast->AddMethodToCXXRecordType(
889 t.GetOpaqueQualType(), class_name, nullptr, function_type,
890 lldb::AccessType::eAccessPublic, is_virtual, is_static, is_inline,
891 is_explicit, is_attr_used, is_artificial);
892 }
893
894 // Complete the definition and check the created record.
895 m_ast->CompleteTagDeclarationDefinition(t);
896 auto *record = llvm::cast<CXXRecordDecl>(ClangUtil::GetAsTagDecl(t));
897 EXPECT_TRUE(record->hasUserDeclaredCopyConstructor());
898 }
899
900 TEST_F(TestTypeSystemClang, AddMethodToObjCObjectType) {
901 // Create an interface decl and mark it as having external storage.
902 CompilerType c = m_ast->CreateObjCClass("A", m_ast->GetTranslationUnitDecl(),
903 OptionalClangModuleID(),
904 /*IsForwardDecl*/ false,
905 /*IsInternal*/ false);
906 ObjCInterfaceDecl *interface = m_ast->GetAsObjCInterfaceDecl(c);
907 m_ast->SetHasExternalStorage(c.GetOpaqueQualType(), true);
908 EXPECT_TRUE(interface->hasExternalLexicalStorage());
909
910 // Add a method to the interface.
911 std::vector<CompilerType> args;
912 CompilerType func_type =
913 m_ast->CreateFunctionType(m_ast->GetBasicType(lldb::eBasicTypeInt),
914 args.data(), args.size(), /*variadic*/ false,
915 /*quals*/ 0, clang::CallingConv::CC_C);
916 bool variadic = false;
917 bool artificial = false;
918 bool objc_direct = false;
919 clang::ObjCMethodDecl *method = TypeSystemClang::AddMethodToObjCObjectType(
920 c, "-[A foo]", func_type, lldb::eAccessPublic, artificial, variadic,
921 objc_direct);
922 ASSERT_NE(method, nullptr);
923
924 // The interface decl should still have external lexical storage.
925 EXPECT_TRUE(interface->hasExternalLexicalStorage());
926
927 // Test some properties of the created ObjCMethodDecl.
928 EXPECT_FALSE(method->isVariadic());
929 EXPECT_TRUE(method->isImplicit());
930 EXPECT_FALSE(method->isDirectMethod());
931 EXPECT_EQ(method->getDeclName().getObjCSelector().getAsString(), "foo");
932 }
933
934 TEST_F(TestTypeSystemClang, GetFullyUnqualifiedType) {
935 CompilerType bool_ = m_ast->GetBasicType(eBasicTypeBool);
936 CompilerType cv_bool = bool_.AddConstModifier().AddVolatileModifier();
937
938 // const volatile bool -> bool
939 EXPECT_EQ(bool_, cv_bool.GetFullyUnqualifiedType());
940
941 // const volatile bool[47] -> bool[47]
942 EXPECT_EQ(bool_.GetArrayType(47),
943 cv_bool.GetArrayType(47).GetFullyUnqualifiedType());
944
945 // const volatile bool[47][42] -> bool[47][42]
946 EXPECT_EQ(
947 bool_.GetArrayType(42).GetArrayType(47),
948 cv_bool.GetArrayType(42).GetArrayType(47).GetFullyUnqualifiedType());
949
950 // const volatile bool * -> bool *
951 EXPECT_EQ(bool_.GetPointerType(),
952 cv_bool.GetPointerType().GetFullyUnqualifiedType());
953
954 // const volatile bool *[47] -> bool *[47]
955 EXPECT_EQ(
956 bool_.GetPointerType().GetArrayType(47),
957 cv_bool.GetPointerType().GetArrayType(47).GetFullyUnqualifiedType());
958 }
959
960 TEST(TestScratchTypeSystemClang, InferSubASTFromLangOpts) {
961 LangOptions lang_opts;
962 EXPECT_EQ(
963 ScratchTypeSystemClang::DefaultAST,
964 ScratchTypeSystemClang::InferIsolatedASTKindFromLangOpts(lang_opts));
965
966 lang_opts.Modules = true;
967 EXPECT_EQ(
968 ScratchTypeSystemClang::IsolatedASTKind::CppModules,
969 ScratchTypeSystemClang::InferIsolatedASTKindFromLangOpts(lang_opts));
970 }
971
972 TEST_F(TestTypeSystemClang, GetDeclContextByNameWhenMissingSymbolFile) {
973 // Test that a type system without a symbol file is handled gracefully.
974 std::vector<CompilerDecl> decls =
975 m_ast->DeclContextFindDeclByName(nullptr, ConstString("SomeName"), true);
976
977 EXPECT_TRUE(decls.empty());
978 }