Skip to content

Commit

Permalink
[wpiutil] Convert a couple of use cases to std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Jan 18, 2025
1 parent 23814d1 commit 243ddc1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
10 changes: 4 additions & 6 deletions wpiutil/src/main/native/unix/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@
#include <cstdio>
#include <string>

#include "wpi/SmallString.h"

namespace wpi {

std::string Demangle(std::string_view mangledSymbol) {
SmallString<128> buf{mangledSymbol};
std::string buf{mangledSymbol};
size_t length;
int32_t status;

char* symbol = abi::__cxa_demangle(buf.c_str(), nullptr, &length, &status);
if (status == 0) {
std::string rv{symbol};
buf = symbol;
std::free(symbol);
return rv;
return buf;
}

// If everything else failed, just return the mangled symbol
return std::string{mangledSymbol};
return buf;
}

} // namespace wpi
7 changes: 3 additions & 4 deletions wpiutil/src/main/native/unix/StackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <string>

#include "wpi/Demangle.h"
#include "wpi/SmallString.h"
#include "wpi/StringExtras.h"
#include "wpi/raw_ostream.h"

Expand All @@ -20,8 +19,8 @@ std::string GetStackTraceDefault(int offset) {
void* stackTrace[128];
int stackSize = backtrace(stackTrace, 128);
char** mangledSymbols = backtrace_symbols(stackTrace, stackSize);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream trace(buf);
std::string buf;
wpi::raw_string_ostream trace(buf);

for (int i = offset; i < stackSize; i++) {
// Only print recursive functions once in a row.
Expand All @@ -38,7 +37,7 @@ std::string GetStackTraceDefault(int offset) {

std::free(mangledSymbols);

return std::string{trace.str()};
return buf;
#else
// backtrace_symbols not supported on android
return "";
Expand Down

0 comments on commit 243ddc1

Please sign in to comment.