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/DIBuilder.h"
19 #include "llvm/DebugInfo.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Transforms/Instrumentation.h"
26 // These tests do not depend on MCJIT, but we use the TrivialModuleBuilder
27 // helper class to construct some trivial Modules.
28 #include "../unittests/ExecutionEngine/MCJIT/MCJITTestBase.h"
32 #include "gtest/gtest.h"
34 #if defined(LLVM_ON_WIN32)
36 #define getcwd_impl _getcwd
37 #elif defined (HAVE_GETCWD)
39 #define getcwd_impl getcwd
40 #endif // LLVM_ON_WIN32
47 /// Insert a mock CUDescriptor with the specified producer
48 void insertCUDescriptor(Module *M, StringRef File, StringRef Dir,
51 B.createCompileUnit(dwarf::DW_LANG_C99, File, Dir, Producer, false, "", 0);
55 /// Attempts to remove file at Path and returns true if it existed, or false if
57 bool removeIfExists(StringRef Path) {
59 sys::fs::remove(Path, existed);
63 char * current_dir() {
64 #if defined(LLVM_ON_WIN32) || defined(HAVE_GETCWD)
65 // calling getcwd (or _getcwd() on windows) with a null buffer makes it
66 // allocate a sufficiently sized buffer to store the current working dir.
67 return getcwd_impl(0, 0);
73 class TestDebugIR : public ::testing::Test, public TrivialModuleBuilder {
76 : TrivialModuleBuilder(sys::getProcessTriple())
77 , cwd(current_dir()) {}
79 ~TestDebugIR() { free(cwd); }
81 /// Returns a concatenated path string consisting of Dir and Filename
82 string getPath(const string &Dir, const string &Filename) {
83 SmallVector<char, 8> Path;
84 sys::path::append(Path, Dir, Filename);
85 Path.resize(Dir.size() + Filename.size() + 2);
86 Path[Dir.size() + Filename.size() + 1] = '\0';
87 return string(Path.data());
96 // Test empty named Module that is not supposed to be output to disk.
97 TEST_F(TestDebugIR, EmptyNamedModuleNoWrite) {
98 string Dir = "MadeUpDirectory";
99 string File = "empty_module.ll";
100 string Path(getPath(Dir, File));
102 M.reset(createEmptyModule(Path));
104 // constructing DebugIR with no args should not result in any file generated.
105 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
108 // verify DebugIR did not generate a file
109 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
112 // Test a non-empty unnamed module that is output to an autogenerated file name.
113 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToAutogeneratedFile) {
114 M.reset(createEmptyModule());
115 insertAddFunction(M.get());
116 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
119 D->runOnModule(*M, Path);
121 // verify DebugIR generated a file, and clean it up
122 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
125 // Test not specifying a name in the module -- DebugIR should generate a name
126 // and write the file contents.
127 TEST_F(TestDebugIR, EmptyModuleWriteAnonymousFile) {
128 M.reset(createEmptyModule());
129 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(false, false)));
132 D->runOnModule(*M, Path);
134 // verify DebugIR generated a file and clean it up
135 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
138 #ifdef HAVE_GETCWD // These tests require get_current_dir_name()
140 // Test empty named Module that is to be output to path specified at Module
142 TEST_F(TestDebugIR, EmptyNamedModuleWriteFile) {
143 string Filename("NamedFile1");
144 string ExpectedPath(getPath(cwd, Filename));
146 M.reset(createEmptyModule(ExpectedPath));
147 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
150 D->runOnModule(*M, Path);
152 // verify DebugIR was able to correctly parse the file name from module ID
153 ASSERT_EQ(ExpectedPath, Path);
155 // verify DebugIR generated a file, and clean it up
156 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
159 // Test an empty unnamed module generates an output file whose path is specified
160 // at DebugIR construction.
161 TEST_F(TestDebugIR, EmptyUnnamedModuleWriteNamedFile) {
162 string Filename("NamedFile2");
164 M.reset(createEmptyModule());
165 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
166 false, false, StringRef(cwd), StringRef(Filename))));
168 D->runOnModule(*M, Path);
170 string ExpectedPath(getPath(cwd, Filename));
171 ASSERT_EQ(ExpectedPath, Path);
173 // verify DebugIR generated a file, and clean it up
174 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
177 // Test an empty named module generates an output file at the path specified
178 // during DebugIR construction.
179 TEST_F(TestDebugIR, EmptyNamedModuleWriteNamedFile) {
180 string Filename("NamedFile3");
182 string UnexpectedPath(getPath(cwd, "UnexpectedFilename"));
183 M.reset(createEmptyModule(UnexpectedPath));
185 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
186 false, false, StringRef(cwd), StringRef(Filename))));
188 D->runOnModule(*M, Path);
190 string ExpectedPath(getPath(cwd, Filename));
191 ASSERT_EQ(ExpectedPath, Path);
193 // verify DebugIR generated a file, and clean it up
194 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
196 // verify DebugIR did not generate a file at the path specified at Module
198 ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path;
201 // Test a non-empty named module that is not supposed to be output to disk
202 TEST_F(TestDebugIR, NonEmptyNamedModuleNoWrite) {
203 string Filename("NamedFile4");
204 string ExpectedPath(getPath(cwd, Filename));
206 M.reset(createEmptyModule(ExpectedPath));
207 insertAddFunction(M.get());
209 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
212 D->runOnModule(*M, Path);
213 ASSERT_EQ(ExpectedPath, Path);
215 // verify DebugIR did not generate a file
216 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
219 // Test a non-empty named module that is output to disk.
220 TEST_F(TestDebugIR, NonEmptyNamedModuleWriteFile) {
221 string Filename("NamedFile5");
222 string ExpectedPath(getPath(cwd, Filename));
224 M.reset(createEmptyModule(ExpectedPath));
225 insertAddFunction(M.get());
227 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
230 D->runOnModule(*M, Path);
231 ASSERT_EQ(ExpectedPath, Path);
233 // verify DebugIR generated a file, and clean it up
234 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
237 // Test a non-empty unnamed module is output to a path specified at DebugIR
239 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToNamedFile) {
240 string Filename("NamedFile6");
242 M.reset(createEmptyModule());
243 insertAddFunction(M.get());
245 D.reset(static_cast<DebugIR *>(
246 llvm::createDebugIRPass(true, true, cwd, Filename)));
248 D->runOnModule(*M, Path);
250 string ExpectedPath(getPath(cwd, Filename));
251 ASSERT_EQ(ExpectedPath, Path);
253 // verify DebugIR generated a file, and clean it up
254 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
257 // Test that information inside existing debug metadata is retained
258 TEST_F(TestDebugIR, ExistingMetadataRetained) {
259 string Filename("NamedFile7");
260 string ExpectedPath(getPath(cwd, Filename));
262 M.reset(createEmptyModule(ExpectedPath));
263 insertAddFunction(M.get());
265 StringRef Producer("TestProducer");
266 insertCUDescriptor(M.get(), Filename, cwd, Producer);
268 DebugInfoFinder Finder;
269 Finder.processModule(*M);
270 ASSERT_EQ((unsigned)1, Finder.compile_unit_count());
271 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
274 D->runOnModule(*M, Path);
275 ASSERT_EQ(ExpectedPath, Path);
277 // verify DebugIR did not generate a file
278 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
280 DICompileUnit CU(*Finder.compile_unit_begin());
282 // Verify original CU information is retained
283 ASSERT_EQ(Filename, CU.getFilename());
284 ASSERT_EQ(cwd, CU.getDirectory());
285 ASSERT_EQ(Producer, CU.getProducer());
288 #endif // HAVE_GETCWD
290 #ifdef GTEST_HAS_DEATH_TEST
292 // Test a non-empty unnamed module that is not supposed to be output to disk
293 // NOTE: this test is expected to die with LLVM_ERROR, and such depends on
294 // google test's "death test" mode.
295 TEST_F(TestDebugIR, NonEmptyUnnamedModuleNoWrite) {
296 M.reset(createEmptyModule(StringRef()));
297 insertAddFunction(M.get());
298 D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
300 // No name in module or on DebugIR construction ==> DebugIR should assert
301 EXPECT_DEATH(D->runOnModule(*M),
302 "DebugIR unable to determine file name in input.");
305 #endif // GTEST_HAS_DEATH_TEST