17a545536SSerge Pavlov //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
27a545536SSerge Pavlov //
37a545536SSerge Pavlov //                     The LLVM Compiler Infrastructure
47a545536SSerge Pavlov //
57a545536SSerge Pavlov // This file is distributed under the University of Illinois Open Source
67a545536SSerge Pavlov // License. See LICENSE.TXT for details.
77a545536SSerge Pavlov //
87a545536SSerge Pavlov //===----------------------------------------------------------------------===//
97a545536SSerge Pavlov //
107a545536SSerge Pavlov // Unit tests for CodeGenAction.
117a545536SSerge Pavlov //
127a545536SSerge Pavlov //===----------------------------------------------------------------------===//
137a545536SSerge Pavlov 
147a545536SSerge Pavlov #include "clang/CodeGen/BackendUtil.h"
159670f847SMehdi Amini #include "clang/CodeGen/CodeGenAction.h"
169670f847SMehdi Amini #include "clang/Frontend/CompilerInstance.h"
179670f847SMehdi Amini #include "clang/Lex/PreprocessorOptions.h"
187a545536SSerge Pavlov #include "gtest/gtest.h"
197a545536SSerge Pavlov 
207a545536SSerge Pavlov using namespace llvm;
217a545536SSerge Pavlov using namespace clang;
227a545536SSerge Pavlov using namespace clang::frontend;
237a545536SSerge Pavlov 
247a545536SSerge Pavlov namespace {
257a545536SSerge Pavlov 
267a545536SSerge Pavlov 
277a545536SSerge Pavlov class NullCodeGenAction : public CodeGenAction {
287a545536SSerge Pavlov public:
297a545536SSerge Pavlov   NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
30cd58bd62SDavid L. Jones     : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
317a545536SSerge Pavlov 
327a545536SSerge Pavlov   // The action does not call methods of ATContext.
337a545536SSerge Pavlov   void ExecuteAction() override {
347a545536SSerge Pavlov     CompilerInstance &CI = getCompilerInstance();
357a545536SSerge Pavlov     if (!CI.hasPreprocessor())
367a545536SSerge Pavlov       return;
377a545536SSerge Pavlov     if (!CI.hasSema())
387a545536SSerge Pavlov       CI.createSema(getTranslationUnitKind(), nullptr);
397a545536SSerge Pavlov   }
407a545536SSerge Pavlov };
417a545536SSerge Pavlov 
427a545536SSerge Pavlov 
437a545536SSerge Pavlov TEST(CodeGenTest, TestNullCodeGen) {
44*ea4395ebSDavid Blaikie   auto Invocation = std::make_shared<CompilerInvocation>();
457a545536SSerge Pavlov   Invocation->getPreprocessorOpts().addRemappedFile(
467a545536SSerge Pavlov       "test.cc",
477a545536SSerge Pavlov       MemoryBuffer::getMemBuffer("").release());
487a545536SSerge Pavlov   Invocation->getFrontendOpts().Inputs.push_back(
497a545536SSerge Pavlov       FrontendInputFile("test.cc", IK_CXX));
507a545536SSerge Pavlov   Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
517a545536SSerge Pavlov   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
527a545536SSerge Pavlov   CompilerInstance Compiler;
53*ea4395ebSDavid Blaikie   Compiler.setInvocation(std::move(Invocation));
547a545536SSerge Pavlov   Compiler.createDiagnostics();
557a545536SSerge Pavlov   EXPECT_TRUE(Compiler.hasDiagnostics());
567a545536SSerge Pavlov 
577a545536SSerge Pavlov   std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
587a545536SSerge Pavlov   bool Success = Compiler.ExecuteAction(*Act);
597a545536SSerge Pavlov   EXPECT_TRUE(Success);
607a545536SSerge Pavlov }
617a545536SSerge Pavlov 
627a545536SSerge Pavlov }
63