gcc attribute 特性记录

记录常用的gcc attribute 属性,随时更新

#include <stdio.h>

#define __compiletime_object_size(obj)    __builtin_object_size(obj, 0)
#define __compiletime_warning(message)    __attribute__((__warning__(message)))
#define __compiletime_error(message)      __attribute__((__error__(message)))
#define __must_check		              __attribute__((warn_unused_result))

#define GCC_VERSION (__GNUC__ * 10000	\
		     + __GNUC_MINOR__ * 100	    \
		     + __GNUC_PATCHLEVEL__)

#if GCC_VERSION > 60000
# error gcc id too new!
#endif

#define xxdk (2 << 30)
#define xxdkUL (2UL << 40)

 __compiletime_warning("warning\n") int add (int a, int b)
{
	return a + b;
}

int __must_check check_add(int a, int b)
{
	return a + b;
}

__compiletime_error("error\n") int sub (int a, int b)
{
	return a - b;
}

enum Tank {
	M1A2,
	K90,
};

struct xxdk_a {
	int a;
	int b;
};

struct xxdk_b {
	char a;
	char b;
};

int struct_add(void* addr_a, void* addr_b)
{
	printf("objsize %lu\n", __compiletime_object_size(addr_a));
	printf("objsize %lu\n", __compiletime_object_size(addr_b));

	return 0;
}

int main()
{
    printf("sizeof macro xxdk: %lu\n", sizeof(xxdk));
    printf("xxdk: %u\n", xxdk);
    printf("sizeof macro xxdkUL: %lu\n", sizeof(xxdkUL));
    printf("xxdk: %ld\n", xxdkUL);
	printf("sizeof enum: %lu\n", sizeof(enum Tank));
    printf("gcc version: %u\n", GCC_VERSION);

	add(1, 2);       ///< warning: call to ‘add’ declared with attribute warning: warning 
	//sub(1, 2);       ///< error: call to ‘sub’ declared with attribute error: error
	check_add(1, 2); ///< warning: ignoring return value of ‘check_add’, declared with attribute warn_unused_result [-Wunused-result]
	struct xxdk_a a;
	struct xxdk_b b;
	struct_add(&a, &b);

	printf("1 is constant? %d\n", __builtin_constant_p(1));
	printf("a is constant? %d\n", __builtin_constant_p(a));

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值