From c59bfc20f7203c1255fce0683dbab3a618bbf62c Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 27 Jul 2022 00:10:11 +0200 Subject: [PATCH] Manager+Util: Switch to custom assert macro --- src/config.cpp | 4 ++-- src/dotfile.cpp | 4 ++-- src/util/file.cpp | 8 ++++---- src/util/format/parser.cpp | 22 ++++++++-------------- src/util/genericlexer.cpp | 3 ++- src/util/genericlexer.h | 1 - src/util/json/fromjson.h | 18 +++++++++--------- src/util/json/parser.cpp | 5 +++-- src/util/json/value.cpp | 24 ++++++++++++------------ 9 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 6f7643e..de99f88 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: MIT */ -#include // assert #include // raise #include // fprintf #include // current_path, recursive_directory @@ -14,6 +13,7 @@ #include "config.h" #include "util/json/json.h" +#include "util/meta/assert.h" Config::Config(s) : m_workingDirectory(std::filesystem::current_path()) @@ -82,7 +82,7 @@ void toJson(Util::Json& json, const Settings& settings) void fromJson(const Util::Json& json, Settings& settings) { - assert(json.type() == Util::Json::Type::Object); + VERIFY(json.type() == Util::Json::Type::Object); if (json.exists("ignorePatterns")) { json.at("ignorePatterns").getTo(settings.ignorePatterns); diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 9c0ba8f..2bff386 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: MIT */ -#include // assert #include // tolower #include // size_t #include // fprintf, printf, stderr @@ -20,6 +19,7 @@ #include "dotfile.h" #include "machine.h" #include "util/file.h" +#include "util/meta/assert.h" Dotfile::Dotfile(s) { @@ -107,7 +107,7 @@ void Dotfile::push(const std::vector& targets) bool Dotfile::match(const std::string& path, const std::vector& patterns) { - assert(path.front() == '/'); + VERIFY(path.front() == '/', "path is not absolute: '{}'", path); // Cut off working directory size_t cutFrom = path.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0; diff --git a/src/util/file.cpp b/src/util/file.cpp index ecaa8cd..f7f5d82 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: MIT */ -#include // assert #include // int32_t #include #include // ifstream, ios, ofstream @@ -12,6 +11,7 @@ #include #include "util/file.h" +#include "util/meta/assert.h" namespace Util { @@ -20,13 +20,13 @@ File::File(const std::string& path) { // Create input stream object and open file std::ifstream file(path, std::ios::in); - assert(file.is_open()); + VERIFY(file.is_open(), "failed to open file: '{}'", path); // Get length of the file file.seekg(0, std::ios::end); int32_t size = file.tellg(); file.seekg(0, std::ios::beg); - assert(size != -1); + VERIFY(size != -1, "failed to read file length: '{}', path"); // Allocate memory filled with zeros auto buffer = std::make_unique(size); @@ -78,7 +78,7 @@ File& File::flush() { // Create output stream object and open file std::ofstream file(m_path, std::ios::out | std::ios::trunc); - assert(file.is_open()); + VERIFY(file.is_open(), "failed to open file: '{}'", m_path); // Write data to disk file.write(m_data.c_str(), m_data.size()); diff --git a/src/util/format/parser.cpp b/src/util/format/parser.cpp index ed4db1c..5ed2f28 100644 --- a/src/util/format/parser.cpp +++ b/src/util/format/parser.cpp @@ -5,13 +5,13 @@ */ #include // replace -#include // assert #include // size_t #include #include #include "util/format/builder.h" #include "util/format/parser.h" +#include "util/meta/assert.h" namespace Util::Format { @@ -32,8 +32,7 @@ void Parser::checkFormatParameterConsistency() { size_t length = m_input.length(); - // Format string does not reference all passed parameters - assert(length >= m_parameterCount * 2); + VERIFY(length >= m_parameterCount * 2, "format string does not reference all parameters"); size_t braceOpen = 0; size_t braceClose = 0; @@ -54,8 +53,7 @@ void Parser::checkFormatParameterConsistency() if (peek0 == '{') { braceOpen++; - // Only empty references {} or specified references {:} are valid - assert(peek1 == '}' || peek1 == ':'); + VERIFY(peek1 == '}' || peek1 == ':', "invalid parameter reference"); } if (peek0 == '}') { braceClose++; @@ -65,17 +63,13 @@ void Parser::checkFormatParameterConsistency() } m_index = 0; - // Extra open braces in format string - assert(!(braceOpen < braceClose)); + VERIFY(!(braceOpen < braceClose), "extra open braces in format string"); - // Extra closing braces in format string - assert(!(braceOpen > braceClose)); + VERIFY(!(braceOpen > braceClose), "extra closing braces in format string"); - // Format string does not reference all passed parameters - assert(!(braceOpen < m_parameterCount)); + VERIFY(!(braceOpen < m_parameterCount), "format string does not reference all passed parameters"); - // Format string references nonexistent parameter - assert(!(braceOpen > m_parameterCount)); + VERIFY(!(braceOpen > m_parameterCount), "format string references nonexistent parameter"); } std::string_view Parser::consumeLiteral() @@ -114,7 +108,7 @@ bool Parser::consumeSpecifier(std::string_view& specifier) } if (!consumeSpecific(':')) { - assert(consumeSpecific('}')); + VERIFY(consumeSpecific('}'), "invalid parameter reference"); specifier = ""; } else { diff --git a/src/util/genericlexer.cpp b/src/util/genericlexer.cpp index a360a12..0945302 100644 --- a/src/util/genericlexer.cpp +++ b/src/util/genericlexer.cpp @@ -7,6 +7,7 @@ #include // max, min #include "util/genericlexer.h" +#include "util/meta/assert.h" namespace Util { @@ -53,7 +54,7 @@ void GenericLexer::retreat(size_t count) char GenericLexer::consume() { - assert(!isEOF()); + VERIFY(!isEOF()); return m_input[m_index++]; } diff --git a/src/util/genericlexer.h b/src/util/genericlexer.h index f178674..ae73035 100644 --- a/src/util/genericlexer.h +++ b/src/util/genericlexer.h @@ -6,7 +6,6 @@ #pragma once -#include // assert #include // size_t #include diff --git a/src/util/json/fromjson.h b/src/util/json/fromjson.h index f2c7a59..b18c2e8 100644 --- a/src/util/json/fromjson.h +++ b/src/util/json/fromjson.h @@ -7,7 +7,6 @@ #pragma once #include // transform -#include // assert #include // nullptr_t #include #include @@ -17,6 +16,7 @@ #include "util/json/array.h" #include "util/json/object.h" +#include "util/meta/assert.h" #include "util/meta/odr.h" namespace Util::JSON { @@ -33,42 +33,42 @@ void fromJson(const Json& json, Json& value) template void fromJson(const Json& json, std::nullptr_t& null) { - assert(json.type() == Json::Type::Null); + VERIFY(json.type() == Json::Type::Null); null = nullptr; } template void fromJson(const Json& json, bool& boolean) { - assert(json.type() == Json::Type::Bool); + VERIFY(json.type() == Json::Type::Bool); boolean = json.asBool(); } template void fromJson(const Json& json, int& number) { - assert(json.type() == Json::Type::Number); + VERIFY(json.type() == Json::Type::Number); number = (int)json.asDouble(); } template void fromJson(const Json& json, double& number) { - assert(json.type() == Json::Type::Number); + VERIFY(json.type() == Json::Type::Number); number = json.asDouble(); } template void fromJson(const Json& json, std::string& string) { - assert(json.type() == Json::Type::String); + VERIFY(json.type() == Json::Type::String); string = json.asString(); } template void fromJson(const Json& json, std::vector& array) { - assert(json.type() == Json::Type::Array); + VERIFY(json.type() == Json::Type::Array); array.resize(json.size()); std::transform( json.asArray().elements().begin(), @@ -82,7 +82,7 @@ void fromJson(const Json& json, std::vector& array) template void fromJson(const Json& json, std::map& object) { - assert(json.type() == Json::Type::Object); + VERIFY(json.type() == Json::Type::Object); object.clear(); for (const auto& [name, value] : json.asObject().members()) { object.emplace(name, value.template get()); @@ -92,7 +92,7 @@ void fromJson(const Json& json, std::map& object) template void fromJson(const Json& json, std::unordered_map& object) { - assert(json.type() == Json::Type::Object); + VERIFY(json.type() == Json::Type::Object); object.clear(); for (const auto& [name, value] : json.asObject().members()) { object.emplace(name, value.template get()); diff --git a/src/util/json/parser.cpp b/src/util/json/parser.cpp index 5d703b5..cb7b655 100644 --- a/src/util/json/parser.cpp +++ b/src/util/json/parser.cpp @@ -17,6 +17,7 @@ #include "util/json/object.h" #include "util/json/parser.h" #include "util/json/value.h" +#include "util/meta/assert.h" namespace Util::JSON { @@ -88,13 +89,13 @@ bool Parser::isEOF() Token Parser::peek() { - assert(!isEOF()); + VERIFY(!isEOF()); return (*m_tokens)[m_index]; } Token Parser::consume() { - assert(!isEOF()); + VERIFY(!isEOF()); return (*m_tokens)[m_index++]; } diff --git a/src/util/json/value.cpp b/src/util/json/value.cpp index 2182983..5e19a92 100644 --- a/src/util/json/value.cpp +++ b/src/util/json/value.cpp @@ -5,7 +5,6 @@ */ #include // all_of -#include // assert #include // uint32_t #include // >> #include // istream, ostream @@ -18,6 +17,7 @@ #include "util/json/object.h" #include "util/json/serializer.h" #include "util/json/value.h" +#include "util/meta/assert.h" namespace Util::JSON { @@ -180,7 +180,7 @@ void Value::emplace_back(Value value) m_value.array = new Array; } - assert(m_type == Type::Array); + VERIFY(m_type == Type::Array); m_value.array->emplace_back(value); } @@ -192,7 +192,7 @@ void Value::emplace(const std::string& key, Value value) m_value.object = new Object; } - assert(m_type == Type::Object); + VERIFY(m_type == Type::Object); m_value.object->emplace(key, value); } @@ -203,7 +203,7 @@ bool Value::exists(size_t index) const bool Value::exists(const std::string& key) const { - assert(m_type == Type::Object); + VERIFY(m_type == Type::Object); return m_value.object->members().find(key) != m_value.object->members().end(); } @@ -217,7 +217,7 @@ Value& Value::operator[](size_t index) m_value.array = new Array; } - assert(m_type == Type::Array); + VERIFY(m_type == Type::Array); return (*m_value.array)[index]; } @@ -229,43 +229,43 @@ Value& Value::operator[](const std::string& key) m_value.object = new Object; } - assert(m_type == Type::Object); + VERIFY(m_type == Type::Object); return (*m_value.object)[key]; } const Value& Value::operator[](size_t index) const { - assert(m_type == Type::Array); + VERIFY(m_type == Type::Array); return (*m_value.array)[index]; } const Value& Value::operator[](const std::string& key) const { - assert(m_type == Type::Object); + VERIFY(m_type == Type::Object); return (*m_value.object)[key]; } Value& Value::at(size_t index) { - assert(m_type == Type::Array); + VERIFY(m_type == Type::Array); return m_value.array->at(index); } Value& Value::at(const std::string& key) { - assert(m_type == Type::Object); + VERIFY(m_type == Type::Object); return m_value.object->at(key); } const Value& Value::at(size_t index) const { - assert(m_type == Type::Array); + VERIFY(m_type == Type::Array); return m_value.array->at(index); } const Value& Value::at(const std::string& key) const { - assert(m_type == Type::Object); + VERIFY(m_type == Type::Object); return m_value.object->at(key); }