Change Utility to use Twine.

This commit is contained in:
Peter Johnson
2017-12-01 22:12:50 -08:00
parent 54326311ad
commit 899892c119
2 changed files with 45 additions and 73 deletions

View File

@@ -16,6 +16,8 @@
#include <cstdlib>
#include <cstring>
#include <llvm/Path.h>
#include "ErrorBase.h"
#include "HAL/DriverStation.h"
#include "HAL/HAL.h"
@@ -30,49 +32,31 @@ using namespace frc;
* This allows breakpoints to be set on an assert. The users don't call this,
* but instead use the wpi_assert macros in Utility.h.
*/
bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText,
llvm::StringRef message, llvm::StringRef fileName,
bool wpi_assert_impl(bool conditionValue, const llvm::Twine& conditionText,
const llvm::Twine& message, llvm::StringRef fileName,
int lineNumber, llvm::StringRef funcName) {
if (!conditionValue) {
llvm::SmallString<128> locBuf;
llvm::raw_svector_ostream locStream(locBuf);
locStream << funcName << " [";
llvm::SmallString<128> fileTemp;
#ifdef _WIN32
char fname[60];
char ext[10];
_splitpath_s(fileName.c_str(fileTemp), nullptr, 0, nullptr, 0, fname, 60,
ext, 10);
locStream << fname << ":" << lineNumber << "]";
#elif __APPLE__
auto file = fileName.c_str(fileTemp);
int len = std::strlen(file) + 1;
char* basestr = new char[len + 1];
std::strncpy(basestr, file, len);
locStream << basestr << ":" << lineNumber << "]";
delete[] basestr;
#else
locStream << basename(fileName.c_str(fileTemp)) << ":" << lineNumber << "]";
#endif
locStream << funcName << " [" << llvm::sys::path::filename(fileName) << ":"
<< lineNumber << "]";
llvm::SmallString<128> errorBuf;
llvm::raw_svector_ostream errorStream(errorBuf);
errorStream << "Assertion \"" << conditionText << "\" ";
if (!message.empty()) {
errorStream << "failed: " << message << "\n";
} else {
if (message.isTriviallyEmpty() ||
(message.isSingleStringRef() && message.getSingleStringRef().empty())) {
errorStream << "failed.\n";
} else {
errorStream << "failed: " << message << "\n";
}
std::string stack = GetStackTrace(2);
std::string location = locStream.str();
std::string error = errorStream.str();
// Print the error and send it to the DriverStation
HAL_SendError(1, 1, 0, error.c_str(), location.c_str(), stack.c_str(), 1);
HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), stack.c_str(), 1);
}
return conditionValue;
@@ -84,32 +68,16 @@ bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText,
* This should not be called directly; it should only be used by
* wpi_assertEqual_impl and wpi_assertNotEqual_impl.
*/
void wpi_assertEqual_common_impl(llvm::StringRef valueA, llvm::StringRef valueB,
llvm::StringRef equalityType,
llvm::StringRef message,
void wpi_assertEqual_common_impl(const llvm::Twine& valueA,
const llvm::Twine& valueB,
const llvm::Twine& equalityType,
const llvm::Twine& message,
llvm::StringRef fileName, int lineNumber,
llvm::StringRef funcName) {
llvm::SmallString<128> locBuf;
llvm::raw_svector_ostream locStream(locBuf);
locStream << funcName << " [";
llvm::SmallString<128> fileTemp;
#ifdef _WIN32
char fname[60];
char ext[10];
_splitpath_s(fileName.c_str(fileTemp), nullptr, 0, nullptr, 0, fname, 60, ext,
10);
locStream << fname << ":" << lineNumber << "]";
#elif __APPLE__
auto file = fileName.c_str(fileTemp);
int len = std::strlen(file) + 1;
char* basestr = new char[len + 1];
std::strncpy(basestr, file, len);
locStream << basestr << ":" << lineNumber << "]";
delete[] basestr;
#else
locStream << basename(fileName.c_str(fileTemp)) << ":" << lineNumber << "]";
#endif
locStream << funcName << " [" << llvm::sys::path::filename(fileName) << ":"
<< lineNumber << "]";
llvm::SmallString<128> errorBuf;
llvm::raw_svector_ostream errorStream(errorBuf);
@@ -117,18 +85,17 @@ void wpi_assertEqual_common_impl(llvm::StringRef valueA, llvm::StringRef valueB,
errorStream << "Assertion \"" << valueA << " " << equalityType << " "
<< valueB << "\" ";
if (!message.empty()) {
errorStream << "failed: " << message << "\n";
} else {
if (message.isTriviallyEmpty() ||
(message.isSingleStringRef() && message.getSingleStringRef().empty())) {
errorStream << "failed.\n";
} else {
errorStream << "failed: " << message << "\n";
}
std::string trace = GetStackTrace(3);
std::string location = locStream.str();
std::string error = errorStream.str();
// Print the error and send it to the DriverStation
HAL_SendError(1, 1, 0, error.c_str(), location.c_str(), trace.c_str(), 1);
HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), trace.c_str(), 1);
}
/**
@@ -138,10 +105,11 @@ void wpi_assertEqual_common_impl(llvm::StringRef valueA, llvm::StringRef valueB,
* of each is printed along with an optional message string. The users don't
* call this, but instead use the wpi_assertEqual macros in Utility.h.
*/
bool wpi_assertEqual_impl(int valueA, int valueB, llvm::StringRef valueAString,
llvm::StringRef valueBString, llvm::StringRef message,
llvm::StringRef fileName, int lineNumber,
llvm::StringRef funcName) {
bool wpi_assertEqual_impl(int valueA, int valueB,
const llvm::Twine& valueAString,
const llvm::Twine& valueBString,
const llvm::Twine& message, llvm::StringRef fileName,
int lineNumber, llvm::StringRef funcName) {
if (!(valueA == valueB)) {
wpi_assertEqual_common_impl(valueAString, valueBString, "==", message,
fileName, lineNumber, funcName);
@@ -157,10 +125,11 @@ bool wpi_assertEqual_impl(int valueA, int valueB, llvm::StringRef valueAString,
* this, but instead use the wpi_assertNotEqual macros in Utility.h.
*/
bool wpi_assertNotEqual_impl(int valueA, int valueB,
llvm::StringRef valueAString,
llvm::StringRef valueBString,
llvm::StringRef message, llvm::StringRef fileName,
int lineNumber, llvm::StringRef funcName) {
const llvm::Twine& valueAString,
const llvm::Twine& valueBString,
const llvm::Twine& message,
llvm::StringRef fileName, int lineNumber,
llvm::StringRef funcName) {
if (!(valueA != valueB)) {
wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message,
fileName, lineNumber, funcName);