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/Frontend/CompilerInstance.h"
157a545536SSerge Pavlov #include "clang/CodeGen/CodeGenAction.h"
167a545536SSerge Pavlov #include "clang/CodeGen/BackendUtil.h"
177a545536SSerge Pavlov #include "gtest/gtest.h"
187a545536SSerge Pavlov 
197a545536SSerge Pavlov using namespace llvm;
207a545536SSerge Pavlov using namespace clang;
217a545536SSerge Pavlov using namespace clang::frontend;
227a545536SSerge Pavlov 
237a545536SSerge Pavlov namespace {
247a545536SSerge Pavlov 
257a545536SSerge Pavlov 
267a545536SSerge Pavlov class NullCodeGenAction : public CodeGenAction {
277a545536SSerge Pavlov public:
287a545536SSerge Pavlov   NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
29*cd58bd62SDavid L. Jones     : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
307a545536SSerge Pavlov 
317a545536SSerge Pavlov   // The action does not call methods of ATContext.
327a545536SSerge Pavlov   void ExecuteAction() override {
337a545536SSerge Pavlov     CompilerInstance &CI = getCompilerInstance();
347a545536SSerge Pavlov     if (!CI.hasPreprocessor())
357a545536SSerge Pavlov       return;
367a545536SSerge Pavlov     if (!CI.hasSema())
377a545536SSerge Pavlov       CI.createSema(getTranslationUnitKind(), nullptr);
387a545536SSerge Pavlov   }
397a545536SSerge Pavlov };
407a545536SSerge Pavlov 
417a545536SSerge Pavlov 
427a545536SSerge Pavlov TEST(CodeGenTest, TestNullCodeGen) {
437a545536SSerge Pavlov   CompilerInvocation *Invocation = new CompilerInvocation;
447a545536SSerge Pavlov   Invocation->getPreprocessorOpts().addRemappedFile(
457a545536SSerge Pavlov       "test.cc",
467a545536SSerge Pavlov       MemoryBuffer::getMemBuffer("").release());
477a545536SSerge Pavlov   Invocation->getFrontendOpts().Inputs.push_back(
487a545536SSerge Pavlov       FrontendInputFile("test.cc", IK_CXX));
497a545536SSerge Pavlov   Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
507a545536SSerge Pavlov   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
517a545536SSerge Pavlov   CompilerInstance Compiler;
527a545536SSerge Pavlov   Compiler.setInvocation(Invocation);
537a545536SSerge Pavlov   Compiler.createDiagnostics();
547a545536SSerge Pavlov   EXPECT_TRUE(Compiler.hasDiagnostics());
557a545536SSerge Pavlov 
567a545536SSerge Pavlov   std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
577a545536SSerge Pavlov   bool Success = Compiler.ExecuteAction(*Act);
587a545536SSerge Pavlov   EXPECT_TRUE(Success);
597a545536SSerge Pavlov }
607a545536SSerge Pavlov 
617a545536SSerge Pavlov }
62