diff options
author | Raphael Isemann <teemperor@gmail.com> | 2019-12-20 15:09:40 +0100 |
---|---|---|
committer | Raphael Isemann <teemperor@gmail.com> | 2019-12-20 16:13:24 +0100 |
commit | aaa34bc0bd1aeb7271ba0ce2c4f3dfba5dbae8e2 (patch) | |
tree | 925503e432e3f03f5ff7e79d4d800d604af23e14 /lldb/unittests/TestingSupport/Symbol | |
parent | 77e906ac78abda8bb2bdcc223e4b5c457f08bb04 (diff) | |
download | bcm5719-llvm-aaa34bc0bd1aeb7271ba0ce2c4f3dfba5dbae8e2.tar.gz bcm5719-llvm-aaa34bc0bd1aeb7271ba0ce2c4f3dfba5dbae8e2.zip |
[lldb][NFC] Move utility functions from ClangASTImporter and ClangExpressionDeclMap to own header
Diffstat (limited to 'lldb/unittests/TestingSupport/Symbol')
-rw-r--r-- | lldb/unittests/TestingSupport/Symbol/ClangTestUtils.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lldb/unittests/TestingSupport/Symbol/ClangTestUtils.h b/lldb/unittests/TestingSupport/Symbol/ClangTestUtils.h new file mode 100644 index 00000000000..b1b1f30e2f6 --- /dev/null +++ b/lldb/unittests/TestingSupport/Symbol/ClangTestUtils.h @@ -0,0 +1,73 @@ +//===- 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 "lldb/Host/HostInfo.h" +#include "lldb/Symbol/ClangASTContext.h" +#include "lldb/Symbol/ClangUtil.h" + +namespace lldb_private { +namespace clang_utils { +inline clang::DeclarationName getDeclarationName(ClangASTContext &ast, + llvm::StringRef name) { + clang::IdentifierInfo &II = ast.getIdentifierTable()->get(name); + return ast.getASTContext()->DeclarationNames.getIdentifier(&II); +} + +inline std::unique_ptr<ClangASTContext> createAST() { + return std::make_unique<ClangASTContext>(HostInfo::GetTargetTriple()); +} + +inline CompilerType createRecord(ClangASTContext &ast, llvm::StringRef name) { + return ast.CreateRecordType(ast.getASTContext()->getTranslationUnitDecl(), + 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(ClangASTContext &ast, + llvm::StringRef record_name, + CompilerType field_type, + llvm::StringRef field_name) { + CompilerType t = createRecord(ast, record_name); + + ClangASTContext::StartTagDeclarationDefinition(t); + ast.AddFieldToRecordType(t, field_name, field_type, + lldb::AccessType::eAccessPublic, 7); + ClangASTContext::CompleteTagDeclarationDefinition(t); + + return t; +} + +/// Constructs a ClangASTContext 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<ClangASTContext> 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 |