From edaa9f496dedd5cd928b234c9e46e2c7ec62510b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 9 Oct 2020 13:48:21 +0200 Subject: [PATCH] Modify THROW macro to use a constexpr function for file name, instead of macro. This prevents potential warnings (such as clang-tidy's "cppcoreguidelines-pro-bounds-pointer-arithmetic") from showing up when using this macro. --- src/Exception.hpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Exception.hpp b/src/Exception.hpp index 3fabf2b..4b7e9f5 100644 --- a/src/Exception.hpp +++ b/src/Exception.hpp @@ -140,12 +140,21 @@ namespace ArbUt { }; } -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +static constexpr const char* file_name(const char* path) { + const char* file = path; + while (*path) { + if (*path++ == '/') { + file = path; + } + } + return file; +} #define THROW(message) \ std::stringstream ___ss; \ - ___ss << "[" << __FILENAME__ << ":" << __LINE__ << "] " << message; \ + ___ss << "[" << file_name(__FILE__) << ":" << __LINE__ << "] " << (message); \ throw ArbUt::Exception(___ss.str()); + #define NOT_REACHABLE THROW("Not reachable"); #define NOT_IMPLEMENTED THROW("Not implemented");