xref: /llvm-project/clang/unittests/Frontend/CodeGenActionTest.cpp (revision ea4395ebcd37905487b7a978db2f460bc576a5d1)
1 //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unit tests for CodeGenAction.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/CodeGen/BackendUtil.h"
15 #include "clang/CodeGen/CodeGenAction.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:
29   NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
30     : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
31 
32   // The action does not call methods of ATContext.
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 
43 TEST(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", IK_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