Debian Codegraph @

lldb/unittests/TestingSupport/Symbol/ClangTestUtils.h

raw
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);
21 return ast.getASTContext().DeclarationNames.getIdentifier(&II);
22 }
23
24 inline CompilerType
25 createRecord(TypeSystemClang &ast, llvm::StringRef name,
26 lldb::LanguageType lang = lldb::LanguageType::eLanguageTypeC) {
27 return ast.CreateRecordType(ast.getASTContext().getTranslationUnitDecl(),
28 OptionalClangModuleID(),
29 lldb::AccessType::eAccessPublic, name, 0, lang);
30 }
31
32 /// Create a record with the given name and a field with the given type
33 /// and name.
34 inline CompilerType createRecordWithField(
35 TypeSystemClang &ast, llvm::StringRef record_name, CompilerType field_type,
36 llvm::StringRef field_name,
37 lldb::LanguageType lang = lldb::LanguageType::eLanguageTypeC) {
38 CompilerType t = createRecord(ast, record_name, lang);
39
40 TypeSystemClang::StartTagDeclarationDefinition(t);
41 ast.AddFieldToRecordType(t, field_name, field_type,
42 lldb::AccessType::eAccessPublic, 7);
43 TypeSystemClang::CompleteTagDeclarationDefinition(t);
44
45 return t;
46 }
47
48 /// Simulates a Clang type system owned by a TypeSystemMap.
49 class TypeSystemClangHolder {
50 std::shared_ptr<TypeSystemClang> m_ast;
51 public:
52 TypeSystemClangHolder(const char *name)
53 : m_ast(std::make_shared<TypeSystemClang>(name,
54 HostInfo::GetTargetTriple())) {}
55 TypeSystemClang *GetAST() const { return m_ast.get(); }
56 };
57
58 /// Constructs a TypeSystemClang that contains a single RecordDecl that contains
59 /// a single FieldDecl. Utility class as this setup is a common starting point
60 /// for unit test that exercise the ASTImporter.
61 struct SourceASTWithRecord {
62 std::unique_ptr<TypeSystemClangHolder> holder;
63 TypeSystemClang *ast;
64 CompilerType record_type;
65 clang::RecordDecl *record_decl = nullptr;
66 clang::FieldDecl *field_decl = nullptr;
67 SourceASTWithRecord(
68 lldb::LanguageType lang = lldb::LanguageType::eLanguageTypeC) {
69 holder = std::make_unique<TypeSystemClangHolder>("test ASTContext");
70 ast = holder->GetAST();
71 record_type = createRecordWithField(
72 *ast, "Source", ast->GetBasicType(lldb::BasicType::eBasicTypeChar),
73 "a_field", lang);
74 record_decl =
75 llvm::cast<clang::RecordDecl>(ClangUtil::GetAsTagDecl(record_type));
76 field_decl = *record_decl->fields().begin();
77 assert(field_decl);
78 }
79 };
80
81 } // namespace clang_utils
82 } // namespace lldb_private
83
84 #endif