ClangTestUtils.h
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===- ClangTestUtils.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_CLANGTESTUTILS_H
#define LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_CLANGTESTUTILS_H
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/Host/HostInfo.h"
namespace lldb_private {
namespace clang_utils {
inline clang::DeclarationName getDeclarationName(TypeSystemClang &ast,
llvm::StringRef name) {
clang::IdentifierInfo &II = ast.getASTContext().Idents.get(name);
return ast.getASTContext().DeclarationNames.getIdentifier(&II);
}
inline std::unique_ptr<TypeSystemClang> createAST() {
return std::make_unique<TypeSystemClang>("test ASTContext",
HostInfo::GetTargetTriple());
}
inline CompilerType createRecord(TypeSystemClang &ast, llvm::StringRef name) {
return ast.CreateRecordType(ast.getASTContext().getTranslationUnitDecl(),
OptionalClangModuleID(),
lldb::AccessType::eAccessPublic, name, 0,
lldb::LanguageType::eLanguageTypeC);
}
/// Create a record with the given name and a field with the given type
/// and name.
inline CompilerType createRecordWithField(TypeSystemClang &ast,
llvm::StringRef record_name,
CompilerType field_type,
llvm::StringRef field_name) {
CompilerType t = createRecord(ast, record_name);
TypeSystemClang::StartTagDeclarationDefinition(t);
ast.AddFieldToRecordType(t, field_name, field_type,
lldb::AccessType::eAccessPublic, 7);
TypeSystemClang::CompleteTagDeclarationDefinition(t);
return t;
}
/// Constructs a TypeSystemClang that contains a single RecordDecl that contains
/// a single FieldDecl. Utility class as this setup is a common starting point
/// for unit test that exercise the ASTImporter.
struct SourceASTWithRecord {
std::unique_ptr<TypeSystemClang> ast;
CompilerType record_type;
clang::RecordDecl *record_decl = nullptr;
clang::FieldDecl *field_decl = nullptr;
SourceASTWithRecord() {
ast = createAST();
record_type = createRecordWithField(
*ast, "Source", ast->GetBasicType(lldb::BasicType::eBasicTypeChar),
"a_field");
record_decl =
llvm::cast<clang::RecordDecl>(ClangUtil::GetAsTagDecl(record_type));
field_decl = *record_decl->fields().begin();
assert(field_decl);
}
};
} // namespace clang_utils
} // namespace lldb_private
#endif