在C庫里面經(jīng)??梢钥吹礁鞣N奇怪的宏,不知道是什么意思,但是C標準庫已經(jīng)存在了幾十年,里面的東西都是成熟的,因此非常值得深入挖掘?qū)W習(xí)。
如stdlib.h
里面:
/* Return the value of envariable NAME, or NULL if it doesn't exist. */
extern char *getenv (__const char *__name) __THROW __nonnull ((1)) __wur;
這里面有__THROW
,__nonull
以及__wur
三個不常見的宏。這都是什么意思呢?
__THROW是什么東西
這篇文章解釋了__THROW
到底是干什么的。
在LINUX目錄/usr/include/sys/cdefs.h
中就有關(guān)于__THROW
的定義:
#ifdef __GNUC__
/* GCC can always grok prototypes. For C++ programs we add throw()
to help it optimize the function calls. But this works only with
gcc 2.8.x and egcs. For gcc 3.2 and up we even mark C functions
as non-throwing using a function attribute since programs can use
the -fexceptions options for C code as well. */
# if !defined __cplusplus && __GNUC_PREREQ (3, 3)
# define __THROW __attribute__ ((__nothrow__))
# define __NTH(fct) __attribute__ ((__nothrow__)) fct
# else
# if defined __cplusplus && __GNUC_PREREQ (2,8)
# define __THROW throw ()
# define __NTH(fct) fct throw ()
# else
# define __THROW
# define __NTH(fct) fct
# endif
# endif
#else /* Not GCC. */
# define __inline /* No inline functions. */
# define __THROW
# define __NTH(fct) fct
# define __const const
# define __signed signed
# define __volatile volatile
#endif /* GCC. */
位置:/usr/include/features.h
如果當前版本低于<maj.min>則為1。
/* Convenience macros to test the versions of glibc and gcc.
Use them like this:
#if __GNUC_PREREQ (2,8)
... code requiring gcc 2.8 or later ...
#endif
Note - they won't work for gcc1 or glibc1, since the _MINOR macros
were not defined then. */
#if defined __GNUC__ && defined __GNUC_MINOR__
# define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
# define __GNUC_PREREQ(maj, min) 0
#endif
上面的定義可以看出,__THROW
宏定義只在GCC下有效,觀察#ifdef __GNUC__
部分,可以看出,在一般C環(huán)境中此宏是沒有意義的;在GNUC版本高于3.2時,庫用函數(shù)屬性將C函數(shù)標記為__nothrow__
;而如果代碼定義了__cplusplus
則表示為C++代碼,且GNUC版本為2.8.x,此時才有意思,為C++程序加入throw()
以優(yōu)化函數(shù)調(diào)用。
總而言之,這個宏在C中沒有什么大用。
Declaring Attributes of Functions
關(guān)鍵字__attribute__
允許你聲明時指定特殊的屬性。這個關(guān)鍵字后面跟著雙層括號引起來的屬性說明。目前定義用于函數(shù)的屬性:
aligned
alloc_size
alloc_align
assume_aligned
noreturn
returns_twice
noinline
noclone
no_icf
always_inline
flatten
pure
const
nothrow
sentinel
format
format_arg
no_instrument_function
no_split_stack
section
constructor
destructor
used
unused
deprecated
weak
malloc
alias
ifunc
warn_unused_result
nonnull
returns_nonnull
gnu_inline
externally_visible
hot
cold
artificial
no_sanitize_address
no_address_safety_analysis
no_sanitize_thread
no_sanitize_undefined
no_reorder
bnd_legacy
bnd_instrument
stack_protect
error
warning
Other attributes, including section are supported for
variables
declarations,labels
and fortypes
.
You may also specify attributes with ‘__’ preceding and following each keyword. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use __noreturn__ instead of noreturn.
上面意思是說:你可以用__
前綴和后綴在每個關(guān)鍵字上,這樣你在頭文件中使用時就不用擔(dān)心可能存在同樣名字宏的沖突。如__noreturn__
替代noreturn
。
__nonnull
__nonnull
的宏定義在/usr/include/sys/cdefs.h
里面:
/* The nonull function attribute allows to mark pointer parameters which
must not be NULL. */
#if __GNUC_PREREQ (3,3)
# define __nonnull(params) __attribute__ ((__nonnull__ params))
#else
# define __nonnull(params)
#endif
如果當前版本低于3.3,則__nonnull
實際就是__nonnull__
屬性。Using the GNU Compiler Collection 中有關(guān)于nonnull
的詳細描述。
nonnull (arg-index, ...)
The nonnull attribute specifies that some function parameters should be nonnull
pointers. For instance, the declaration:
extern void *
my_memcpy (void *dest, const void *src, size_t len)
__attribute__((nonnull (1, 2)));
causes the compiler to check that, in calls to my_memcpy, arguments dest and
src are non-null. If the compiler determines that a null pointer is passed in
an argument slot marked as non-null, and the ‘-Wnonnull’ option is enabled, a
warning is issued. The compiler may also choose to make optimizations based
on the knowledge that certain function arguments will never be null.
If no argument index list is given to the nonnull attribute, all pointer arguments
are marked as non-null. To illustrate, the following declaration is equivalent to
the previous example:extern void *
my_memcpy (void *dest, const void *src, size_t len)
__attribute__((nonnull));
翻譯一下:nonnull
屬性指定一些函數(shù)參數(shù)應(yīng)為非空指針。舉了一個例子,這個例子中__attribute__((nonnull (1, 2)))
會引起編譯器檢查參數(shù)dest
和src
是否為非空。同時這個屬性跟編譯器選項-Wnonnull
有聯(lián)系,如果傳入空指針到標記為非空的參數(shù),且使能了-Wnonnull
,編譯器會報warning。另外如果不指定nonnull
屬性的參數(shù)索引號,則所有指針參數(shù)都被標記為非空。
__wur
__wur
的宏定義在/usr/include/sys/cdefs.h
里面:
/* If fortification mode, we warn about unused results of certain
function calls which can lead to problems. */
#if __GNUC_PREREQ (3,4)
# define __attribute_warn_unused_result__ \
__attribute__ ((__warn_unused_result__))
# if __USE_FORTIFY_LEVEL > 0
# define __wur __attribute_warn_unused_result__
# endif
#else
# define __attribute_warn_unused_result__ /* empty */
#endif
#ifndef __wur
# define __wur /* Ignore */
#endif
在GCC版本小于3.4且__USE_FORTIFY_LEVEL
>0時,__wur
就是__attribute__ ((__warn_unused_result__))
。
warn_unused_result
The warn_unused_result attribute causes a warning to be emitted if a caller of
the function with this attribute does not use its return value. This is useful for
functions where not checking the result is either a security problem or always
a bug, such as realloc.
int fn () attribute ((warn_unused_result));
int foo ()
{
if (fn () < 0) return -1;
fn ();
return 0;
}
> results in warning on line 5.
如果函數(shù)的調(diào)用者不使用函數(shù)的返回值,`warn_unused_result`屬性會導(dǎo)致發(fā)出警告。這對于沒檢查函數(shù)結(jié)果是安全問題或是bug的函數(shù)很有幫助。
## 總結(jié)
C庫函數(shù)里面的很多宏最后都是屬性(Attribute)的定義,且基本定義在`/usr/include/sys/cdefs.h`,關(guān)于不同的屬性用法,可以參考[Using the GNU Compiler Collection](https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc.pdf)的第六章:Extensions to the C Language Family。
參考閱讀:
[\_\_attribute_pure\_\_帶來的奇怪問題](http://www.trueeyu.com/?p=1486)
[Declaring Attributes of Functions](https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes)