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