mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
Removed unnecessary ::std::string calls.
All the Error and assert calls were using const ::std::string & arguments. When provided with a char*, the compiler was creating a temporary string object to pass in. This was triggering mallocs everywhere, even in the fast paths. Change-Id: Ie0ad1f240334de677618086bddd64113c56aae6e
This commit is contained in:
committed by
Peter Johnson
parent
055ee09825
commit
61760c839a
@@ -25,17 +25,17 @@
|
||||
* The users don't call this, but instead use the wpi_assert macros in
|
||||
* Utility.h.
|
||||
*/
|
||||
bool wpi_assert_impl(bool conditionValue, const std::string &conditionText,
|
||||
const std::string &message, const std::string &fileName,
|
||||
uint32_t lineNumber, const std::string &funcName) {
|
||||
bool wpi_assert_impl(bool conditionValue, const char *conditionText,
|
||||
const char *message, const char *fileName,
|
||||
uint32_t lineNumber, const char *funcName) {
|
||||
if (!conditionValue) {
|
||||
std::stringstream errorStream;
|
||||
|
||||
errorStream << "Assertion \"" << conditionText << "\" ";
|
||||
errorStream << "on line " << lineNumber << " ";
|
||||
errorStream << "of " << basename(fileName.c_str()) << " ";
|
||||
errorStream << "of " << basename(fileName) << " ";
|
||||
|
||||
if (message.size() > 0) {
|
||||
if (message[0] != '\0') {
|
||||
errorStream << "failed: " << message << std::endl;
|
||||
} else {
|
||||
errorStream << "failed." << std::endl;
|
||||
@@ -59,18 +59,20 @@ bool wpi_assert_impl(bool conditionValue, const std::string &conditionText,
|
||||
* wpi_assertEqual_impl
|
||||
* and wpi_assertNotEqual_impl.
|
||||
*/
|
||||
void wpi_assertEqual_common_impl(const std::string &valueA, const std::string &valueB,
|
||||
const std::string &equalityType, const std::string &message,
|
||||
const std::string &fileName, uint32_t lineNumber,
|
||||
const std::string &funcName) {
|
||||
void wpi_assertEqual_common_impl(const char *valueA, const char *valueB,
|
||||
const char *equalityType,
|
||||
const char *message,
|
||||
const char *fileName,
|
||||
uint32_t lineNumber,
|
||||
const char *funcName) {
|
||||
std::stringstream errorStream;
|
||||
|
||||
errorStream << "Assertion \"" << valueA << " " << equalityType << " "
|
||||
<< valueB << "\" ";
|
||||
errorStream << "on line " << lineNumber << " ";
|
||||
errorStream << "of " << basename(fileName.c_str()) << " ";
|
||||
errorStream << "of " << basename(fileName) << " ";
|
||||
|
||||
if (message.size() > 0) {
|
||||
if (message[0] != '\0') {
|
||||
errorStream << "failed: " << message << std::endl;
|
||||
} else {
|
||||
errorStream << "failed." << std::endl;
|
||||
@@ -92,10 +94,10 @@ void wpi_assertEqual_common_impl(const std::string &valueA, const std::string &v
|
||||
* The users don't call this, but instead use the wpi_assertEqual macros in
|
||||
* Utility.h.
|
||||
*/
|
||||
bool wpi_assertEqual_impl(int valueA, int valueB, const std::string &valueAString,
|
||||
const std::string &valueBString, const std::string &message,
|
||||
const std::string &fileName, uint32_t lineNumber,
|
||||
const std::string &funcName) {
|
||||
bool wpi_assertEqual_impl(int valueA, int valueB, const char *valueAString,
|
||||
const char *valueBString, const char *message,
|
||||
const char *fileName, uint32_t lineNumber,
|
||||
const char *funcName) {
|
||||
if (!(valueA == valueB)) {
|
||||
wpi_assertEqual_common_impl(valueAString, valueBString, "==", message,
|
||||
fileName, lineNumber, funcName);
|
||||
@@ -110,10 +112,10 @@ bool wpi_assertEqual_impl(int valueA, int valueB, const std::string &valueAStrin
|
||||
* The users don't call this, but instead use the wpi_assertNotEqual macros in
|
||||
* Utility.h.
|
||||
*/
|
||||
bool wpi_assertNotEqual_impl(int valueA, int valueB, const std::string &valueAString,
|
||||
const std::string &valueBString, const std::string &message,
|
||||
const std::string &fileName, uint32_t lineNumber,
|
||||
const std::string &funcName) {
|
||||
bool wpi_assertNotEqual_impl(int valueA, int valueB, const char *valueAString,
|
||||
const char *valueBString, const char *message,
|
||||
const char *fileName, uint32_t lineNumber,
|
||||
const char *funcName) {
|
||||
if (!(valueA != valueB)) {
|
||||
wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message,
|
||||
fileName, lineNumber, funcName);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "llvm/StringRef.h"
|
||||
|
||||
// Forward declarations
|
||||
class ErrorBase;
|
||||
@@ -41,8 +42,8 @@ class Error {
|
||||
const ErrorBase* GetOriginatingObject() const;
|
||||
double GetTimestamp() const;
|
||||
void Clear();
|
||||
void Set(Code code, const std::string& contextMessage,
|
||||
const std::string& filename, const std::string& function,
|
||||
void Set(Code code, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber, const ErrorBase* originatingObject);
|
||||
|
||||
private:
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "Error.h"
|
||||
|
||||
#include "HAL/cpp/priority_mutex.h"
|
||||
#include "llvm/StringRef.h"
|
||||
|
||||
#define wpi_setErrnoErrorWithContext(context) \
|
||||
(this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__))
|
||||
@@ -60,34 +61,29 @@ class ErrorBase {
|
||||
|
||||
virtual Error& GetError();
|
||||
virtual const Error& GetError() const;
|
||||
virtual void SetErrnoError(const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
virtual void SetErrnoError(llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const;
|
||||
virtual void SetImaqError(int success, const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
virtual void SetImaqError(int success, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const;
|
||||
virtual void SetError(Error::Code code, const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function, uint32_t lineNumber) const;
|
||||
virtual void SetWPIError(const std::string& errorMessage, Error::Code code,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
virtual void SetError(Error::Code code, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const;
|
||||
virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code,
|
||||
llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const;
|
||||
virtual void CloneError(const ErrorBase& rhs) const;
|
||||
virtual void ClearError() const;
|
||||
virtual bool StatusIsFatal() const;
|
||||
static void SetGlobalError(Error::Code code,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function, uint32_t lineNumber);
|
||||
static void SetGlobalWPIError(const std::string& errorMessage,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber);
|
||||
static void SetGlobalError(Error::Code code, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber);
|
||||
static void SetGlobalWPIError(llvm::StringRef errorMessage,
|
||||
llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename,
|
||||
llvm::StringRef function, uint32_t lineNumber);
|
||||
static Error& GetGlobalError();
|
||||
|
||||
protected:
|
||||
|
||||
@@ -30,17 +30,17 @@
|
||||
wpi_assertNotEqual_impl(a, b, #a, #b, message, __FILE__, __LINE__, \
|
||||
__FUNCTION__)
|
||||
|
||||
bool wpi_assert_impl(bool conditionValue, const std::string &conditionText,
|
||||
const std::string &message, const std::string &fileName,
|
||||
uint32_t lineNumber, const std::string &funcName);
|
||||
bool wpi_assertEqual_impl(int valueA, int valueB, const std::string &valueAString,
|
||||
const std::string &valueBString, const std::string &message,
|
||||
const std::string &fileName, uint32_t lineNumber,
|
||||
const std::string &funcName);
|
||||
bool wpi_assertNotEqual_impl(int valueA, int valueB, const std::string &valueAString,
|
||||
const std::string &valueBString, const std::string &message,
|
||||
const std::string &fileName, uint32_t lineNumber,
|
||||
const std::string &funcName);
|
||||
bool wpi_assert_impl(bool conditionValue, const char *conditionText,
|
||||
const char *message, const char *fileName,
|
||||
uint32_t lineNumber, const char *funcName);
|
||||
bool wpi_assertEqual_impl(int valueA, int valueB, const char *valueAString,
|
||||
const char *valueBString, const char *message,
|
||||
const char *fileName, uint32_t lineNumber,
|
||||
const char *funcName);
|
||||
bool wpi_assertNotEqual_impl(int valueA, int valueB, const char *valueAString,
|
||||
const char *valueBString, const char *message,
|
||||
const char *fileName, uint32_t lineNumber,
|
||||
const char *funcName);
|
||||
|
||||
void wpi_suspendOnAssertEnabled(bool enabled);
|
||||
|
||||
|
||||
@@ -43,8 +43,8 @@ const ErrorBase* Error::GetOriginatingObject() const {
|
||||
|
||||
double Error::GetTimestamp() const { return m_timestamp; }
|
||||
|
||||
void Error::Set(Code code, const std::string& contextMessage,
|
||||
const std::string& filename, const std::string& function,
|
||||
void Error::Set(Code code, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber, const ErrorBase* originatingObject) {
|
||||
bool report = true;
|
||||
|
||||
|
||||
@@ -39,18 +39,19 @@ void ErrorBase::ClearError() const { m_error.Clear(); }
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetErrnoError(const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
void ErrorBase::SetErrnoError(llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename,
|
||||
llvm::StringRef function,
|
||||
uint32_t lineNumber) const {
|
||||
std::string err;
|
||||
int errNo = errno;
|
||||
if (errNo == 0) {
|
||||
err = "OK: " + contextMessage;
|
||||
err = "OK: ";
|
||||
err += contextMessage;
|
||||
} else {
|
||||
char buf[256];
|
||||
snprintf(buf, 256, "%s (0x%08X): %s", strerror(errNo), errNo,
|
||||
contextMessage.c_str());
|
||||
snprintf(buf, 256, "%s (0x%08X): %.*s", strerror(errNo), errNo,
|
||||
contextMessage.size(), contextMessage.data());
|
||||
err = buf;
|
||||
}
|
||||
|
||||
@@ -74,9 +75,8 @@ void ErrorBase::SetErrnoError(const std::string& contextMessage,
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetImaqError(int success, const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
void ErrorBase::SetImaqError(int success, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const {
|
||||
// If there was an error
|
||||
if (success <= 0) {
|
||||
@@ -103,9 +103,8 @@ void ErrorBase::SetImaqError(int success, const std::string& contextMessage,
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetError(Error::Code code, const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
void ErrorBase::SetError(Error::Code code, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const {
|
||||
// If there was an error
|
||||
if (code != 0) {
|
||||
@@ -129,12 +128,11 @@ void ErrorBase::SetError(Error::Code code, const std::string& contextMessage,
|
||||
* @param function Function of the error source
|
||||
* @param lineNumber Line number of the error source
|
||||
*/
|
||||
void ErrorBase::SetWPIError(const std::string& errorMessage, Error::Code code,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
void ErrorBase::SetWPIError(llvm::StringRef errorMessage, Error::Code code,
|
||||
llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename, llvm::StringRef function,
|
||||
uint32_t lineNumber) const {
|
||||
std::string err = errorMessage + ": " + contextMessage;
|
||||
std::string err = errorMessage.str() + ": " + contextMessage.str();
|
||||
|
||||
// Set the current error information for this object.
|
||||
m_error.Set(code, err, filename, function, lineNumber, this);
|
||||
@@ -157,11 +155,9 @@ void ErrorBase::CloneError(const ErrorBase& rhs) const {
|
||||
*/
|
||||
bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; }
|
||||
|
||||
void ErrorBase::SetGlobalError(Error::Code code,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
uint32_t lineNumber) {
|
||||
void ErrorBase::SetGlobalError(Error::Code code, llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename,
|
||||
llvm::StringRef function, uint32_t lineNumber) {
|
||||
// If there was an error
|
||||
if (code != 0) {
|
||||
std::lock_guard<priority_mutex> mutex(_globalErrorMutex);
|
||||
@@ -172,12 +168,12 @@ void ErrorBase::SetGlobalError(Error::Code code,
|
||||
}
|
||||
}
|
||||
|
||||
void ErrorBase::SetGlobalWPIError(const std::string& errorMessage,
|
||||
const std::string& contextMessage,
|
||||
const std::string& filename,
|
||||
const std::string& function,
|
||||
void ErrorBase::SetGlobalWPIError(llvm::StringRef errorMessage,
|
||||
llvm::StringRef contextMessage,
|
||||
llvm::StringRef filename,
|
||||
llvm::StringRef function,
|
||||
uint32_t lineNumber) {
|
||||
std::string err = errorMessage + ": " + contextMessage;
|
||||
std::string err = errorMessage.str() + ": " + contextMessage.str();
|
||||
|
||||
std::lock_guard<priority_mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() != 0) {
|
||||
|
||||
Reference in New Issue
Block a user