1 //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
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
7 //===----------------------------------------------------------------------===//
9 // Unit tests for CodeGenAction.
11 //===----------------------------------------------------------------------===//
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"
21 using namespace clang
;
22 using namespace clang::frontend
;
27 class NullCodeGenAction
: public CodeGenAction
{
29 NullCodeGenAction(llvm::LLVMContext
*_VMContext
= nullptr)
30 : CodeGenAction(Backend_EmitMCNull
, _VMContext
) {}
32 // The action does not call methods of ATContext.
33 void ExecuteAction() override
{
34 CompilerInstance
&CI
= getCompilerInstance();
35 if (!CI
.hasPreprocessor())
38 CI
.createSema(getTranslationUnitKind(), nullptr);
43 TEST(CodeGenTest
, TestNullCodeGen
) {
44 auto Invocation
= std::make_shared
<CompilerInvocation
>();
45 Invocation
->getPreprocessorOpts().addRemappedFile(
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());
57 std::unique_ptr
<FrontendAction
> Act(new NullCodeGenAction
);
58 bool Success
= Compiler
.ExecuteAction(*Act
);