Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Last active August 29, 2015 14:02
Show Gist options
  • Save mlabbe/6df2f9f8cdfe5a3b6e7e to your computer and use it in GitHub Desktop.
Save mlabbe/6df2f9f8cdfe5a3b6e7e to your computer and use it in GitHub Desktop.
Compiler extensions made portable.
/*
Compiler extensions made portable
Usage:
In MSVC, attributes must be on declarations in addition to prototypes.
Use _ATTRIBUTES to conditionally declare them, depending on compiler.
// Prototype
EXT_warn_unused_result EXT_force_inline int SomeFunc( void );
// Decl
_ATTRIBUTES(EXT_warn_unused_result EXT_force_inline) int SomeFunc( void );
\endcode
*/
#if defined(__GNUC__) && (__gnuc__ >= 4)
#define EXT_warn_unused_result __attribute__((warn_unused_result))
#define EXT_force_inline __attribute__((always_inline))
#define EXT_pure_function __attribute__((pure))
#define EXT_const_function __attribute__((const))
#define EXT_no_vtable
#define _ATTRIBUTES(x)
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
#define EXT_warn_unused_result _Check_return_
#define EXT_force_inline __forceinline
#define EXT_pure_function
#define EXT_const_function
#define EXT_no_vtable __declspec(novtable)
#define _ATTRIBUTES(x) x
#else
#define EXT_warn_unused_result
#define EXT_force_inline inline
#define EXT_pure_function
#define EXT_const_function
#define _ATTRIBUTES(x)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment