Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Macros | Functions
code_annotation.h File Reference
#include "lib/sysdep/compiler.h"
#include "lib/sysdep/arch.h"

Go to the source code of this file.

Classes

struct  static_assert_< bool >
 
struct  static_assert_< true >
 
class  RValue< T >
 

Macros

#define UNUSED(param)
 mark a function parameter as unused and avoid the corresponding compiler warning. More...
 
#define UNUSED2(param)   ((void)(param))
 mark a function local variable or parameter as unused and avoid the corresponding compiler warning. More...
 
#define NOTHROW_DECLARE
 indicate a function will not throw any synchronous exceptions, thus hopefully generating smaller and more efficient code. More...
 
#define NOTHROW_DEFINE
 
#define UNREACHABLE
 "unreachable code" helpers More...
 
#define HAVE_ASSUME_UNREACHABLE   1
 
#define ASSUME_UNREACHABLE
 
#define HAVE_ASSUME_UNREACHABLE   0
 
#define UNREACHABLE
 "unreachable code" helpers More...
 
#define NODEFAULT   default: UNREACHABLE
 convenient specialization of UNREACHABLE for switch statements whose default can never be reached. More...
 
#define PASTE3_HIDDEN__(a, b, c)   a ## b ## c
 
#define PASTE3__(a, b, c)   PASTE3_HIDDEN__(a, b, c)
 
#define UID__   PASTE3__(LINE_, __LINE__, _)
 
#define UID2__   PASTE3__(LINE_, __LINE__, _2)
 
#define UNUSED_ATTRIBUTE
 
#define cassert(expr)   typedef static_assert_<(expr)>::type UID__ UNUSED_ATTRIBUTE
 Compile-time assertion. More...
 
#define cassert_dependent(expr)   typedef typename static_assert_<(expr)>::type UID__ UNUSED_ATTRIBUTE
 Compile-time assertion. More...
 
#define cassert2(expr)   extern char CASSERT_FAILURE[1][(expr)]
 Compile-time assertion. More...
 
#define NONCOPYABLE(className)
 
#define ASSUME_ALIGNED(ptr, multiple)
 
#define PRINTF_ARGS(fmtpos)
 
#define VPRINTF_ARGS(fmtpos)
 
#define WPRINTF_ARGS(fmtpos)
 
#define VWPRINTF_ARGS(fmtpos)
 
#define SENTINEL_ARG
 
#define COMPILER_FENCE
 prevent the compiler from reordering loads or stores across this point. More...
 
#define _W64
 
#define RESTRICT
 
#define ARRAY_SIZE(name)   (sizeof(*ArraySizeDeducer(name)))
 
#define __func__   "(unknown)"
 
#define EXTERN_C   extern
 
#define INLINE   inline
 
#define CALL_CONV
 
#define DECORATED_NAME(name)   name
 
#define STRINGIZE2(id)   # id
 
#define STRINGIZE(id)   STRINGIZE2(id)
 
#define WIDEN2(x)   L ## x
 
#define WIDEN(x)   WIDEN2(x)
 
#define RVALUE_REF(T)   T&&
 expands to the type `rvalue reference to T'; used in function parameter declarations. More...
 
#define LVALUE(rvalue)   rvalue
 convert an rvalue to an lvalue More...
 
#define RVALUE(lvalue)   std::move(lvalue)
 convert anything (lvalue or rvalue) to an rvalue More...
 
#define RVALUE_REF(T)   const RValue<T>&
 expands to the type `rvalue reference to T'; used in function parameter declarations. More...
 
#define LVALUE(rvalue)   rvalue.LValue()
 convert an rvalue to an lvalue More...
 
#define RVALUE(lvalue)   ToRValue(lvalue)
 convert anything (lvalue or rvalue) to an rvalue More...
 

Functions

template<typename T , size_t n>
char(* ArraySizeDeducer (T(&)[n]))[n]
 
template<class T >
static RValue< TToRValue (T &lvalue)
 
template<class T >
static RValue< TToRValue (const T &lvalue)
 
template<class T >
static RValue< TToRValue (const RValue< T > &rvalue)
 

Macro Definition Documentation

#define __func__   "(unknown)"

Definition at line 368 of file code_annotation.h.

#define _W64

Definition at line 289 of file code_annotation.h.

#define ARRAY_SIZE (   name)    (sizeof(*ArraySizeDeducer(name)))

Definition at line 355 of file code_annotation.h.

#define ASSUME_ALIGNED (   ptr,
  multiple 
)

Definition at line 234 of file code_annotation.h.

#define ASSUME_UNREACHABLE

Definition at line 127 of file code_annotation.h.

#define CALL_CONV

Definition at line 390 of file code_annotation.h.

#define cassert (   expr)    typedef static_assert_<(expr)>::type UID__ UNUSED_ATTRIBUTE

Compile-time assertion.

Causes a compile error if the expression evaluates to zero/false.

No runtime overhead; may be used anywhere, including file scope. Especially useful for testing sizeof types.

Parameters
exprExpression that is expected to evaluate to non-zero at compile-time.

Definition at line 193 of file code_annotation.h.

#define cassert2 (   expr)    extern char CASSERT_FAILURE[1][(expr)]

Compile-time assertion.

Causes a compile error if the expression evaluates to zero/false.

No runtime overhead; may be used anywhere, including file scope. Especially useful for testing sizeof types.

Parameters
exprExpression that is expected to evaluate to non-zero at compile-time.

This version has a less helpful error message, but redefinition doesn't trigger warnings.

Definition at line 214 of file code_annotation.h.

#define cassert_dependent (   expr)    typedef typename static_assert_<(expr)>::type UID__ UNUSED_ATTRIBUTE

Compile-time assertion.

Causes a compile error if the expression evaluates to zero/false.

No runtime overhead; may be used anywhere, including file scope. Especially useful for testing sizeof types.

Parameters
exprExpression that is expected to evaluate to non-zero at compile-time.

This version must be used if expr uses a dependent type (e.g. depends on a template parameter).

Definition at line 206 of file code_annotation.h.

#define COMPILER_FENCE

prevent the compiler from reordering loads or stores across this point.

Definition at line 277 of file code_annotation.h.

#define DECORATED_NAME (   name)    name

Definition at line 397 of file code_annotation.h.

#define EXTERN_C   extern

Definition at line 376 of file code_annotation.h.

#define HAVE_ASSUME_UNREACHABLE   1

Definition at line 129 of file code_annotation.h.

#define HAVE_ASSUME_UNREACHABLE   0

Definition at line 129 of file code_annotation.h.

#define INLINE   inline

Definition at line 383 of file code_annotation.h.

#define LVALUE (   rvalue)    rvalue

convert an rvalue to an lvalue

Definition at line 477 of file code_annotation.h.

#define LVALUE (   rvalue)    rvalue.LValue()

convert an rvalue to an lvalue

Definition at line 477 of file code_annotation.h.

#define NODEFAULT   default: UNREACHABLE

convenient specialization of UNREACHABLE for switch statements whose default can never be reached.

example usage: int x; switch(x % 2) { case 0: break; case 1: break; NODEFAULT; }

Definition at line 160 of file code_annotation.h.

#define NONCOPYABLE (   className)
Value:
private:\
className(const className&);\
const className& operator=(const className&)

Definition at line 226 of file code_annotation.h.

#define NOTHROW_DECLARE

indicate a function will not throw any synchronous exceptions, thus hopefully generating smaller and more efficient code.

must be placed BEFORE return types because "The [VC++] compiler ignores, without warning, any __declspec keywords placed after *". such syntax is apparently also legal in GCC, per the example "__attribute__((noreturn)) void d0 (void)".

example: NOTHROW_DECLARE void function(); NOTHROW_DEFINE void function() {}

Definition at line 85 of file code_annotation.h.

#define NOTHROW_DEFINE

Definition at line 86 of file code_annotation.h.

#define PASTE3__ (   a,
  b,
 
)    PASTE3_HIDDEN__(a, b, c)

Definition at line 169 of file code_annotation.h.

#define PASTE3_HIDDEN__ (   a,
  b,
 
)    a ## b ## c

Definition at line 168 of file code_annotation.h.

#define PRINTF_ARGS (   fmtpos)

Definition at line 251 of file code_annotation.h.

#define RESTRICT

Definition at line 328 of file code_annotation.h.

#define RVALUE (   lvalue)    std::move(lvalue)

convert anything (lvalue or rvalue) to an rvalue

Definition at line 478 of file code_annotation.h.

#define RVALUE (   lvalue)    ToRValue(lvalue)

convert anything (lvalue or rvalue) to an rvalue

Definition at line 478 of file code_annotation.h.

#define RVALUE_REF (   T)    T&&

expands to the type `rvalue reference to T'; used in function parameter declarations.

for example, UniqueRange's move ctor is: UniqueRange(RVALUE_REF(UniqueRange) rvalue) { ... }

Definition at line 476 of file code_annotation.h.

#define RVALUE_REF (   T)    const RValue<T>&

expands to the type `rvalue reference to T'; used in function parameter declarations.

for example, UniqueRange's move ctor is: UniqueRange(RVALUE_REF(UniqueRange) rvalue) { ... }

Definition at line 476 of file code_annotation.h.

#define SENTINEL_ARG

Definition at line 262 of file code_annotation.h.

#define STRINGIZE (   id)    STRINGIZE2(id)

Definition at line 404 of file code_annotation.h.

#define STRINGIZE2 (   id)    # id

Definition at line 403 of file code_annotation.h.

#define UID2__   PASTE3__(LINE_, __LINE__, _2)

Definition at line 171 of file code_annotation.h.

#define UID__   PASTE3__(LINE_, __LINE__, _)

Definition at line 170 of file code_annotation.h.

#define UNREACHABLE

"unreachable code" helpers

unreachable lines of code are often the source or symptom of subtle bugs. they are flagged by compiler warnings; however, the opposite problem - erroneously reaching certain spots (e.g. due to missing return statement) is worse and not detected automatically.

to defend against this, the programmer can annotate their code to indicate to humans that a particular spot should never be reached. however, that isn't much help; better is a sentinel that raises an error if if it is actually reached. hence, the UNREACHABLE macro.

ironically, if the code guarded by UNREACHABLE works as it should, compilers may flag the macro's code as unreachable. this would distract from genuine warnings, which is unacceptable.

even worse, compilers differ in their code checking: GCC only complains if non-void functions end without returning a value (i.e. missing return statement), while VC checks if lines are unreachable (e.g. if they are preceded by a return on all paths).

the implementation below enables optimization and automated checking without raising warnings.

Definition at line 142 of file code_annotation.h.

#define UNREACHABLE
Value:
DEBUG_WARN_ERR(ERR::LOGIC); /* hit supposedly unreachable code */\
for(;;){};\
)
const Status LOGIC
Definition: status.h:409
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:331
#define STMT(STMT_code__)
package code into a single statement.

"unreachable code" helpers

unreachable lines of code are often the source or symptom of subtle bugs. they are flagged by compiler warnings; however, the opposite problem - erroneously reaching certain spots (e.g. due to missing return statement) is worse and not detected automatically.

to defend against this, the programmer can annotate their code to indicate to humans that a particular spot should never be reached. however, that isn't much help; better is a sentinel that raises an error if if it is actually reached. hence, the UNREACHABLE macro.

ironically, if the code guarded by UNREACHABLE works as it should, compilers may flag the macro's code as unreachable. this would distract from genuine warnings, which is unacceptable.

even worse, compilers differ in their code checking: GCC only complains if non-void functions end without returning a value (i.e. missing return statement), while VC checks if lines are unreachable (e.g. if they are preceded by a return on all paths).

the implementation below enables optimization and automated checking without raising warnings.

Definition at line 142 of file code_annotation.h.

#define UNUSED (   param)

mark a function parameter as unused and avoid the corresponding compiler warning.

wrap around the parameter name, e.g. void f(int UNUSED(x))

Definition at line 38 of file code_annotation.h.

#define UNUSED2 (   param)    ((void)(param))

mark a function local variable or parameter as unused and avoid the corresponding compiler warning.

note that UNUSED is not applicable to variable definitions that involve initialization, nor is it sufficient in cases where an argument is unused only in certain situations. example: void f(int x) { ASSERT(x == 0); UNUSED2(x); } this asserts in debug builds and avoids warnings in release.

Definition at line 56 of file code_annotation.h.

#define UNUSED_ATTRIBUTE

Definition at line 181 of file code_annotation.h.

#define VPRINTF_ARGS (   fmtpos)

Definition at line 252 of file code_annotation.h.

#define VWPRINTF_ARGS (   fmtpos)

Definition at line 254 of file code_annotation.h.

#define WIDEN (   x)    WIDEN2(x)

Definition at line 410 of file code_annotation.h.

#define WIDEN2 (   x)    L ## x

Definition at line 409 of file code_annotation.h.

#define WPRINTF_ARGS (   fmtpos)

Definition at line 253 of file code_annotation.h.

Function Documentation

template<typename T , size_t n>
char(* ArraySizeDeducer ( T(&)  [n]) )[n]
template<class T >
static RValue<T> ToRValue ( T lvalue)
inlinestatic

Definition at line 455 of file code_annotation.h.

template<class T >
static RValue<T> ToRValue ( const T lvalue)
inlinestatic

Definition at line 461 of file code_annotation.h.

template<class T >
static RValue<T> ToRValue ( const RValue< T > &  rvalue)
inlinestatic

Definition at line 467 of file code_annotation.h.