1
2 #include "llvm/Testing/Support/SupportHelpers.h"
3
4 #include "llvm/ADT/SmallString.h"
5 #include "llvm/ADT/Twine.h"
6 #include "llvm/Support/FileSystem.h"
7 #include "llvm/Support/MemoryBuffer.h"
8 #include "llvm/Support/Path.h"
9
10 #include "gtest/gtest.h"
11
12 using namespace llvm;
13 using namespace llvm::unittest;
14
findSrcDirMap(StringRef Argv0)15 static std::pair<bool, SmallString<128>> findSrcDirMap(StringRef Argv0) {
16 SmallString<128> BaseDir = llvm::sys::path::parent_path(Argv0);
17
18 llvm::sys::fs::make_absolute(BaseDir);
19
20 SmallString<128> PathInSameDir = BaseDir;
21 llvm::sys::path::append(PathInSameDir, "llvm.srcdir.txt");
22
23 if (llvm::sys::fs::is_regular_file(PathInSameDir))
24 return std::make_pair(true, std::move(PathInSameDir));
25
26 SmallString<128> PathInParentDir = llvm::sys::path::parent_path(BaseDir);
27
28 llvm::sys::path::append(PathInParentDir, "llvm.srcdir.txt");
29 if (llvm::sys::fs::is_regular_file(PathInParentDir))
30 return std::make_pair(true, std::move(PathInParentDir));
31
32 return std::pair<bool, SmallString<128>>(false, {});
33 }
34
getInputFileDirectory(const char * Argv0)35 SmallString<128> llvm::unittest::getInputFileDirectory(const char *Argv0) {
36 bool Found = false;
37 SmallString<128> InputFilePath;
38 std::tie(Found, InputFilePath) = findSrcDirMap(Argv0);
39
40 EXPECT_TRUE(Found) << "Unit test source directory file does not exist.";
41
42 auto File = MemoryBuffer::getFile(InputFilePath, /*IsText=*/true);
43
44 EXPECT_TRUE(static_cast<bool>(File))
45 << "Could not open unit test source directory file.";
46
47 InputFilePath.clear();
48 InputFilePath.append((*File)->getBuffer().trim());
49 llvm::sys::path::append(InputFilePath, "Inputs");
50 llvm::sys::path::native(InputFilePath);
51 return InputFilePath;
52 }
53