17a545536SSerge Pavlov //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
27a545536SSerge Pavlov //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67a545536SSerge Pavlov //
77a545536SSerge Pavlov //===----------------------------------------------------------------------===//
87a545536SSerge Pavlov //
97a545536SSerge Pavlov // Unit tests for CodeGenAction.
107a545536SSerge Pavlov //
117a545536SSerge Pavlov //===----------------------------------------------------------------------===//
127a545536SSerge Pavlov 
137a545536SSerge Pavlov #include "clang/CodeGen/BackendUtil.h"
149670f847SMehdi Amini #include "clang/CodeGen/CodeGenAction.h"
159670f847SMehdi Amini #include "clang/Frontend/CompilerInstance.h"
169670f847SMehdi Amini #include "clang/Lex/PreprocessorOptions.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)
29cd58bd62SDavid 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) {
43ea4395ebSDavid Blaikie   auto Invocation = std::make_shared<CompilerInvocation>();
447a545536SSerge Pavlov   Invocation->getPreprocessorOpts().addRemappedFile(
457a545536SSerge Pavlov       "test.cc",
467a545536SSerge Pavlov       MemoryBuffer::getMemBuffer("").release());
477a545536SSerge Pavlov   Invocation->getFrontendOpts().Inputs.push_back(
4840c0efa5SRichard Smith       FrontendInputFile("test.cc", InputKind::CXX));
497a545536SSerge Pavlov   Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
507a545536SSerge Pavlov   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
517a545536SSerge Pavlov   CompilerInstance Compiler;
52ea4395ebSDavid Blaikie   Compiler.setInvocation(std::move(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