add asserts.

This commit is contained in:
Your Name 2025-04-26 18:15:49 +12:00
parent 4953d4988d
commit 977e878eff
4 changed files with 93 additions and 1 deletions

View File

@ -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")

View File

@ -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..."

View File

@ -4,6 +4,7 @@
#include "templates.hpp"
#include "services.hpp"
#include "servers.hpp"
#include "utils/assert.hpp"
#include <algorithm>
#include <iostream>
@ -22,6 +23,7 @@ void dropshell::autocomplete(const std::vector<std::string> &args)
return;
}
ASSERT(args.size() >= 3);
std::string cmd = args[2];
// std::cout<<" cmd = ["<<cmd<<"]"<<std::endl;

81
src/utils/assert.hpp Normal file
View File

@ -0,0 +1,81 @@
#ifndef DROPSHELL_ASSERT_HPP
#define DROPSHELL_ASSERT_HPP
#include <iostream>
#include <string_view>
#include <cstdlib> // 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