17a545536SSerge Pavlov //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
27a545536SSerge Pavlov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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 
139670f847SMehdi Amini #include "clang/CodeGen/CodeGenAction.h"
1409d890d7SRainer Orth #include "clang/Basic/LangStandard.h"
1509d890d7SRainer Orth #include "clang/CodeGen/BackendUtil.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) {
44ea4395ebSDavid 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(
4909d890d7SRainer Orth       FrontendInputFile("test.cc", Language::CXX));
507a545536SSerge Pavlov   Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
517a545536SSerge Pavlov   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
527a545536SSerge Pavlov   CompilerInstance Compiler;
53ea4395ebSDavid 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 
62*b3dae59bSRyan Senanayake TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
63*b3dae59bSRyan Senanayake   auto Invocation = std::make_shared<CompilerInvocation>();
64*b3dae59bSRyan Senanayake   std::unique_ptr<MemoryBuffer> MemBuffer =
65*b3dae59bSRyan Senanayake       MemoryBuffer::getMemBuffer("", "test.ll");
66*b3dae59bSRyan Senanayake   Invocation->getFrontendOpts().Inputs.push_back(
67*b3dae59bSRyan Senanayake       FrontendInputFile(*MemBuffer, Language::LLVM_IR));
68*b3dae59bSRyan Senanayake   Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly;
69*b3dae59bSRyan Senanayake   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
70*b3dae59bSRyan Senanayake   CompilerInstance Compiler;
71*b3dae59bSRyan Senanayake   Compiler.setInvocation(std::move(Invocation));
72*b3dae59bSRyan Senanayake   Compiler.createDiagnostics();
73*b3dae59bSRyan Senanayake   EXPECT_TRUE(Compiler.hasDiagnostics());
74*b3dae59bSRyan Senanayake 
75*b3dae59bSRyan Senanayake   EmitLLVMOnlyAction Action;
76*b3dae59bSRyan Senanayake   bool Success = Compiler.ExecuteAction(Action);
77*b3dae59bSRyan Senanayake   EXPECT_TRUE(Success);
78*b3dae59bSRyan Senanayake }
797a545536SSerge Pavlov }
80