From 14dcd0d26ff3fbb4e9b3fc3048d22fc9f2ad48e7 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 4 Nov 2023 22:18:42 -0700 Subject: [PATCH] Use char instead of uint8_t for json::parse (#5877) The uint8_t usage causes warnings on newer clang versions. Add GetCharBuffer() to MemoryBuffer classes to make this easy. --- .../src/main/native/cpp/AprilTagFieldLayout.cpp | 2 +- .../multiCameraServer/src/main/native/cpp/main.cpp | 2 +- glass/src/lib/native/cpp/Context.cpp | 10 ++++------ .../main/native/cpp/analysis/AnalysisManager.cpp | 4 ++-- .../src/main/native/cpp/analysis/JSONConverter.cpp | 2 +- .../main/native/cpp/trajectory/TrajectoryUtil.cpp | 2 +- .../thirdparty/llvm/include/wpi/MemoryBuffer.h | 13 +++++++++++++ 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp b/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp index c971a488fc..59b30ec30e 100644 --- a/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp +++ b/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp @@ -22,7 +22,7 @@ AprilTagFieldLayout::AprilTagFieldLayout(std::string_view path) { throw std::runtime_error(fmt::format("Cannot open file: {}", path)); } - wpi::json json = wpi::json::parse(fileBuffer->begin(), fileBuffer->end()); + wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer()); for (const auto& tag : json.at("tags").get>()) { m_apriltags[tag.ID] = tag; diff --git a/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp b/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp index 279eebaaa1..706e2a2656 100644 --- a/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp +++ b/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp @@ -105,7 +105,7 @@ bool ReadConfig() { // parse file wpi::json j; try { - j = wpi::json::parse(fileBuffer->begin(), fileBuffer->end()); + j = wpi::json::parse(fileBuffer->GetCharBuffer()); } catch (const wpi::json::parse_error& e) { fmt::print(stderr, "config error in '{}': byte {}: {}\n", configFile, e.byte, e.what()); diff --git a/glass/src/lib/native/cpp/Context.cpp b/glass/src/lib/native/cpp/Context.cpp index e09a86efbd..1213bf438f 100644 --- a/glass/src/lib/native/cpp/Context.cpp +++ b/glass/src/lib/native/cpp/Context.cpp @@ -137,9 +137,8 @@ static bool LoadWindowStorageImpl(const std::string& filename) { return false; } else { try { - return JsonToWindow( - wpi::json::parse(fileBuffer->begin(), fileBuffer->end()), - filename.c_str()); + return JsonToWindow(wpi::json::parse(fileBuffer->GetCharBuffer()), + filename.c_str()); } catch (wpi::json::parse_error& e) { ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what()); return false; @@ -164,9 +163,8 @@ static bool LoadStorageRootImpl(Context* ctx, const std::string& filename, createdStorage = true; } try { - storage->FromJson( - wpi::json::parse(fileBuffer->begin(), fileBuffer->end()), - filename.c_str()); + storage->FromJson(wpi::json::parse(fileBuffer->GetCharBuffer()), + filename.c_str()); } catch (wpi::json::parse_error& e) { ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what()); if (createdStorage) { diff --git a/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp b/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp index b15cae84c8..0710d93e0f 100644 --- a/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp +++ b/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp @@ -457,7 +457,7 @@ AnalysisManager::AnalysisManager(std::string_view path, Settings& settings, throw FileReadingError(path); } - m_json = wpi::json::parse(fileBuffer->begin(), fileBuffer->end()); + m_json = wpi::json::parse(fileBuffer->GetCharBuffer()); WPI_INFO(m_logger, "Read {}", path); } @@ -475,7 +475,7 @@ AnalysisManager::AnalysisManager(std::string_view path, Settings& settings, throw FileReadingError(newPath); } - m_json = wpi::json::parse(fileBuffer->begin(), fileBuffer->end()); + m_json = wpi::json::parse(fileBuffer->GetCharBuffer()); WPI_INFO(m_logger, "Read {}", newPath); } diff --git a/sysid/src/main/native/cpp/analysis/JSONConverter.cpp b/sysid/src/main/native/cpp/analysis/JSONConverter.cpp index 9bab8c681e..5060679d98 100644 --- a/sysid/src/main/native/cpp/analysis/JSONConverter.cpp +++ b/sysid/src/main/native/cpp/analysis/JSONConverter.cpp @@ -40,7 +40,7 @@ static wpi::json GetJSON(std::string_view path, wpi::Logger& logger) { throw std::runtime_error(fmt::format("Unable to read: {}", path)); } - wpi::json json = wpi::json::parse(fileBuffer->begin(), fileBuffer->end()); + wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer()); WPI_INFO(logger, "Read frc-characterization JSON from {}", path); return json; } diff --git a/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp b/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp index da9c955efc..d9cb853a5b 100644 --- a/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp +++ b/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp @@ -35,7 +35,7 @@ Trajectory TrajectoryUtil::FromPathweaverJson(std::string_view path) { throw std::runtime_error(fmt::format("Cannot open file: {}", path)); } - wpi::json json = wpi::json::parse(fileBuffer->begin(), fileBuffer->end()); + wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer()); return Trajectory{json.get>()}; } diff --git a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h index 7907c07d67..b5eaea406f 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h +++ b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h @@ -62,6 +62,11 @@ class MemoryBuffer { std::span GetBuffer() const { return {begin(), end()}; } + std::span GetCharBuffer() const { + return {reinterpret_cast(begin()), + reinterpret_cast(end())}; + } + /// Return an identifier for this buffer, typically the filename it was read /// from. virtual std::string_view GetBufferIdentifier() const { @@ -145,6 +150,10 @@ class WritableMemoryBuffer : public MemoryBuffer { uint8_t* begin() { return const_cast(MemoryBuffer::begin()); } uint8_t* end() { return const_cast(MemoryBuffer::end()); } std::span GetBuffer() { return {begin(), end()}; } + std::span GetCharBuffer() const { + return {reinterpret_cast(const_cast(begin())), + reinterpret_cast(const_cast(end()))}; + } static std::unique_ptr GetFile( std::string_view filename, std::error_code& ec, int64_t fileSize = -1); @@ -196,6 +205,10 @@ class WriteThroughMemoryBuffer : public MemoryBuffer { uint8_t* begin() { return const_cast(MemoryBuffer::begin()); } uint8_t* end() { return const_cast(MemoryBuffer::end()); } std::span GetBuffer() { return {begin(), end()}; } + std::span GetCharBuffer() const { + return {reinterpret_cast(const_cast(begin())), + reinterpret_cast(const_cast(end()))}; + } static std::unique_ptr GetFile( std::string_view filename, std::error_code& ec, int64_t fileSize = -1);