From ef33e8920745801c11b62edf798a4eabcaed9138 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 2 May 2024 20:43:52 -0700 Subject: [PATCH] Clean up some clang-tidy warnings in the router --- router/alc.cpp | 81 ++++++++++++++++++++++++++++--------------------------- router/router.cpp | 54 ++++++++++++++++++------------------- router/router.h | 40 ++++++++++++++------------- 3 files changed, 90 insertions(+), 85 deletions(-) diff --git a/router/alc.cpp b/router/alc.cpp index 227a9acc..7e40f4d2 100644 --- a/router/alc.cpp +++ b/router/alc.cpp @@ -16,12 +16,13 @@ #include "AL/alc.h" #include "almalloc.h" -#include "alstring.h" #include "router.h" namespace { +using namespace std::string_view_literals; + struct FuncExportEntry { const char *funcName; void *address; @@ -277,15 +278,18 @@ const std::array alcEnumerations{ }; #undef DECL -constexpr ALCchar alcNoError[] = "No Error"; -constexpr ALCchar alcErrInvalidDevice[] = "Invalid Device"; -constexpr ALCchar alcErrInvalidContext[] = "Invalid Context"; -constexpr ALCchar alcErrInvalidEnum[] = "Invalid Enum"; -constexpr ALCchar alcErrInvalidValue[] = "Invalid Value"; -constexpr ALCchar alcErrOutOfMemory[] = "Out of Memory"; -constexpr ALCchar alcExtensionList[] = - "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE " - "ALC_EXT_thread_local_context"; +[[nodiscard]] constexpr auto GetNoErrorString() noexcept { return "No Error"; } +[[nodiscard]] constexpr auto GetInvalidDeviceString() noexcept { return "Invalid Device"; } +[[nodiscard]] constexpr auto GetInvalidContextString() noexcept { return "Invalid Context"; } +[[nodiscard]] constexpr auto GetInvalidEnumString() noexcept { return "Invalid Enum"; } +[[nodiscard]] constexpr auto GetInvalidValueString() noexcept { return "Invalid Value"; } +[[nodiscard]] constexpr auto GetOutOfMemoryString() noexcept { return "Out of Memory"; } + +[[nodiscard]] constexpr auto GetExtensionList() noexcept -> std::string_view +{ + return "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE " + "ALC_EXT_thread_local_context"sv; +} constexpr ALCint alcMajorVersion = 1; constexpr ALCint alcMinorVersion = 1; @@ -318,7 +322,8 @@ struct EnumeratedList { } void AppendDeviceList(const ALCchar *names, ALint idx); - ALint GetDriverIndexForName(const std::string_view name) const; + [[nodiscard]] + auto GetDriverIndexForName(const std::string_view name) const -> ALint; }; EnumeratedList DevicesList; EnumeratedList AllDevicesList; @@ -329,24 +334,24 @@ void EnumeratedList::AppendDeviceList(const ALCchar* names, ALint idx) const ALCchar *name_end = names; if(!name_end) return; - ALCsizei count = 0; + size_t count{0}; while(*name_end) { TRACE("Enumerated \"%s\", driver %d\n", name_end, idx); ++count; - name_end += strlen(name_end)+1; + name_end += strlen(name_end)+1; /* NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) */ } if(names == name_end) return; - Names.reserve(Names.size() + (name_end - names) + 1); + Names.reserve(Names.size() + static_cast(name_end - names) + 1); Names.insert(Names.cend(), names, name_end); Indicies.reserve(Indicies.size() + count); Indicies.insert(Indicies.cend(), count, idx); } -ALint EnumeratedList::GetDriverIndexForName(const std::string_view name) const +auto EnumeratedList::GetDriverIndexForName(const std::string_view name) const -> ALint { auto devnames = Names.cbegin(); auto index = Indicies.cbegin(); @@ -355,7 +360,7 @@ ALint EnumeratedList::GetDriverIndexForName(const std::string_view name) const { if(name == al::to_address(devnames)) return *index; - devnames += strlen(al::to_address(devnames))+1; + devnames += ptrdiff_t(strlen(al::to_address(devnames))+1); ++index; } return -1; @@ -423,10 +428,8 @@ ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *devicename) noexcep * quality hint for the wrapper driver. Ignore them since there's no sane * way to map them. */ - if(devicename && (devicename[0] == '\0' || - strcmp(devicename, "DirectSound3D") == 0 || - strcmp(devicename, "DirectSound") == 0 || - strcmp(devicename, "MMSYSTEM") == 0)) + if(devicename && (*devicename == '\0' || devicename == "DirectSound3D"sv + || devicename == "DirectSound"sv || devicename == "MMSYSTEM"sv)) devicename = nullptr; if(devicename) { @@ -629,18 +632,16 @@ ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const A return ALC_FALSE; } - const size_t len{strlen(extname)}; - const char *ptr{alcExtensionList}; - while(ptr && *ptr) + const auto tofind = std::string_view{extname}; + const auto extlist = GetExtensionList(); + auto matchpos = extlist.find(tofind); + while(matchpos != std::string_view::npos) { - if(al::strncasecmp(ptr, extname, len) == 0 && (ptr[len] == '\0' || isspace(ptr[len]))) + const auto endpos = matchpos + tofind.size(); + if((matchpos == 0 || std::isspace(extlist[matchpos-1])) + && (endpos == extlist.size() || std::isspace(extlist[endpos]))) return ALC_TRUE; - if((ptr=strchr(ptr, ' ')) != nullptr) - { - do { - ++ptr; - } while(isspace(*ptr)); - } + matchpos = extlist.find(tofind, matchpos+1); } return ALC_FALSE; } @@ -694,13 +695,13 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *device, ALCenum para switch(param) { - case ALC_NO_ERROR: return alcNoError; - case ALC_INVALID_ENUM: return alcErrInvalidEnum; - case ALC_INVALID_VALUE: return alcErrInvalidValue; - case ALC_INVALID_DEVICE: return alcErrInvalidDevice; - case ALC_INVALID_CONTEXT: return alcErrInvalidContext; - case ALC_OUT_OF_MEMORY: return alcErrOutOfMemory; - case ALC_EXTENSIONS: return alcExtensionList; + case ALC_NO_ERROR: return GetNoErrorString(); + case ALC_INVALID_ENUM: return GetInvalidEnumString(); + case ALC_INVALID_VALUE: return GetInvalidValueString(); + case ALC_INVALID_DEVICE: return GetInvalidDeviceString(); + case ALC_INVALID_CONTEXT: return GetInvalidContextString(); + case ALC_OUT_OF_MEMORY: return GetOutOfMemoryString(); + case ALC_EXTENSIONS: return GetExtensionList().data(); case ALC_DEVICE_SPECIFIER: { @@ -829,14 +830,14 @@ ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsi case ALC_MAJOR_VERSION: if(size >= 1) { - values[0] = alcMajorVersion; + *values = alcMajorVersion; return; } /*fall-through*/ case ALC_MINOR_VERSION: if(size >= 1) { - values[0] = alcMinorVersion; + *values = alcMinorVersion; return; } LastError.store(ALC_INVALID_VALUE); @@ -866,7 +867,7 @@ ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *devicename, ALCdevice *device{nullptr}; ALint idx{0}; - if(devicename && devicename[0] == '\0') + if(devicename && *devicename == '\0') devicename = nullptr; if(devicename) { diff --git a/router/router.cpp b/router/router.cpp index 036a1692..56239afd 100644 --- a/router/router.cpp +++ b/router/router.cpp @@ -4,6 +4,7 @@ #include "router.h" #include +#include #include #include #include @@ -14,15 +15,15 @@ #include "AL/alc.h" #include "AL/al.h" -#include "almalloc.h" #include "alstring.h" +#include "opthelpers.h" #include "strutils.h" #include "version.h" -enum LogLevel LogLevel = LogLevel_Error; -FILE *LogFile; +eLogLevel LogLevel{eLogLevel::Error}; +gsl::owner LogFile; namespace { @@ -170,7 +171,7 @@ void AddModule(HMODULE module, const std::wstring_view name) LOAD_PROC(alDistanceModel); if(!err) { - ALCint alc_ver[2] = { 0, 0 }; + std::array alc_ver{0, 0}; newdrv.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alc_ver[0]); newdrv.alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alc_ver[1]); if(newdrv.alcGetError(nullptr) == ALC_NO_ERROR) @@ -240,22 +241,21 @@ void SearchDrivers(const std::wstring_view path) WIN32_FIND_DATAW fdata{}; HANDLE srchHdl{FindFirstFileW(srchPath.c_str(), &fdata)}; - if(srchHdl != INVALID_HANDLE_VALUE) - { - do { - srchPath = path; - srchPath += L"\\"; - srchPath += fdata.cFileName; - TRACE("Found %ls\n", srchPath.c_str()); - - HMODULE mod{LoadLibraryW(srchPath.c_str())}; - if(!mod) - WARN("Could not load %ls\n", srchPath.c_str()); - else - AddModule(mod, fdata.cFileName); - } while(FindNextFileW(srchHdl, &fdata)); - FindClose(srchHdl); - } + if(srchHdl == INVALID_HANDLE_VALUE) return; + + do { + srchPath = path; + srchPath += L"\\"; + srchPath += std::data(fdata.cFileName); + TRACE("Found %ls\n", srchPath.c_str()); + + HMODULE mod{LoadLibraryW(srchPath.c_str())}; + if(!mod) + WARN("Could not load %ls\n", srchPath.c_str()); + else + AddModule(mod, std::data(fdata.cFileName)); + } while(FindNextFileW(srchHdl, &fdata)); + FindClose(srchHdl); } bool GetLoadedModuleDirectory(const WCHAR *name, std::wstring *moddir) @@ -265,7 +265,7 @@ bool GetLoadedModuleDirectory(const WCHAR *name, std::wstring *moddir) if(name) { module = GetModuleHandleW(name); - if(!module) return 0; + if(!module) return false; } moddir->assign(256, '\0'); @@ -384,10 +384,9 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*) switch(reason) { case DLL_PROCESS_ATTACH: - LogFile = stderr; if(auto logfname = al::getenv("ALROUTER_LOGFILE")) { - FILE *f = fopen(logfname->c_str(), "w"); + gsl::owner f{fopen(logfname->c_str(), "w")}; if(f == nullptr) ERR("Could not open log file: %s\n", logfname->c_str()); else @@ -396,13 +395,14 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*) if(auto loglev = al::getenv("ALROUTER_LOGLEVEL")) { char *end = nullptr; - long l = strtol(loglev->c_str(), &end, 0); + long l{strtol(loglev->c_str(), &end, 0)}; if(!end || *end != '\0') ERR("Invalid log level value: %s\n", loglev->c_str()); - else if(l < LogLevel_None || l > LogLevel_Trace) + else if(l < al::to_underlying(eLogLevel::None) + || l > al::to_underlying(eLogLevel::Trace)) ERR("Log level out of range: %s\n", loglev->c_str()); else - LogLevel = static_cast(l); + LogLevel = static_cast(l); } TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH); LoadDriverList(); @@ -417,7 +417,7 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*) case DLL_PROCESS_DETACH: DriverList.clear(); - if(LogFile && LogFile != stderr) + if(LogFile) fclose(LogFile); LogFile = nullptr; diff --git a/router/router.h b/router/router.h index bae0cb0e..24ee76b1 100644 --- a/router/router.h +++ b/router/router.h @@ -5,9 +5,8 @@ #include #include -#include - #include +#include #include #include #include @@ -18,6 +17,8 @@ #include "AL/al.h" #include "AL/alext.h" +#include "almalloc.h" + #define MAKE_ALC_VER(major, minor) (((major)<<8) | (minor)) @@ -182,34 +183,37 @@ inline DriverIface *GetThreadDriver() noexcept { return ThreadCtxDriver; } inline void SetThreadDriver(DriverIface *driver) noexcept { ThreadCtxDriver = driver; } -enum LogLevel { - LogLevel_None = 0, - LogLevel_Error = 1, - LogLevel_Warn = 2, - LogLevel_Trace = 3, +enum class eLogLevel { + None = 0, + Error = 1, + Warn = 2, + Trace = 3, }; -extern enum LogLevel LogLevel; -extern FILE *LogFile; +extern eLogLevel LogLevel; +extern gsl::owner LogFile; #define TRACE(...) do { \ - if(LogLevel >= LogLevel_Trace) \ + if(LogLevel >= eLogLevel::Trace) \ { \ - fprintf(LogFile, "AL Router (II): " __VA_ARGS__); \ - fflush(LogFile); \ + std::FILE *file{LogFile ? LogFile : stderr}; \ + fprintf(file, "AL Router (II): " __VA_ARGS__); \ + fflush(file); \ } \ } while(0) #define WARN(...) do { \ - if(LogLevel >= LogLevel_Warn) \ + if(LogLevel >= eLogLevel::Warn) \ { \ - fprintf(LogFile, "AL Router (WW): " __VA_ARGS__); \ - fflush(LogFile); \ + std::FILE *file{LogFile ? LogFile : stderr}; \ + fprintf(file, "AL Router (WW): " __VA_ARGS__); \ + fflush(file); \ } \ } while(0) #define ERR(...) do { \ - if(LogLevel >= LogLevel_Error) \ + if(LogLevel >= eLogLevel::Error) \ { \ - fprintf(LogFile, "AL Router (EE): " __VA_ARGS__); \ - fflush(LogFile); \ + std::FILE *file{LogFile ? LogFile : stderr}; \ + fprintf(file, "AL Router (EE): " __VA_ARGS__); \ + fflush(file); \ } \ } while(0) -- 2.11.4.GIT