Skip to content

Commit

Permalink
fix: addressing cppcheck warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed Jul 12, 2024
1 parent ebb9368 commit fa81b1d
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 53 deletions.
6 changes: 3 additions & 3 deletions include/Ark/Compiler/AST/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ namespace Ark::internal

std::optional<Node> field()
{
std::string symbol;
if (!name(&symbol))
std::string sym;
if (!name(&sym))
return std::nullopt;

std::optional<Node> leaf { Node(NodeType::Field) };
setNodePosAndFilename(leaf.value());
leaf->push_back(Node(NodeType::Symbol, symbol));
leaf->push_back(Node(NodeType::Symbol, sym));

while (true)
{
Expand Down
2 changes: 1 addition & 1 deletion include/Ark/Compiler/AST/Predicates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace Ark::internal
explicit IsChar(const char c) :
CharPred("'" + std::string(1, c) + "'"), m_k(c)
{}
explicit IsChar(const utf8_char_t c) :
explicit IsChar(const utf8_char_t& c) :
CharPred(std::string(c.c_str())), m_k(c.codepoint())
{}
bool operator()(const utf8_char_t::codepoint_t c) const override
Expand Down
12 changes: 6 additions & 6 deletions include/Ark/Compiler/AST/utf8_char.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Ark::internal
// https://github.com/sheredom/utf8.h/blob/4e4d828174c35e4564c31a9e35580c299c69a063/utf8.h#L1178
static std::pair<std::string::iterator, utf8_char_t> at(std::string::iterator it, const std::string::iterator end)
{
codepoint_t codepoint;
codepoint_t cp;
length_t length;
repr_t repr = {};

Expand All @@ -34,7 +34,7 @@ namespace Ark::internal
if (it + 3 == end || it + 2 == end || it + 1 == end)
return std::make_pair(end, utf8_char_t {});

codepoint = (static_cast<codepoint_t>(0x07 & *it) << 18) |
cp = (static_cast<codepoint_t>(0x07 & *it) << 18) |
(static_cast<codepoint_t>(0x3f & *(it + 1)) << 12) |
(static_cast<codepoint_t>(0x3f & *(it + 2)) << 6) |
static_cast<codepoint_t>(0x3f & *(it + 3));
Expand All @@ -45,7 +45,7 @@ namespace Ark::internal
if (it + 2 == end || it + 1 == end)
return std::make_pair(end, utf8_char_t {});

codepoint = (static_cast<codepoint_t>(0x0f & *it) << 12) |
cp = (static_cast<codepoint_t>(0x0f & *it) << 12) |
(static_cast<codepoint_t>(0x3f & *(it + 1)) << 6) |
static_cast<codepoint_t>(0x3f & *(it + 2));
length = 3;
Expand All @@ -55,21 +55,21 @@ namespace Ark::internal
if (it + 1 == end)
return std::make_pair(end, utf8_char_t {});

codepoint = (static_cast<codepoint_t>(0x1f & *it) << 6) |
cp = (static_cast<codepoint_t>(0x1f & *it) << 6) |
static_cast<codepoint_t>(0x3f & *(it + 1));
length = 2;
}
else // 1 byte utf8 codepoint otherwise
{
codepoint = static_cast<unsigned char>(*it);
cp = static_cast<unsigned char>(*it);
length = 1;
}

for (length_t i = 0; i < length; ++i)
repr[i] = static_cast<unsigned char>(*(it + static_cast<int>(i)));

return std::make_pair(it + static_cast<long>(length),
utf8_char_t(codepoint, length, repr));
utf8_char_t(cp, length, repr));
}

[[nodiscard]] bool isPrintable() const
Expand Down
8 changes: 4 additions & 4 deletions include/Ark/TypeChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file TypeChecker.hpp
* @author Alexandre Plateau (lexplt.dev@gmail.com)
* @brief
* @version 0.4
* @version 1.0
* @date 2022-01-16
*
* @copyright Copyright (c) 2022-2024
Expand Down Expand Up @@ -66,11 +66,11 @@ namespace Ark::types
std::vector<ValueType> types;
bool variadic;

Typedef(const std::string_view type_name, const ValueType type, const bool is_variadic = false) :
Typedef(const std::string_view& type_name, const ValueType type, const bool is_variadic = false) :
name(type_name), types { type }, variadic(is_variadic)
{}

Typedef(const std::string_view type_name, const std::vector<ValueType>& type_list, const bool is_variadic = false) :
Typedef(const std::string_view& type_name, const std::vector<ValueType>& type_list, const bool is_variadic = false) :
name(type_name), types(type_list), variadic(is_variadic)
{}
};
Expand All @@ -91,7 +91,7 @@ namespace Ark::types
* @param contracts types contracts the function can follow
* @param args provided argument list
*/
ARK_API void generateError [[noreturn]] (std::string_view funcname, const std::vector<Contract>& contracts, const std::vector<Value>& args);
ARK_API void generateError [[noreturn]] (const std::string_view& funcname, const std::vector<Contract>& contracts, const std::vector<Value>& args);
}

#endif
19 changes: 6 additions & 13 deletions src/arkreactor/Compiler/AST/BaseParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,9 @@ namespace Ark::internal

bool BaseParser::sequence(const std::string& s)
{
for (const char i : s)
{
if (!accept(IsChar(i)))
return false;
}

return true;
return std::ranges::all_of(s, [this](const char c) {
return accept(IsChar(c));
});
}

bool BaseParser::packageName(std::string* s)
Expand Down Expand Up @@ -354,11 +350,8 @@ namespace Ark::internal
if (s)
*s = buffer;

for (const auto& word : words)
{
if (word == buffer)
return true;
}
return false;
return std::ranges::any_of(words, [&buffer](const std::string& word) {
return word == buffer;
});
}
}
15 changes: 9 additions & 6 deletions src/arkreactor/Compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ namespace Ark
{
// push the string, null terminated
std::string s = sym.string();
for (const char i : s)
m_bytecode.push_back(static_cast<uint8_t>(i));
std::ranges::transform(s, std::back_inserter(m_bytecode), [](const char i) {
return static_cast<uint8_t>(i);
});
m_bytecode.push_back(0_u8);
}

Expand All @@ -161,15 +162,17 @@ namespace Ark
m_bytecode.push_back(NUMBER_TYPE);
const auto n = std::get<double>(val.value);
std::string t = std::to_string(n);
for (const char i : t)
m_bytecode.push_back(static_cast<uint8_t>(i));
std::ranges::transform(t, std::back_inserter(m_bytecode), [](const char i) {
return static_cast<uint8_t>(i);
});
}
else if (val.type == ValTableElemType::String)
{
m_bytecode.push_back(STRING_TYPE);
auto t = std::get<std::string>(val.value);
for (const char i : t)
m_bytecode.push_back(static_cast<uint8_t>(i));
std::ranges::transform(t, std::back_inserter(m_bytecode), [](const char i) {
return static_cast<uint8_t>(i);
});
}
else if (val.type == ValTableElemType::PageAddr)
{
Expand Down
5 changes: 3 additions & 2 deletions src/arkreactor/Compiler/ImportSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ namespace Ark::internal
module_node.push_back(Node(Keyword::Import));

auto package_node = Node(NodeType::List);
for (const std::string& stem : import.package)
package_node.push_back(Node(NodeType::String, stem));
std::ranges::transform(import.package, std::back_inserter(package_node.list()), [](const std::string& stem) {
return Node(NodeType::String, stem);
});
module_node.push_back(package_node);
// empty symbols list
module_node.push_back(Node(NodeType::List));
Expand Down
2 changes: 1 addition & 1 deletion src/arkreactor/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace Ark::types
}
}

[[noreturn]] void generateError(std::string_view funcname, const std::vector<Contract>& contracts, const std::vector<Value>& args)
[[noreturn]] void generateError(const std::string_view& funcname, const std::vector<Contract>& contracts, const std::vector<Value>& args)
{
std::cout << "Function " << termcolor::blue << funcname << termcolor::reset << " expected ";

Expand Down
8 changes: 3 additions & 5 deletions src/arkreactor/VM/Future.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
namespace Ark::internal
{
Future::Future(ExecutionContext* context, VM* vm, std::vector<Value>& args) :
m_context(context), m_vm(vm)
{
m_value = std::async(std::launch::async, [vm, context, args]() mutable {
m_context(context), m_vm(vm), m_value(std::async(std::launch::async, [vm, context, args]() mutable {
return vm->resolve(context, args);
});
}
}))
{}

Value Future::resolve()
{
Expand Down
5 changes: 3 additions & 2 deletions src/arkreactor/VM/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ namespace Ark
void State::setArgs(const std::vector<std::string>& args) noexcept
{
Value val(ValueType::List);
for (const std::string& arg : args)
val.push_back(Value(arg));
std::ranges::transform(args, std::back_inserter(val.list()), [](const std::string& arg) {
return Value(arg);
});
m_binded["sys:args"] = val;

m_binded["sys:platform"] = Value(ARK_PLATFORM_NAME);
Expand Down
11 changes: 5 additions & 6 deletions src/arkreactor/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ namespace Ark

Value& VM::operator[](const std::string& name) noexcept
{
ExecutionContext& context = *m_execution_contexts.front();

// find id of object
const auto it = std::ranges::find(m_state.m_symbols, name);
if (it == m_state.m_symbols.end())
Expand All @@ -80,11 +78,14 @@ namespace Ark
const auto dist = std::distance(m_state.m_symbols.begin(), it);
if (std::cmp_less(dist, std::numeric_limits<uint16_t>::max()))
{
ExecutionContext& context = *m_execution_contexts.front();

const auto id = static_cast<uint16_t>(dist);
Value* var = findNearestVariable(id, context);
if (var != nullptr)
return *var;
}

m_no_value = Builtins::nil;
return m_no_value;
}
Expand Down Expand Up @@ -558,8 +559,7 @@ namespace Ark
{ { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
{ *list, *next });

for (auto& val : next->list())
obj.push_back(val);
std::ranges::copy(next->list(), std::back_inserter(obj.list()));
}
push(std::move(obj), context);
break;
Expand Down Expand Up @@ -608,8 +608,7 @@ namespace Ark
{ { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
{ *list, *next });

for (auto& it : next->list())
list->push_back(it);
std::ranges::copy(next->list(), std::back_inserter(list->list()));
}

break;
Expand Down
10 changes: 6 additions & 4 deletions src/arkscript/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,17 @@ int main(int argc, char** argv)
// if arkscript lib paths were provided by the CLI, bypass the automatic lookup
if (!libdir.empty())
{
for (const auto& path : Utils::splitString(libdir, ';'))
lib_paths.emplace_back(path);
std::ranges::transform(Utils::splitString(libdir, ';'), std::back_inserter(lib_paths), [](const std::string& path) {
return std::filesystem::path(path);
});
}
else
{
if (const char* arkpath = std::getenv("ARKSCRIPT_PATH"))
{
for (const auto& path : Utils::splitString(arkpath, ';'))
lib_paths.emplace_back(path);
std::ranges::transform(Utils::splitString(arkpath, ';'), std::back_inserter(lib_paths), [](const std::string& path) {
return std::filesystem::path(path);
});
}
else if (Utils::fileExists("./lib"))
lib_paths.emplace_back("lib");
Expand Down

0 comments on commit fa81b1d

Please sign in to comment.