Skip to content

Commit

Permalink
Move the test compiler setup in a common place. NFCI
Browse files Browse the repository at this point in the history
This patch reduces the copy paste in the unittest/CodeGen folder by moving the
common compiler setup phase in a header file.

Differential revision: https://reviews.llvm.org/D91061
  • Loading branch information
vgvassilev committed Nov 14, 2020
1 parent a70b511 commit 888d06d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 229 deletions.
54 changes: 13 additions & 41 deletions clang/unittests/CodeGen/BufferSourceTest.cpp
Expand Up @@ -6,7 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include "clang/AST/ASTConsumer.h"
#include "TestCompiler.h"

#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/TargetInfo.h"
Expand All @@ -26,54 +27,25 @@ using namespace clang;

namespace {

// Emitting constructors for global objects involves looking
// at the source file name. This makes sure that we don't crash
// if the source file is a memory buffer.
const char TestProgram[] =
TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
// Emitting constructors for global objects involves looking
// at the source file name. This makes sure that we don't crash
// if the source file is a memory buffer.
const char TestProgram[] =
"class EmitCXXGlobalInitFunc "
"{ "
"public: "
" EmitCXXGlobalInitFunc() {} "
"}; "
"EmitCXXGlobalInitFunc test; ";

TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
LLVMContext Context;
CompilerInstance compiler;

compiler.createDiagnostics();
compiler.getLangOpts().CPlusPlus = 1;
compiler.getLangOpts().CPlusPlus11 = 1;

compiler.getTargetOpts().Triple = llvm::Triple::normalize(
llvm::sys::getProcessTriple());
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
compiler.getDiagnostics(),
std::make_shared<clang::TargetOptions>(
compiler.getTargetOpts())));

compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
compiler.createPreprocessor(clang::TU_Prefix);

compiler.createASTContext();

compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
CreateLLVMCodeGen(
compiler.getDiagnostics(),
"EmitCXXGlobalInitFuncTest",
compiler.getHeaderSearchOpts(),
compiler.getPreprocessorOpts(),
compiler.getCodeGenOpts(),
Context)));

compiler.createSema(clang::TU_Prefix, nullptr);

clang::SourceManager &sm = compiler.getSourceManager();
sm.setMainFileID(sm.createFileID(
llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
clang::LangOptions LO;
LO.CPlusPlus = 1;
LO.CPlusPlus11 = 1;
TestCompiler Compiler(LO);
Compiler.init(TestProgram);

clang::ParseAST(compiler.getSema(), false, false);
clang::ParseAST(Compiler.compiler.getSema(), false, false);
}

} // end anonymous namespace
52 changes: 13 additions & 39 deletions clang/unittests/CodeGen/CodeGenExternalTest.cpp
Expand Up @@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include "TestCompiler.h"

#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/GlobalDecl.h"
Expand Down Expand Up @@ -50,11 +52,11 @@ static bool test_codegen_fns_ran;
// before forwarding that function to the CodeGenerator.

struct MyASTConsumer : public ASTConsumer {
std::unique_ptr<CodeGenerator> Builder;
CodeGenerator* Builder;
std::vector<Decl*> toplevel_decls;

MyASTConsumer(std::unique_ptr<CodeGenerator> Builder_in)
: ASTConsumer(), Builder(std::move(Builder_in))
MyASTConsumer(CodeGenerator* Builder_in)
: ASTConsumer(), Builder(Builder_in)
{
}

Expand Down Expand Up @@ -257,45 +259,17 @@ static void test_codegen_fns(MyASTConsumer *my) {
}

TEST(CodeGenExternalTest, CodeGenExternalTest) {
LLVMContext Context;
CompilerInstance compiler;

compiler.createDiagnostics();
compiler.getLangOpts().CPlusPlus = 1;
compiler.getLangOpts().CPlusPlus11 = 1;

compiler.getTargetOpts().Triple = llvm::Triple::normalize(
llvm::sys::getProcessTriple());
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
compiler.getDiagnostics(),
std::make_shared<clang::TargetOptions>(
compiler.getTargetOpts())));

compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
compiler.createPreprocessor(clang::TU_Prefix);

compiler.createASTContext();


compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
new MyASTConsumer(std::unique_ptr<CodeGenerator>(
CreateLLVMCodeGen(compiler.getDiagnostics(),
"MemoryTypesTest",
compiler.getHeaderSearchOpts(),
compiler.getPreprocessorOpts(),
compiler.getCodeGenOpts(),
Context)))));

compiler.createSema(clang::TU_Prefix, nullptr);
clang::LangOptions LO;
LO.CPlusPlus = 1;
LO.CPlusPlus11 = 1;
TestCompiler Compiler(LO);
auto CustomASTConsumer = std::make_unique<MyASTConsumer>(Compiler.CG);

clang::SourceManager &sm = compiler.getSourceManager();
sm.setMainFileID(sm.createFileID(
llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
Compiler.init(TestProgram, std::move(CustomASTConsumer));

clang::ParseAST(compiler.getSema(), false, false);
clang::ParseAST(Compiler.compiler.getSema(), false, false);

ASSERT_TRUE(test_codegen_fns_ran);
ASSERT_TRUE(test_codegen_fns_ran);
}

} // end anonymous namespace
50 changes: 15 additions & 35 deletions clang/unittests/CodeGen/IncrementalProcessingTest.cpp
Expand Up @@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include "TestCompiler.h"

#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
Expand Down Expand Up @@ -108,52 +110,30 @@ const Function* getGlobalInit(llvm::Module& M) {
}

TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
LLVMContext Context;
CompilerInstance compiler;

compiler.createDiagnostics();
compiler.getLangOpts().CPlusPlus = 1;
compiler.getLangOpts().CPlusPlus11 = 1;

compiler.getTargetOpts().Triple = llvm::Triple::normalize(
llvm::sys::getProcessTriple());
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
compiler.getDiagnostics(),
std::make_shared<clang::TargetOptions>(
compiler.getTargetOpts())));

compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
compiler.createPreprocessor(clang::TU_Prefix);
compiler.getPreprocessor().enableIncrementalProcessing();

compiler.createASTContext();

CodeGenerator* CG =
CreateLLVMCodeGen(
compiler.getDiagnostics(),
"main-module",
compiler.getHeaderSearchOpts(),
compiler.getPreprocessorOpts(),
compiler.getCodeGenOpts(),
Context);
compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(CG));
compiler.createSema(clang::TU_Prefix, nullptr);
Sema& S = compiler.getSema();
clang::LangOptions LO;
LO.CPlusPlus = 1;
LO.CPlusPlus11 = 1;
TestCompiler Compiler(LO);
clang::CompilerInstance &CI = Compiler.compiler;
CI.getPreprocessor().enableIncrementalProcessing();
CI.setASTConsumer(std::unique_ptr<ASTConsumer>(Compiler.CG));
CI.createSema(clang::TU_Prefix, nullptr);

Sema& S = CI.getSema();

std::unique_ptr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
/*SkipFunctionBodies*/ false));
Parser &P = *ParseOP.get();

std::array<std::unique_ptr<llvm::Module>, 3> M;
M[0] = IncrementalParseAST(compiler, P, *CG, nullptr);
M[0] = IncrementalParseAST(CI, P, *Compiler.CG, nullptr);
ASSERT_TRUE(M[0]);

M[1] = IncrementalParseAST(compiler, P, *CG, TestProgram1);
M[1] = IncrementalParseAST(CI, P, *Compiler.CG, TestProgram1);
ASSERT_TRUE(M[1]);
ASSERT_TRUE(M[1]->getFunction("funcForProg1"));

M[2] = IncrementalParseAST(compiler, P, *CG, TestProgram2);
M[2] = IncrementalParseAST(CI, P, *Compiler.CG, TestProgram2);
ASSERT_TRUE(M[2]);
ASSERT_TRUE(M[2]->getFunction("funcForProg2"));
// First code should not end up in second module:
Expand Down

0 comments on commit 888d06d

Please sign in to comment.