Debian Codegraph @

clang/unittests/Frontend/CodeGenActionTest.cpp

raw
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 "llvm/Support/FormatVariadic.h"
19 #include "llvm/Support/VirtualFileSystem.h"
20 #include "gtest/gtest.h"
21
22 using namespace llvm;
23 using namespace clang;
24 using namespace clang::frontend;
25
26 namespace {
27
28
29 class NullCodeGenAction : public CodeGenAction {
30 public:
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
45 TEST(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
58 std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
59 bool Success = Compiler.ExecuteAction(*Act);
60 EXPECT_TRUE(Success);
61 }
62
63 TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
64 auto Invocation = std::make_shared<CompilerInvocation>();
65 std::unique_ptr<MemoryBuffer> MemBuffer =
66 MemoryBuffer::getMemBuffer("", "test.ll");
67 Invocation->getFrontendOpts().Inputs.push_back(
68 FrontendInputFile(*MemBuffer, Language::LLVM_IR));
69 Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly;
70 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
71 CompilerInstance Compiler(std::move(Invocation));
72 Compiler.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
73 Compiler.createDiagnostics();
74
75 EmitLLVMOnlyAction Action;
76 bool Success = Compiler.ExecuteAction(Action);
77 EXPECT_TRUE(Success);
78 }
79 }