sta-core/include/sta/lang.hpp
2022-05-10 15:36:48 +02:00

117 lines
1.9 KiB
C++

/**
* @file
* @brief Helper for useful compiler features.
*/
#ifndef STA_LANG_HPP
#define STA_LANG_HPP
/**
* @defgroup staCoreLang Lang
* @ingroup staCore
* @brief Compiler instructions.
* @{
*/
/**
* @brief Assembler instruction.
*/
#ifndef STA_ASM
# define STA_ASM __asm
#endif // STA_ASM
/**
* @brief Mark as deprecated.
*/
#ifndef STA_DEPRECATED
# define STA_DEPRECATED __attribute__((deprecated))
#endif // STA_DEPRECATED
/**
* @brief Mark as weak definition.
*/
#ifndef STA_WEAK
# define STA_WEAK __attribute__((weak))
#endif // STA_WEAK
/**
* @brief Mark as field w/o added padding.
*/
#ifndef STA_PACKED
# define STA_PACKED __attribute__((packed))
#endif // STA_PACKED
/**
* @brief Mark as struct w/o added padding.
*/
#ifndef STA_PACKED_STRUCT
# define STA_PACKED_STRUCT struct STA_PACKED
#endif // STA_PACKED_STRUCT
/**
* @brief Mark as union w/o added padding.
*/
#ifndef STA_PACKED_UNION
# define STA_PACKED_UNION union STA_PACKED
#endif // STA_PACKED_UNION
/**
* @brief Add breakpoint.
*
* @param value Additional uint16_t data for debugger
*/
#ifndef STA_BKPT
# define STA_BKPT(value) STA_ASM volatile ("bkpt "#value)
#endif // STA_BKPT
// TODO Check for GCC
/**
* @brief Silencing compiler warnings in unreachable code locations.
*
* Example:
* @code
* enum class E { A };
*
* E val = A;
*
* switch (val)
* {
* case E::A:
* break;
*
* default:
* // All enum values already handled
* STA_UNREACHABLE();
* }
* @endcode
*/
#ifndef STA_UNREACHABLE
# define STA_UNREACHABLE() __builtin_unreachable()
#endif // !STA_UNREACHABLE
/**
* @brief Silencing compiler warnings for intended switch case fallthrough.
*
* Example:
* @code
* switch (val)
* {
* case A: STA_FALLTHROUGH();
* case B:
* do();
* break;
* }
* @endcode
*/
#ifndef STA_FALLTRHOUGH
# define STA_FALLTHROUGH() __attribute__((fallthrough))
#endif // !STA_FALLTRHOUGH
/** @} */
#endif // STA_LANG_HPP