[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- FileSystemTest.cpp ------------------------------------------------===//
|
2016-11-02 00:11:14 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-11-02 00:11:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-01 05:49:27 +08:00
|
|
|
#include "gmock/gmock.h"
|
2016-11-02 00:11:14 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "lldb/Host/FileSystem.h"
|
2018-11-01 05:49:27 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2016-11-02 00:11:14 +08:00
|
|
|
|
|
|
|
extern const char *TestMainArgv0;
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
2018-11-01 05:49:27 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using llvm::sys::fs::UniqueID;
|
|
|
|
|
|
|
|
// Modified from llvm/unittests/Support/VirtualFileSystemTest.cpp
|
|
|
|
namespace {
|
|
|
|
struct DummyFile : public vfs::File {
|
|
|
|
vfs::Status S;
|
|
|
|
explicit DummyFile(vfs::Status S) : S(S) {}
|
|
|
|
llvm::ErrorOr<vfs::Status> status() override { return S; }
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatile) override {
|
|
|
|
llvm_unreachable("unimplemented");
|
|
|
|
}
|
|
|
|
std::error_code close() override { return std::error_code(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class DummyFileSystem : public vfs::FileSystem {
|
|
|
|
int FSID; // used to produce UniqueIDs
|
2022-03-15 04:32:03 +08:00
|
|
|
int FileID = 0; // used to produce UniqueIDs
|
2018-11-01 05:49:27 +08:00
|
|
|
std::string cwd;
|
|
|
|
std::map<std::string, vfs::Status> FilesAndDirs;
|
|
|
|
|
|
|
|
static int getNextFSID() {
|
|
|
|
static int Count = 0;
|
|
|
|
return Count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2022-03-15 04:32:03 +08:00
|
|
|
DummyFileSystem() : FSID(getNextFSID()) {}
|
2018-11-01 05:49:27 +08:00
|
|
|
|
|
|
|
ErrorOr<vfs::Status> status(const Twine &Path) override {
|
|
|
|
std::map<std::string, vfs::Status>::iterator I =
|
|
|
|
FilesAndDirs.find(Path.str());
|
|
|
|
if (I == FilesAndDirs.end())
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
ErrorOr<std::unique_ptr<vfs::File>>
|
|
|
|
openFileForRead(const Twine &Path) override {
|
|
|
|
auto S = status(Path);
|
|
|
|
if (S)
|
|
|
|
return std::unique_ptr<vfs::File>(new DummyFile{*S});
|
|
|
|
return S.getError();
|
|
|
|
}
|
|
|
|
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
|
|
|
|
return cwd;
|
|
|
|
}
|
|
|
|
std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
|
|
|
|
cwd = Path.str();
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
// Map any symlink to "/symlink".
|
2018-11-09 23:18:02 +08:00
|
|
|
std::error_code getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const override {
|
2018-11-01 05:49:27 +08:00
|
|
|
auto I = FilesAndDirs.find(Path.str());
|
|
|
|
if (I == FilesAndDirs.end())
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
if (I->second.isSymlink()) {
|
|
|
|
Output.clear();
|
|
|
|
Twine("/symlink").toVector(Output);
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
Output.clear();
|
|
|
|
Path.toVector(Output);
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DirIterImpl : public llvm::vfs::detail::DirIterImpl {
|
|
|
|
std::map<std::string, vfs::Status> &FilesAndDirs;
|
|
|
|
std::map<std::string, vfs::Status>::iterator I;
|
|
|
|
std::string Path;
|
|
|
|
bool isInPath(StringRef S) {
|
|
|
|
if (Path.size() < S.size() && S.find(Path) == 0) {
|
|
|
|
auto LastSep = S.find_last_of('/');
|
|
|
|
if (LastSep == Path.size() || LastSep == Path.size() - 1)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DirIterImpl(std::map<std::string, vfs::Status> &FilesAndDirs,
|
|
|
|
const Twine &_Path)
|
|
|
|
: FilesAndDirs(FilesAndDirs), I(FilesAndDirs.begin()),
|
|
|
|
Path(_Path.str()) {
|
|
|
|
for (; I != FilesAndDirs.end(); ++I) {
|
|
|
|
if (isInPath(I->first)) {
|
2020-01-29 03:23:46 +08:00
|
|
|
CurrentEntry = vfs::directory_entry(std::string(I->second.getName()),
|
|
|
|
I->second.getType());
|
2018-11-01 05:49:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::error_code increment() override {
|
|
|
|
++I;
|
|
|
|
for (; I != FilesAndDirs.end(); ++I) {
|
|
|
|
if (isInPath(I->first)) {
|
2020-01-29 03:23:46 +08:00
|
|
|
CurrentEntry = vfs::directory_entry(std::string(I->second.getName()),
|
|
|
|
I->second.getType());
|
2018-11-01 05:49:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (I == FilesAndDirs.end())
|
|
|
|
CurrentEntry = vfs::directory_entry();
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vfs::directory_iterator dir_begin(const Twine &Dir,
|
|
|
|
std::error_code &EC) override {
|
|
|
|
return vfs::directory_iterator(
|
|
|
|
std::make_shared<DirIterImpl>(FilesAndDirs, Dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEntry(StringRef Path, const vfs::Status &Status) {
|
2020-01-29 03:23:46 +08:00
|
|
|
FilesAndDirs[std::string(Path)] = Status;
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
|
|
|
|
vfs::Status S(Path, UniqueID(FSID, FileID++),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 1024,
|
|
|
|
sys::fs::file_type::regular_file, Perms);
|
|
|
|
addEntry(Path, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) {
|
|
|
|
vfs::Status S(Path, UniqueID(FSID, FileID++),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 0,
|
|
|
|
sys::fs::file_type::directory_file, Perms);
|
|
|
|
addEntry(Path, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addSymlink(StringRef Path) {
|
|
|
|
vfs::Status S(Path, UniqueID(FSID, FileID++),
|
|
|
|
std::chrono::system_clock::now(), 0, 0, 0,
|
|
|
|
sys::fs::file_type::symlink_file, sys::fs::all_all);
|
|
|
|
addEntry(Path, S);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2016-11-02 00:11:14 +08:00
|
|
|
|
|
|
|
TEST(FileSystemTest, FileAndDirectoryComponents) {
|
|
|
|
using namespace std::chrono;
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSystem fs;
|
2016-11-02 00:11:14 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec fs1("C:\\FILE\\THAT\\DOES\\NOT\\EXIST.TXT");
|
2016-11-02 00:11:14 +08:00
|
|
|
#else
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec fs1("/file/that/does/not/exist.txt");
|
2016-11-02 00:11:14 +08:00
|
|
|
#endif
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec fs2(TestMainArgv0);
|
2016-11-02 00:11:14 +08:00
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
fs.Resolve(fs2);
|
2018-11-01 05:49:27 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(system_clock::time_point(), fs.GetModificationTime(fs1));
|
2016-11-02 00:11:14 +08:00
|
|
|
EXPECT_LT(system_clock::time_point() + hours(24 * 365 * 20),
|
2018-11-01 05:49:27 +08:00
|
|
|
fs.GetModificationTime(fs2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntrusiveRefCntPtr<DummyFileSystem> GetSimpleDummyFS() {
|
|
|
|
IntrusiveRefCntPtr<DummyFileSystem> D(new DummyFileSystem());
|
|
|
|
D->addRegularFile("/foo");
|
|
|
|
D->addDirectory("/bar");
|
|
|
|
D->addSymlink("/baz");
|
|
|
|
D->addRegularFile("/qux", ~sys::fs::perms::all_read);
|
|
|
|
D->setCurrentWorkingDirectory("/");
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, Exists) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_TRUE(fs.Exists("/foo"));
|
2018-11-02 05:05:36 +08:00
|
|
|
EXPECT_TRUE(fs.Exists(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, Readable) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_TRUE(fs.Readable("/foo"));
|
2018-11-02 05:05:36 +08:00
|
|
|
EXPECT_TRUE(fs.Readable(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
|
|
|
|
EXPECT_FALSE(fs.Readable("/qux"));
|
2018-11-02 05:05:36 +08:00
|
|
|
EXPECT_FALSE(fs.Readable(FileSpec("/qux", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, GetByteSize) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_EQ((uint64_t)1024, fs.GetByteSize("/foo"));
|
|
|
|
EXPECT_EQ((uint64_t)1024,
|
2018-11-02 05:05:36 +08:00
|
|
|
fs.GetByteSize(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, GetPermissions) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
EXPECT_EQ(sys::fs::all_all, fs.GetPermissions("/foo"));
|
|
|
|
EXPECT_EQ(sys::fs::all_all,
|
2018-11-02 05:05:36 +08:00
|
|
|
fs.GetPermissions(FileSpec("/foo", FileSpec::Style::posix)));
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, MakeAbsolute) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
{
|
|
|
|
StringRef foo_relative = "foo";
|
|
|
|
SmallString<16> foo(foo_relative);
|
|
|
|
auto EC = fs.MakeAbsolute(foo);
|
|
|
|
EXPECT_FALSE(EC);
|
|
|
|
EXPECT_TRUE(foo.equals("/foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec file_spec("foo");
|
2018-11-01 05:49:27 +08:00
|
|
|
auto EC = fs.MakeAbsolute(file_spec);
|
|
|
|
EXPECT_FALSE(EC);
|
2018-11-28 09:18:10 +08:00
|
|
|
EXPECT_EQ(FileSpec("/foo"), file_spec);
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, Resolve) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
{
|
|
|
|
StringRef foo_relative = "foo";
|
|
|
|
SmallString<16> foo(foo_relative);
|
|
|
|
fs.Resolve(foo);
|
|
|
|
EXPECT_TRUE(foo.equals("/foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec file_spec("foo");
|
2018-11-01 05:49:27 +08:00
|
|
|
fs.Resolve(file_spec);
|
2018-11-28 09:18:10 +08:00
|
|
|
EXPECT_EQ(FileSpec("/foo"), file_spec);
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
StringRef foo_relative = "bogus";
|
|
|
|
SmallString<16> foo(foo_relative);
|
|
|
|
fs.Resolve(foo);
|
|
|
|
EXPECT_TRUE(foo.equals("bogus"));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSpec file_spec("bogus");
|
2018-11-01 05:49:27 +08:00
|
|
|
fs.Resolve(file_spec);
|
2018-11-28 09:18:10 +08:00
|
|
|
EXPECT_EQ(FileSpec("bogus"), file_spec);
|
2018-11-01 05:49:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 08:26:09 +08:00
|
|
|
|
|
|
|
FileSystem::EnumerateDirectoryResult
|
|
|
|
VFSCallback(void *baton, llvm::sys::fs::file_type file_type,
|
|
|
|
llvm::StringRef path) {
|
|
|
|
auto visited = static_cast<std::vector<std::string> *>(baton);
|
|
|
|
visited->push_back(path.str());
|
|
|
|
return FileSystem::eEnumerateDirectoryResultNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileSystemTest, EnumerateDirectory) {
|
|
|
|
FileSystem fs(GetSimpleDummyFS());
|
|
|
|
|
|
|
|
std::vector<std::string> visited;
|
|
|
|
|
|
|
|
constexpr bool find_directories = true;
|
|
|
|
constexpr bool find_files = true;
|
|
|
|
constexpr bool find_other = true;
|
|
|
|
|
|
|
|
fs.EnumerateDirectory("/", find_directories, find_files, find_other,
|
|
|
|
VFSCallback, &visited);
|
|
|
|
|
|
|
|
EXPECT_THAT(visited,
|
|
|
|
testing::UnorderedElementsAre("/foo", "/bar", "/baz", "/qux"));
|
|
|
|
}
|
2019-09-27 01:54:59 +08:00
|
|
|
|
|
|
|
TEST(FileSystemTest, OpenErrno) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
FileSpec spec("C:\\FILE\\THAT\\DOES\\NOT\\EXIST.TXT");
|
|
|
|
#else
|
|
|
|
FileSpec spec("/file/that/does/not/exist.txt");
|
|
|
|
#endif
|
|
|
|
FileSystem fs;
|
2021-07-29 02:07:03 +08:00
|
|
|
auto file = fs.Open(spec, File::eOpenOptionReadOnly, 0, true);
|
2019-09-27 01:54:59 +08:00
|
|
|
ASSERT_FALSE(file);
|
|
|
|
std::error_code code = errorToErrorCode(file.takeError());
|
|
|
|
EXPECT_EQ(code.category(), std::system_category());
|
|
|
|
EXPECT_EQ(code.value(), ENOENT);
|
|
|
|
}
|
|
|
|
|
2020-04-04 00:18:47 +08:00
|
|
|
TEST(FileSystemTest, EmptyTest) {
|
|
|
|
FileSpec spec;
|
|
|
|
FileSystem fs;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::error_code ec;
|
|
|
|
fs.DirBegin(spec, ec);
|
|
|
|
EXPECT_EQ(ec.category(), std::system_category());
|
|
|
|
EXPECT_EQ(ec.value(), ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
llvm::ErrorOr<vfs::Status> status = fs.GetStatus(spec);
|
|
|
|
ASSERT_FALSE(status);
|
|
|
|
EXPECT_EQ(status.getError().category(), std::system_category());
|
|
|
|
EXPECT_EQ(status.getError().value(), ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(sys::TimePoint<>(), fs.GetModificationTime(spec));
|
|
|
|
EXPECT_EQ(static_cast<uint64_t>(0), fs.GetByteSize(spec));
|
|
|
|
EXPECT_EQ(llvm::sys::fs::perms::perms_not_known, fs.GetPermissions(spec));
|
|
|
|
EXPECT_FALSE(fs.Exists(spec));
|
|
|
|
EXPECT_FALSE(fs.Readable(spec));
|
|
|
|
EXPECT_FALSE(fs.IsDirectory(spec));
|
|
|
|
EXPECT_FALSE(fs.IsLocal(spec));
|
|
|
|
}
|