From f8369fe4360bd756da64d9d326847d0e2fa22101 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Wed, 3 Mar 2021 16:53:50 +0400 Subject: [PATCH] Fixed a few clang-tidy warnings in libdebug. --- src/libdebug/dchannel.h | 2 +- src/libdebug/dcmdarg.cpp | 8 ++--- src/libdebug/dflags.cpp | 41 +++++++++++-------------- src/libdebug/dout.cpp | 33 ++++---------------- src/libdebug/dout.h | 65 ++++++++++++++++++++++++---------------- src/libdebug/dstate.h | 6 ++-- src/libdebug/dstream.h | 12 ++++---- 7 files changed, 75 insertions(+), 92 deletions(-) diff --git a/src/libdebug/dchannel.h b/src/libdebug/dchannel.h index 1b1dd45..cedec46 100644 --- a/src/libdebug/dchannel.h +++ b/src/libdebug/dchannel.h @@ -69,7 +69,7 @@ class DebugChannelOStream : public DebugChannelBase { // Non-debug-API members: /// Get the ostream. - std::ostream& get_ostream() + [[nodiscard]] std::ostream& get_ostream() { return os_; } diff --git a/src/libdebug/dcmdarg.cpp b/src/libdebug/dcmdarg.cpp index 6adeaae..5321e24 100644 --- a/src/libdebug/dcmdarg.cpp +++ b/src/libdebug/dcmdarg.cpp @@ -57,13 +57,11 @@ namespace debug_internal { - /// Libdebug global command-line arguments - static DebugCmdArgs s_debug_cmd_args; - /// Get libdebug command-line arguments - inline DebugCmdArgs* debug_get_args_holder() + inline DebugCmdArgs* get_debug_get_args_holder() { - return &s_debug_cmd_args; + static DebugCmdArgs args; + return &args; } diff --git a/src/libdebug/dflags.cpp b/src/libdebug/dflags.cpp index 8e2ea9a..54a7afa 100644 --- a/src/libdebug/dflags.cpp +++ b/src/libdebug/dflags.cpp @@ -14,50 +14,45 @@ Copyright: #include -namespace { +namespace debug_level { - /// Debug level names - const std::map s_level_names = { +/// Get debug level name +const char* get_name(flag level) +{ + static const std::map level_names = { {debug_level::fatal, "fatal"}, {debug_level::error, "error"}, {debug_level::warn, "warn"}, {debug_level::info, "info"}, {debug_level::dump, "dump"} }; + return level_names.at(level); +} - /// Debug level color start sequences - const std::map s_level_colors = { + +/// Get debug level color start sequence +const char* get_color_start(flag level) +{ + static const std::map level_colors = { {debug_level::fatal, "\033[1;4;31m"}, // red underlined {debug_level::error, "\033[1;4;31m"}, // red {debug_level::warn, "\033[1;35m"}, // magenta {debug_level::info, "\033[1;36m"}, // cyan {debug_level::dump, "\033[1;32m"} // green }; - - + return level_colors.at(level); } -namespace debug_level { +/// Get debug level color stop sequence +const char* get_color_stop([[maybe_unused]] flag level) +{ + return "\033[0m"; +} - const char* get_name(flag level) - { - return s_level_names.at(level); - } - - - const char* get_color_start(flag level) - { - return s_level_colors.at(level); - } - - const char* get_color_stop([[maybe_unused]] flag level) - { - return "\033[0m"; - } } diff --git a/src/libdebug/dout.cpp b/src/libdebug/dout.cpp index 1cc9d26..549923e 100644 --- a/src/libdebug/dout.cpp +++ b/src/libdebug/dout.cpp @@ -31,17 +31,8 @@ std::ostream& debug_out(debug_level::flag level, const std::string& domain) auto level_map = dm.find(domain); if (level_map == dm.end()) { // no such domain - std::string msg = "debug_out(): Debug state doesn't contain the requested domain: \"" + domain + "\"."; - - if (domain != "default") { - debug_out(debug_level::warn, "default") << msg << "\n"; - debug_out(debug_level::info, "default") << "Auto-creating the missing domain.\n"; - debug_register_domain(domain); - debug_out(debug_level::warn, "default") << "The message follows:\n"; - return debug_out(level, domain); // try again - } - // this is an internal error + std::string msg = "debug_out(): Debug state doesn't contain the requested domain: \"" + domain + "\"."; throw debug_internal_error(msg.c_str()); } @@ -103,20 +94,20 @@ namespace debug_internal { std::ostringstream os; os << "("; - if (enabled_types.to_ulong() & debug_pos::func_name) { + if (enabled_types.test(debug_pos::func_name)) { os << "function: " << func_name; - } else if (enabled_types.to_ulong() & debug_pos::func) { + } else if (enabled_types.test(debug_pos::func)) { os << "function: " << func << "()"; } - if (enabled_types.to_ulong() & debug_pos::file) { + if (enabled_types.test(debug_pos::file)) { if (os.str() != "(") os << ", "; os << "file: " << file; } - if (enabled_types.to_ulong() & debug_pos::line) { + if (enabled_types.test(debug_pos::line)) { if (os.str() != "(") os << ", "; os << "line: " << line; @@ -161,20 +152,6 @@ void debug_indent_reset() -namespace debug_internal { - - - // manupulator objects - DebugIndent debug_indent; - DebugUnindent debug_unindent; - DebugResetIndent debug_resindent; - - -} - - - - diff --git a/src/libdebug/dout.h b/src/libdebug/dout.h index 21c53d5..5b086e5 100644 --- a/src/libdebug/dout.h +++ b/src/libdebug/dout.h @@ -16,6 +16,7 @@ Copyright: // Note: Sun compiler refuses to compile without (iosfwd is not enough). // Since every useful operator << is defined in ostream, we include it here anyway. #include // std::ostream +#include #include "hz/system_specific.h" // HZ_FUNC_PRINTF_ISO_CHECK @@ -109,17 +110,18 @@ namespace debug_internal { struct DebugSourcePos { /// Constructor - inline DebugSourcePos(const std::string& file_, int line_, const std::string& func_name_, const std::string& func_) - : func_name(func_name_), func(func_), line(line_), file(file_), enabled_types(debug_pos::def) + inline DebugSourcePos(std::string par_file, int line_, std::string par_func_name, std::string par_func) + : func_name(std::move(par_func_name)), func(std::move(par_func)), line(line_), file(std::move(par_file)), + enabled_types(debug_pos::def) { } /// Formatted output string - std::string str() const; + [[nodiscard]] std::string str() const; std::string func_name; ///< Function name only std::string func; ///< Function name with namespaces and classes - int line; ///< Source line + int line = 0; ///< Source line std::string file; ///< Source file debug_pos::type enabled_types; ///< Enabled formatting types @@ -230,6 +232,28 @@ namespace debug_internal { } else (void)0 +/// Prints generic message to error-level if assertion fails. Don't need to send into stream, it prints by itself. +/// Returns from the function on assertion failure. +#define DBG_ASSERT_RETURN(cond, return_value) \ + do { \ + if (!(cond)) { \ + debug_out_error("default", "ASSERTION FAILED: " << #cond << " at " << DBG_POS << "\n"); \ + return (return_value); \ + } \ + } while(false) + + +/// Prints generic message to error-level if assertion fails. Don't need to send into stream, it prints by itself. +/// Returns from the function on assertion failure. +#define DBG_ASSERT_RETURN_NONE(cond) \ + do { \ + if (!(cond)) { \ + debug_out_error("default", "ASSERTION FAILED: " << #cond << " at " << DBG_POS << "\n"); \ + return; \ + } \ + } while(false) + + // ------------------ Indentation and manipulators @@ -259,7 +283,7 @@ namespace debug_internal { /// A stream manipulator that increases the indentation level struct DebugIndent { /// Constructor - DebugIndent(int indent_level = 1) : by(indent_level) + constexpr explicit DebugIndent(int indent_level = 1) : by(indent_level) { } /// Constructs a new DebugIndent object @@ -275,7 +299,7 @@ namespace debug_internal { /// A stream manipulator that decreases the indentation level struct DebugUnindent { /// Constructor - DebugUnindent(int unindent_level = 1) : by(unindent_level) + constexpr explicit DebugUnindent(int unindent_level = 1) : by(unindent_level) { } /// Constructs a new DebugUnindent object @@ -304,7 +328,7 @@ namespace debug_internal { // operands are for ADL to work inside _other_ namespaces. /// A stream manipulator operator - inline std::ostream& operator<< (std::ostream& os, debug_internal::DebugIndent& m) + inline std::ostream& operator<< (std::ostream& os, const debug_internal::DebugIndent& m) { debug_indent_inc(m.by); return os; @@ -312,7 +336,7 @@ namespace debug_internal { /// A stream manipulator operator - inline std::ostream& operator<< (std::ostream& os, debug_internal::DebugUnindent& m) + inline std::ostream& operator<< (std::ostream& os, const debug_internal::DebugUnindent& m) { debug_indent_dec(m.by); return os; @@ -320,36 +344,25 @@ namespace debug_internal { /// A stream manipulator operator - inline std::ostream& operator<< (std::ostream& os, [[maybe_unused]] debug_internal::DebugResetIndent& m) + inline std::ostream& operator<< (std::ostream& os, [[maybe_unused]] const debug_internal::DebugResetIndent& m) { debug_indent_reset(); return os; } - // Manipulator objects: - - /// Send this to libdebug-backed stream to increase the indentation level by 1. - extern DebugIndent debug_indent; - - /// Send this to libdebug-backed stream to decrease the indentation level by 1. - extern DebugUnindent debug_unindent; - - /// Send this to libdebug-backed stream to reset the indentation level to 0. - extern DebugResetIndent debug_resindent; - - } // ns -// manupulator objects -using debug_internal::debug_indent; -using debug_internal::debug_unindent; -using debug_internal::debug_resindent; - +/// Manupulator object - send this to libdebug-backed stream to increase the indentation level by 1. +constexpr inline debug_internal::DebugIndent debug_indent; +/// Manupulator object - send this to libdebug-backed stream to decrease the indentation level by 1. +constexpr inline debug_internal::DebugUnindent debug_unindent; +/// Manupulator object - send this to libdebug-backed stream to reset the indentation level to 0. +constexpr inline debug_internal::DebugResetIndent debug_resindent; diff --git a/src/libdebug/dstate.h b/src/libdebug/dstate.h index 50142af..0f39990 100644 --- a/src/libdebug/dstate.h +++ b/src/libdebug/dstate.h @@ -56,14 +56,14 @@ namespace debug_internal { /// Get the domain/level mapping. - domain_map_t& get_domain_map() + [[nodiscard]] domain_map_t& get_domain_map() { return domain_map; } /// Get current indentation level. - int get_indent_level() const + [[nodiscard]] int get_indent_level() const { return indent_level_; } @@ -91,7 +91,7 @@ namespace debug_internal { } /// Check if we're inside a debug_begin() context. - bool get_inside_begin() const + [[nodiscard]] bool get_inside_begin() const { if (inside_begin_.empty()) return false; diff --git a/src/libdebug/dstream.h b/src/libdebug/dstream.h index e06efb4..635c404 100644 --- a/src/libdebug/dstream.h +++ b/src/libdebug/dstream.h @@ -48,7 +48,7 @@ namespace debug_internal { public: /// Constructor - DebugStreamBuf(DebugOutStream* dos) : dos_(dos) + explicit DebugStreamBuf(DebugOutStream* dos) : dos_(dos) { // in case of overflow for output, overflow() will be called to _output_ the data. @@ -70,7 +70,7 @@ namespace debug_internal { /// Virtual destructor - virtual ~DebugStreamBuf() + ~DebugStreamBuf() override { sync(); delete[] pbase(); // delete the buffer @@ -109,7 +109,7 @@ namespace debug_internal { /// Sort-of flush the buffer. Only makes sense if there is a buffer. /// Reimplemented. - int sync() override + int sync() final { if (pbase() != pptr()) { // pptr() - current position; condition is true only if there is something in the buffer. // write_out(std::string(pbase(), pptr() - pbase())); @@ -193,7 +193,7 @@ namespace debug_internal { } /// Get format flags - debug_format::type get_format() const + [[nodiscard]] debug_format::type get_format() const { return format_; } @@ -210,7 +210,7 @@ namespace debug_internal { } /// Check whether the stream is enabled or not. - bool get_enabled() const + [[nodiscard]] bool get_enabled() const { return (rdbuf() == &buf_); } @@ -238,7 +238,7 @@ namespace debug_internal { /// Check if the last sent output is still on the same line /// as the first one. - bool get_is_first_line() + [[nodiscard]] bool get_is_first_line() { return is_first_line_; }