core: clang-tidy and comp fixes (#63)

This commit is contained in:
Honkazel 2025-02-08 05:44:54 +05:00 committed by GitHub
parent f41271d35c
commit 7a59f2de3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 170 additions and 76 deletions

101
.clang-tidy Normal file
View file

@ -0,0 +1,101 @@
WarningsAsErrors: '*'
HeaderFilterRegex: '.*\.hpp'
FormatStyle: file
Checks: >
-*,
bugprone-*,
-bugprone-easily-swappable-parameters,
-bugprone-forward-declararion-namespace,
-bugprone-forward-declararion-namespace,
-bugprone-macro-parentheses,
-bugprone-narrowing-conversions,
-bugprone-branch-clone,
-bugprone-assignment-in-if-condition,
concurrency-*,
-concurrency-mt-unsafe,
cppcoreguidelines-*,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-avoid-goto,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-special-member-functions,
-cppcoreguidelines-explicit-virtual-functions,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-narrowing-conversions,
-cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-pro-type-member-init,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-macro-to-enum,
-cppcoreguidelines-init-variables,
-cppcoreguidelines-pro-type-cstyle-cast,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-pro-type-reinterpret-cast,
google-global-names-in-headers,
-google-readability-casting,
google-runtime-operator,
misc-*,
-misc-unused-parameters,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-misc-include-cleaner,
-misc-use-anonymous-namespace,
-misc-const-correctness,
modernize-*,
-modernize-return-braced-init-list,
-modernize-use-trailing-return-type,
-modernize-use-using,
-modernize-use-override,
-modernize-avoid-c-arrays,
-modernize-macro-to-enum,
-modernize-loop-convert,
-modernize-use-nodiscard,
-modernize-pass-by-value,
-modernize-use-auto,
performance-*,
-performance-avoid-endl,
-performance-unnecessary-value-param,
portability-std-allocator-const,
readability-*,
-readability-function-cognitive-complexity,
-readability-function-size,
-readability-identifier-length,
-readability-magic-numbers,
-readability-uppercase-literal-suffix,
-readability-braces-around-statements,
-readability-redundant-access-specifiers,
-readability-else-after-return,
-readability-container-data-pointer,
-readability-implicit-bool-conversion,
-readability-avoid-nested-conditional-operator,
-readability-redundant-member-init,
-readability-redundant-string-init,
-readability-avoid-const-params-in-decls,
-readability-named-parameter,
-readability-convert-member-functions-to-static,
-readability-qualified-auto,
-readability-make-member-function-const,
-readability-isolate-declaration,
-readability-inconsistent-declaration-parameter-name,
-clang-diagnostic-error,
CheckOptions:
performance-for-range-copy.WarnOnAllAutoCopies: true
performance-inefficient-string-concatenation.StrictMode: true
readability-braces-around-statements.ShortStatementLines: 0
readability-identifier-naming.ClassCase: CamelCase
readability-identifier-naming.ClassIgnoredRegexp: I.*
readability-identifier-naming.ClassPrefix: C # We can't use regex here?!?!?!?
readability-identifier-naming.EnumCase: CamelCase
readability-identifier-naming.EnumPrefix: e
readability-identifier-naming.EnumConstantCase: UPPER_CASE
readability-identifier-naming.FunctionCase: camelBack
readability-identifier-naming.NamespaceCase: CamelCase
readability-identifier-naming.NamespacePrefix: N
readability-identifier-naming.StructPrefix: S
readability-identifier-naming.StructCase: CamelCase

View file

@ -28,6 +28,13 @@ endif()
add_compile_definitions(HYPRLANG_INTERNAL) add_compile_definitions(HYPRLANG_INTERNAL)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
add_compile_options(
-Wall
-Wextra
-Wpedantic
-Wno-unused-parameter
-Wno-missing-field-initializers)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(deps REQUIRED IMPORTED_TARGET hyprutils>=0.1.1) pkg_check_modules(deps REQUIRED IMPORTED_TARGET hyprutils>=0.1.1)

View file

@ -1,6 +1,6 @@
#include "public.hpp" #include "public.hpp"
#include "config.hpp" #include "config.hpp"
#include <string.h> #include <cstring>
using namespace Hyprlang; using namespace Hyprlang;
@ -30,38 +30,28 @@ CConfigValue::~CConfigValue() {
} }
} }
CConfigValue::CConfigValue(const int64_t value) { CConfigValue::CConfigValue(const int64_t value) : m_eType(CONFIGDATATYPE_INT), m_pData(new int64_t) {
m_pData = new int64_t;
*reinterpret_cast<int64_t*>(m_pData) = value; *reinterpret_cast<int64_t*>(m_pData) = value;
m_eType = CONFIGDATATYPE_INT;
} }
CConfigValue::CConfigValue(const float value) { CConfigValue::CConfigValue(const float value) : m_eType(CONFIGDATATYPE_FLOAT), m_pData(new float) {
m_pData = new float;
*reinterpret_cast<float*>(m_pData) = value; *reinterpret_cast<float*>(m_pData) = value;
m_eType = CONFIGDATATYPE_FLOAT;
} }
CConfigValue::CConfigValue(const SVector2D value) { CConfigValue::CConfigValue(const SVector2D value) : m_eType(CONFIGDATATYPE_VEC2), m_pData(new SVector2D) {
m_pData = new SVector2D;
*reinterpret_cast<SVector2D*>(m_pData) = value; *reinterpret_cast<SVector2D*>(m_pData) = value;
m_eType = CONFIGDATATYPE_VEC2;
} }
CConfigValue::CConfigValue(const char* value) { CConfigValue::CConfigValue(const char* value) : m_eType(CONFIGDATATYPE_STR), m_pData(new char[strlen(value) + 1]) {
m_pData = new char[strlen(value) + 1];
strncpy((char*)m_pData, value, strlen(value)); strncpy((char*)m_pData, value, strlen(value));
((char*)m_pData)[strlen(value)] = '\0'; ((char*)m_pData)[strlen(value)] = '\0';
m_eType = CONFIGDATATYPE_STR;
} }
CConfigValue::CConfigValue(CConfigCustomValueType&& value) { CConfigValue::CConfigValue(CConfigCustomValueType&& value) : m_eType(CONFIGDATATYPE_CUSTOM), m_pData(new CConfigCustomValueType(value)) {
m_pData = new CConfigCustomValueType(value); ;
m_eType = CONFIGDATATYPE_CUSTOM;
} }
CConfigValue::CConfigValue(const CConfigValue& other) { CConfigValue::CConfigValue(const CConfigValue& other) : m_eType(other.m_eType) {
m_eType = other.m_eType;
setFrom(&other); setFrom(&other);
} }
@ -77,11 +67,9 @@ void* const* CConfigValue::getDataStaticPtr() const {
return &m_pData; return &m_pData;
} }
CConfigCustomValueType::CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* def) { CConfigCustomValueType::CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* def) :
handler = handler_; handler(handler_), dtor(dtor_), defaultVal(def), lastVal(def) {
dtor = dtor_; ;
defaultVal = def;
lastVal = def;
} }
CConfigCustomValueType::~CConfigCustomValueType() { CConfigCustomValueType::~CConfigCustomValueType() {
@ -216,4 +204,4 @@ void CConfigValue::setFrom(std::any ref) {
throw "bad defaultFrom type"; throw "bad defaultFrom type";
} }
} }
} }

View file

@ -20,17 +20,18 @@ using namespace Hyprutils::String;
#include <crt_externs.h> #include <crt_externs.h>
#define environ (*_NSGetEnviron()) #define environ (*_NSGetEnviron())
#else #else
// NOLINTNEXTLINE
extern "C" char** environ; extern "C" char** environ;
#endif #endif
// defines // defines
inline constexpr const char* ANONYMOUS_KEY = "__hyprlang_internal_anonymous_key"; inline constexpr const char* ANONYMOUS_KEY = "__hyprlang_internal_anonymous_key";
inline constexpr const char* MULTILINE_SPACE_CHARSET = " \t"; inline constexpr const char* MULTILINE_SPACE_CHARSET = " \t";
// //
static size_t seekABIStructSize(const void* begin, size_t startOffset, size_t maxSize) { static size_t seekABIStructSize(const void* begin, size_t startOffset, size_t maxSize) {
for (size_t off = startOffset; off < maxSize; off += 4) { for (size_t off = startOffset; off < maxSize; off += 4) {
if (*(int*)((unsigned char*)begin + off) == int{HYPRLANG_END_MAGIC}) if (*(int*)((unsigned char*)begin + off) == HYPRLANG_END_MAGIC)
return off; return off;
} }
@ -48,7 +49,7 @@ static std::expected<std::string, eGetNextLineFailure> getNextLine(std::istream&
while (line.length() > 0 && line.at(line.length() - 1) == '\\') { while (line.length() > 0 && line.at(line.length() - 1) == '\\') {
const auto lastNonSpace = line.length() < 2 ? -1 : line.find_last_not_of(MULTILINE_SPACE_CHARSET, line.length() - 2); const auto lastNonSpace = line.length() < 2 ? -1 : line.find_last_not_of(MULTILINE_SPACE_CHARSET, line.length() - 2);
line = line.substr(0, lastNonSpace + 1); line = line.substr(0, lastNonSpace + 1);
if (!std::getline(str, nextLine)) if (!std::getline(str, nextLine))
return std::unexpected(GETNEXTLINEFAILURE_BACKSLASH); return std::unexpected(GETNEXTLINEFAILURE_BACKSLASH);
@ -60,13 +61,10 @@ static std::expected<std::string, eGetNextLineFailure> getNextLine(std::istream&
return line; return line;
} }
CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options_) : impl(new CConfigImpl) {
CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options_) {
SConfigOptions options; SConfigOptions options;
std::memcpy(&options, &options_, seekABIStructSize(&options_, 16, sizeof(SConfigOptions))); std::memcpy(&options, &options_, seekABIStructSize(&options_, 16, sizeof(SConfigOptions)));
impl = new CConfigImpl;
if (options.pathIsStream) if (options.pathIsStream)
impl->rawConfigString = path; impl->rawConfigString = path;
else else
@ -85,7 +83,7 @@ CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options_) {
impl->envVariables.push_back({VARIABLE, VALUE}); impl->envVariables.push_back({VARIABLE, VALUE});
} }
std::sort(impl->envVariables.begin(), impl->envVariables.end(), [&](const auto& a, const auto& b) { return a.name.length() > b.name.length(); }); std::ranges::sort(impl->envVariables, [&](const auto& a, const auto& b) { return a.name.length() > b.name.length(); });
impl->configOptions = options; impl->configOptions = options;
} }
@ -99,40 +97,42 @@ void CConfig::addConfigValue(const char* name, const CConfigValue& value) {
throw "Cannot addConfigValue after commence()"; throw "Cannot addConfigValue after commence()";
if ((eDataType)value.m_eType != CONFIGDATATYPE_CUSTOM && (eDataType)value.m_eType != CONFIGDATATYPE_STR) if ((eDataType)value.m_eType != CONFIGDATATYPE_CUSTOM && (eDataType)value.m_eType != CONFIGDATATYPE_STR)
impl->defaultValues.emplace(name, SConfigDefaultValue{value.getValue(), (eDataType)value.m_eType}); impl->defaultValues.emplace(name, SConfigDefaultValue{.data = value.getValue(), .type = (eDataType)value.m_eType});
else if ((eDataType)value.m_eType == CONFIGDATATYPE_STR) else if ((eDataType)value.m_eType == CONFIGDATATYPE_STR)
impl->defaultValues.emplace(name, SConfigDefaultValue{std::string{std::any_cast<const char*>(value.getValue())}, (eDataType)value.m_eType}); impl->defaultValues.emplace(name, SConfigDefaultValue{.data = std::string{std::any_cast<const char*>(value.getValue())}, .type = (eDataType)value.m_eType});
else else
impl->defaultValues.emplace(name, impl->defaultValues.emplace(name,
SConfigDefaultValue{reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->defaultVal, (eDataType)value.m_eType, SConfigDefaultValue{.data = reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->defaultVal,
reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->handler, .type = (eDataType)value.m_eType,
reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->dtor}); .handler = reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->handler,
.dtor = reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->dtor});
} }
void CConfig::addSpecialConfigValue(const char* cat, const char* name, const CConfigValue& value) { void CConfig::addSpecialConfigValue(const char* cat, const char* name, const CConfigValue& value) {
const auto IT = std::find_if(impl->specialCategoryDescriptors.begin(), impl->specialCategoryDescriptors.end(), [&](const auto& other) { return other->name == cat; }); const auto IT = std::ranges::find_if(impl->specialCategoryDescriptors, [&](const auto& other) { return other->name == cat; });
if (IT == impl->specialCategoryDescriptors.end()) if (IT == impl->specialCategoryDescriptors.end())
throw "No such category"; throw "No such category";
if ((eDataType)value.m_eType != CONFIGDATATYPE_CUSTOM && (eDataType)value.m_eType != CONFIGDATATYPE_STR) if ((eDataType)value.m_eType != CONFIGDATATYPE_CUSTOM && (eDataType)value.m_eType != CONFIGDATATYPE_STR)
IT->get()->defaultValues.emplace(name, SConfigDefaultValue{value.getValue(), (eDataType)value.m_eType}); IT->get()->defaultValues.emplace(name, SConfigDefaultValue{.data = value.getValue(), .type = (eDataType)value.m_eType});
else if ((eDataType)value.m_eType == CONFIGDATATYPE_STR) else if ((eDataType)value.m_eType == CONFIGDATATYPE_STR)
IT->get()->defaultValues.emplace(name, SConfigDefaultValue{std::string{std::any_cast<const char*>(value.getValue())}, (eDataType)value.m_eType}); IT->get()->defaultValues.emplace(name, SConfigDefaultValue{.data = std::string{std::any_cast<const char*>(value.getValue())}, .type = (eDataType)value.m_eType});
else else
IT->get()->defaultValues.emplace(name, IT->get()->defaultValues.emplace(name,
SConfigDefaultValue{reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->defaultVal, (eDataType)value.m_eType, SConfigDefaultValue{.data = reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->defaultVal,
reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->handler, .type = (eDataType)value.m_eType,
reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->dtor}); .handler = reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->handler,
.dtor = reinterpret_cast<CConfigCustomValueType*>(value.m_pData)->dtor});
const auto CAT = std::find_if(impl->specialCategories.begin(), impl->specialCategories.end(), [cat, name](const auto& other) { return other->name == cat && other->isStatic; }); const auto CAT = std::ranges::find_if(impl->specialCategories, [cat](const auto& other) { return other->name == cat && other->isStatic; });
if (CAT != impl->specialCategories.end()) if (CAT != impl->specialCategories.end())
CAT->get()->values[name].defaultFrom(IT->get()->defaultValues[name]); CAT->get()->values[name].defaultFrom(IT->get()->defaultValues[name]);
} }
void CConfig::removeSpecialConfigValue(const char* cat, const char* name) { void CConfig::removeSpecialConfigValue(const char* cat, const char* name) {
const auto IT = std::find_if(impl->specialCategoryDescriptors.begin(), impl->specialCategoryDescriptors.end(), [&](const auto& other) { return other->name == cat; }); const auto IT = std::ranges::find_if(impl->specialCategoryDescriptors, [&](const auto& other) { return other->name == cat; });
if (IT == impl->specialCategoryDescriptors.end()) if (IT == impl->specialCategoryDescriptors.end())
throw "No such category"; throw "No such category";
@ -163,9 +163,8 @@ void CConfig::addSpecialCategory(const char* name, SSpecialCategoryOptions optio
} }
// sort longest to shortest // sort longest to shortest
std::sort(impl->specialCategories.begin(), impl->specialCategories.end(), [](const auto& a, const auto& b) -> int { return a->name.length() > b->name.length(); }); std::ranges::sort(impl->specialCategories, [](const auto& a, const auto& b) -> int { return a->name.length() > b->name.length(); });
std::sort(impl->specialCategoryDescriptors.begin(), impl->specialCategoryDescriptors.end(), std::ranges::sort(impl->specialCategoryDescriptors, [](const auto& a, const auto& b) -> int { return a->name.length() > b->name.length(); });
[](const auto& a, const auto& b) -> int { return a->name.length() > b->name.length(); });
} }
void CConfig::removeSpecialCategory(const char* name) { void CConfig::removeSpecialCategory(const char* name) {
@ -220,7 +219,7 @@ static std::expected<int64_t, std::string> configStringToInt(const std::string&
if (!r.has_value() || !g.has_value() || !b.has_value()) if (!r.has_value() || !g.has_value() || !b.has_value())
return std::unexpected("failed parsing " + VALUEWITHOUTFUNC); return std::unexpected("failed parsing " + VALUEWITHOUTFUNC);
return a * (Hyprlang::INT)0x1000000 + r.value() * (Hyprlang::INT)0x10000 + g.value() * (Hyprlang::INT)0x100 + b.value(); return (a * (Hyprlang::INT)0x1000000) + (r.value() * (Hyprlang::INT)0x10000) + (g.value() * (Hyprlang::INT)0x100) + b.value();
} else if (VALUEWITHOUTFUNC.length() == 8) { } else if (VALUEWITHOUTFUNC.length() == 8) {
const auto RGBA = parseHex(VALUEWITHOUTFUNC); const auto RGBA = parseHex(VALUEWITHOUTFUNC);
@ -228,7 +227,7 @@ static std::expected<int64_t, std::string> configStringToInt(const std::string&
return RGBA; return RGBA;
// now we need to RGBA -> ARGB. The config holds ARGB only. // now we need to RGBA -> ARGB. The config holds ARGB only.
return (RGBA.value() >> 8) + 0x1000000 * (RGBA.value() & 0xFF); return (RGBA.value() >> 8) + (0x1000000 * (RGBA.value() & 0xFF));
} }
return std::unexpected("rgba() expects length of 8 characters (4 bytes) or 4 comma separated values"); return std::unexpected("rgba() expects length of 8 characters (4 bytes) or 4 comma separated values");
@ -249,7 +248,7 @@ static std::expected<int64_t, std::string> configStringToInt(const std::string&
if (!r.has_value() || !g.has_value() || !b.has_value()) if (!r.has_value() || !g.has_value() || !b.has_value())
return std::unexpected("failed parsing " + VALUEWITHOUTFUNC); return std::unexpected("failed parsing " + VALUEWITHOUTFUNC);
return (Hyprlang::INT)0xFF000000 + r.value() * (Hyprlang::INT)0x10000 + g.value() * (Hyprlang::INT)0x100 + b.value(); return (Hyprlang::INT)0xFF000000 + (r.value() * (Hyprlang::INT)0x10000) + (g.value() * (Hyprlang::INT)0x100) + b.value();
} else if (VALUEWITHOUTFUNC.length() == 6) { } else if (VALUEWITHOUTFUNC.length() == 6) {
const auto RGB = parseHex(VALUEWITHOUTFUNC); const auto RGB = parseHex(VALUEWITHOUTFUNC);
@ -382,8 +381,7 @@ CParseResult CConfig::configSetValueSafe(const std::string& command, const std::
// find suitable key // find suitable key
size_t biggest = 0; size_t biggest = 0;
for (auto& catt : impl->specialCategories) { for (auto& catt : impl->specialCategories) {
if (catt->anonymousID > biggest) biggest = std::max(catt->anonymousID, biggest);
biggest = catt->anonymousID;
} }
biggest++; biggest++;
@ -442,7 +440,7 @@ CParseResult CConfig::configSetValueSafe(const std::string& command, const std::
if (LHS.contains(" ") || RHS.contains(" ")) if (LHS.contains(" ") || RHS.contains(" "))
throw std::runtime_error("too many args"); throw std::runtime_error("too many args");
VALUEIT->second.setFrom(SVector2D{std::stof(LHS), std::stof(RHS)}); VALUEIT->second.setFrom(SVector2D{.x = std::stof(LHS), .y = std::stof(RHS)});
} catch (std::exception& e) { } catch (std::exception& e) {
result.setError(std::format("failed parsing a vec2: {}", e.what())); result.setError(std::format("failed parsing a vec2: {}", e.what()));
return result; return result;
@ -476,14 +474,14 @@ CParseResult CConfig::configSetValueSafe(const std::string& command, const std::
} }
CParseResult CConfig::parseVariable(const std::string& lhs, const std::string& rhs, bool dynamic) { CParseResult CConfig::parseVariable(const std::string& lhs, const std::string& rhs, bool dynamic) {
auto IT = std::find_if(impl->variables.begin(), impl->variables.end(), [&](const auto& v) { return v.name == lhs.substr(1); }); auto IT = std::ranges::find_if(impl->variables, [&](const auto& v) { return v.name == lhs.substr(1); });
if (IT != impl->variables.end()) if (IT != impl->variables.end())
IT->value = rhs; IT->value = rhs;
else { else {
impl->variables.push_back({lhs.substr(1), rhs}); impl->variables.push_back({lhs.substr(1), rhs});
std::sort(impl->variables.begin(), impl->variables.end(), [](const auto& lhs, const auto& rhs) { return lhs.name.length() > rhs.name.length(); }); std::ranges::sort(impl->variables, [](const auto& lhs, const auto& rhs) { return lhs.name.length() > rhs.name.length(); });
IT = std::find_if(impl->variables.begin(), impl->variables.end(), [&](const auto& v) { return v.name == lhs.substr(1); }); IT = std::ranges::find_if(impl->variables, [&](const auto& v) { return v.name == lhs.substr(1); });
} }
if (dynamic) { if (dynamic) {
@ -618,7 +616,7 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) {
size_t idx = 0; size_t idx = 0;
size_t depth = 0; size_t depth = 0;
while ((colon = HANDLERNAME.find(":", idx)) != std::string::npos && impl->categories.size() > depth) { while ((colon = HANDLERNAME.find(':', idx)) != std::string::npos && impl->categories.size() > depth) {
auto actual = HANDLERNAME.substr(idx, colon - idx); auto actual = HANDLERNAME.substr(idx, colon - idx);
if (actual != impl->categories[depth]) if (actual != impl->categories[depth])
@ -720,8 +718,8 @@ CParseResult CConfig::parse() {
CParseResult CConfig::parseRawStream(const std::string& stream) { CParseResult CConfig::parseRawStream(const std::string& stream) {
CParseResult result; CParseResult result;
int rawLineNum = 0; int rawLineNum = 0;
int lineNum = 0; int lineNum = 0;
std::stringstream str(stream); std::stringstream str(stream);
@ -863,7 +861,7 @@ CConfigValue* CConfig::getSpecialConfigValuePtr(const char* category, const char
void CConfig::registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options_) { void CConfig::registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options_) {
SHandlerOptions options; SHandlerOptions options;
std::memcpy(&options, &options_, seekABIStructSize(&options_, 0, sizeof(SHandlerOptions))); std::memcpy(&options, &options_, seekABIStructSize(&options_, 0, sizeof(SHandlerOptions)));
impl->handlers.push_back(SHandler{name, options, func}); impl->handlers.push_back(SHandler{.name = name, .options = options, .func = func});
} }
void CConfig::unregisterHandler(const char* name) { void CConfig::unregisterHandler(const char* name) {

View file

@ -11,7 +11,7 @@ std::string garbage() {
std::string chars; std::string chars;
for (int i = 0; i < len; ++i) { for (int i = 0; i < len; ++i) {
chars += rand() % 254 + 1; chars += std::to_string((rand() % 254) + 1);
} }
return chars; return chars;

28
tests/parse/main.cpp Executable file → Normal file
View file

@ -48,7 +48,7 @@ static Hyprlang::CParseResult handleFlagsTest(const char* COMMAND, const char* V
} }
static Hyprlang::CParseResult handleCategoryKeyword(const char* COMMAND, const char* VALUE) { static Hyprlang::CParseResult handleCategoryKeyword(const char* COMMAND, const char* VALUE) {
categoryKeywordActualValues.push_back(VALUE); categoryKeywordActualValues.emplace_back(VALUE);
return Hyprlang::CParseResult(); return Hyprlang::CParseResult();
} }
@ -108,7 +108,7 @@ int main(int argc, char** argv, char** envp) {
// setup config // setup config
config.addConfigValue("testInt", (Hyprlang::INT)0); config.addConfigValue("testInt", (Hyprlang::INT)0);
config.addConfigValue("testFloat", 0.F); config.addConfigValue("testFloat", 0.F);
config.addConfigValue("testVec", Hyprlang::SVector2D{69, 420}); config.addConfigValue("testVec", Hyprlang::SVector2D{.x = 69, .y = 420});
config.addConfigValue("testString", ""); config.addConfigValue("testString", "");
config.addConfigValue("testStringColon", ""); config.addConfigValue("testStringColon", "");
config.addConfigValue("testEnv", ""); config.addConfigValue("testEnv", "");
@ -131,27 +131,27 @@ int main(int argc, char** argv, char** envp) {
config.addConfigValue("myColors:random", (Hyprlang::INT)0); config.addConfigValue("myColors:random", (Hyprlang::INT)0);
config.addConfigValue("customType", {Hyprlang::CConfigCustomValueType{&handleCustomValueSet, &handleCustomValueDestroy, "def"}}); config.addConfigValue("customType", {Hyprlang::CConfigCustomValueType{&handleCustomValueSet, &handleCustomValueDestroy, "def"}});
config.registerHandler(&handleDoABarrelRoll, "doABarrelRoll", {false}); config.registerHandler(&handleDoABarrelRoll, "doABarrelRoll", {.allowFlags = false});
config.registerHandler(&handleFlagsTest, "flags", {true}); config.registerHandler(&handleFlagsTest, "flags", {.allowFlags = true});
config.registerHandler(&handleSource, "source", {false}); config.registerHandler(&handleSource, "source", {.allowFlags = false});
config.registerHandler(&handleTestIgnoreKeyword, "testIgnoreKeyword", {false}); config.registerHandler(&handleTestIgnoreKeyword, "testIgnoreKeyword", {.allowFlags = false});
config.registerHandler(&handleTestUseKeyword, ":testUseKeyword", {false}); config.registerHandler(&handleTestUseKeyword, ":testUseKeyword", {.allowFlags = false});
config.registerHandler(&handleNoop, "testCategory:testUseKeyword", {false}); config.registerHandler(&handleNoop, "testCategory:testUseKeyword", {.allowFlags = false});
config.registerHandler(&handleCategoryKeyword, "testCategory:categoryKeyword", {false}); config.registerHandler(&handleCategoryKeyword, "testCategory:categoryKeyword", {.allowFlags = false});
config.addSpecialCategory("special", {"key"}); config.addSpecialCategory("special", {.key = "key"});
config.addSpecialConfigValue("special", "value", (Hyprlang::INT)0); config.addSpecialConfigValue("special", "value", (Hyprlang::INT)0);
config.addSpecialCategory("specialAnonymous", {nullptr, false, true}); config.addSpecialCategory("specialAnonymous", {.key = nullptr, .ignoreMissing = false, .anonymousKeyBased = true});
config.addSpecialConfigValue("specialAnonymous", "value", (Hyprlang::INT)0); config.addSpecialConfigValue("specialAnonymous", "value", (Hyprlang::INT)0);
config.addConfigValue("multiline", ""); config.addConfigValue("multiline", "");
config.commence(); config.commence();
config.addSpecialCategory("specialGeneric:one", {nullptr, true}); config.addSpecialCategory("specialGeneric:one", {.key = nullptr, .ignoreMissing = true});
config.addSpecialConfigValue("specialGeneric:one", "value", (Hyprlang::INT)0); config.addSpecialConfigValue("specialGeneric:one", "value", (Hyprlang::INT)0);
config.addSpecialCategory("specialGeneric:two", {nullptr, true}); config.addSpecialCategory("specialGeneric:two", {.key = nullptr, .ignoreMissing = true});
config.addSpecialConfigValue("specialGeneric:two", "value", (Hyprlang::INT)0); config.addSpecialConfigValue("specialGeneric:two", "value", (Hyprlang::INT)0);
const Hyprlang::CConfigValue copyTest = {(Hyprlang::INT)1}; const Hyprlang::CConfigValue copyTest = {(Hyprlang::INT)1};
@ -169,7 +169,7 @@ int main(int argc, char** argv, char** envp) {
std::cout << " → Testing values\n"; std::cout << " → Testing values\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testInt")), 123); EXPECT(std::any_cast<int64_t>(config.getConfigValue("testInt")), 123);
EXPECT(std::any_cast<float>(config.getConfigValue("testFloat")), 123.456f); EXPECT(std::any_cast<float>(config.getConfigValue("testFloat")), 123.456f);
auto EXP = Hyprlang::SVector2D{69, 420}; auto EXP = Hyprlang::SVector2D{.x = 69, .y = 420};
EXPECT(std::any_cast<Hyprlang::SVector2D>(config.getConfigValue("testVec")), EXP); EXPECT(std::any_cast<Hyprlang::SVector2D>(config.getConfigValue("testVec")), EXP);
EXPECT(std::any_cast<const char*>(config.getConfigValue("testString")), std::string{"Hello World! # This is not a comment!"}); EXPECT(std::any_cast<const char*>(config.getConfigValue("testString")), std::string{"Hello World! # This is not a comment!"});
EXPECT(std::any_cast<const char*>(config.getConfigValue("testStringQuotes")), std::string{"\"Hello World!\""}); EXPECT(std::any_cast<const char*>(config.getConfigValue("testStringQuotes")), std::string{"\"Hello World!\""});