[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
|
|
|
//===-- Status.cpp --------------------------------------------------------===//
|
2010-06-09 00:52:24 +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
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-04-07 02:12:24 +08:00
|
|
|
|
|
|
|
#include "lldb/Utility/VASPrintf.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "lldb/lldb-defines.h"
|
|
|
|
#include "lldb/lldb-enumerations.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-06 22:06:17 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/Support/FormatProviders.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-03-11 07:57:12 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdarg>
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <string>
|
2017-03-09 01:56:08 +08:00
|
|
|
#include <system_error>
|
2016-03-11 07:57:12 +08:00
|
|
|
|
2017-04-07 02:12:24 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
2016-03-11 07:57:12 +08:00
|
|
|
|
2018-10-20 02:58:24 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <stdint.h>
|
2017-04-07 02:12:24 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class raw_ostream;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
|
2011-01-14 12:54:56 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status::Status(ValueType err, ErrorType type)
|
2010-06-09 00:52:24 +08:00
|
|
|
: m_code(err), m_type(type), m_string() {}
|
2011-01-14 12:54:56 +08:00
|
|
|
|
2020-04-23 21:55:39 +08:00
|
|
|
// This logic is confusing because c++ calls the traditional (posix) errno codes
|
|
|
|
// "generic errors", while we use the term "generic" to mean completely
|
|
|
|
// arbitrary (text-based) errors.
|
2017-05-12 12:51:55 +08:00
|
|
|
Status::Status(std::error_code EC)
|
2020-04-23 21:55:39 +08:00
|
|
|
: m_code(EC.value()),
|
|
|
|
m_type(EC.category() == std::generic_category() ? eErrorTypePOSIX
|
|
|
|
: eErrorTypeGeneric),
|
2017-03-09 01:56:08 +08:00
|
|
|
m_string(EC.message()) {}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status::Status(const char *format, ...)
|
2011-07-15 10:26:42 +08:00
|
|
|
: m_code(0), m_type(eErrorTypeInvalid), m_string() {
|
2013-09-12 10:20:34 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
SetErrorToGenericError();
|
|
|
|
SetErrorStringWithVarArg(format, args);
|
|
|
|
va_end(args);
|
2011-07-15 10:26:42 +08:00
|
|
|
}
|
|
|
|
|
2017-06-15 19:23:26 +08:00
|
|
|
const Status &Status::operator=(llvm::Error error) {
|
|
|
|
if (!error) {
|
|
|
|
Clear();
|
|
|
|
return *this;
|
|
|
|
}
|
2017-05-18 20:46:50 +08:00
|
|
|
|
|
|
|
// if the error happens to be a errno error, preserve the error code
|
|
|
|
error = llvm::handleErrors(
|
|
|
|
std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
|
|
|
|
std::error_code ec = e->convertToErrorCode();
|
|
|
|
if (ec.category() == std::generic_category()) {
|
|
|
|
m_code = ec.value();
|
|
|
|
m_type = ErrorType::eErrorTypePOSIX;
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
return llvm::Error(std::move(e));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Otherwise, just preserve the message
|
2017-06-15 19:23:26 +08:00
|
|
|
if (error) {
|
|
|
|
SetErrorToGenericError();
|
2017-05-18 20:46:50 +08:00
|
|
|
SetErrorString(llvm::toString(std::move(error)));
|
2017-06-15 19:23:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
2017-05-18 20:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error Status::ToError() const {
|
|
|
|
if (Success())
|
|
|
|
return llvm::Error::success();
|
|
|
|
if (m_type == ErrorType::eErrorTypePOSIX)
|
2018-10-20 02:58:24 +08:00
|
|
|
return llvm::errorCodeToError(
|
|
|
|
std::error_code(m_code, std::generic_category()));
|
2017-05-18 20:46:50 +08:00
|
|
|
return llvm::make_error<llvm::StringError>(AsCString(),
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status::~Status() = default;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-10-20 02:58:24 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
static std::string RetrieveWin32ErrorString(uint32_t error_code) {
|
|
|
|
char *buffer = nullptr;
|
|
|
|
std::string message;
|
|
|
|
// Retrieve win32 system error.
|
2019-11-29 03:15:13 +08:00
|
|
|
// First, attempt to load a en-US message
|
2018-10-20 02:58:24 +08:00
|
|
|
if (::FormatMessageA(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
2019-11-29 03:15:13 +08:00
|
|
|
NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
2018-10-20 02:58:24 +08:00
|
|
|
(LPSTR)&buffer, 0, NULL)) {
|
|
|
|
message.assign(buffer);
|
|
|
|
::LocalFree(buffer);
|
|
|
|
}
|
2019-11-29 03:15:13 +08: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-20 02:58:24 +08:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-01 00:49:04 +08: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 12:51:55 +08:00
|
|
|
const char *Status::AsCString(const char *default_error_str) const {
|
2010-06-09 00:52:24 +08:00
|
|
|
if (Success())
|
2016-03-11 07:57:12 +08:00
|
|
|
return nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (m_string.empty()) {
|
|
|
|
switch (m_type) {
|
|
|
|
case eErrorTypeMachKernel:
|
2011-03-09 06:40:15 +08:00
|
|
|
#if defined(__APPLE__)
|
2017-06-06 22:06:17 +08:00
|
|
|
if (const char *s = ::mach_error_string(m_code))
|
|
|
|
m_string.assign(s);
|
2010-06-11 07:45:58 +08:00
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eErrorTypePOSIX:
|
2017-06-06 22:06:17 +08:00
|
|
|
m_string = llvm::sys::StrError(m_code);
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
2018-10-20 02:58:24 +08:00
|
|
|
case eErrorTypeWin32:
|
|
|
|
#if defined(_WIN32)
|
|
|
|
m_string = RetrieveWin32ErrorString(m_code);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
if (m_string.empty()) {
|
|
|
|
if (default_error_str)
|
|
|
|
m_string.assign(default_error_str);
|
|
|
|
else
|
2016-03-11 07:57:12 +08:00
|
|
|
return nullptr; // User wanted a nullptr string back...
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return m_string.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the error and any cached error string that it might contain.
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::Clear() {
|
2010-06-09 00:52:24 +08:00
|
|
|
m_code = 0;
|
2015-01-23 03:30:05 +08:00
|
|
|
m_type = eErrorTypeInvalid;
|
2010-06-09 00:52:24 +08:00
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Access the error value.
|
2017-05-12 12:51:55 +08:00
|
|
|
Status::ValueType Status::GetError() const { return m_code; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// Access the error type.
|
2017-05-12 12:51:55 +08:00
|
|
|
ErrorType Status::GetType() const { return m_type; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Returns true if this object contains a value that describes an error or
|
|
|
|
// otherwise non-success result.
|
2017-05-12 12:51:55 +08:00
|
|
|
bool Status::Fail() const { return m_code != 0; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-05-29 17:10:46 +08:00
|
|
|
// Set accessor for the error value to "err" and the type to
|
2010-06-09 00:52:24 +08:00
|
|
|
// "eErrorTypeMachKernel"
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::SetMachError(uint32_t err) {
|
2010-06-09 00:52:24 +08:00
|
|
|
m_code = err;
|
|
|
|
m_type = eErrorTypeMachKernel;
|
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::SetExpressionError(lldb::ExpressionResults result,
|
|
|
|
const char *mssg) {
|
2014-05-05 10:26:40 +08:00
|
|
|
m_code = result;
|
|
|
|
m_type = eErrorTypeExpression;
|
|
|
|
m_string = mssg;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
|
|
|
|
const char *format, ...) {
|
2014-05-05 10:26:40 +08:00
|
|
|
int length = 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-03-11 07:57:12 +08:00
|
|
|
if (format != nullptr && format[0]) {
|
2014-05-05 10:26:40 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
length = SetErrorStringWithVarArg(format, args);
|
|
|
|
va_end(args);
|
|
|
|
} else {
|
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
m_code = result;
|
|
|
|
m_type = eErrorTypeExpression;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:10:46 +08:00
|
|
|
// Set accessor for the error value and type.
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::SetError(ValueType err, ErrorType type) {
|
2010-06-09 00:52:24 +08:00
|
|
|
m_code = err;
|
|
|
|
m_type = type;
|
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Update the error value to be "errno" and update the type to be "POSIX".
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::SetErrorToErrno() {
|
2010-06-09 00:52:24 +08:00
|
|
|
m_code = errno;
|
|
|
|
m_type = eErrorTypePOSIX;
|
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Update the error value to be LLDB_GENERIC_ERROR and update the type to be
|
|
|
|
// "Generic".
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::SetErrorToGenericError() {
|
2010-06-09 00:52:24 +08:00
|
|
|
m_code = LLDB_GENERIC_ERROR;
|
|
|
|
m_type = eErrorTypeGeneric;
|
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Set accessor for the error string value for a specific error. This allows
|
|
|
|
// any string to be supplied as an error explanation. The error string value
|
|
|
|
// will remain until the error value is cleared or a new error value/type is
|
|
|
|
// assigned.
|
2017-05-12 12:51:55 +08:00
|
|
|
void Status::SetErrorString(llvm::StringRef err_str) {
|
2016-11-17 05:15:24 +08:00
|
|
|
if (!err_str.empty()) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// If we have an error string, we should always at least have an error set
|
|
|
|
// to a generic value.
|
2010-06-09 00:52:24 +08:00
|
|
|
if (Success())
|
|
|
|
SetErrorToGenericError();
|
2016-11-17 05:15:24 +08:00
|
|
|
}
|
2020-01-29 03:23:46 +08:00
|
|
|
m_string = std::string(err_str);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the current error string to a formatted error string.
|
|
|
|
///
|
2019-03-12 01:09:29 +08:00
|
|
|
/// \param format
|
2010-06-09 00:52:24 +08:00
|
|
|
/// A printf style format string
|
2017-05-12 12:51:55 +08:00
|
|
|
int Status::SetErrorStringWithFormat(const char *format, ...) {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (format != nullptr && format[0]) {
|
2010-06-09 00:52:24 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int length = SetErrorStringWithVarArg(format, args);
|
|
|
|
va_end(args);
|
|
|
|
return length;
|
|
|
|
} else {
|
|
|
|
m_string.clear();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
|
2016-03-11 07:57:12 +08:00
|
|
|
if (format != nullptr && format[0]) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// If we have an error string, we should always at least have an error set
|
|
|
|
// to a generic value.
|
2010-06-09 00:52:24 +08:00
|
|
|
if (Success())
|
|
|
|
SetErrorToGenericError();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-02-17 03:38:21 +08:00
|
|
|
llvm::SmallString<1024> buf;
|
|
|
|
VASprintf(buf, format, args);
|
2020-01-29 03:23:46 +08:00
|
|
|
m_string = std::string(buf.str());
|
2017-02-17 03:38:21 +08:00
|
|
|
return buf.size();
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2010-06-09 00:52:24 +08:00
|
|
|
m_string.clear();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Returns true if the error code in this object is considered a successful
|
|
|
|
// return value.
|
2017-05-12 12:51:55 +08:00
|
|
|
bool Status::Success() const { return m_code == 0; }
|
2013-06-08 06:09:53 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
bool Status::WasInterrupted() const {
|
2016-03-11 07:57:12 +08:00
|
|
|
return (m_type == eErrorTypePOSIX && m_code == EINTR);
|
2013-06-08 06:09:53 +08:00
|
|
|
}
|
2017-02-07 02:31:44 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
void llvm::format_provider<lldb_private::Status>::format(
|
|
|
|
const lldb_private::Status &error, llvm::raw_ostream &OS,
|
2017-02-07 02:31:44 +08:00
|
|
|
llvm::StringRef Options) {
|
|
|
|
llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
|
|
|
|
Options);
|
|
|
|
}
|