Re-sort all of the includes with ./utils/sort_includes.py so that
[oota-llvm.git] / unittests / Transforms / DebugIR / DebugIR.cpp
1 //===- DebugIR.cpp - Unit tests for the DebugIR pass ----------------------===//
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 // The tests in this file verify the DebugIR pass that generates debug metadata
11 // for LLVM IR.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
25
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"
29
30 #include <string>
31
32 #include "gtest/gtest.h"
33
34 #if defined(LLVM_ON_WIN32)
35 #include <direct.h>
36 #define getcwd_impl _getcwd
37 #elif defined (HAVE_GETCWD)
38 #include <unistd.h>
39 #define getcwd_impl getcwd
40 #endif // LLVM_ON_WIN32
41
42 using namespace llvm;
43 using namespace std;
44
45 namespace {
46
47 /// Insert a mock CUDescriptor with the specified producer
48 void insertCUDescriptor(Module *M, StringRef File, StringRef Dir,
49                         StringRef Producer) {
50   DIBuilder B(*M);
51   B.createCompileUnit(dwarf::DW_LANG_C99, File, Dir, Producer, false, "", 0);
52   B.finalize();
53 }
54
55 /// Attempts to remove file at Path and returns true if it existed, or false if
56 /// it did not.
57 bool removeIfExists(StringRef Path) {
58   bool existed = false;
59   sys::fs::remove(Path, existed);
60   return existed;
61 }
62
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);
68 #else
69   return 0;
70 #endif
71 }
72
73 class TestDebugIR : public ::testing::Test, public TrivialModuleBuilder {
74 protected:
75   TestDebugIR()
76       : TrivialModuleBuilder(sys::getProcessTriple())
77       , cwd(current_dir()) {}
78
79   ~TestDebugIR() { free(cwd); }
80
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());
88   }
89
90   LLVMContext Context;
91   char *cwd;
92   OwningPtr<Module> M;
93   OwningPtr<DebugIR> D;
94 };
95
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));
101
102   M.reset(createEmptyModule(Path));
103
104   // constructing DebugIR with no args should not result in any file generated.
105   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
106   D->runOnModule(*M);
107
108   // verify DebugIR did not generate a file
109   ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
110 }
111
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)));
117
118   string Path;
119   D->runOnModule(*M, Path);
120
121   // verify DebugIR generated a file, and clean it up
122   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
123 }
124
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)));
130
131   string Path;
132   D->runOnModule(*M, Path);
133
134   // verify DebugIR generated a file and clean it up
135   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
136 }
137
138 #ifdef HAVE_GETCWD // These tests require get_current_dir_name()
139
140 // Test empty named Module that is to be output to path specified at Module
141 // construction.
142 TEST_F(TestDebugIR, EmptyNamedModuleWriteFile) {
143   string Filename("NamedFile1");
144   string ExpectedPath(getPath(cwd, Filename));
145
146   M.reset(createEmptyModule(ExpectedPath));
147   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
148
149   string Path;
150   D->runOnModule(*M, Path);
151
152   // verify DebugIR was able to correctly parse the file name from module ID
153   ASSERT_EQ(ExpectedPath, Path);
154
155   // verify DebugIR generated a file, and clean it up
156   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
157 }
158
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");
163
164   M.reset(createEmptyModule());
165   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
166       false, false, StringRef(cwd), StringRef(Filename))));
167   string Path;
168   D->runOnModule(*M, Path);
169
170   string ExpectedPath(getPath(cwd, Filename));
171   ASSERT_EQ(ExpectedPath, Path);
172
173   // verify DebugIR generated a file, and clean it up
174   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
175 }
176
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");
181
182   string UnexpectedPath(getPath(cwd, "UnexpectedFilename"));
183   M.reset(createEmptyModule(UnexpectedPath));
184
185   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
186       false, false, StringRef(cwd), StringRef(Filename))));
187   string Path;
188   D->runOnModule(*M, Path);
189
190   string ExpectedPath(getPath(cwd, Filename));
191   ASSERT_EQ(ExpectedPath, Path);
192
193   // verify DebugIR generated a file, and clean it up
194   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
195
196   // verify DebugIR did not generate a file at the path specified at Module
197   // construction.
198   ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path;
199 }
200
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));
205
206   M.reset(createEmptyModule(ExpectedPath));
207   insertAddFunction(M.get());
208
209   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
210
211   string Path;
212   D->runOnModule(*M, Path);
213   ASSERT_EQ(ExpectedPath, Path);
214
215   // verify DebugIR did not generate a file
216   ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
217 }
218
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));
223
224   M.reset(createEmptyModule(ExpectedPath));
225   insertAddFunction(M.get());
226
227   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));
228
229   string Path;
230   D->runOnModule(*M, Path);
231   ASSERT_EQ(ExpectedPath, Path);
232
233   // verify DebugIR generated a file, and clean it up
234   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
235 }
236
237 // Test a non-empty unnamed module is output to a path specified at DebugIR
238 // construction.
239 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToNamedFile) {
240   string Filename("NamedFile6");
241
242   M.reset(createEmptyModule());
243   insertAddFunction(M.get());
244
245   D.reset(static_cast<DebugIR *>(
246       llvm::createDebugIRPass(true, true, cwd, Filename)));
247   string Path;
248   D->runOnModule(*M, Path);
249
250   string ExpectedPath(getPath(cwd, Filename));
251   ASSERT_EQ(ExpectedPath, Path);
252
253   // verify DebugIR generated a file, and clean it up
254   ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
255 }
256
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));
261
262   M.reset(createEmptyModule(ExpectedPath));
263   insertAddFunction(M.get());
264
265   StringRef Producer("TestProducer");
266   insertCUDescriptor(M.get(), Filename, cwd, Producer);
267
268   DebugInfoFinder Finder;
269   Finder.processModule(*M);
270   ASSERT_EQ((unsigned)1, Finder.compile_unit_count());
271   D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
272
273   string Path;
274   D->runOnModule(*M, Path);
275   ASSERT_EQ(ExpectedPath, Path);
276
277   // verify DebugIR did not generate a file
278   ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
279
280   DICompileUnit CU(*Finder.compile_unit_begin());
281
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());
286 }
287
288 #endif // HAVE_GETCWD
289
290 #ifdef GTEST_HAS_DEATH_TEST
291
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()));
299
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.");
303 }
304
305 #endif // GTEST_HAS_DEATH_TEST
306 }