1 //===- ClangTestUtils.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_CLANGTESTUTILS_H
10 #define LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_CLANGTESTUTILS_H
12 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
13 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14 #include "lldb/Host/HostInfo.h"
16 namespace lldb_private
{
17 namespace clang_utils
{
18 inline clang::DeclarationName
getDeclarationName(TypeSystemClang
&ast
,
19 llvm::StringRef name
) {
20 clang::IdentifierInfo
&II
= ast
.getASTContext().Idents
.get(name
);
21 return ast
.getASTContext().DeclarationNames
.getIdentifier(&II
);
24 inline CompilerType
createRecord(TypeSystemClang
&ast
, llvm::StringRef name
) {
25 return ast
.CreateRecordType(ast
.getASTContext().getTranslationUnitDecl(),
26 OptionalClangModuleID(),
27 lldb::AccessType::eAccessPublic
, name
, 0,
28 lldb::LanguageType::eLanguageTypeC
);
31 /// Create a record with the given name and a field with the given type
33 inline CompilerType
createRecordWithField(TypeSystemClang
&ast
,
34 llvm::StringRef record_name
,
35 CompilerType field_type
,
36 llvm::StringRef field_name
) {
37 CompilerType t
= createRecord(ast
, record_name
);
39 TypeSystemClang::StartTagDeclarationDefinition(t
);
40 ast
.AddFieldToRecordType(t
, field_name
, field_type
,
41 lldb::AccessType::eAccessPublic
, 7);
42 TypeSystemClang::CompleteTagDeclarationDefinition(t
);
47 /// Simulates a Clang type system owned by a TypeSystemMap.
48 class TypeSystemClangHolder
{
49 std::shared_ptr
<TypeSystemClang
> m_ast
;
51 TypeSystemClangHolder(const char *name
)
52 : m_ast(std::make_shared
<TypeSystemClang
>(name
,
53 HostInfo::GetTargetTriple())) {}
54 TypeSystemClang
*GetAST() const { return m_ast
.get(); }
57 /// Constructs a TypeSystemClang that contains a single RecordDecl that contains
58 /// a single FieldDecl. Utility class as this setup is a common starting point
59 /// for unit test that exercise the ASTImporter.
60 struct SourceASTWithRecord
{
61 std::unique_ptr
<TypeSystemClangHolder
> holder
;
63 CompilerType record_type
;
64 clang::RecordDecl
*record_decl
= nullptr;
65 clang::FieldDecl
*field_decl
= nullptr;
66 SourceASTWithRecord() {
67 holder
= std::make_unique
<TypeSystemClangHolder
>("test ASTContext");
68 ast
= holder
->GetAST();
69 record_type
= createRecordWithField(
70 *ast
, "Source", ast
->GetBasicType(lldb::BasicType::eBasicTypeChar
),
73 llvm::cast
<clang::RecordDecl
>(ClangUtil::GetAsTagDecl(record_type
));
74 field_decl
= *record_decl
->fields().begin();
79 } // namespace clang_utils
80 } // namespace lldb_private