diff --git a/CMakeLists.txt b/CMakeLists.txt index bd9ae06..9b98ea9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,15 @@ project(dropshell VERSION 1.0.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +# Set default build type to Release if not specified +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build (Debug or Release)" FORCE) +endif() + +# Configure build-specific compiler flags +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG") + # Configure version information string(TIMESTAMP CURRENT_YEAR "%Y") string(TIMESTAMP CURRENT_MONTH "%m") diff --git a/build.sh b/build.sh index 278f6fd..3f35d47 100755 --- a/build.sh +++ b/build.sh @@ -74,7 +74,7 @@ fi # Configure with CMake print_status "Configuring with CMake..." -cmake .. -DCMAKE_BUILD_TYPE=Release +cmake .. -DCMAKE_BUILD_TYPE=Debug # Build the project print_status "Building project..." diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index 4d94473..641b4b1 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -4,6 +4,7 @@ #include "templates.hpp" #include "services.hpp" #include "servers.hpp" +#include "utils/assert.hpp" #include #include @@ -22,6 +23,7 @@ void dropshell::autocomplete(const std::vector &args) return; } + ASSERT(args.size() >= 3); std::string cmd = args[2]; // std::cout<<" cmd = ["< +#include +#include // For std::exit and EXIT_FAILURE + +namespace ds { + +struct SourceLocation { + const char* file_name; + int line; + const char* function_name; +}; + +// Helper macro to create a SourceLocation with current context +#define DS_CURRENT_LOCATION ds::SourceLocation{__FILE__, __LINE__, __func__} + +[[noreturn]] inline void assert_fail( + const char* expression, + const SourceLocation& location, + const char* message = nullptr) { + + std::cerr << "\033[1;31mAssertion failed!\033[0m\n" + << "Expression: \033[1;33m" << expression << "\033[0m\n" + << "Location: \033[1;36m" << location.file_name << ":" + << location.line << "\033[0m\n" + << "Function: \033[1;36m" << location.function_name << "\033[0m\n"; + + if (message) { + std::cerr << "Message: \033[1;37m" << message << "\033[0m\n"; + } + + std::cerr << std::endl; + + // Exit the program without creating a core dump + std::exit(EXIT_FAILURE); +} + +} // namespace ds + +// Define assertion macros with different behaviors based on build configuration +#if defined(NDEBUG) && !defined(DS_ENABLE_RELEASE_ASSERTS) + // Assertions disabled in release builds by default + #define ASSERT(condition) ((void)0) + #define ASSERT_MSG(condition, message) ((void)0) +#else + // Standard assertion + #define ASSERT(condition) \ + do { \ + if (!(condition)) { \ + ds::assert_fail(#condition, DS_CURRENT_LOCATION); \ + } \ + } while (false) + + // Assertion with custom message + #define ASSERT_MSG(condition, message) \ + do { \ + if (!(condition)) { \ + ds::assert_fail(#condition, DS_CURRENT_LOCATION, message); \ + } \ + } while (false) +#endif + +// Always-active assertion for critical checks, even in release builds +#define ASSERT_ALWAYS(condition) \ + do { \ + if (!(condition)) { \ + ds::assert_fail(#condition, DS_CURRENT_LOCATION); \ + } \ + } while (false) + +// Always-active assertion with custom message for critical checks +#define ASSERT_ALWAYS_MSG(condition, message) \ + do { \ + if (!(condition)) { \ + ds::assert_fail(#condition, DS_CURRENT_LOCATION, message); \ + } \ + } while (false) + +#endif // DROPSHELL_ASSERT_HPP