82 lines · cpp
1//===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Unit tests for CodeGenAction.10//11//===----------------------------------------------------------------------===//12 13#include "clang/CodeGen/CodeGenAction.h"14#include "clang/Basic/LangStandard.h"15#include "clang/CodeGen/BackendUtil.h"16#include "clang/Frontend/CompilerInstance.h"17#include "clang/Lex/PreprocessorOptions.h"18#include "llvm/Support/FormatVariadic.h"19#include "llvm/Support/VirtualFileSystem.h"20#include "gtest/gtest.h"21 22using namespace llvm;23using namespace clang;24using namespace clang::frontend;25 26namespace {27 28 29class NullCodeGenAction : public CodeGenAction {30public:31 NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)32 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}33 34 // The action does not call methods of ATContext.35 void ExecuteAction() override {36 CompilerInstance &CI = getCompilerInstance();37 if (!CI.hasPreprocessor())38 return;39 if (!CI.hasSema())40 CI.createSema(getTranslationUnitKind(), nullptr);41 }42};43 44 45TEST(CodeGenTest, TestNullCodeGen) {46 auto Invocation = std::make_shared<CompilerInvocation>();47 Invocation->getPreprocessorOpts().addRemappedFile(48 "test.cc",49 MemoryBuffer::getMemBuffer("").release());50 Invocation->getFrontendOpts().Inputs.push_back(51 FrontendInputFile("test.cc", Language::CXX));52 Invocation->getFrontendOpts().ProgramAction = EmitLLVM;53 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";54 CompilerInstance Compiler(std::move(Invocation));55 Compiler.setVirtualFileSystem(llvm::vfs::getRealFileSystem());56 Compiler.createDiagnostics();57 EXPECT_TRUE(Compiler.hasDiagnostics());58 59 std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);60 bool Success = Compiler.ExecuteAction(*Act);61 EXPECT_TRUE(Success);62}63 64TEST(CodeGenTest, CodeGenFromIRMemBuffer) {65 auto Invocation = std::make_shared<CompilerInvocation>();66 std::unique_ptr<MemoryBuffer> MemBuffer =67 MemoryBuffer::getMemBuffer("", "test.ll");68 Invocation->getFrontendOpts().Inputs.push_back(69 FrontendInputFile(*MemBuffer, Language::LLVM_IR));70 Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly;71 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";72 CompilerInstance Compiler(std::move(Invocation));73 Compiler.setVirtualFileSystem(llvm::vfs::getRealFileSystem());74 Compiler.createDiagnostics();75 EXPECT_TRUE(Compiler.hasDiagnostics());76 77 EmitLLVMOnlyAction Action;78 bool Success = Compiler.ExecuteAction(Action);79 EXPECT_TRUE(Success);80}81}82