Fixed a bunch of clang-tidy warnings.

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

View File

@ -53,7 +53,7 @@ int backtraceFullCallback(void* data, std::uintptr_t programCounter, const char*
{ {
BacktraceData& btData = *static_cast<BacktraceData*>(data); BacktraceData& btData = *static_cast<BacktraceData*>(data);
btData.stackframes.push_back({ btData.stackframes.push_back({
.address = reinterpret_cast<void*>(programCounter), .address = reinterpret_cast<void*>(programCounter), // NOLINT(performance-no-int-to-ptr)
.filename = filename ? filename : "<unknown>", .filename = filename ? filename : "<unknown>",
.function = function ? function : "<unknown>", .function = function ? function : "<unknown>",
.lineNumber = lineno .lineNumber = lineno
@ -121,7 +121,7 @@ using cxa_rethrow_type = void (*)();
cxa_throw_type orig_cxa_throw = nullptr; // Address of the original __cxa_throw cxa_throw_type orig_cxa_throw = nullptr; // Address of the original __cxa_throw
// cxa_rethrow_type orig_cxa_rethrow = nullptr; // Address of the original __cxa_rethrow // cxa_rethrow_type orig_cxa_rethrow = nullptr; // Address of the original __cxa_rethrow
extern "C" void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*)) extern "C" void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*)) // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
{ {
if (!orig_cxa_throw) { if (!orig_cxa_throw) {
orig_cxa_throw = reinterpret_cast<cxa_throw_type>(dlsym(RTLD_NEXT, "__cxa_throw")); orig_cxa_throw = reinterpret_cast<cxa_throw_type>(dlsym(RTLD_NEXT, "__cxa_throw"));

View File

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

View File

@ -26,7 +26,7 @@ public:
[[nodiscard]] inline bool isOpen() const { return handle != nullptr; } [[nodiscard]] inline bool isOpen() const { return handle != nullptr; }
// Stream overrides // 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; StreamError writeRaw(std::span<const std::uint8_t> buffer) override;
std::size_t tell() override; std::size_t tell() override;
StreamError seek(std::intptr_t pos, SeekMode seekMode = SeekMode::ABSOLUTE) 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."); 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()); 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) { if (error != StreamError::SUCCESS) {
return error; return error;
} }
@ -118,12 +118,12 @@ StreamError Stream::getTotalLength(std::size_t& outLength)
return StreamError::NOT_SUPPORTED; return StreamError::NOT_SUPPORTED;
} }
const std::size_t origPos = tell(); 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; return error;
} }
outLength = tell(); 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; return error;
} }
@ -134,12 +134,12 @@ StreamError Stream::readRest(TypelessBuffer& outBuffer)
{ {
// first try to allocate everything at once // first try to allocate everything at once
std::size_t length = 0; 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()?"); MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
length -= tell(); length -= tell();
outBuffer.resize(length); 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; return error;
} }
@ -153,13 +153,13 @@ StreamError Stream::readRest(TypelessBuffer& outBuffer)
while (!isAtEnd()) while (!isAtEnd())
{ {
std::size_t bytesRead = 0; 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; return error;
} }
outBuffer.resize(outBuffer.byteSize() + bytesRead); 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)); std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
} }
return StreamError::SUCCESS; return StreamError::SUCCESS;
@ -213,14 +213,14 @@ StreamError Stream::readLine(std::string& outString)
{ {
// read into the buffer // read into the buffer
std::size_t bytesRead = 0; 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; return error;
} }
// try to find a \n // try to find a \n
auto begin = buffer.begin(); auto begin = buffer.begin(); // NOLINT(readability-qualified-auto)
auto end = buffer.begin() + bytesRead; auto end = buffer.begin() + bytesRead; // NOLINT(readability-qualified-auto)
auto newline = std::find(begin, end, '\n'); auto newline = std::find(begin, end, '\n'); // NOLINT(readability-qualified-auto)
if (newline != end) if (newline != end)
{ {
@ -235,7 +235,7 @@ StreamError Stream::readLine(std::string& outString)
} }
// read again, this time to skip // 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; return error;
} }
@ -262,9 +262,9 @@ mijin::Task<StreamError> Stream::c_readLine(std::string& outString)
co_return error; co_return error;
} }
// try to find a \n // try to find a \n
auto begin = buffer.begin(); auto begin = buffer.begin(); // NOLINT(readability-qualified-auto)
auto end = buffer.begin() + bytesRead; auto end = buffer.begin() + bytesRead; // NOLINT(readability-qualified-auto)
auto newline = std::find(begin, end, '\n'); auto newline = std::find(begin, end, '\n'); // NOLINT(readability-qualified-auto)
if (newline != end) if (newline != end)
{ {
@ -359,7 +359,7 @@ StreamError FileStream::readRaw(std::span<std::uint8_t> buffer, const ReadOption
} }
if (options.peek) 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; return error;
} }

View File

@ -82,7 +82,7 @@ static std::size_t getOrCreateStringID(std::string_view string)
// 3. copy global state and check local again // 3. copy global state and check local again
{ {
std::shared_lock lock(getGlobalNamesMutex()); const std::shared_lock lock(getGlobalNamesMutex());
copyNamesToLocal(); copyNamesToLocal();
} }
@ -96,7 +96,7 @@ static std::size_t getOrCreateStringID(std::string_view string)
} }
// 4. insert a new ID // 4. insert a new ID
std::unique_lock lock(getGlobalNamesMutex()); const std::unique_lock lock(getGlobalNamesMutex());
const std::size_t idx = getGlobalNames().size(); const std::size_t idx = getGlobalNames().size();
getGlobalNames().emplace_back(string); getGlobalNames().emplace_back(string);
copyNamesToLocal(); copyNamesToLocal();

View File

@ -4,6 +4,7 @@
#include <filesystem> #include <filesystem>
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX #if MIJIN_TARGET_OS == MIJIN_OS_LINUX
#include <mutex>
#include <dlfcn.h> #include <dlfcn.h>
#include <pthread.h> #include <pthread.h>
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS #elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
@ -31,6 +32,13 @@ namespace mijin
// internal variables // internal variables
// //
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
namespace
{
std::mutex gDlErrorMutex; // dlerror may not be thread-safe
}
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX
// //
// internal functions // internal functions
// //
@ -42,6 +50,7 @@ namespace mijin
Result<LibraryHandle> openSharedLibrary(std::string_view libraryFile) noexcept Result<LibraryHandle> openSharedLibrary(std::string_view libraryFile) noexcept
{ {
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX #if MIJIN_TARGET_OS == MIJIN_OS_LINUX
const std::unique_lock dlErrorLock(gDlErrorMutex);
dlerror(); dlerror();
const fs::path libraryPath = fs::absolute(libraryFile); const fs::path libraryPath = fs::absolute(libraryFile);

View File

@ -44,7 +44,7 @@ std::vector<FileInfo> OSFileSystemAdapter::listFiles(const fs::path& folder)
{ {
std::vector<FileInfo> entries; std::vector<FileInfo> entries;
std::error_code err; std::error_code err;
fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err); const fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err);
if (err) { if (err) {
return {}; // TODO: propagate? return {}; // TODO: propagate?
} }

View File

@ -107,7 +107,7 @@ StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mo
{ {
for (auto& adapter : adapters_) for (auto& adapter : adapters_)
{ {
FileInfo fileInfo = adapter->getFileInfo(path); const FileInfo fileInfo = adapter->getFileInfo(path);
if (fileInfo.exists) { if (fileInfo.exists) {
return adapter->open(path, mode, outStream); return adapter->open(path, mode, outStream);
} }