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