| 1 |
//=== unittests/CodeGen/TestCompiler.h - Match on the LLVM IR ---*- 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 CLANG_UNITTESTS_CODEGEN_TESTCOMPILER_H |
| 10 |
#define CLANG_UNITTESTS_CODEGEN_TESTCOMPILER_H |
| 11 |
|
| 12 |
|
| 13 |
#include "clang/AST/ASTConsumer.h" |
| 14 |
#include "clang/Basic/TargetInfo.h" |
| 15 |
#include "clang/Basic/TargetOptions.h" |
| 16 |
#include "clang/CodeGen/ModuleBuilder.h" |
| 17 |
#include "clang/Frontend/CompilerInstance.h" |
| 18 |
#include "clang/Parse/ParseAST.h" |
| 19 |
|
| 20 |
#include "llvm/IR/Constants.h" |
| 21 |
#include "llvm/IR/LLVMContext.h" |
| 22 |
#include "llvm/IR/Module.h" |
| 23 |
#include "llvm/Support/VirtualFileSystem.h" |
| 24 |
#include "llvm/TargetParser/Host.h" |
| 25 |
|
| 26 |
namespace llvm { |
| 27 |
|
| 28 |
struct TestCompiler { |
| 29 |
LLVMContext Context; |
| 30 |
clang::CompilerInstance compiler; |
| 31 |
std::unique_ptr<clang::CodeGenerator> CG; |
| 32 |
llvm::Module *M = nullptr; |
| 33 |
unsigned PtrSize = 0; |
| 34 |
|
| 35 |
TestCompiler(clang::LangOptions LO, |
| 36 |
clang::CodeGenOptions CGO = clang::CodeGenOptions()) { |
| 37 |
compiler.getLangOpts() = LO; |
| 38 |
compiler.getCodeGenOpts() = CGO; |
| 39 |
compiler.setVirtualFileSystem(llvm::vfs::getRealFileSystem()); |
| 40 |
compiler.createDiagnostics(); |
| 41 |
|
| 42 |
std::string TrStr = llvm::Triple::normalize(llvm::sys::getProcessTriple()); |
| 43 |
llvm::Triple Tr(TrStr); |
| 44 |
Tr.setOS(Triple::Linux); |
| 45 |
Tr.setVendor(Triple::VendorType::UnknownVendor); |
| 46 |
Tr.setEnvironment(Triple::EnvironmentType::UnknownEnvironment); |
| 47 |
compiler.getTargetOpts().Triple = Tr.getTriple(); |
| 48 |
compiler.setTarget(clang::TargetInfo::CreateTargetInfo( |
| 49 |
compiler.getDiagnostics(), compiler.getTargetOpts())); |
| 50 |
|
| 51 |
const clang::TargetInfo &TInfo = compiler.getTarget(); |
| 52 |
PtrSize = TInfo.getPointerWidth(clang::LangAS::Default) / 8; |
| 53 |
|
| 54 |
compiler.createFileManager(); |
| 55 |
compiler.createSourceManager(); |
| 56 |
compiler.createPreprocessor(clang::TU_Prefix); |
| 57 |
|
| 58 |
compiler.createASTContext(); |
| 59 |
|
| 60 |
CG = CreateLLVMCodeGen(compiler, "main-module", Context); |
| 61 |
} |
| 62 |
|
| 63 |
void init(const char *TestProgram, |
| 64 |
std::unique_ptr<clang::ASTConsumer> Consumer = nullptr) { |
| 65 |
if (!Consumer) |
| 66 |
Consumer = std::move(CG); |
| 67 |
|
| 68 |
compiler.setASTConsumer(std::move(Consumer)); |
| 69 |
|
| 70 |
compiler.createSema(clang::TU_Prefix, nullptr); |
| 71 |
|
| 72 |
clang::SourceManager &sm = compiler.getSourceManager(); |
| 73 |
sm.setMainFileID(sm.createFileID( |
| 74 |
llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User)); |
| 75 |
} |
| 76 |
|
| 77 |
const BasicBlock *compile() { |
| 78 |
clang::ParseAST(compiler.getSema(), false, false); |
| 79 |
M = |
| 80 |
static_cast<clang::CodeGenerator&>(compiler.getASTConsumer()).GetModule(); |
| 81 |
|
| 82 |
// Do not expect more than one function definition. |
| 83 |
auto FuncPtr = M->begin(); |
| 84 |
for (; FuncPtr != M->end(); ++FuncPtr) |
| 85 |
if (!FuncPtr->isDeclaration()) |
| 86 |
break; |
| 87 |
assert(FuncPtr != M->end()); |
| 88 |
const llvm::Function &Func = *FuncPtr; |
| 89 |
++FuncPtr; |
| 90 |
for (; FuncPtr != M->end(); ++FuncPtr) |
| 91 |
if (!FuncPtr->isDeclaration()) |
| 92 |
break; |
| 93 |
assert(FuncPtr == M->end()); |
| 94 |
|
| 95 |
// The function must consist of single basic block. |
| 96 |
auto BBPtr = Func.begin(); |
| 97 |
assert(Func.begin() != Func.end()); |
| 98 |
const BasicBlock &BB = *BBPtr; |
| 99 |
++BBPtr; |
| 100 |
assert(BBPtr == Func.end()); |
| 101 |
|
| 102 |
return &BB; |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
} // namespace llvm |
| 107 |
#endif // CLANG_UNITTESTS_CODEGEN_TESTCOMPILER_H |