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