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-exception 6 // 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 "gtest/gtest.h" 19 20 using namespace llvm; 21 using namespace clang; 22 using namespace clang::frontend; 23 24 namespace { 25 26 27 class NullCodeGenAction : public CodeGenAction { 28 public: NullCodeGenAction(llvm::LLVMContext * _VMContext=nullptr)29 NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr) 30 : CodeGenAction(Backend_EmitMCNull, _VMContext) {} 31 32 // The action does not call methods of ATContext. ExecuteAction()33 void ExecuteAction() override { 34 CompilerInstance &CI = getCompilerInstance(); 35 if (!CI.hasPreprocessor()) 36 return; 37 if (!CI.hasSema()) 38 CI.createSema(getTranslationUnitKind(), nullptr); 39 } 40 }; 41 42 TEST(CodeGenTest,TestNullCodeGen)43TEST(CodeGenTest, TestNullCodeGen) { 44 auto Invocation = std::make_shared<CompilerInvocation>(); 45 Invocation->getPreprocessorOpts().addRemappedFile( 46 "test.cc", 47 MemoryBuffer::getMemBuffer("").release()); 48 Invocation->getFrontendOpts().Inputs.push_back( 49 FrontendInputFile("test.cc", Language::CXX)); 50 Invocation->getFrontendOpts().ProgramAction = EmitLLVM; 51 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; 52 CompilerInstance Compiler; 53 Compiler.setInvocation(std::move(Invocation)); 54 Compiler.createDiagnostics(); 55 EXPECT_TRUE(Compiler.hasDiagnostics()); 56 57 std::unique_ptr<FrontendAction> Act(new NullCodeGenAction); 58 bool Success = Compiler.ExecuteAction(*Act); 59 EXPECT_TRUE(Success); 60 } 61 62 } 63