1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
//===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Unit tests for CodeGenAction.
//
//===----------------------------------------------------------------------===//
#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Basic/LangStandard.h"
#include "clang/CodeGen/BackendUtil.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
using namespace clang::frontend;
namespace {
class NullCodeGenAction : public CodeGenAction {
public:
NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
: CodeGenAction(Backend_EmitMCNull, _VMContext) {}
// The action does not call methods of ATContext.
void ExecuteAction() override {
CompilerInstance &CI = getCompilerInstance();
if (!CI.hasPreprocessor())
return;
if (!CI.hasSema())
CI.createSema(getTranslationUnitKind(), nullptr);
}
};
TEST(CodeGenTest, TestNullCodeGen) {
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc",
MemoryBuffer::getMemBuffer("").release());
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", Language::CXX));
Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance Compiler(std::move(Invocation));
Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem());
EXPECT_TRUE(Compiler.hasDiagnostics());
std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
bool Success = Compiler.ExecuteAction(*Act);
EXPECT_TRUE(Success);
}
TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
auto Invocation = std::make_shared<CompilerInvocation>();
std::unique_ptr<MemoryBuffer> MemBuffer =
MemoryBuffer::getMemBuffer("", "test.ll");
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile(*MemBuffer, Language::LLVM_IR));
Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly;
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance Compiler(std::move(Invocation));
Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem());
EXPECT_TRUE(Compiler.hasDiagnostics());
EmitLLVMOnlyAction Action;
bool Success = Compiler.ExecuteAction(Action);
EXPECT_TRUE(Success);
}
TEST(CodeGenTest, DebugInfoCWDCodeGen) {
// Check that debug info is accessing the current working directory from the
// VFS instead of calling \p llvm::sys::fs::current_path() directly.
auto Sept = llvm::sys::path::get_separator();
auto VFS = std::make_unique<llvm::vfs::InMemoryFileSystem>();
VFS->setCurrentWorkingDirectory(
std::string(llvm::formatv("{0}in-memory-fs-cwd", Sept)));
std::string TestPath =
std::string(llvm::formatv("{0}in-memory-fs-cwd{0}test.cpp", Sept));
VFS->addFile(TestPath, 0, llvm::MemoryBuffer::getMemBuffer("int x;\n"));
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cpp", Language::CXX));
Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
Invocation->getTargetOpts().Triple = "x86_64-unknown-linux-gnu";
Invocation->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
CompilerInstance Compiler(std::move(Invocation));
SmallString<256> IRBuffer;
Compiler.setOutputStream(std::make_unique<raw_svector_ostream>(IRBuffer));
Compiler.createDiagnostics(*VFS);
Compiler.createFileManager(std::move(VFS));
EmitLLVMAction Action;
bool Success = Compiler.ExecuteAction(Action);
EXPECT_TRUE(Success);
SmallString<128> RealCWD;
llvm::sys::fs::current_path(RealCWD);
EXPECT_TRUE(!RealCWD.empty());
EXPECT_FALSE(IRBuffer.str().contains(RealCWD));
EXPECT_TRUE(IRBuffer.str().contains("in-memory-fs-cwd"));
}
}
|