Browse Source

Manager+Util: Switch to custom assert macro

master
Riyyi 2 years ago
parent
commit
c59bfc20f7
  1. 4
      src/config.cpp
  2. 4
      src/dotfile.cpp
  3. 8
      src/util/file.cpp
  4. 22
      src/util/format/parser.cpp
  5. 3
      src/util/genericlexer.cpp
  6. 1
      src/util/genericlexer.h
  7. 18
      src/util/json/fromjson.h
  8. 5
      src/util/json/parser.cpp
  9. 24
      src/util/json/value.cpp

4
src/config.cpp

@ -4,7 +4,6 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <cassert> // assert
#include <csignal> // raise #include <csignal> // raise
#include <cstdio> // fprintf #include <cstdio> // fprintf
#include <filesystem> // current_path, recursive_directory #include <filesystem> // current_path, recursive_directory
@ -14,6 +13,7 @@
#include "config.h" #include "config.h"
#include "util/json/json.h" #include "util/json/json.h"
#include "util/meta/assert.h"
Config::Config(s) Config::Config(s)
: m_workingDirectory(std::filesystem::current_path()) : 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) 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")) { if (json.exists("ignorePatterns")) {
json.at("ignorePatterns").getTo(settings.ignorePatterns); json.at("ignorePatterns").getTo(settings.ignorePatterns);

4
src/dotfile.cpp

@ -4,7 +4,6 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <cassert> // assert
#include <cctype> // tolower #include <cctype> // tolower
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdio> // fprintf, printf, stderr #include <cstdio> // fprintf, printf, stderr
@ -20,6 +19,7 @@
#include "dotfile.h" #include "dotfile.h"
#include "machine.h" #include "machine.h"
#include "util/file.h" #include "util/file.h"
#include "util/meta/assert.h"
Dotfile::Dotfile(s) Dotfile::Dotfile(s)
{ {
@ -107,7 +107,7 @@ void Dotfile::push(const std::vector<std::string>& targets)
bool Dotfile::match(const std::string& path, const std::vector<std::string>& patterns) bool Dotfile::match(const std::string& path, const std::vector<std::string>& patterns)
{ {
assert(path.front() == '/'); VERIFY(path.front() == '/', "path is not absolute: '{}'", path);
// Cut off working directory // Cut off working directory
size_t cutFrom = path.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0; size_t cutFrom = path.find(Config::the().workingDirectory()) == 0 ? Config::the().workingDirectorySize() : 0;

8
src/util/file.cpp

@ -4,7 +4,6 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <cassert> // assert
#include <cstdint> // int32_t #include <cstdint> // int32_t
#include <filesystem> #include <filesystem>
#include <fstream> // ifstream, ios, ofstream #include <fstream> // ifstream, ios, ofstream
@ -12,6 +11,7 @@
#include <string> #include <string>
#include "util/file.h" #include "util/file.h"
#include "util/meta/assert.h"
namespace Util { namespace Util {
@ -20,13 +20,13 @@ File::File(const std::string& path)
{ {
// Create input stream object and open file // Create input stream object and open file
std::ifstream file(path, std::ios::in); 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 // Get length of the file
file.seekg(0, std::ios::end); file.seekg(0, std::ios::end);
int32_t size = file.tellg(); int32_t size = file.tellg();
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
assert(size != -1); VERIFY(size != -1, "failed to read file length: '{}', path");
// Allocate memory filled with zeros // Allocate memory filled with zeros
auto buffer = std::make_unique<char[]>(size); auto buffer = std::make_unique<char[]>(size);
@ -78,7 +78,7 @@ File& File::flush()
{ {
// Create output stream object and open file // Create output stream object and open file
std::ofstream file(m_path, std::ios::out | std::ios::trunc); 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 // Write data to disk
file.write(m_data.c_str(), m_data.size()); file.write(m_data.c_str(), m_data.size());

22
src/util/format/parser.cpp

@ -5,13 +5,13 @@
*/ */
#include <algorithm> // replace #include <algorithm> // replace
#include <cassert> // assert
#include <cstddef> // size_t #include <cstddef> // size_t
#include <string> #include <string>
#include <string_view> #include <string_view>
#include "util/format/builder.h" #include "util/format/builder.h"
#include "util/format/parser.h" #include "util/format/parser.h"
#include "util/meta/assert.h"
namespace Util::Format { namespace Util::Format {
@ -32,8 +32,7 @@ void Parser::checkFormatParameterConsistency()
{ {
size_t length = m_input.length(); size_t length = m_input.length();
// Format string does not reference all passed parameters VERIFY(length >= m_parameterCount * 2, "format string does not reference all parameters");
assert(length >= m_parameterCount * 2);
size_t braceOpen = 0; size_t braceOpen = 0;
size_t braceClose = 0; size_t braceClose = 0;
@ -54,8 +53,7 @@ void Parser::checkFormatParameterConsistency()
if (peek0 == '{') { if (peek0 == '{') {
braceOpen++; braceOpen++;
// Only empty references {} or specified references {:} are valid VERIFY(peek1 == '}' || peek1 == ':', "invalid parameter reference");
assert(peek1 == '}' || peek1 == ':');
} }
if (peek0 == '}') { if (peek0 == '}') {
braceClose++; braceClose++;
@ -65,17 +63,13 @@ void Parser::checkFormatParameterConsistency()
} }
m_index = 0; m_index = 0;
// Extra open braces in format string VERIFY(!(braceOpen < braceClose), "extra open braces in format string");
assert(!(braceOpen < braceClose));
// Extra closing braces in format string VERIFY(!(braceOpen > braceClose), "extra closing braces in format string");
assert(!(braceOpen > braceClose));
// Format string does not reference all passed parameters VERIFY(!(braceOpen < m_parameterCount), "format string does not reference all passed parameters");
assert(!(braceOpen < m_parameterCount));
// Format string references nonexistent parameter VERIFY(!(braceOpen > m_parameterCount), "format string references nonexistent parameter");
assert(!(braceOpen > m_parameterCount));
} }
std::string_view Parser::consumeLiteral() std::string_view Parser::consumeLiteral()
@ -114,7 +108,7 @@ bool Parser::consumeSpecifier(std::string_view& specifier)
} }
if (!consumeSpecific(':')) { if (!consumeSpecific(':')) {
assert(consumeSpecific('}')); VERIFY(consumeSpecific('}'), "invalid parameter reference");
specifier = ""; specifier = "";
} }
else { else {

3
src/util/genericlexer.cpp

@ -7,6 +7,7 @@
#include <algorithm> // max, min #include <algorithm> // max, min
#include "util/genericlexer.h" #include "util/genericlexer.h"
#include "util/meta/assert.h"
namespace Util { namespace Util {
@ -53,7 +54,7 @@ void GenericLexer::retreat(size_t count)
char GenericLexer::consume() char GenericLexer::consume()
{ {
assert(!isEOF()); VERIFY(!isEOF());
return m_input[m_index++]; return m_input[m_index++];
} }

1
src/util/genericlexer.h

@ -6,7 +6,6 @@
#pragma once #pragma once
#include <cassert> // assert
#include <cstddef> // size_t #include <cstddef> // size_t
#include <string_view> #include <string_view>

18
src/util/json/fromjson.h

@ -7,7 +7,6 @@
#pragma once #pragma once
#include <algorithm> // transform #include <algorithm> // transform
#include <cassert> // assert
#include <cstddef> // nullptr_t #include <cstddef> // nullptr_t
#include <map> #include <map>
#include <string> #include <string>
@ -17,6 +16,7 @@
#include "util/json/array.h" #include "util/json/array.h"
#include "util/json/object.h" #include "util/json/object.h"
#include "util/meta/assert.h"
#include "util/meta/odr.h" #include "util/meta/odr.h"
namespace Util::JSON { namespace Util::JSON {
@ -33,42 +33,42 @@ void fromJson(const Json& json, Json& value)
template<typename Json> template<typename Json>
void fromJson(const Json& json, std::nullptr_t& null) void fromJson(const Json& json, std::nullptr_t& null)
{ {
assert(json.type() == Json::Type::Null); VERIFY(json.type() == Json::Type::Null);
null = nullptr; null = nullptr;
} }
template<typename Json> template<typename Json>
void fromJson(const Json& json, bool& boolean) void fromJson(const Json& json, bool& boolean)
{ {
assert(json.type() == Json::Type::Bool); VERIFY(json.type() == Json::Type::Bool);
boolean = json.asBool(); boolean = json.asBool();
} }
template<typename Json> template<typename Json>
void fromJson(const Json& json, int& number) void fromJson(const Json& json, int& number)
{ {
assert(json.type() == Json::Type::Number); VERIFY(json.type() == Json::Type::Number);
number = (int)json.asDouble(); number = (int)json.asDouble();
} }
template<typename Json> template<typename Json>
void fromJson(const Json& json, double& number) void fromJson(const Json& json, double& number)
{ {
assert(json.type() == Json::Type::Number); VERIFY(json.type() == Json::Type::Number);
number = json.asDouble(); number = json.asDouble();
} }
template<typename Json> template<typename Json>
void fromJson(const Json& json, std::string& string) void fromJson(const Json& json, std::string& string)
{ {
assert(json.type() == Json::Type::String); VERIFY(json.type() == Json::Type::String);
string = json.asString(); string = json.asString();
} }
template<typename Json, typename T> template<typename Json, typename T>
void fromJson(const Json& json, std::vector<T>& array) void fromJson(const Json& json, std::vector<T>& array)
{ {
assert(json.type() == Json::Type::Array); VERIFY(json.type() == Json::Type::Array);
array.resize(json.size()); array.resize(json.size());
std::transform( std::transform(
json.asArray().elements().begin(), json.asArray().elements().begin(),
@ -82,7 +82,7 @@ void fromJson(const Json& json, std::vector<T>& array)
template<typename Json, typename T> template<typename Json, typename T>
void fromJson(const Json& json, std::map<std::string, T>& object) void fromJson(const Json& json, std::map<std::string, T>& object)
{ {
assert(json.type() == Json::Type::Object); VERIFY(json.type() == Json::Type::Object);
object.clear(); object.clear();
for (const auto& [name, value] : json.asObject().members()) { for (const auto& [name, value] : json.asObject().members()) {
object.emplace(name, value.template get<T>()); object.emplace(name, value.template get<T>());
@ -92,7 +92,7 @@ void fromJson(const Json& json, std::map<std::string, T>& object)
template<typename Json, typename T> template<typename Json, typename T>
void fromJson(const Json& json, std::unordered_map<std::string, T>& object) void fromJson(const Json& json, std::unordered_map<std::string, T>& object)
{ {
assert(json.type() == Json::Type::Object); VERIFY(json.type() == Json::Type::Object);
object.clear(); object.clear();
for (const auto& [name, value] : json.asObject().members()) { for (const auto& [name, value] : json.asObject().members()) {
object.emplace(name, value.template get<T>()); object.emplace(name, value.template get<T>());

5
src/util/json/parser.cpp

@ -17,6 +17,7 @@
#include "util/json/object.h" #include "util/json/object.h"
#include "util/json/parser.h" #include "util/json/parser.h"
#include "util/json/value.h" #include "util/json/value.h"
#include "util/meta/assert.h"
namespace Util::JSON { namespace Util::JSON {
@ -88,13 +89,13 @@ bool Parser::isEOF()
Token Parser::peek() Token Parser::peek()
{ {
assert(!isEOF()); VERIFY(!isEOF());
return (*m_tokens)[m_index]; return (*m_tokens)[m_index];
} }
Token Parser::consume() Token Parser::consume()
{ {
assert(!isEOF()); VERIFY(!isEOF());
return (*m_tokens)[m_index++]; return (*m_tokens)[m_index++];
} }

24
src/util/json/value.cpp

@ -5,7 +5,6 @@
*/ */
#include <algorithm> // all_of #include <algorithm> // all_of
#include <cassert> // assert
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <fstream> // >> #include <fstream> // >>
#include <iostream> // istream, ostream #include <iostream> // istream, ostream
@ -18,6 +17,7 @@
#include "util/json/object.h" #include "util/json/object.h"
#include "util/json/serializer.h" #include "util/json/serializer.h"
#include "util/json/value.h" #include "util/json/value.h"
#include "util/meta/assert.h"
namespace Util::JSON { namespace Util::JSON {
@ -180,7 +180,7 @@ void Value::emplace_back(Value value)
m_value.array = new Array; m_value.array = new Array;
} }
assert(m_type == Type::Array); VERIFY(m_type == Type::Array);
m_value.array->emplace_back(value); m_value.array->emplace_back(value);
} }
@ -192,7 +192,7 @@ void Value::emplace(const std::string& key, Value value)
m_value.object = new Object; m_value.object = new Object;
} }
assert(m_type == Type::Object); VERIFY(m_type == Type::Object);
m_value.object->emplace(key, value); 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 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(); 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; m_value.array = new Array;
} }
assert(m_type == Type::Array); VERIFY(m_type == Type::Array);
return (*m_value.array)[index]; return (*m_value.array)[index];
} }
@ -229,43 +229,43 @@ Value& Value::operator[](const std::string& key)
m_value.object = new Object; m_value.object = new Object;
} }
assert(m_type == Type::Object); VERIFY(m_type == Type::Object);
return (*m_value.object)[key]; return (*m_value.object)[key];
} }
const Value& Value::operator[](size_t index) const const Value& Value::operator[](size_t index) const
{ {
assert(m_type == Type::Array); VERIFY(m_type == Type::Array);
return (*m_value.array)[index]; return (*m_value.array)[index];
} }
const Value& Value::operator[](const std::string& key) const const Value& Value::operator[](const std::string& key) const
{ {
assert(m_type == Type::Object); VERIFY(m_type == Type::Object);
return (*m_value.object)[key]; return (*m_value.object)[key];
} }
Value& Value::at(size_t index) Value& Value::at(size_t index)
{ {
assert(m_type == Type::Array); VERIFY(m_type == Type::Array);
return m_value.array->at(index); return m_value.array->at(index);
} }
Value& Value::at(const std::string& key) Value& Value::at(const std::string& key)
{ {
assert(m_type == Type::Object); VERIFY(m_type == Type::Object);
return m_value.object->at(key); return m_value.object->at(key);
} }
const Value& Value::at(size_t index) const const Value& Value::at(size_t index) const
{ {
assert(m_type == Type::Array); VERIFY(m_type == Type::Array);
return m_value.array->at(index); return m_value.array->at(index);
} }
const Value& Value::at(const std::string& key) const 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); return m_value.object->at(key);
} }

Loading…
Cancel
Save