1*7a545536SSerge Pavlov //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===// 2*7a545536SSerge Pavlov // 3*7a545536SSerge Pavlov // The LLVM Compiler Infrastructure 4*7a545536SSerge Pavlov // 5*7a545536SSerge Pavlov // This file is distributed under the University of Illinois Open Source 6*7a545536SSerge Pavlov // License. See LICENSE.TXT for details. 7*7a545536SSerge Pavlov // 8*7a545536SSerge Pavlov //===----------------------------------------------------------------------===// 9*7a545536SSerge Pavlov // 10*7a545536SSerge Pavlov // Unit tests for CodeGenAction. 11*7a545536SSerge Pavlov // 12*7a545536SSerge Pavlov //===----------------------------------------------------------------------===// 13*7a545536SSerge Pavlov 14*7a545536SSerge Pavlov #include "clang/Frontend/CompilerInstance.h" 15*7a545536SSerge Pavlov #include "clang/CodeGen/CodeGenAction.h" 16*7a545536SSerge Pavlov #include "clang/CodeGen/BackendUtil.h" 17*7a545536SSerge Pavlov #include "gtest/gtest.h" 18*7a545536SSerge Pavlov 19*7a545536SSerge Pavlov using namespace llvm; 20*7a545536SSerge Pavlov using namespace clang; 21*7a545536SSerge Pavlov using namespace clang::frontend; 22*7a545536SSerge Pavlov 23*7a545536SSerge Pavlov namespace { 24*7a545536SSerge Pavlov 25*7a545536SSerge Pavlov 26*7a545536SSerge Pavlov class NullCodeGenAction : public CodeGenAction { 27*7a545536SSerge Pavlov public: 28*7a545536SSerge Pavlov NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr) 29*7a545536SSerge Pavlov : CodeGenAction(Backend_EmitLL, _VMContext) {} 30*7a545536SSerge Pavlov 31*7a545536SSerge Pavlov // The action does not call methods of ATContext. 32*7a545536SSerge Pavlov void ExecuteAction() override { 33*7a545536SSerge Pavlov CompilerInstance &CI = getCompilerInstance(); 34*7a545536SSerge Pavlov if (!CI.hasPreprocessor()) 35*7a545536SSerge Pavlov return; 36*7a545536SSerge Pavlov if (!CI.hasSema()) 37*7a545536SSerge Pavlov CI.createSema(getTranslationUnitKind(), nullptr); 38*7a545536SSerge Pavlov } 39*7a545536SSerge Pavlov }; 40*7a545536SSerge Pavlov 41*7a545536SSerge Pavlov 42*7a545536SSerge Pavlov TEST(CodeGenTest, TestNullCodeGen) { 43*7a545536SSerge Pavlov CompilerInvocation *Invocation = new CompilerInvocation; 44*7a545536SSerge Pavlov Invocation->getPreprocessorOpts().addRemappedFile( 45*7a545536SSerge Pavlov "test.cc", 46*7a545536SSerge Pavlov MemoryBuffer::getMemBuffer("").release()); 47*7a545536SSerge Pavlov Invocation->getFrontendOpts().Inputs.push_back( 48*7a545536SSerge Pavlov FrontendInputFile("test.cc", IK_CXX)); 49*7a545536SSerge Pavlov Invocation->getFrontendOpts().ProgramAction = EmitLLVM; 50*7a545536SSerge Pavlov Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; 51*7a545536SSerge Pavlov CompilerInstance Compiler; 52*7a545536SSerge Pavlov Compiler.setInvocation(Invocation); 53*7a545536SSerge Pavlov Compiler.createDiagnostics(); 54*7a545536SSerge Pavlov EXPECT_TRUE(Compiler.hasDiagnostics()); 55*7a545536SSerge Pavlov 56*7a545536SSerge Pavlov std::unique_ptr<FrontendAction> Act(new NullCodeGenAction); 57*7a545536SSerge Pavlov bool Success = Compiler.ExecuteAction(*Act); 58*7a545536SSerge Pavlov EXPECT_TRUE(Success); 59*7a545536SSerge Pavlov } 60*7a545536SSerge Pavlov 61*7a545536SSerge Pavlov } 62