From b76161e41bc2c07cd47f9c61f875d1be95e26d10 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Tue, 16 May 2017 19:47:58 +0000 Subject: Vendor import of lldb trunk r303197: https://llvm.org/svn/llvm-project/lldb/trunk@303197 --- source/Utility/CMakeLists.txt | 2 +- source/Utility/Error.cpp | 274 --------------------------------------- source/Utility/JSON.cpp | 22 ++-- source/Utility/SelectHelper.cpp | 8 +- source/Utility/Status.cpp | 275 ++++++++++++++++++++++++++++++++++++++++ source/Utility/UUID.cpp | 16 ++- 6 files changed, 301 insertions(+), 296 deletions(-) delete mode 100644 source/Utility/Error.cpp create mode 100644 source/Utility/Status.cpp (limited to 'source/Utility') diff --git a/source/Utility/CMakeLists.txt b/source/Utility/CMakeLists.txt index d4e8e361017cd..a1675670f0b45 100644 --- a/source/Utility/CMakeLists.txt +++ b/source/Utility/CMakeLists.txt @@ -5,7 +5,6 @@ add_lldb_library(lldbUtility DataBufferLLVM.cpp DataEncoder.cpp DataExtractor.cpp - Error.cpp FastDemangle.cpp FileSpec.cpp History.cpp @@ -18,6 +17,7 @@ add_lldb_library(lldbUtility RegularExpression.cpp SelectHelper.cpp SharingPtr.cpp + Status.cpp Stream.cpp StreamCallback.cpp StreamGDBRemote.cpp diff --git a/source/Utility/Error.cpp b/source/Utility/Error.cpp deleted file mode 100644 index b21ee57b61aff..0000000000000 --- a/source/Utility/Error.cpp +++ /dev/null @@ -1,274 +0,0 @@ -//===-- Error.cpp -----------------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Utility/Error.h" - -#include "lldb/Utility/VASPrintf.h" -#include "lldb/lldb-defines.h" // for LLDB_GENERIC_ERROR -#include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType::eErr... -#include "llvm/ADT/SmallString.h" // for SmallString -#include "llvm/ADT/StringRef.h" // for StringRef -#include "llvm/Support/FormatProviders.h" // for format_provider - -#include -#include -#include // for string -#include - -#ifdef __APPLE__ -#include -#endif - -#include // for uint32_t -#include // for strerror - -namespace llvm { -class raw_ostream; -} - -using namespace lldb; -using namespace lldb_private; - -Error::Error() : m_code(0), m_type(eErrorTypeInvalid), m_string() {} - -Error::Error(ValueType err, ErrorType type) - : m_code(err), m_type(type), m_string() {} - -Error::Error(std::error_code EC) - : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric), - m_string(EC.message()) {} - -Error::Error(const Error &rhs) = default; - -Error::Error(const char *format, ...) - : m_code(0), m_type(eErrorTypeInvalid), m_string() { - va_list args; - va_start(args, format); - SetErrorToGenericError(); - SetErrorStringWithVarArg(format, args); - va_end(args); -} - -//---------------------------------------------------------------------- -// Assignment operator -//---------------------------------------------------------------------- -const Error &Error::operator=(const Error &rhs) { - if (this != &rhs) { - m_code = rhs.m_code; - m_type = rhs.m_type; - m_string = rhs.m_string; - } - return *this; -} - -//---------------------------------------------------------------------- -// Assignment operator -//---------------------------------------------------------------------- -const Error &Error::operator=(uint32_t err) { - m_code = err; - m_type = eErrorTypeMachKernel; - m_string.clear(); - return *this; -} - -Error::~Error() = default; - -//---------------------------------------------------------------------- -// 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. -//---------------------------------------------------------------------- -const char *Error::AsCString(const char *default_error_str) const { - if (Success()) - return nullptr; - - if (m_string.empty()) { - const char *s = nullptr; - switch (m_type) { - case eErrorTypeMachKernel: -#if defined(__APPLE__) - s = ::mach_error_string(m_code); -#endif - break; - - case eErrorTypePOSIX: - s = ::strerror(m_code); - break; - - default: - break; - } - if (s != nullptr) - m_string.assign(s); - } - if (m_string.empty()) { - if (default_error_str) - m_string.assign(default_error_str); - else - return nullptr; // User wanted a nullptr string back... - } - return m_string.c_str(); -} - -//---------------------------------------------------------------------- -// Clear the error and any cached error string that it might contain. -//---------------------------------------------------------------------- -void Error::Clear() { - m_code = 0; - m_type = eErrorTypeInvalid; - m_string.clear(); -} - -//---------------------------------------------------------------------- -// Access the error value. -//---------------------------------------------------------------------- -Error::ValueType Error::GetError() const { return m_code; } - -//---------------------------------------------------------------------- -// Access the error type. -//---------------------------------------------------------------------- -ErrorType Error::GetType() const { return m_type; } - -//---------------------------------------------------------------------- -// Returns true if this object contains a value that describes an -// error or otherwise non-success result. -//---------------------------------------------------------------------- -bool Error::Fail() const { return m_code != 0; } - -//---------------------------------------------------------------------- -// Set accesssor for the error value to "err" and the type to -// "eErrorTypeMachKernel" -//---------------------------------------------------------------------- -void Error::SetMachError(uint32_t err) { - m_code = err; - m_type = eErrorTypeMachKernel; - m_string.clear(); -} - -void Error::SetExpressionError(lldb::ExpressionResults result, - const char *mssg) { - m_code = result; - m_type = eErrorTypeExpression; - m_string = mssg; -} - -int Error::SetExpressionErrorWithFormat(lldb::ExpressionResults result, - const char *format, ...) { - int length = 0; - - if (format != nullptr && format[0]) { - 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; -} - -//---------------------------------------------------------------------- -// Set accesssor for the error value and type. -//---------------------------------------------------------------------- -void Error::SetError(ValueType err, ErrorType type) { - m_code = err; - m_type = type; - m_string.clear(); -} - -//---------------------------------------------------------------------- -// Update the error value to be "errno" and update the type to -// be "POSIX". -//---------------------------------------------------------------------- -void Error::SetErrorToErrno() { - m_code = errno; - m_type = eErrorTypePOSIX; - m_string.clear(); -} - -//---------------------------------------------------------------------- -// Update the error value to be LLDB_GENERIC_ERROR and update the type -// to be "Generic". -//---------------------------------------------------------------------- -void Error::SetErrorToGenericError() { - m_code = LLDB_GENERIC_ERROR; - m_type = eErrorTypeGeneric; - m_string.clear(); -} - -//---------------------------------------------------------------------- -// 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. -//---------------------------------------------------------------------- -void Error::SetErrorString(llvm::StringRef err_str) { - if (!err_str.empty()) { - // If we have an error string, we should always at least have an error - // set to a generic value. - if (Success()) - SetErrorToGenericError(); - } - m_string = err_str; -} - -//------------------------------------------------------------------ -/// Set the current error string to a formatted error string. -/// -/// @param format -/// A printf style format string -//------------------------------------------------------------------ -int Error::SetErrorStringWithFormat(const char *format, ...) { - if (format != nullptr && format[0]) { - va_list args; - va_start(args, format); - int length = SetErrorStringWithVarArg(format, args); - va_end(args); - return length; - } else { - m_string.clear(); - } - return 0; -} - -int Error::SetErrorStringWithVarArg(const char *format, va_list args) { - if (format != nullptr && format[0]) { - // If we have an error string, we should always at least have - // an error set to a generic value. - if (Success()) - SetErrorToGenericError(); - - llvm::SmallString<1024> buf; - VASprintf(buf, format, args); - m_string = buf.str(); - return buf.size(); - } else { - m_string.clear(); - } - return 0; -} - -//---------------------------------------------------------------------- -// Returns true if the error code in this object is considered a -// successful return value. -//---------------------------------------------------------------------- -bool Error::Success() const { return m_code == 0; } - -bool Error::WasInterrupted() const { - return (m_type == eErrorTypePOSIX && m_code == EINTR); -} - -void llvm::format_provider::format( - const lldb_private::Error &error, llvm::raw_ostream &OS, - llvm::StringRef Options) { - llvm::format_provider::format(error.AsCString(), OS, - Options); -} diff --git a/source/Utility/JSON.cpp b/source/Utility/JSON.cpp index d20d9e46fefd6..cb23f140cbfe0 100644 --- a/source/Utility/JSON.cpp +++ b/source/Utility/JSON.cpp @@ -246,7 +246,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { "error: an error occurred getting a character from offset %" PRIu64, start_index); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } else { const bool is_end_quote = escaped_ch == '"'; @@ -259,13 +259,13 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { "character 0x%4.4x at offset %" PRIu64, escaped_ch, start_index); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } } else if (is_end_quote) { return Token::String; } else if (is_null) { value = "error: missing end quote for string"; - return Token::Error; + return Token::Status; } } } @@ -316,7 +316,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { error.Printf("error: extra decimal point found at offset %" PRIu64, start_index); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } else { got_decimal_point = true; ++m_index; // Skip this character @@ -330,7 +330,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { "error: extra exponent character found at offset %" PRIu64, start_index); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } else { exp_index = m_index; ++m_index; // Skip this character @@ -346,7 +346,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { error.Printf("error: unexpected %c character at offset %" PRIu64, next_ch, start_index); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } break; @@ -368,7 +368,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { "at offset in float value \"%s\"", value.c_str()); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } } else { // No exponent, but we need at least one decimal after the decimal @@ -379,7 +379,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { error.Printf("error: no digits after decimal point \"%s\"", value.c_str()); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } } } else { @@ -390,14 +390,14 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { } else { error.Printf("error: no digits negate sign \"%s\"", value.c_str()); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } } } else { error.Printf("error: invalid number found at offset %" PRIu64, start_index); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } } break; default: @@ -407,7 +407,7 @@ JSONParser::Token JSONParser::GetToken(std::string &value) { " (around character '%c')", start_index, ch); value = std::move(error.GetString()); - return Token::Error; + return Token::Status; } int JSONParser::GetEscapedChar(bool &was_escaped) { diff --git a/source/Utility/SelectHelper.cpp b/source/Utility/SelectHelper.cpp index 7b0557ea192ca..a46213f8bfcb8 100644 --- a/source/Utility/SelectHelper.cpp +++ b/source/Utility/SelectHelper.cpp @@ -15,8 +15,8 @@ #endif #include "lldb/Utility/SelectHelper.h" -#include "lldb/Utility/Error.h" #include "lldb/Utility/LLDBAssert.h" +#include "lldb/Utility/Status.h" #include "lldb/lldb-enumerations.h" // for ErrorType::eErrorTypePOSIX #include "lldb/lldb-types.h" // for socket_t @@ -90,14 +90,14 @@ static void updateMaxFd(llvm::Optional &vold, vold = std::max(*vold, vnew); } -lldb_private::Error SelectHelper::Select() { - lldb_private::Error error; +lldb_private::Status SelectHelper::Select() { + lldb_private::Status error; #ifdef _MSC_VER // On windows FD_SETSIZE limits the number of file descriptors, not their // numeric value. lldbassert(m_fd_map.size() <= FD_SETSIZE); if (m_fd_map.size() > FD_SETSIZE) - return lldb_private::Error("Too many file descriptors for select()"); + return lldb_private::Status("Too many file descriptors for select()"); #endif llvm::Optional max_read_fd; diff --git a/source/Utility/Status.cpp b/source/Utility/Status.cpp new file mode 100644 index 0000000000000..5996be1e4e059 --- /dev/null +++ b/source/Utility/Status.cpp @@ -0,0 +1,275 @@ +//===-- Status.cpp -----------------------------------------------*- C++ +//-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Utility/Status.h" + +#include "lldb/Utility/VASPrintf.h" +#include "lldb/lldb-defines.h" // for LLDB_GENERIC_ERROR +#include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType::eErr... +#include "llvm/ADT/SmallString.h" // for SmallString +#include "llvm/ADT/StringRef.h" // for StringRef +#include "llvm/Support/FormatProviders.h" // for format_provider + +#include +#include +#include // for string +#include + +#ifdef __APPLE__ +#include +#endif + +#include // for uint32_t +#include // for strerror + +namespace llvm { +class raw_ostream; +} + +using namespace lldb; +using namespace lldb_private; + +Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {} + +Status::Status(ValueType err, ErrorType type) + : m_code(err), m_type(type), m_string() {} + +Status::Status(std::error_code EC) + : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric), + m_string(EC.message()) {} + +Status::Status(const Status &rhs) = default; + +Status::Status(const char *format, ...) + : m_code(0), m_type(eErrorTypeInvalid), m_string() { + va_list args; + va_start(args, format); + SetErrorToGenericError(); + SetErrorStringWithVarArg(format, args); + va_end(args); +} + +//---------------------------------------------------------------------- +// Assignment operator +//---------------------------------------------------------------------- +const Status &Status::operator=(const Status &rhs) { + if (this != &rhs) { + m_code = rhs.m_code; + m_type = rhs.m_type; + m_string = rhs.m_string; + } + return *this; +} + +//---------------------------------------------------------------------- +// Assignment operator +//---------------------------------------------------------------------- +const Status &Status::operator=(uint32_t err) { + m_code = err; + m_type = eErrorTypeMachKernel; + m_string.clear(); + return *this; +} + +Status::~Status() = default; + +//---------------------------------------------------------------------- +// 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. +//---------------------------------------------------------------------- +const char *Status::AsCString(const char *default_error_str) const { + if (Success()) + return nullptr; + + if (m_string.empty()) { + const char *s = nullptr; + switch (m_type) { + case eErrorTypeMachKernel: +#if defined(__APPLE__) + s = ::mach_error_string(m_code); +#endif + break; + + case eErrorTypePOSIX: + s = ::strerror(m_code); + break; + + default: + break; + } + if (s != nullptr) + m_string.assign(s); + } + if (m_string.empty()) { + if (default_error_str) + m_string.assign(default_error_str); + else + return nullptr; // User wanted a nullptr string back... + } + return m_string.c_str(); +} + +//---------------------------------------------------------------------- +// Clear the error and any cached error string that it might contain. +//---------------------------------------------------------------------- +void Status::Clear() { + m_code = 0; + m_type = eErrorTypeInvalid; + m_string.clear(); +} + +//---------------------------------------------------------------------- +// Access the error value. +//---------------------------------------------------------------------- +Status::ValueType Status::GetError() const { return m_code; } + +//---------------------------------------------------------------------- +// Access the error type. +//---------------------------------------------------------------------- +ErrorType Status::GetType() const { return m_type; } + +//---------------------------------------------------------------------- +// Returns true if this object contains a value that describes an +// error or otherwise non-success result. +//---------------------------------------------------------------------- +bool Status::Fail() const { return m_code != 0; } + +//---------------------------------------------------------------------- +// Set accesssor for the error value to "err" and the type to +// "eErrorTypeMachKernel" +//---------------------------------------------------------------------- +void Status::SetMachError(uint32_t err) { + m_code = err; + m_type = eErrorTypeMachKernel; + m_string.clear(); +} + +void Status::SetExpressionError(lldb::ExpressionResults result, + const char *mssg) { + m_code = result; + m_type = eErrorTypeExpression; + m_string = mssg; +} + +int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result, + const char *format, ...) { + int length = 0; + + if (format != nullptr && format[0]) { + 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; +} + +//---------------------------------------------------------------------- +// Set accesssor for the error value and type. +//---------------------------------------------------------------------- +void Status::SetError(ValueType err, ErrorType type) { + m_code = err; + m_type = type; + m_string.clear(); +} + +//---------------------------------------------------------------------- +// Update the error value to be "errno" and update the type to +// be "POSIX". +//---------------------------------------------------------------------- +void Status::SetErrorToErrno() { + m_code = errno; + m_type = eErrorTypePOSIX; + m_string.clear(); +} + +//---------------------------------------------------------------------- +// Update the error value to be LLDB_GENERIC_ERROR and update the type +// to be "Generic". +//---------------------------------------------------------------------- +void Status::SetErrorToGenericError() { + m_code = LLDB_GENERIC_ERROR; + m_type = eErrorTypeGeneric; + m_string.clear(); +} + +//---------------------------------------------------------------------- +// 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. +//---------------------------------------------------------------------- +void Status::SetErrorString(llvm::StringRef err_str) { + if (!err_str.empty()) { + // If we have an error string, we should always at least have an error + // set to a generic value. + if (Success()) + SetErrorToGenericError(); + } + m_string = err_str; +} + +//------------------------------------------------------------------ +/// Set the current error string to a formatted error string. +/// +/// @param format +/// A printf style format string +//------------------------------------------------------------------ +int Status::SetErrorStringWithFormat(const char *format, ...) { + if (format != nullptr && format[0]) { + va_list args; + va_start(args, format); + int length = SetErrorStringWithVarArg(format, args); + va_end(args); + return length; + } else { + m_string.clear(); + } + return 0; +} + +int Status::SetErrorStringWithVarArg(const char *format, va_list args) { + if (format != nullptr && format[0]) { + // If we have an error string, we should always at least have + // an error set to a generic value. + if (Success()) + SetErrorToGenericError(); + + llvm::SmallString<1024> buf; + VASprintf(buf, format, args); + m_string = buf.str(); + return buf.size(); + } else { + m_string.clear(); + } + return 0; +} + +//---------------------------------------------------------------------- +// Returns true if the error code in this object is considered a +// successful return value. +//---------------------------------------------------------------------- +bool Status::Success() const { return m_code == 0; } + +bool Status::WasInterrupted() const { + return (m_type == eErrorTypePOSIX && m_code == EINTR); +} + +void llvm::format_provider::format( + const lldb_private::Status &error, llvm::raw_ostream &OS, + llvm::StringRef Options) { + llvm::format_provider::format(error.AsCString(), OS, + Options); +} diff --git a/source/Utility/UUID.cpp b/source/Utility/UUID.cpp index d82f4d41215e3..b47f8b52f1c24 100644 --- a/source/Utility/UUID.cpp +++ b/source/Utility/UUID.cpp @@ -160,12 +160,9 @@ llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p, bytes_decoded = uuid_byte_idx; return p; } -size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) { - if (cstr == NULL) - return 0; - llvm::StringRef orig(cstr); - llvm::StringRef p = orig; +size_t UUID::SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes) { + llvm::StringRef p = str; // Skip leading whitespace characters p = p.ltrim(); @@ -178,12 +175,19 @@ size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) { // were consumed if (bytes_decoded == num_uuid_bytes) { m_num_uuid_bytes = num_uuid_bytes; - return orig.size() - rest.size(); + return str.size() - rest.size(); } // Else return zero to indicate we were not able to parse a UUID value return 0; } + +size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) { + if (cstr == NULL) + return 0; + + return SetFromStringRef(cstr, num_uuid_bytes); +} } bool lldb_private::operator==(const lldb_private::UUID &lhs, -- cgit v1.2.3