1 //===- DebugIR.cpp - Unit tests for the DebugIR pass ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The tests in this file verify the DebugIR pass that generates debug metadata
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/Triple.h"
16 #include "../lib/Transforms/Instrumentation/DebugIR.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/IR/DIBuilder.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Host.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Transforms/Instrumentation.h"
27 // These tests do not depend on MCJIT, but we use the TrivialModuleBuilder
28 // helper class to construct some trivial Modules.
29 #include "../unittests/ExecutionEngine/MCJIT/MCJITTestBase.h"
33 #include "gtest/gtest.h"
35 #if defined(LLVM_ON_WIN32)
37 #define getcwd_impl _getcwd
38 #elif defined (HAVE_GETCWD)
40 #define getcwd_impl getcwd
41 #endif // LLVM_ON_WIN32
48 /// Insert a mock CUDescriptor with the specified producer
49 void insertCUDescriptor(Module *M, StringRef File, StringRef Dir,
52 B.createCompileUnit(dwarf::DW_LANG_C99, File, Dir, Producer, false, "", 0);
56 /// Attempts to remove file at Path and returns true if it existed, or false if
58 bool removeIfExists(StringRef Path) {
59 // This is an approximation, on error we don't know in general if the file
61 std::error_code EC = sys::fs::remove(Path, false);
62 return EC != llvm::errc::no_such_file_or_directory;
65 char * current_dir() {
66 #if defined(LLVM_ON_WIN32) || defined(HAVE_GETCWD)
67 // calling getcwd (or _getcwd() on windows) with a null buffer makes it
68 // allocate a sufficiently sized buffer to store the current working dir.
69 return getcwd_impl(nullptr, 0);
75 class TestDebugIR : public ::testing::Test, public TrivialModuleBuilder {
78 : TrivialModuleBuilder(sys::getProcessTriple())
79 , cwd(current_dir()) {}
81 ~TestDebugIR() { free(cwd); }
83 /// Returns a concatenated path string consisting of Dir and Filename
84 string getPath(const string &Dir, const string &Filename) {
85 SmallVector<char, 8> Path;
86 sys::path::append(Path, Dir, Filename);
87 Path.resize(Dir.size() + Filename.size() + 2);
88 Path[Dir.size() + Filename.size() + 1] = '\0';
89 return string(Path.data());
94 std::unique_ptr<Module> M;
95 std::unique_ptr<DebugIR> D;
98 // Test empty named Module that is not supposed to be output to disk.
99 TEST_F(TestDebugIR, EmptyNamedModuleNoWrite) {
100 string Dir = "MadeUpDirectory";
101 string File = "empty_module.ll";
102 string Path(getPath(Dir, File));
104 M.reset(createEmptyModule(Path));
106 // constructing DebugIR with no args should not result in any file generated.
107 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
110 // verify DebugIR did not generate a file
111 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
114 // Test a non-empty unnamed module that is output to an autogenerated file name.
115 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToAutogeneratedFile) {
116 M.reset(createEmptyModule());
117 insertAddFunction(M.get());
118 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
121 D->runOnModule(*M, Path);
123 // verify DebugIR generated a file, and clean it up
124 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
127 // Test not specifying a name in the module -- DebugIR should generate a name
128 // and write the file contents.
129 TEST_F(TestDebugIR, EmptyModuleWriteAnonymousFile) {
130 M.reset(createEmptyModule());
131 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(false, false)));
134 D->runOnModule(*M, Path);
136 // verify DebugIR generated a file and clean it up
137 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
140 #ifdef HAVE_GETCWD // These tests require get_current_dir_name()
142 // Test empty named Module that is to be output to path specified at Module
144 TEST_F(TestDebugIR, EmptyNamedModuleWriteFile) {
145 string Filename("NamedFile1");
146 string ExpectedPath(getPath(cwd, Filename));
148 M.reset(createEmptyModule(ExpectedPath));
149 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
152 D->runOnModule(*M, Path);
154 // verify DebugIR was able to correctly parse the file name from module ID
155 ASSERT_EQ(ExpectedPath, Path);
157 // verify DebugIR generated a file, and clean it up
158 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
161 // Test an empty unnamed module generates an output file whose path is specified
162 // at DebugIR construction.
163 TEST_F(TestDebugIR, EmptyUnnamedModuleWriteNamedFile) {
164 string Filename("NamedFile2");
166 M.reset(createEmptyModule());
167 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
168 false, false, StringRef(cwd), StringRef(Filename))));
170 D->runOnModule(*M, Path);
172 string ExpectedPath(getPath(cwd, Filename));
173 ASSERT_EQ(ExpectedPath, Path);
175 // verify DebugIR generated a file, and clean it up
176 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
179 // Test an empty named module generates an output file at the path specified
180 // during DebugIR construction.
181 TEST_F(TestDebugIR, EmptyNamedModuleWriteNamedFile) {
182 string Filename("NamedFile3");
184 string UnexpectedPath(getPath(cwd, "UnexpectedFilename"));
185 M.reset(createEmptyModule(UnexpectedPath));
187 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
188 false, false, StringRef(cwd), StringRef(Filename))));
190 D->runOnModule(*M, Path);
192 string ExpectedPath(getPath(cwd, Filename));
193 ASSERT_EQ(ExpectedPath, Path);
195 // verify DebugIR generated a file, and clean it up
196 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
198 // verify DebugIR did not generate a file at the path specified at Module
200 ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path;
203 // Test a non-empty named module that is not supposed to be output to disk
204 TEST_F(TestDebugIR, NonEmptyNamedModuleNoWrite) {
205 string Filename("NamedFile4");
206 string ExpectedPath(getPath(cwd, Filename));
208 M.reset(createEmptyModule(ExpectedPath));
209 insertAddFunction(M.get());
211 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
214 D->runOnModule(*M, Path);
215 ASSERT_EQ(ExpectedPath, Path);
217 // verify DebugIR did not generate a file
218 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
221 // Test a non-empty named module that is output to disk.
222 TEST_F(TestDebugIR, NonEmptyNamedModuleWriteFile) {
223 string Filename("NamedFile5");
224 string ExpectedPath(getPath(cwd, Filename));
226 M.reset(createEmptyModule(ExpectedPath));
227 insertAddFunction(M.get());
229 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
232 D->runOnModule(*M, Path);
233 ASSERT_EQ(ExpectedPath, Path);
235 // verify DebugIR generated a file, and clean it up
236 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
239 // Test a non-empty unnamed module is output to a path specified at DebugIR
241 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToNamedFile) {
242 string Filename("NamedFile6");
244 M.reset(createEmptyModule());
245 insertAddFunction(M.get());
247 D.reset(static_cast<DebugIR *>(
248 llvm::createDebugIRPass(true, true, cwd, Filename)));
250 D->runOnModule(*M, Path);
252 string ExpectedPath(getPath(cwd, Filename));
253 ASSERT_EQ(ExpectedPath, Path);
255 // verify DebugIR generated a file, and clean it up
256 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
259 // Test that information inside existing debug metadata is retained
260 TEST_F(TestDebugIR, ExistingMetadataRetained) {
261 string Filename("NamedFile7");
262 string ExpectedPath(getPath(cwd, Filename));
264 M.reset(createEmptyModule(ExpectedPath));
265 insertAddFunction(M.get());
267 StringRef Producer("TestProducer");
268 insertCUDescriptor(M.get(), Filename, cwd, Producer);
270 DebugInfoFinder Finder;
271 Finder.processModule(*M);
272 ASSERT_EQ((unsigned)1, Finder.compile_unit_count());
273 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
276 D->runOnModule(*M, Path);
277 ASSERT_EQ(ExpectedPath, Path);
279 // verify DebugIR did not generate a file
280 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
282 DICompileUnit CU(*Finder.compile_units().begin());
284 // Verify original CU information is retained
285 ASSERT_EQ(Filename, CU.getFilename());
286 ASSERT_EQ(cwd, CU.getDirectory());
287 ASSERT_EQ(Producer, CU.getProducer());
290 #endif // HAVE_GETCWD
292 #ifdef GTEST_HAS_DEATH_TEST
294 // Test a non-empty unnamed module that is not supposed to be output to disk
295 // NOTE: this test is expected to die with LLVM_ERROR, and such depends on
296 // google test's "death test" mode.
297 TEST_F(TestDebugIR, NonEmptyUnnamedModuleNoWrite) {
298 M.reset(createEmptyModule(StringRef()));
299 insertAddFunction(M.get());
300 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
302 // No name in module or on DebugIR construction ==> DebugIR should assert
303 EXPECT_DEATH(D->runOnModule(*M),
304 "DebugIR unable to determine file name in input.");
307 #endif // GTEST_HAS_DEATH_TEST