[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
|
|
|
//===-- Reproducer.cpp ----------------------------------------------------===//
|
2018-11-14 03:18:16 +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
|
2018-11-14 03:18:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Utility/Reproducer.h"
|
2018-12-04 01:28:29 +08:00
|
|
|
#include "lldb/Utility/LLDBAssert.h"
|
2018-11-14 03:18:16 +08:00
|
|
|
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace lldb_private::repro;
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::yaml;
|
|
|
|
|
2020-01-16 11:12:50 +08:00
|
|
|
static llvm::Optional<bool> GetEnv(const char *var) {
|
|
|
|
std::string val = llvm::StringRef(getenv(var)).lower();
|
|
|
|
if (val == "0" || val == "off")
|
|
|
|
return false;
|
|
|
|
if (val == "1" || val == "on")
|
|
|
|
return true;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-12-04 01:28:29 +08:00
|
|
|
Reproducer &Reproducer::Instance() { return *InstanceImpl(); }
|
|
|
|
|
|
|
|
llvm::Error Reproducer::Initialize(ReproducerMode mode,
|
|
|
|
llvm::Optional<FileSpec> root) {
|
|
|
|
lldbassert(!InstanceImpl() && "Already initialized.");
|
|
|
|
InstanceImpl().emplace();
|
|
|
|
|
2019-12-05 08:06:48 +08:00
|
|
|
// The environment can override the capture mode.
|
|
|
|
if (mode != ReproducerMode::Replay) {
|
2020-01-16 11:12:50 +08:00
|
|
|
if (llvm::Optional<bool> override = GetEnv("LLDB_CAPTURE_REPRODUCER")) {
|
|
|
|
if (*override)
|
|
|
|
mode = ReproducerMode::Capture;
|
|
|
|
else
|
|
|
|
mode = ReproducerMode::Off;
|
|
|
|
}
|
2019-12-05 08:06:48 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 01:28:29 +08:00
|
|
|
switch (mode) {
|
|
|
|
case ReproducerMode::Capture: {
|
|
|
|
if (!root) {
|
|
|
|
SmallString<128> repro_dir;
|
|
|
|
auto ec = sys::fs::createUniqueDirectory("reproducer", repro_dir);
|
|
|
|
if (ec)
|
|
|
|
return make_error<StringError>(
|
|
|
|
"unable to create unique reproducer directory", ec);
|
|
|
|
root.emplace(repro_dir);
|
|
|
|
} else {
|
2018-12-05 02:16:49 +08:00
|
|
|
auto ec = sys::fs::create_directory(root->GetPath());
|
2018-12-04 01:28:29 +08:00
|
|
|
if (ec)
|
|
|
|
return make_error<StringError>("unable to create reproducer directory",
|
|
|
|
ec);
|
|
|
|
}
|
|
|
|
return Instance().SetCapture(root);
|
|
|
|
} break;
|
|
|
|
case ReproducerMode::Replay:
|
2020-04-21 00:37:07 +08:00
|
|
|
return Instance().SetReplay(root, /*passive*/ false);
|
|
|
|
case ReproducerMode::PassiveReplay:
|
|
|
|
return Instance().SetReplay(root, /*passive*/ true);
|
2018-12-04 01:28:29 +08:00
|
|
|
case ReproducerMode::Off:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-02-22 06:26:16 +08:00
|
|
|
bool Reproducer::Initialized() { return InstanceImpl().operator bool(); }
|
|
|
|
|
2018-12-04 01:28:29 +08:00
|
|
|
void Reproducer::Terminate() {
|
|
|
|
lldbassert(InstanceImpl() && "Already terminated.");
|
|
|
|
InstanceImpl().reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<Reproducer> &Reproducer::InstanceImpl() {
|
|
|
|
static Optional<Reproducer> g_reproducer;
|
2018-11-14 03:18:16 +08:00
|
|
|
return g_reproducer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Generator *Reproducer::GetGenerator() const {
|
|
|
|
std::lock_guard<std::mutex> guard(m_mutex);
|
2018-11-28 06:11:02 +08:00
|
|
|
if (m_generator)
|
|
|
|
return &(*m_generator);
|
2018-11-14 03:18:16 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Loader *Reproducer::GetLoader() const {
|
|
|
|
std::lock_guard<std::mutex> guard(m_mutex);
|
2018-11-28 06:11:02 +08:00
|
|
|
if (m_loader)
|
|
|
|
return &(*m_loader);
|
2018-11-14 03:18:16 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Generator *Reproducer::GetGenerator() {
|
|
|
|
std::lock_guard<std::mutex> guard(m_mutex);
|
2018-11-28 06:11:02 +08:00
|
|
|
if (m_generator)
|
|
|
|
return &(*m_generator);
|
2018-11-14 03:18:16 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader *Reproducer::GetLoader() {
|
|
|
|
std::lock_guard<std::mutex> guard(m_mutex);
|
2018-11-28 06:11:02 +08:00
|
|
|
if (m_loader)
|
|
|
|
return &(*m_loader);
|
2018-11-14 03:18:16 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
llvm::Error Reproducer::SetCapture(llvm::Optional<FileSpec> root) {
|
2018-11-14 03:18:16 +08:00
|
|
|
std::lock_guard<std::mutex> guard(m_mutex);
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
if (root && m_loader)
|
2018-11-14 03:18:16 +08:00
|
|
|
return make_error<StringError>(
|
|
|
|
"cannot generate a reproducer when replay one",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
2018-12-04 01:28:29 +08:00
|
|
|
if (!root) {
|
2018-11-28 06:11:02 +08:00
|
|
|
m_generator.reset();
|
2018-12-04 01:28:29 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2018-12-04 01:28:29 +08:00
|
|
|
m_generator.emplace(*root);
|
2018-11-14 03:18:16 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:37:07 +08:00
|
|
|
llvm::Error Reproducer::SetReplay(llvm::Optional<FileSpec> root, bool passive) {
|
2018-11-14 03:18:16 +08:00
|
|
|
std::lock_guard<std::mutex> guard(m_mutex);
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
if (root && m_generator)
|
2018-11-14 03:18:16 +08:00
|
|
|
return make_error<StringError>(
|
|
|
|
"cannot replay a reproducer when generating one",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
2018-12-04 01:28:29 +08:00
|
|
|
if (!root) {
|
2018-11-28 06:11:02 +08:00
|
|
|
m_loader.reset();
|
2018-12-04 01:28:29 +08:00
|
|
|
return Error::success();
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:37:07 +08:00
|
|
|
m_loader.emplace(*root, passive);
|
2018-12-04 01:28:29 +08:00
|
|
|
if (auto e = m_loader->LoadIndex())
|
|
|
|
return e;
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
return Error::success();
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSpec Reproducer::GetReproducerPath() const {
|
|
|
|
if (auto g = GetGenerator())
|
2018-11-28 06:11:02 +08:00
|
|
|
return g->GetRoot();
|
|
|
|
if (auto l = GetLoader())
|
|
|
|
return l->GetRoot();
|
2018-11-14 03:18:16 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-09-28 01:30:40 +08:00
|
|
|
static FileSpec MakeAbsolute(FileSpec file_spec) {
|
|
|
|
SmallString<128> path;
|
|
|
|
file_spec.GetPath(path, false);
|
|
|
|
llvm::sys::fs::make_absolute(path);
|
|
|
|
return FileSpec(path, file_spec.GetPathStyle());
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:16:33 +08:00
|
|
|
Generator::Generator(FileSpec root) : m_root(MakeAbsolute(std::move(root))) {
|
2019-10-17 08:01:53 +08:00
|
|
|
GetOrCreate<repro::WorkingDirectoryProvider>();
|
|
|
|
}
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2019-11-13 12:16:33 +08:00
|
|
|
Generator::~Generator() {
|
2020-01-16 11:44:46 +08:00
|
|
|
if (!m_done) {
|
|
|
|
if (m_auto_generate)
|
|
|
|
Keep();
|
|
|
|
else
|
|
|
|
Discard();
|
|
|
|
}
|
2019-11-13 12:16:33 +08:00
|
|
|
}
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
ProviderBase *Generator::Register(std::unique_ptr<ProviderBase> provider) {
|
2018-11-14 03:18:16 +08:00
|
|
|
std::lock_guard<std::mutex> lock(m_providers_mutex);
|
2018-11-28 06:11:02 +08:00
|
|
|
std::pair<const void *, std::unique_ptr<ProviderBase>> key_value(
|
|
|
|
provider->DynamicClassID(), std::move(provider));
|
|
|
|
auto e = m_providers.insert(std::move(key_value));
|
|
|
|
return e.first->getSecond().get();
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Generator::Keep() {
|
|
|
|
assert(!m_done);
|
|
|
|
m_done = true;
|
|
|
|
|
|
|
|
for (auto &provider : m_providers)
|
2018-11-28 06:11:02 +08:00
|
|
|
provider.second->Keep();
|
|
|
|
|
|
|
|
AddProvidersToIndex();
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Generator::Discard() {
|
|
|
|
assert(!m_done);
|
|
|
|
m_done = true;
|
|
|
|
|
|
|
|
for (auto &provider : m_providers)
|
2018-11-28 06:11:02 +08:00
|
|
|
provider.second->Discard();
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
llvm::sys::fs::remove_directories(m_root.GetPath());
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 11:44:46 +08:00
|
|
|
void Generator::SetAutoGenerate(bool b) { m_auto_generate = b; }
|
|
|
|
|
2020-01-16 12:22:20 +08:00
|
|
|
bool Generator::IsAutoGenerate() const { return m_auto_generate; }
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
const FileSpec &Generator::GetRoot() const { return m_root; }
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
void Generator::AddProvidersToIndex() {
|
|
|
|
FileSpec index = m_root;
|
2018-11-14 03:18:16 +08:00
|
|
|
index.AppendPathComponent("index.yaml");
|
|
|
|
|
|
|
|
std::error_code EC;
|
2019-08-15 06:19:23 +08:00
|
|
|
auto strm = std::make_unique<raw_fd_ostream>(index.GetPath(), EC,
|
2019-09-12 07:15:12 +08:00
|
|
|
sys::fs::OpenFlags::OF_None);
|
2018-11-14 03:18:16 +08:00
|
|
|
yaml::Output yout(*strm);
|
2018-11-28 06:11:02 +08:00
|
|
|
|
2019-01-18 09:04:59 +08:00
|
|
|
std::vector<std::string> files;
|
|
|
|
files.reserve(m_providers.size());
|
2018-11-28 06:11:02 +08:00
|
|
|
for (auto &provider : m_providers) {
|
2019-01-18 09:04:59 +08:00
|
|
|
files.emplace_back(provider.second->GetFile());
|
2018-11-28 06:11:02 +08:00
|
|
|
}
|
2019-01-18 09:04:59 +08:00
|
|
|
|
|
|
|
yout << files;
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:37:07 +08:00
|
|
|
Loader::Loader(FileSpec root, bool passive)
|
|
|
|
: m_root(MakeAbsolute(std::move(root))), m_loaded(false),
|
|
|
|
m_passive_replay(passive) {}
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
llvm::Error Loader::LoadIndex() {
|
2018-11-14 03:18:16 +08:00
|
|
|
if (m_loaded)
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
FileSpec index = m_root.CopyByAppendingPathComponent("index.yaml");
|
2018-11-14 03:18:16 +08:00
|
|
|
|
|
|
|
auto error_or_file = MemoryBuffer::getFile(index.GetPath());
|
|
|
|
if (auto err = error_or_file.getError())
|
2018-12-04 01:28:29 +08:00
|
|
|
return make_error<StringError>("unable to load reproducer index", err);
|
2018-11-14 03:18:16 +08:00
|
|
|
|
|
|
|
yaml::Input yin((*error_or_file)->getBuffer());
|
2019-01-18 09:04:59 +08:00
|
|
|
yin >> m_files;
|
2018-11-14 03:18:16 +08:00
|
|
|
if (auto err = yin.error())
|
2018-12-04 01:28:29 +08:00
|
|
|
return make_error<StringError>("unable to read reproducer index", err);
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2019-01-18 09:04:59 +08:00
|
|
|
// Sort files to speed up search.
|
|
|
|
llvm::sort(m_files);
|
2018-11-14 03:18:16 +08:00
|
|
|
|
2019-01-18 09:04:59 +08:00
|
|
|
// Remember that we've loaded the index.
|
2018-11-14 03:18:16 +08:00
|
|
|
m_loaded = true;
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:04:59 +08:00
|
|
|
bool Loader::HasFile(StringRef file) {
|
2018-11-14 03:18:16 +08:00
|
|
|
assert(m_loaded);
|
2019-01-18 09:04:59 +08:00
|
|
|
auto it = std::lower_bound(m_files.begin(), m_files.end(), file.str());
|
|
|
|
return (it != m_files.end()) && (*it == file);
|
2018-11-14 03:18:16 +08:00
|
|
|
}
|
2018-11-28 06:11:02 +08:00
|
|
|
|
2019-03-02 08:20:26 +08:00
|
|
|
llvm::Expected<std::unique_ptr<DataRecorder>>
|
2019-05-23 13:45:49 +08:00
|
|
|
DataRecorder::Create(const FileSpec &filename) {
|
2019-03-02 08:20:26 +08:00
|
|
|
std::error_code ec;
|
2019-08-15 06:19:23 +08:00
|
|
|
auto recorder = std::make_unique<DataRecorder>(std::move(filename), ec);
|
2019-03-02 08:20:26 +08:00
|
|
|
if (ec)
|
|
|
|
return llvm::errorCodeToError(ec);
|
2019-03-03 00:23:07 +08:00
|
|
|
return std::move(recorder);
|
2019-03-02 08:20:26 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 03:45:38 +08:00
|
|
|
llvm::Expected<std::unique_ptr<YamlRecorder>>
|
|
|
|
YamlRecorder::Create(const FileSpec &filename) {
|
2019-03-02 08:20:26 +08:00
|
|
|
std::error_code ec;
|
2020-07-11 03:45:38 +08:00
|
|
|
auto recorder = std::make_unique<YamlRecorder>(std::move(filename), ec);
|
2019-03-02 08:20:26 +08:00
|
|
|
if (ec)
|
2020-07-11 03:45:38 +08:00
|
|
|
return llvm::errorCodeToError(ec);
|
|
|
|
return std::move(recorder);
|
2019-03-02 08:20:26 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 12:35:22 +08:00
|
|
|
void VersionProvider::Keep() {
|
|
|
|
FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
|
|
|
|
std::error_code ec;
|
2019-08-05 13:43:48 +08:00
|
|
|
llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
|
2019-06-13 12:35:22 +08:00
|
|
|
if (ec)
|
|
|
|
return;
|
2019-06-13 13:14:25 +08:00
|
|
|
os << m_version << "\n";
|
2019-06-13 12:35:22 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 08:01:53 +08:00
|
|
|
void WorkingDirectoryProvider::Keep() {
|
|
|
|
FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
|
|
|
|
std::error_code ec;
|
|
|
|
llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
|
|
|
|
if (ec)
|
|
|
|
return;
|
|
|
|
os << m_cwd << "\n";
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:20:20 +08:00
|
|
|
void FileProvider::RecordInterestingDirectory(const llvm::Twine &dir) {
|
2020-07-21 23:59:44 +08:00
|
|
|
if (m_collector)
|
|
|
|
m_collector->addFile(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileProvider::RecordInterestingDirectoryRecursive(const llvm::Twine &dir) {
|
2020-04-01 02:35:55 +08:00
|
|
|
if (m_collector)
|
|
|
|
m_collector->addDirectory(dir);
|
|
|
|
}
|
|
|
|
|
2018-11-28 06:11:02 +08:00
|
|
|
void ProviderBase::anchor() {}
|
2019-03-02 08:20:26 +08:00
|
|
|
char CommandProvider::ID = 0;
|
2019-06-13 12:35:22 +08:00
|
|
|
char FileProvider::ID = 0;
|
2019-09-12 07:15:12 +08:00
|
|
|
char ProviderBase::ID = 0;
|
2019-06-13 12:35:22 +08:00
|
|
|
char VersionProvider::ID = 0;
|
2019-10-17 08:01:53 +08:00
|
|
|
char WorkingDirectoryProvider::ID = 0;
|
2019-06-13 06:17:38 +08:00
|
|
|
const char *CommandProvider::Info::file = "command-interpreter.yaml";
|
2019-06-13 12:35:22 +08:00
|
|
|
const char *CommandProvider::Info::name = "command-interpreter";
|
|
|
|
const char *FileProvider::Info::file = "files.yaml";
|
|
|
|
const char *FileProvider::Info::name = "files";
|
|
|
|
const char *VersionProvider::Info::file = "version.txt";
|
|
|
|
const char *VersionProvider::Info::name = "version";
|
2019-10-17 08:01:53 +08:00
|
|
|
const char *WorkingDirectoryProvider::Info::file = "cwd.txt";
|
|
|
|
const char *WorkingDirectoryProvider::Info::name = "cwd";
|