1 | //===- ClangTestUtils.h -----------------------------------------*- C++ -*-===// |
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 | #ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_CLANGTESTUTILS_H |
10 | #define LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_CLANGTESTUTILS_H |
11 | |
12 | #include "Plugins/ExpressionParser/Clang/ClangUtil.h" |
13 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
14 | #include "lldb/Host/HostInfo.h" |
15 | |
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: name); |
21 | return ast.getASTContext().DeclarationNames.getIdentifier(ID: &II); |
22 | } |
23 | |
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); |
29 | } |
30 | |
31 | /// Create a record with the given name and a field with the given type |
32 | /// and name. |
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, name: record_name); |
38 | |
39 | TypeSystemClang::StartTagDeclarationDefinition(type: t); |
40 | ast.AddFieldToRecordType(type: t, name: field_name, field_type, |
41 | access: lldb::AccessType::eAccessPublic, bitfield_bit_size: 7); |
42 | TypeSystemClang::CompleteTagDeclarationDefinition(type: t); |
43 | |
44 | return t; |
45 | } |
46 | |
47 | /// Simulates a Clang type system owned by a TypeSystemMap. |
48 | class TypeSystemClangHolder { |
49 | std::shared_ptr<TypeSystemClang> m_ast; |
50 | public: |
51 | TypeSystemClangHolder(const char *name) |
52 | : m_ast(std::make_shared<TypeSystemClang>(args&: name, |
53 | args: HostInfo::GetTargetTriple())) {} |
54 | TypeSystemClang *GetAST() const { return m_ast.get(); } |
55 | }; |
56 | |
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; |
62 | TypeSystemClang *ast; |
63 | CompilerType record_type; |
64 | clang::RecordDecl *record_decl = nullptr; |
65 | clang::FieldDecl *field_decl = nullptr; |
66 | SourceASTWithRecord() { |
67 | holder = std::make_unique<TypeSystemClangHolder>(args: "test ASTContext" ); |
68 | ast = holder->GetAST(); |
69 | record_type = createRecordWithField( |
70 | ast&: *ast, record_name: "Source" , field_type: ast->GetBasicType(type: lldb::BasicType::eBasicTypeChar), |
71 | field_name: "a_field" ); |
72 | record_decl = |
73 | llvm::cast<clang::RecordDecl>(Val: ClangUtil::GetAsTagDecl(type: record_type)); |
74 | field_decl = *record_decl->fields().begin(); |
75 | assert(field_decl); |
76 | } |
77 | }; |
78 | |
79 | } // namespace clang_utils |
80 | } // namespace lldb_private |
81 | |
82 | #endif |
83 | |