summaryrefslogtreecommitdiff
path: root/compiler/oops
diff options
context:
space:
mode:
authorChunseok Lee <chunseok.lee@samsung.com>2020-04-23 14:45:49 +0900
committerChunseok Lee <chunseok.lee@samsung.com>2020-04-23 14:45:49 +0900
commite2ef8438a24f7c56a0744eb579a6e293ee2fbf8e (patch)
tree44a1a7951d168dd4370e13593ed03f4bc6d920c5 /compiler/oops
parent302e6564a7a76109e1178207e44e45a58631c477 (diff)
downloadnnfw-e2ef8438a24f7c56a0744eb579a6e293ee2fbf8e.tar.gz
nnfw-e2ef8438a24f7c56a0744eb579a6e293ee2fbf8e.tar.bz2
nnfw-e2ef8438a24f7c56a0744eb579a6e293ee2fbf8e.zip
Imported Upstream version 1.4.0upstream/1.4.0submit/tizen/20200423.054851
Diffstat (limited to 'compiler/oops')
-rw-r--r--compiler/oops/CMakeLists.txt12
-rw-r--r--compiler/oops/include/oops/InternalExn.h80
-rw-r--r--compiler/oops/include/oops/UserExn.h89
-rw-r--r--compiler/oops/test.cpp94
4 files changed, 275 insertions, 0 deletions
diff --git a/compiler/oops/CMakeLists.txt b/compiler/oops/CMakeLists.txt
new file mode 100644
index 000000000..f12572d54
--- /dev/null
+++ b/compiler/oops/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_library(oops INTERFACE)
+target_include_directories(oops INTERFACE include)
+target_link_libraries(oops INTERFACE pepper_str)
+
+if(NOT ENABLE_TEST)
+ return()
+endif(NOT ENABLE_TEST)
+
+nnas_find_package(GTest REQUIRED)
+
+GTest_AddTest(oops_test test.cpp)
+target_link_libraries(oops_test oops)
diff --git a/compiler/oops/include/oops/InternalExn.h b/compiler/oops/include/oops/InternalExn.h
new file mode 100644
index 000000000..0e11085c0
--- /dev/null
+++ b/compiler/oops/include/oops/InternalExn.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __OOPS_INTERNAL_EXN_H__
+#define __OOPS_INTERNAL_EXN_H__
+
+#include <exception>
+#include <string>
+
+/// @ brief throw internal exception with message
+#define INTERNAL_EXN(msg) throw oops::InternalExn(__FILE__, __LINE__, msg)
+
+/// @ brief throw internal exception with message and value
+#define INTERNAL_EXN_V(msg, val) throw oops::InternalExn(__FILE__, __LINE__, msg, val)
+
+namespace oops
+{
+
+template <typename T> uint32_t to_uint32(T a) { return static_cast<uint32_t>(a); }
+
+/**
+ * @brief Exception caused by internal error
+ *
+ * Note: Please use the above MACROs
+ */
+class InternalExn : public std::exception
+{
+public:
+ InternalExn(const char *filename, const int line, const std::string &msg)
+ : _filename(filename), _line(line), _msg(msg)
+ {
+ construct_full_msg();
+ }
+
+ explicit InternalExn(const char *filename, const int line, const std::string &msg, uint32_t val)
+ : _filename(filename), _line(line), _msg(msg + ": " + std::to_string(val))
+ {
+ construct_full_msg();
+ }
+
+ explicit InternalExn(const char *filename, const int line, const std::string &msg,
+ const std::string &val)
+ : _filename(filename), _line(line), _msg(msg + ": " + val)
+ {
+ construct_full_msg();
+ }
+
+ const char *what() const noexcept override { return _full_msg.c_str(); }
+
+private:
+ const std::string _filename;
+ const uint32_t _line;
+ const std::string _msg;
+
+private:
+ void construct_full_msg()
+ {
+ _full_msg =
+ "Internal Exception. " + _msg + " [" + _filename + ":" + std::to_string(_line) + "]";
+ }
+
+ std::string _full_msg;
+};
+
+} // namespace oops
+
+#endif // __OOPS_INTERNAL_EXN_H__
diff --git a/compiler/oops/include/oops/UserExn.h b/compiler/oops/include/oops/UserExn.h
new file mode 100644
index 000000000..d0138322d
--- /dev/null
+++ b/compiler/oops/include/oops/UserExn.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __OOPS_USER_EXN_H__
+#define __OOPS_USER_EXN_H__
+
+#include <pepper/str.h>
+
+#include <exception>
+#include <string>
+#include <map>
+
+namespace oops
+{
+
+/**
+ * @brief Exception to user
+ *
+ * Pass msg and one additional info, e.g.,
+ * ex) UserExn("Unsupported rank", 4);
+ * ex) UserExn("Unsupported layout", "NHWC");
+ *
+ * Or pass msg with attribute pairs of name & val ,
+ * ex) UserExn("Node has unsupported layout",
+ * "Node", node->name(),
+ * "layout", node->layout());
+ */
+class UserExn : public std::exception
+{
+public:
+ UserExn() = delete;
+
+ template <typename... Info> UserExn(const std::string &msg, Info &&... args)
+ {
+ std::stringstream out;
+
+ out << "Error: " << msg + ": ";
+
+ build_info(out, args...);
+
+ _msg = out.str();
+ }
+
+ const char *what() const noexcept override { return _msg.c_str(); };
+
+private:
+ template <typename Attr, typename Val, typename... AttsVals>
+ void build_info(std::stringstream &out, Attr &attr, Val &val, AttsVals &... args)
+ {
+ out << pepper::str(attr, " = ", val);
+ out << ", ";
+
+ build_info(out, args...);
+ }
+
+ template <typename Attr, typename Val>
+ void build_info(std::stringstream &out, Attr &attr, Val &val)
+ {
+ out << pepper::str(attr, " = ", val);
+ }
+
+ void build_info(std::stringstream &) { /* empty */}
+
+ // when only one info of string is provided
+ void build_info(std::stringstream &out, const std::string &val) { out << val; }
+
+ // when only one info of uint32_t is provided
+ void build_info(std::stringstream &out, const uint32_t &val) { out << val; }
+
+private:
+ std::string _msg;
+};
+
+} // namespace oops
+
+#endif // __OOPS_USER_EXN_H__
diff --git a/compiler/oops/test.cpp b/compiler/oops/test.cpp
new file mode 100644
index 000000000..666f62f54
--- /dev/null
+++ b/compiler/oops/test.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "oops/InternalExn.h"
+#include "oops/UserExn.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+
+void batman() { INTERNAL_EXN("Here comes Joker"); }
+
+void star_wars() { INTERNAL_EXN_V("Something is approaching", "Darth Vader"); }
+
+enum class InfinityStones
+{
+ SpaceStone,
+ RealityStone,
+ OtherStones,
+};
+
+void avengers()
+{
+ std::string where;
+ std::string separator = ":";
+ try
+ {
+ // exception will be raised in next line
+ where = __FILE__ + separator + std::to_string(__LINE__ + 1);
+ INTERNAL_EXN_V("Last stone was gathered", oops::to_uint32(InfinityStones::SpaceStone));
+ }
+ catch (const oops::InternalExn &e)
+ {
+ auto msg = std::string(e.what());
+ ASSERT_TRUE(msg.find("Last stone was gathered: 0") != std::string::npos);
+ ASSERT_TRUE(msg.find(where) != std::string::npos);
+ }
+}
+
+} // namespace
+
+TEST(oopsTest, InternalExn)
+{
+ ASSERT_THROW(batman(), oops::InternalExn);
+ ASSERT_THROW(star_wars(), oops::InternalExn);
+
+ avengers();
+}
+
+TEST(oopsTest, UserExn_one_info_after_msg)
+{
+ try
+ {
+ throw oops::UserExn("Not a member of Avenger", "Kingsman");
+ }
+ catch (const oops::UserExn &e)
+ {
+ auto msg = std::string(e.what());
+ ASSERT_TRUE(msg.find("Not a member of Avenger: Kingsman") != std::string::npos);
+ }
+}
+
+TEST(oopsTest, UserExn_two_pairs_after_msg)
+{
+ try
+ {
+ std::string hero("Spiderman");
+
+ // clang-format off
+ throw oops::UserExn("Hero's age is wrong",
+ "Hero", hero,
+ "Age", 97);
+ // clang-format on
+ }
+ catch (const oops::UserExn &e)
+ {
+ auto msg = std::string(e.what());
+ ASSERT_TRUE(msg.find("Hero = Spiderman, Age = 97") != std::string::npos);
+ }
+}