Path: blob/main/contrib/llvm-project/llvm/lib/Testing/Support/SupportHelpers.cpp
35266 views
1#include "llvm/Testing/Support/SupportHelpers.h"23#include "llvm/ADT/SmallString.h"4#include "llvm/ADT/Twine.h"5#include "llvm/Support/Error.h"6#include "llvm/Support/FileSystem.h"7#include "llvm/Support/MemoryBuffer.h"8#include "llvm/Support/Path.h"910#include "gtest/gtest.h"1112using namespace llvm;13using namespace llvm::unittest;1415static std::pair<bool, SmallString<128>> findSrcDirMap(StringRef Argv0) {16SmallString<128> BaseDir = llvm::sys::path::parent_path(Argv0);1718llvm::sys::fs::make_absolute(BaseDir);1920SmallString<128> PathInSameDir = BaseDir;21llvm::sys::path::append(PathInSameDir, "llvm.srcdir.txt");2223if (llvm::sys::fs::is_regular_file(PathInSameDir))24return std::make_pair(true, std::move(PathInSameDir));2526SmallString<128> PathInParentDir = llvm::sys::path::parent_path(BaseDir);2728llvm::sys::path::append(PathInParentDir, "llvm.srcdir.txt");29if (llvm::sys::fs::is_regular_file(PathInParentDir))30return std::make_pair(true, std::move(PathInParentDir));3132return std::pair<bool, SmallString<128>>(false, {});33}3435SmallString<128> llvm::unittest::getInputFileDirectory(const char *Argv0) {36bool Found = false;37SmallString<128> InputFilePath;38std::tie(Found, InputFilePath) = findSrcDirMap(Argv0);3940EXPECT_TRUE(Found) << "Unit test source directory file does not exist.";4142auto File = MemoryBuffer::getFile(InputFilePath, /*IsText=*/true);4344EXPECT_TRUE(static_cast<bool>(File))45<< "Could not open unit test source directory file.";4647InputFilePath.clear();48InputFilePath.append((*File)->getBuffer().trim());49llvm::sys::path::append(InputFilePath, "Inputs");50llvm::sys::path::native(InputFilePath);51return InputFilePath;52}535455