LLVM 20.0.0git
SupportHelpers.cpp
Go to the documentation of this file.
1
3
5#include "llvm/ADT/Twine.h"
9#include "llvm/Support/Path.h"
10
11#include "gtest/gtest.h"
12
13using namespace llvm;
14using namespace llvm::unittest;
15
16static std::pair<bool, SmallString<128>> findSrcDirMap(StringRef Argv0) {
18
20
21 SmallString<128> PathInSameDir = BaseDir;
22 llvm::sys::path::append(PathInSameDir, "llvm.srcdir.txt");
23
24 if (llvm::sys::fs::is_regular_file(PathInSameDir))
25 return std::make_pair(true, std::move(PathInSameDir));
26
27 SmallString<128> PathInParentDir = llvm::sys::path::parent_path(BaseDir);
28
29 llvm::sys::path::append(PathInParentDir, "llvm.srcdir.txt");
30 if (llvm::sys::fs::is_regular_file(PathInParentDir))
31 return std::make_pair(true, std::move(PathInParentDir));
32
33 return std::pair<bool, SmallString<128>>(false, {});
34}
35
37 bool Found = false;
38 SmallString<128> InputFilePath;
39 std::tie(Found, InputFilePath) = findSrcDirMap(Argv0);
40
41 EXPECT_TRUE(Found) << "Unit test source directory file does not exist.";
42
43 auto File = MemoryBuffer::getFile(InputFilePath, /*IsText=*/true);
44
45 EXPECT_TRUE(static_cast<bool>(File))
46 << "Could not open unit test source directory file.";
47
48 InputFilePath.clear();
49 InputFilePath.append((*File)->getBuffer().trim());
50 llvm::sys::path::append(InputFilePath, "Inputs");
51 llvm::sys::path::native(InputFilePath);
52 return InputFilePath;
53}
This file defines the SmallString class.
static std::pair< bool, SmallString< 128 > > findSrcDirMap(StringRef Argv0)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool is_regular_file(const basic_file_status &status)
Does status represent a regular file?
Definition: Path.cpp:1104
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:906
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:467
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:456
SmallString< 128 > getInputFileDirectory(const char *Argv0)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18