Fixed a bunch of clang-tidy warnings.

This commit is contained in:
2024-08-18 13:07:38 +02:00
parent d73fa25ed8
commit 9f011952c2
8 changed files with 46 additions and 39 deletions

View File

@@ -4,7 +4,7 @@
#include <algorithm>
#include <sstream>
#include "../detect.hpp"
#include "../util/iterators.hpp"
#include "../debug/assert.hpp"
#include "../util/string.hpp"
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX || MIJIN_TARGET_OS == MIJIN_OS_OSX
@@ -34,10 +34,10 @@ StreamError ProcessStream::open(const char* command, FileOpenMode mode_)
modeStr = "rw";
break;
default:
assert(!"Unsupported mode for ProcessStream::open()");
MIJIN_ERROR("Unsupported mode for ProcessStream::open()");
return StreamError::NOT_SUPPORTED;
}
handle = popen(command, modeStr); // NOLINT(cppcoreguidelines-owning-memory)
handle = popen(command, modeStr); // NOLINT(cppcoreguidelines-owning-memory,cert-env33-c)
if (!handle) {
return StreamError::IO_ERROR;
}
@@ -47,7 +47,7 @@ StreamError ProcessStream::open(const char* command, FileOpenMode mode_)
int ProcessStream::close()
{
assert(handle);
MIJIN_ASSERT(handle != nullptr, "Process stream has not been opened.");
const int result = pclose(handle);
handle = nullptr;
return result;
@@ -55,10 +55,10 @@ int ProcessStream::close()
StreamError ProcessStream::readRaw(std::span<std::uint8_t> buffer, const ReadOptions& options, std::size_t* outBytesRead)
{
assert(handle);
assert(mode == FileOpenMode::READ || mode == FileOpenMode::READ_WRITE);
MIJIN_ASSERT(handle != nullptr, "Process stream has not been openend.");
MIJIN_ASSERT(mode == FileOpenMode::READ || mode == FileOpenMode::READ_WRITE, "Cannot read from this process stream.");
if (bufferedChar >= 0 && buffer.size() > 0)
if (bufferedChar >= 0 && !buffer.empty())
{
buffer[0] = static_cast<std::uint8_t>(bufferedChar);
}
@@ -80,8 +80,8 @@ StreamError ProcessStream::readRaw(std::span<std::uint8_t> buffer, const ReadOpt
StreamError ProcessStream::writeRaw(std::span<const std::uint8_t> buffer)
{
assert(handle);
assert(mode == FileOpenMode::WRITE || mode == FileOpenMode::READ_WRITE);
MIJIN_ASSERT(handle != nullptr, "Process stream has not been opened.");
MIJIN_ASSERT(mode == FileOpenMode::WRITE || mode == FileOpenMode::READ_WRITE, "Cannot write to this process stream.");
const std::size_t written = std::fwrite(buffer.data(), 1, buffer.size(), handle);
if (written != buffer.size() || std::ferror(handle)) {
@@ -93,7 +93,7 @@ StreamError ProcessStream::writeRaw(std::span<const std::uint8_t> buffer)
std::size_t ProcessStream::tell()
{
assert(handle);
MIJIN_ASSERT(handle != nullptr, "Process stream has not been opened.");
return std::ftell(handle); // TODO: does this work?
}
@@ -108,22 +108,19 @@ StreamError ProcessStream::seek(std::intptr_t pos, SeekMode seekMode)
void ProcessStream::flush()
{
const int result = std::fflush(handle);
assert(result == 0);
MIJIN_ASSERT(result == 0, "Error flushing process stream.");
}
bool ProcessStream::isAtEnd()
{
assert(handle);
MIJIN_ASSERT(handle != nullptr, "Process stream has not been opened.");
if (bufferedChar >= 0) {
return false;
}
bufferedChar = std::fgetc(handle);
if (std::feof(handle)) {
return true;
}
return false;
return static_cast<bool>(std::feof(handle));
}
StreamFeatures ProcessStream::getFeatures()
@@ -161,6 +158,7 @@ std::string shellEscape(const std::string& arg) noexcept
case '$':
oss << '\\';
break;
default: break;
}
oss << chr;
}

View File

@@ -26,7 +26,7 @@ public:
[[nodiscard]] inline bool isOpen() const { return handle != nullptr; }
// Stream overrides
StreamError readRaw(std::span<std::uint8_t> buffer, const ReadOptions& options = {}, std::size_t* outBytesRead = nullptr) override;
StreamError readRaw(std::span<std::uint8_t> buffer, const ReadOptions& options, std::size_t* outBytesRead) override;
StreamError writeRaw(std::span<const std::uint8_t> buffer) override;
std::size_t tell() override;
StreamError seek(std::intptr_t pos, SeekMode seekMode = SeekMode::ABSOLUTE) override;

View File

@@ -75,7 +75,7 @@ StreamError Stream::writeBinaryString(std::string_view str)
{
MIJIN_ASSERT(str.length() <= std::numeric_limits<std::uint32_t>::max(), "Binary string is too long.");
const std::uint32_t length = static_cast<std::uint32_t>(str.length());
StreamError error = write(length);
const StreamError error = write(length);
if (error != StreamError::SUCCESS) {
return error;
}
@@ -118,12 +118,12 @@ StreamError Stream::getTotalLength(std::size_t& outLength)
return StreamError::NOT_SUPPORTED;
}
const std::size_t origPos = tell();
if (StreamError error = seek(0, SeekMode::RELATIVE_TO_END); error != StreamError::SUCCESS)
if (const StreamError error = seek(0, SeekMode::RELATIVE_TO_END); error != StreamError::SUCCESS)
{
return error;
}
outLength = tell();
if (StreamError error = seek(static_cast<std::intptr_t>(origPos)); error != StreamError::SUCCESS)
if (const StreamError error = seek(static_cast<std::intptr_t>(origPos)); error != StreamError::SUCCESS)
{
return error;
}
@@ -134,12 +134,12 @@ StreamError Stream::readRest(TypelessBuffer& outBuffer)
{
// first try to allocate everything at once
std::size_t length = 0;
if (StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
if (const StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
{
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
length -= tell();
outBuffer.resize(length);
if (StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
if (const StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
{
return error;
}
@@ -153,13 +153,13 @@ StreamError Stream::readRest(TypelessBuffer& outBuffer)
while (!isAtEnd())
{
std::size_t bytesRead = 0;
if (StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
{
return error;
}
outBuffer.resize(outBuffer.byteSize() + bytesRead);
std::span<std::byte> bufferBytes = outBuffer.makeSpan<std::byte>();
const std::span<std::byte> bufferBytes = outBuffer.makeSpan<std::byte>();
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
}
return StreamError::SUCCESS;
@@ -213,14 +213,14 @@ StreamError Stream::readLine(std::string& outString)
{
// read into the buffer
std::size_t bytesRead = 0;
if (StreamError error = readRaw(buffer, {.partial = true, .peek = true}, &bytesRead); error != StreamError::SUCCESS)
if (const StreamError error = readRaw(buffer, {.partial = true, .peek = true}, &bytesRead); error != StreamError::SUCCESS)
{
return error;
}
// try to find a \n
auto begin = buffer.begin();
auto end = buffer.begin() + bytesRead;
auto newline = std::find(begin, end, '\n');
auto begin = buffer.begin(); // NOLINT(readability-qualified-auto)
auto end = buffer.begin() + bytesRead; // NOLINT(readability-qualified-auto)
auto newline = std::find(begin, end, '\n'); // NOLINT(readability-qualified-auto)
if (newline != end)
{
@@ -235,7 +235,7 @@ StreamError Stream::readLine(std::string& outString)
}
// read again, this time to skip
if (StreamError error = readSpan(begin, end); error != StreamError::SUCCESS)
if (const StreamError error = readSpan(begin, end); error != StreamError::SUCCESS)
{
return error;
}
@@ -262,9 +262,9 @@ mijin::Task<StreamError> Stream::c_readLine(std::string& outString)
co_return error;
}
// try to find a \n
auto begin = buffer.begin();
auto end = buffer.begin() + bytesRead;
auto newline = std::find(begin, end, '\n');
auto begin = buffer.begin(); // NOLINT(readability-qualified-auto)
auto end = buffer.begin() + bytesRead; // NOLINT(readability-qualified-auto)
auto newline = std::find(begin, end, '\n'); // NOLINT(readability-qualified-auto)
if (newline != end)
{
@@ -359,7 +359,7 @@ StreamError FileStream::readRaw(std::span<std::uint8_t> buffer, const ReadOption
}
if (options.peek)
{
if (StreamError error = seek(-static_cast<std::intptr_t>(readBytes), SeekMode::RELATIVE); error != StreamError::SUCCESS)
if (const StreamError error = seek(-static_cast<std::intptr_t>(readBytes), SeekMode::RELATIVE); error != StreamError::SUCCESS)
{
return error;
}