#include "llvm/ADT/Triple.h"
#include "llvm/Config/config.h"
#include "llvm/DebugInfo.h"
#include "llvm/DIBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Transforms/Instrumentation.h"
#include "../lib/Transforms/Instrumentation/DebugIR.h"
#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 {
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();
}
bool removeIfExists(StringRef Path) {
bool existed = false;
sys::fs::remove(Path, existed);
return existed;
}
char * current_dir() {
#if defined(LLVM_ON_WIN32) || defined(HAVE_GETCWD)
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); }
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_F(TestDebugIR, EmptyNamedModuleNoWrite) {
string Dir = "MadeUpDirectory";
string File = "empty_module.ll";
string Path(getPath(Dir, File));
M.reset(createEmptyModule(Path));
D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
D->runOnModule(*M);
ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
}
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);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}
TEST_F(TestDebugIR, EmptyModuleWriteAnonymousFile) {
M.reset(createEmptyModule());
D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass(false, false)));
string Path;
D->runOnModule(*M, Path);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}
#ifdef HAVE_GETCWD // These tests require get_current_dir_name()
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);
ASSERT_EQ(ExpectedPath, Path);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}
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);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}
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);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path;
}
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);
ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
}
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);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}
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);
ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
}
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);
ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
DICompileUnit CU(*Finder.compile_unit_begin());
ASSERT_EQ(Filename, CU.getFilename());
ASSERT_EQ(cwd, CU.getDirectory());
ASSERT_EQ(Producer, CU.getProducer());
}
#endif // HAVE_GETCWD
#ifdef GTEST_HAS_DEATH_TEST
TEST_F(TestDebugIR, NonEmptyUnnamedModuleNoWrite) {
M.reset(createEmptyModule(StringRef()));
insertAddFunction(M.get());
D.reset(static_cast<DebugIR *>(llvm::createDebugIRPass()));
EXPECT_DEATH(D->runOnModule(*M),
"DebugIR unable to determine file name in input.");
}
#endif // GTEST_HAS_DEATH_TEST
}