summaryrefslogtreecommitdiff
path: root/unittests/Transforms/DebugIR/DebugIR.cpp
blob: 590fa8eac6ce531ac89253bac2790d783c50d62c (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//===- DebugIR.cpp - Unit tests for the DebugIR pass ----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// The tests in this file verify the DebugIR pass that generates debug metadata
// for LLVM IR.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Triple.h"
#include "../lib/Transforms/Instrumentation/DebugIR.h"
#include "llvm/Config/config.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Path.h"
#include "llvm/Transforms/Instrumentation.h"

// These tests do not depend on MCJIT, but we use the TrivialModuleBuilder
// helper class to construct some trivial Modules.
#include "../unittests/ExecutionEngine/MCJIT/MCJITTestBase.h"

#include <string>

#include "gtest/gtest.h"

#if defined(LLVM_ON_WIN32)
#include <direct.h>
#define getcwd_impl _getcwd
#elif defined (HAVE_GETCWD)
#include <unistd.h>
#define getcwd_impl getcwd
#endif // LLVM_ON_WIN32

using namespace llvm;
using namespace std;

namespace {

/// Insert a mock CUDescriptor with the specified producer
void insertCUDescriptor(Module *M, StringRef File, StringRef Dir,
                        StringRef Producer) {
  DIBuilder B(*M);
  B.createCompileUnit(dwarf::DW_LANG_C99, File, Dir, Producer, false, "", 0);
  B.finalize();
}

/// Attempts to remove file at Path and returns true if it existed, or false if
/// it did not.
bool removeIfExists(StringRef Path) {
  // This is an approximation, on error we don't know in general if the file
  // existed or not.
  llvm::error_code EC = sys::fs::remove(Path, false);
  return EC != llvm::errc::no_such_file_or_directory;
}

char * current_dir() {
#if defined(LLVM_ON_WIN32) || defined(HAVE_GETCWD)
  // calling getcwd (or _getcwd() on windows) with a null buffer makes it
  // allocate a sufficiently sized buffer to store the current working dir.
  return getcwd_impl(0, 0);
#else
  return 0;
#endif
}

class TestDebugIR : public ::testing::Test, public TrivialModuleBuilder {
protected:
  TestDebugIR()
      : TrivialModuleBuilder(sys::getProcessTriple())
      , cwd(current_dir()) {}

  ~TestDebugIR() { free(cwd); }

  /// Returns a concatenated path string consisting of Dir and Filename
  string getPath(const string &Dir, const string &Filename) {
    SmallVector<char, 8> Path;
    sys::path::append(Path, Dir, Filename);
    Path.resize(Dir.size() + Filename.size() + 2);
    Path[Dir.size() + Filename.size() + 1] = '\0';
    return string(Path.data());
  }

  LLVMContext Context;
  char *cwd;
  OwningPtr<Module> M;
  OwningPtr<DebugIR> D;
};

// Test empty named Module that is not supposed to be output to disk.
TEST_F(TestDebugIR, EmptyNamedModuleNoWrite) {
  string Dir = "MadeUpDirectory";
  string File = "empty_module.ll";
  string Path(getPath(Dir, File));

  M.reset(createEmptyModule(Path));

  // constructing DebugIR with no args should not result in any file generated.
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
  D->runOnModule(*M);

  // verify DebugIR did not generate a file
  ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
}

// Test a non-empty unnamed module that is output to an autogenerated file name.
TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToAutogeneratedFile) {
  M.reset(createEmptyModule());
  insertAddFunction(M.get());
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));

  string Path;
  D->runOnModule(*M, Path);

  // verify DebugIR generated a file, and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}

// Test not specifying a name in the module -- DebugIR should generate a name
// and write the file contents.
TEST_F(TestDebugIR, EmptyModuleWriteAnonymousFile) {
  M.reset(createEmptyModule());
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(false, false)));

  string Path;
  D->runOnModule(*M, Path);

  // verify DebugIR generated a file and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}

#ifdef HAVE_GETCWD // These tests require get_current_dir_name()

// Test empty named Module that is to be output to path specified at Module
// construction.
TEST_F(TestDebugIR, EmptyNamedModuleWriteFile) {
  string Filename("NamedFile1");
  string ExpectedPath(getPath(cwd, Filename));

  M.reset(createEmptyModule(ExpectedPath));
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));

  string Path;
  D->runOnModule(*M, Path);

  // verify DebugIR was able to correctly parse the file name from module ID
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR generated a file, and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}

// Test an empty unnamed module generates an output file whose path is specified
// at DebugIR construction.
TEST_F(TestDebugIR, EmptyUnnamedModuleWriteNamedFile) {
  string Filename("NamedFile2");

  M.reset(createEmptyModule());
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
      false, false, StringRef(cwd), StringRef(Filename))));
  string Path;
  D->runOnModule(*M, Path);

  string ExpectedPath(getPath(cwd, Filename));
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR generated a file, and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}

// Test an empty named module generates an output file at the path specified
// during DebugIR construction.
TEST_F(TestDebugIR, EmptyNamedModuleWriteNamedFile) {
  string Filename("NamedFile3");

  string UnexpectedPath(getPath(cwd, "UnexpectedFilename"));
  M.reset(createEmptyModule(UnexpectedPath));

  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(
      false, false, StringRef(cwd), StringRef(Filename))));
  string Path;
  D->runOnModule(*M, Path);

  string ExpectedPath(getPath(cwd, Filename));
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR generated a file, and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;

  // verify DebugIR did not generate a file at the path specified at Module
  // construction.
  ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path;
}

// Test a non-empty named module that is not supposed to be output to disk
TEST_F(TestDebugIR, NonEmptyNamedModuleNoWrite) {
  string Filename("NamedFile4");
  string ExpectedPath(getPath(cwd, Filename));

  M.reset(createEmptyModule(ExpectedPath));
  insertAddFunction(M.get());

  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));

  string Path;
  D->runOnModule(*M, Path);
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR did not generate a file
  ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
}

// Test a non-empty named module that is output to disk.
TEST_F(TestDebugIR, NonEmptyNamedModuleWriteFile) {
  string Filename("NamedFile5");
  string ExpectedPath(getPath(cwd, Filename));

  M.reset(createEmptyModule(ExpectedPath));
  insertAddFunction(M.get());

  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(true, true)));

  string Path;
  D->runOnModule(*M, Path);
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR generated a file, and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}

// Test a non-empty unnamed module is output to a path specified at DebugIR
// construction.
TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToNamedFile) {
  string Filename("NamedFile6");

  M.reset(createEmptyModule());
  insertAddFunction(M.get());

  D.reset(static_cast<DebugIR *>(
      llvm::createDebugIRPass(true, true, cwd, Filename)));
  string Path;
  D->runOnModule(*M, Path);

  string ExpectedPath(getPath(cwd, Filename));
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR generated a file, and clean it up
  ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}

// Test that information inside existing debug metadata is retained
TEST_F(TestDebugIR, ExistingMetadataRetained) {
  string Filename("NamedFile7");
  string ExpectedPath(getPath(cwd, Filename));

  M.reset(createEmptyModule(ExpectedPath));
  insertAddFunction(M.get());

  StringRef Producer("TestProducer");
  insertCUDescriptor(M.get(), Filename, cwd, Producer);

  DebugInfoFinder Finder;
  Finder.processModule(*M);
  ASSERT_EQ((unsigned)1, Finder.compile_unit_count());
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));

  string Path;
  D->runOnModule(*M, Path);
  ASSERT_EQ(ExpectedPath, Path);

  // verify DebugIR did not generate a file
  ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;

  DICompileUnit CU(*Finder.compile_unit_begin());

  // Verify original CU information is retained
  ASSERT_EQ(Filename, CU.getFilename());
  ASSERT_EQ(cwd, CU.getDirectory());
  ASSERT_EQ(Producer, CU.getProducer());
}

#endif // HAVE_GETCWD

#ifdef GTEST_HAS_DEATH_TEST

// Test a non-empty unnamed module that is not supposed to be output to disk
// NOTE: this test is expected to die with LLVM_ERROR, and such depends on
// google test's "death test" mode.
TEST_F(TestDebugIR, NonEmptyUnnamedModuleNoWrite) {
  M.reset(createEmptyModule(StringRef()));
  insertAddFunction(M.get());
  D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));

  // No name in module or on DebugIR construction ==> DebugIR should assert
  EXPECT_DEATH(D->runOnModule(*M),
               "DebugIR unable to determine file name in input.");
}

#endif // GTEST_HAS_DEATH_TEST
}