[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 08:23:27 +01:00
|
|
|
//===-- Status.cpp --------------------------------------------------------===//
|
2010-06-08 16:52:24 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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
|
2010-06-08 16:52:24 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2017-05-12 04:51:55 +00:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-04-06 18:12:24 +00:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
#include "lldb/Utility/LLDBLog.h"
|
|
|
|
|
#include "lldb/Utility/Log.h"
|
2017-04-06 18:12:24 +00:00
|
|
|
#include "lldb/Utility/VASPrintf.h"
|
2018-11-11 23:16:43 +00:00
|
|
|
#include "lldb/lldb-defines.h"
|
|
|
|
|
#include "lldb/lldb-enumerations.h"
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-06 14:06:17 +00:00
|
|
|
#include "llvm/Support/Errno.h"
|
2018-11-11 23:16:43 +00:00
|
|
|
#include "llvm/Support/FormatProviders.h"
|
2010-06-08 16:52:24 +00:00
|
|
|
|
2016-03-10 23:57:12 +00:00
|
|
|
#include <cerrno>
|
|
|
|
|
#include <cstdarg>
|
2018-11-11 23:16:43 +00:00
|
|
|
#include <string>
|
2017-03-08 17:56:08 +00:00
|
|
|
#include <system_error>
|
2016-03-10 23:57:12 +00:00
|
|
|
|
2017-04-06 18:12:24 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
#include <mach/mach.h>
|
|
|
|
|
#endif
|
2016-03-10 23:57:12 +00:00
|
|
|
|
2018-10-19 18:58:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
2021-05-26 12:19:37 +02:00
|
|
|
#include <cstdint>
|
2017-04-06 18:12:24 +00:00
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
class raw_ostream;
|
|
|
|
|
}
|
2010-06-08 16:52:24 +00:00
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
char CloneableError::ID;
|
|
|
|
|
char CloneableECError::ID;
|
|
|
|
|
char MachKernelError::ID;
|
|
|
|
|
char Win32Error::ID;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
/// A std::error_code category for eErrorTypeGeneric.
|
|
|
|
|
class LLDBGenericCategory : public std::error_category {
|
|
|
|
|
const char *name() const noexcept override { return "LLDBGenericCategory"; }
|
|
|
|
|
std::string message(int __ev) const override { return "generic LLDB error"; };
|
|
|
|
|
};
|
|
|
|
|
LLDBGenericCategory &lldb_generic_category() {
|
|
|
|
|
static LLDBGenericCategory g_generic_category;
|
|
|
|
|
return g_generic_category;
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
Status::Status() : m_error(llvm::Error::success()) {}
|
|
|
|
|
|
|
|
|
|
static llvm::Error ErrorFromEnums(Status::ValueType err, ErrorType type,
|
|
|
|
|
std::string msg) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case eErrorTypeMachKernel:
|
|
|
|
|
return llvm::make_error<MachKernelError>(
|
|
|
|
|
std::error_code(err, std::system_category()));
|
|
|
|
|
case eErrorTypeWin32:
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (err == NO_ERROR)
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
#endif
|
|
|
|
|
return llvm::make_error<Win32Error>(
|
|
|
|
|
std::error_code(err, std::system_category()));
|
|
|
|
|
case eErrorTypePOSIX:
|
|
|
|
|
if (msg.empty())
|
|
|
|
|
return llvm::errorCodeToError(
|
|
|
|
|
std::error_code(err, std::generic_category()));
|
|
|
|
|
return llvm::createStringError(
|
|
|
|
|
std::move(msg), std::error_code(err, std::generic_category()));
|
|
|
|
|
default:
|
|
|
|
|
return llvm::createStringError(
|
|
|
|
|
std::move(msg), std::error_code(err, lldb_generic_category()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-14 04:54:56 +00:00
|
|
|
|
2024-08-27 10:59:31 -07:00
|
|
|
Status::Status(ValueType err, ErrorType type, std::string msg)
|
2024-09-18 14:54:49 -07:00
|
|
|
: m_error(ErrorFromEnums(err, type, msg)) {}
|
2011-01-14 04:54:56 +00:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
// This logic is confusing because C++ calls the traditional (posix) errno codes
|
2020-04-23 15:55:39 +02:00
|
|
|
// "generic errors", while we use the term "generic" to mean completely
|
|
|
|
|
// arbitrary (text-based) errors.
|
2017-05-12 04:51:55 +00:00
|
|
|
Status::Status(std::error_code EC)
|
2024-09-18 14:54:49 -07:00
|
|
|
: m_error(!EC ? llvm::Error::success() : llvm::errorCodeToError(EC)) {}
|
2017-03-08 17:56:08 +00:00
|
|
|
|
2024-08-27 10:59:31 -07:00
|
|
|
Status::Status(std::string err_str)
|
2024-09-18 14:54:49 -07:00
|
|
|
: m_error(
|
|
|
|
|
llvm::createStringError(llvm::inconvertibleErrorCode(), err_str)) {}
|
2017-05-18 12:46:50 +00:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
const Status &Status::operator=(Status &&other) {
|
|
|
|
|
Clear();
|
|
|
|
|
llvm::consumeError(std::move(m_error));
|
|
|
|
|
m_error = std::move(other.m_error);
|
|
|
|
|
return *this;
|
2017-05-18 12:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 10:59:31 -07:00
|
|
|
Status Status::FromErrorStringWithFormat(const char *format, ...) {
|
|
|
|
|
std::string string;
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
if (format != nullptr && format[0]) {
|
|
|
|
|
llvm::SmallString<1024> buf;
|
|
|
|
|
VASprintf(buf, format, args);
|
|
|
|
|
string = std::string(buf.str());
|
|
|
|
|
}
|
|
|
|
|
va_end(args);
|
|
|
|
|
return Status(string);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
/// Creates a deep copy of all known errors and converts all other
|
|
|
|
|
/// errors to a new llvm::StringError.
|
|
|
|
|
static llvm::Error CloneError(const llvm::Error &error) {
|
|
|
|
|
llvm::Error result = llvm::Error::success();
|
|
|
|
|
auto clone = [](const llvm::ErrorInfoBase &e) {
|
|
|
|
|
if (e.isA<CloneableError>())
|
|
|
|
|
return llvm::Error(static_cast<const CloneableError &>(e).Clone());
|
|
|
|
|
if (e.isA<llvm::ECError>())
|
|
|
|
|
return llvm::errorCodeToError(e.convertToErrorCode());
|
|
|
|
|
return llvm::make_error<llvm::StringError>(e.message(),
|
|
|
|
|
e.convertToErrorCode(), true);
|
|
|
|
|
};
|
|
|
|
|
llvm::visitErrors(error, [&](const llvm::ErrorInfoBase &e) {
|
|
|
|
|
result = joinErrors(std::move(result), clone(e));
|
|
|
|
|
});
|
|
|
|
|
return result;
|
2017-05-18 12:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
Status Status::FromError(llvm::Error error) { return Status(std::move(error)); }
|
|
|
|
|
|
|
|
|
|
llvm::Error Status::ToError() const { return CloneError(m_error); }
|
2024-09-18 14:54:49 -07:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
Status::~Status() { llvm::consumeError(std::move(m_error)); }
|
2024-09-05 12:44:13 -07:00
|
|
|
|
2018-10-19 18:58:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
static std::string RetrieveWin32ErrorString(uint32_t error_code) {
|
|
|
|
|
char *buffer = nullptr;
|
|
|
|
|
std::string message;
|
|
|
|
|
// Retrieve win32 system error.
|
2019-11-28 14:15:13 -05:00
|
|
|
// First, attempt to load a en-US message
|
2018-10-19 18:58:24 +00:00
|
|
|
if (::FormatMessageA(
|
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
2019-11-28 14:15:13 -05:00
|
|
|
NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
2018-10-19 18:58:24 +00:00
|
|
|
(LPSTR)&buffer, 0, NULL)) {
|
|
|
|
|
message.assign(buffer);
|
|
|
|
|
::LocalFree(buffer);
|
|
|
|
|
}
|
2019-11-28 14:15:13 -05:00
|
|
|
// If the previous didn't work, use the default OS language
|
|
|
|
|
else if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
|
|
|
|
NULL, error_code, 0, (LPSTR)&buffer, 0, NULL)) {
|
|
|
|
|
message.assign(buffer);
|
|
|
|
|
::LocalFree(buffer);
|
|
|
|
|
}
|
2018-10-19 18:58:24 +00:00
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
std::string MachKernelError::message() const {
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
if (const char *s = ::mach_error_string(convertToErrorCode().value()))
|
|
|
|
|
return s;
|
|
|
|
|
#endif
|
|
|
|
|
return "MachKernelError";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Win32Error::message() const {
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
return RetrieveWin32ErrorString(convertToErrorCode().value());
|
|
|
|
|
#endif
|
|
|
|
|
return "Win32Error";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CloneableError> MachKernelError::Clone() const {
|
|
|
|
|
return std::make_unique<MachKernelError>(convertToErrorCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CloneableError> Win32Error::Clone() const {
|
|
|
|
|
return std::make_unique<Win32Error>(convertToErrorCode());
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 16:49:04 +00:00
|
|
|
// Get the error value as a NULL C string. The error string will be fetched and
|
|
|
|
|
// cached on demand. The cached error string value will remain until the error
|
|
|
|
|
// value is changed or cleared.
|
2017-05-12 04:51:55 +00:00
|
|
|
const char *Status::AsCString(const char *default_error_str) const {
|
2010-06-08 16:52:24 +00:00
|
|
|
if (Success())
|
2016-03-10 23:57:12 +00:00
|
|
|
return nullptr;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
m_string = llvm::toStringWithoutConsuming(m_error);
|
|
|
|
|
// Backwards compatibility with older implementations of Status.
|
|
|
|
|
if (m_error.isA<llvm::ECError>())
|
|
|
|
|
if (!m_string.empty() && m_string[m_string.size() - 1] == '\n')
|
|
|
|
|
m_string.pop_back();
|
2018-10-19 18:58:24 +00:00
|
|
|
|
2010-06-08 16:52:24 +00:00
|
|
|
if (m_string.empty()) {
|
|
|
|
|
if (default_error_str)
|
|
|
|
|
m_string.assign(default_error_str);
|
|
|
|
|
else
|
2016-03-10 23:57:12 +00:00
|
|
|
return nullptr; // User wanted a nullptr string back...
|
2010-06-08 16:52:24 +00:00
|
|
|
}
|
|
|
|
|
return m_string.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear the error and any cached error string that it might contain.
|
2017-05-12 04:51:55 +00:00
|
|
|
void Status::Clear() {
|
2024-09-18 14:54:49 -07:00
|
|
|
if (m_error)
|
|
|
|
|
LLDB_LOG_ERRORV(GetLog(LLDBLog::API), std::move(m_error),
|
|
|
|
|
"dropping error {0}");
|
|
|
|
|
m_error = llvm::Error::success();
|
2010-06-08 16:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
Status::ValueType Status::GetError() const {
|
|
|
|
|
Status::ValueType result = 0;
|
|
|
|
|
llvm::visitErrors(m_error, [&](const llvm::ErrorInfoBase &error) {
|
|
|
|
|
// Return the first only.
|
|
|
|
|
if (result)
|
|
|
|
|
return;
|
|
|
|
|
std::error_code ec = error.convertToErrorCode();
|
|
|
|
|
result = ec.value();
|
|
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2010-06-08 16:52:24 +00:00
|
|
|
|
2024-09-27 16:09:52 -07:00
|
|
|
static ErrorType ErrorCodeToErrorType(std::error_code ec) {
|
|
|
|
|
if (ec.category() == std::generic_category())
|
|
|
|
|
return eErrorTypePOSIX;
|
|
|
|
|
if (ec.category() == lldb_generic_category() ||
|
|
|
|
|
ec == llvm::inconvertibleErrorCode())
|
|
|
|
|
return eErrorTypeGeneric;
|
|
|
|
|
return eErrorTypeInvalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorType CloneableECError::GetErrorType() const {
|
|
|
|
|
return ErrorCodeToErrorType(EC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lldb::ErrorType MachKernelError::GetErrorType() const {
|
|
|
|
|
return lldb::eErrorTypeMachKernel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lldb::ErrorType Win32Error::GetErrorType() const {
|
|
|
|
|
return lldb::eErrorTypeWin32;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 13:02:54 -08:00
|
|
|
StructuredData::ObjectSP Status::GetAsStructuredData() const {
|
|
|
|
|
auto dict_up = std::make_unique<StructuredData::Dictionary>();
|
|
|
|
|
auto array_up = std::make_unique<StructuredData::Array>();
|
|
|
|
|
llvm::visitErrors(m_error, [&](const llvm::ErrorInfoBase &error) {
|
|
|
|
|
if (error.isA<CloneableError>())
|
|
|
|
|
array_up->AddItem(
|
|
|
|
|
static_cast<const CloneableError &>(error).GetAsStructuredData());
|
|
|
|
|
else
|
|
|
|
|
array_up->AddStringItem(error.message());
|
|
|
|
|
});
|
|
|
|
|
dict_up->AddIntegerItem("version", 1u);
|
|
|
|
|
dict_up->AddIntegerItem("type", (unsigned)GetType());
|
|
|
|
|
dict_up->AddItem("errors", std::move(array_up));
|
|
|
|
|
return dict_up;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StructuredData::ObjectSP CloneableECError::GetAsStructuredData() const {
|
|
|
|
|
auto dict_up = std::make_unique<StructuredData::Dictionary>();
|
|
|
|
|
dict_up->AddIntegerItem("version", 1u);
|
|
|
|
|
dict_up->AddIntegerItem("error_code", EC.value());
|
|
|
|
|
dict_up->AddStringItem("message", message());
|
|
|
|
|
return dict_up;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
ErrorType Status::GetType() const {
|
|
|
|
|
ErrorType result = eErrorTypeInvalid;
|
|
|
|
|
llvm::visitErrors(m_error, [&](const llvm::ErrorInfoBase &error) {
|
|
|
|
|
// Return the first only.
|
|
|
|
|
if (result != eErrorTypeInvalid)
|
|
|
|
|
return;
|
2024-11-21 11:11:25 -08:00
|
|
|
if (error.isA<CloneableError>())
|
|
|
|
|
result = static_cast<const CloneableError &>(error).GetErrorType();
|
|
|
|
|
else
|
|
|
|
|
result = ErrorCodeToErrorType(error.convertToErrorCode());
|
|
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2024-09-18 14:54:49 -07:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
bool Status::Fail() const {
|
|
|
|
|
// Note that this does not clear the checked flag in
|
|
|
|
|
// m_error. Otherwise we'd need to make this thread-safe.
|
|
|
|
|
return m_error.isA<llvm::ErrorInfoBase>();
|
2024-09-23 10:50:25 -07:00
|
|
|
}
|
2024-09-18 14:54:49 -07:00
|
|
|
|
2024-09-18 14:54:49 -07:00
|
|
|
Status Status::FromErrno() { return Status(llvm::errnoAsErrorCode()); }
|
|
|
|
|
|
2018-04-30 16:49:04 +00:00
|
|
|
// Returns true if the error code in this object is considered a successful
|
|
|
|
|
// return value.
|
2024-09-18 14:54:49 -07:00
|
|
|
bool Status::Success() const { return !Fail(); }
|
2013-06-07 22:09:53 +00:00
|
|
|
|
2017-05-12 04:51:55 +00:00
|
|
|
void llvm::format_provider<lldb_private::Status>::format(
|
|
|
|
|
const lldb_private::Status &error, llvm::raw_ostream &OS,
|
2017-02-06 18:31:44 +00:00
|
|
|
llvm::StringRef Options) {
|
|
|
|
|
llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
|
|
|
|
|
Options);
|
|
|
|
|
}
|