Skip to content
Snippets Groups Projects
Commit c4f3bf7f authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Improved error handling

parent e65e2059
No related branches found
No related tags found
No related merge requests found
......@@ -24,19 +24,9 @@
do { std::printf(__VA_ARGS__); std::abort(); } while (false)
#else
#include <stdexcept>
#include <memory>
#include "aidge/utils/Formatting.hpp"
#define AIDGE_THROW_OR_ABORT(ex, ...) \
do { \
int n = 128; \
std::unique_ptr<char[]> formatted; \
formatted.reset(new char[n]); \
const int len = std::snprintf(formatted.get(), n, __VA_ARGS__); \
if (len >= n) { \
formatted.reset(new char[len + 1]); \
std::snprintf(formatted.get(), len + 1, __VA_ARGS__); \
}; \
throw ex(formatted.get()); \
} while (false)
throw ex(stringFormat(__VA_ARGS__))
#endif
/**
......
/********************************************************************************
* Copyright (c) 2023 CEA-List
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
#ifndef AIDGE_FORMATTING_H_
#define AIDGE_FORMATTING_H_
#include <memory>
#include <string>
#include <vector>
namespace Aidge {
// The code snippet below is licensed under CC0 1.0.
template<typename ... Args>
std::string stringFormat(const std::string& format, Args... args) {
// Disable security warning on GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
// Disable security warning on MSVC
#pragma warning(push)
#pragma warning(disable : 4774)
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size_s <= 0) {
std::printf("Error during formatting.");
std::abort();
}
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
#pragma warning(pop)
#pragma GCC diagnostic pop
}
/**
* Print any iterable object in a std::string.
*/
template <class T>
std::string print(const T& vec, const std::string& format) {
std::string str = "{";
bool first = true;
for (const auto& val : vec) {
if (!first) {
str += ", ";
}
else {
first = false;
}
str += stringFormat(format, val);
}
str += "}";
return str;
}
}
#endif //AIDGE_FORMATTING_H_
......@@ -334,7 +334,7 @@ void Aidge::GraphView::_forwardDims(std::set<std::shared_ptr<Node>> listNodes) {
}
// Internal check to make sure we won't enter in an infinite loop!
AIDGE_INTERNAL_ASSERT(nextList != listNodes);
AIDGE_ASSERT(nextList != listNodes, "Unable to forward dimensions (circular dependency and/or wrong dimensions?)");
if (!nextList.empty()) {
_forwardDims(nextList);
......
......@@ -34,4 +34,7 @@ void Aidge::Mul_Op::computeOutputDims() {
{
mOutputs[0]->resize(getInput(0)->dims());
}
else if (!getInput(0)->empty() && !getInput(1)->empty()) {
AIDGE_THROW_OR_ABORT(std::runtime_error, "Incompatible input dimensions for Operator Mul");
}
}
\ No newline at end of file
......@@ -32,17 +32,21 @@ Aidge::Operator::~Operator() noexcept = default;
///////////////////////////////////////////////////////
Aidge::NbElts_t Aidge::Operator::getNbRequiredData(const Aidge::IOIndex_t inputIdx) const {
AIDGE_ASSERT(mImpl != nullptr, "getNbRequiredData(): an implementation is required!");
return mImpl->getNbRequiredData(inputIdx);
}
Aidge::NbElts_t Aidge::Operator::getNbConsumedData(Aidge::IOIndex_t inputIdx) const {
AIDGE_ASSERT(mImpl != nullptr, "getNbConsumedData(): an implementation is required!");
return mImpl->getNbConsumedData(inputIdx);
}
Aidge::NbElts_t Aidge::Operator::getNbProducedData(Aidge::IOIndex_t outputIdx) const {
AIDGE_ASSERT(mImpl != nullptr, "getNbProducedData(): an implementation is required!");
return mImpl->getNbProducedData(outputIdx);
}
void Aidge::Operator::updateConsummerProducer(){
AIDGE_ASSERT(mImpl != nullptr, "updateConsummerProducer(): an implementation is required!");
mImpl->updateConsummerProducer();
}
......@@ -52,12 +56,9 @@ void Aidge::Operator::runHooks() const {
}
}
void Aidge::Operator::forward() {
if(mImpl) {
mImpl->forward();
runHooks();
} else {
printf("forward: No implementation is linked.\n");
}
AIDGE_ASSERT(mImpl != nullptr, "forward(): an implementation is required!");
mImpl->forward();
runHooks();
}
void Aidge::Operator::backward() { mImpl->backward(); }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment