在铿锵,你如何使用每个功能优化的属性? [英] In clang, how do you use per-function optimization attributes?

查看:1219
本文介绍了在铿锵,你如何使用每个功能优化的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用,以prevent某些与安全相关的调用编译没有优化的特定功能,以 memset的()被优化掉。

I'm trying to compile a specific function with no optimization using clang, in order to prevent certain security-related calls to memset() from being optimized away.

根据,可以在这里找到的文件,存在一个 optnone 属性,它允许这样做。此外,一个例子可以在这里找到

According to the documentation that can be found here, there exists an optnone attribute which allows this. Also, an example can be found here.

不幸的是,(至少在下面版本铛的,在OS X 10.9.5),这是导致编译器警告,如在本实施例中可以看出:

Unfortunately, (at least on the below version of clang, on OS X 10.9.5), this is causing compiler warnings, as can be seen in this example:

$ clang --version
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

$ cat optnone.c
#include <string.h>

__attribute__((optnone)) void*
always_memset(void *b, int c, size_t len)
{
    return memset(b, c, len);
}

$ clang -Wall -O3 -c -o optnone.o optnone.c
optnone.c:3:16: warning: unknown attribute 'optnone' ignored [-Wattributes]
__attribute__((optnone)) void*
               ^
1 warning generated.

我也尝试使用的#pragma铛优化关,但这引起了未知编译忽略警告。

有谁知道这是为什么不工作?我错过了使用此功能的prerequisite? (我也用各种不同的 -std = 参数,包括试图 C11 gnu11 C99 gnu99 ,但没有改变的行为。)

Does anyone know why this isn't working? Did I miss a prerequisite for using this feature? (I also tried using various different -std= parameters, including c11, gnu11, c99, and gnu99, but nothing changed the behavior.)

推荐答案

随着铿锵文档中说,

锵支持GCC的GNU属性命名空间。所有GCC属性这是接受了 __属性__((富))语法也被接纳为 [GNU :: foo的] 。这仅延伸到这是由GCC指定属性(见GCC功能属性的列表,GCC变量属性和GCC类型属性)。与海湾合作委员会的实施,这些属性必须附属于声明符-ID在声明中,这意味着它们必须要么在声明的开头或声明的名字之后。

Clang supports GCC’s gnu attribute namespace. All GCC attributes which are accepted with the __attribute__((foo)) syntax are also accepted as [[gnu::foo]]. This only extends to attributes which are specified by GCC (see the list of GCC function attributes, GCC variable attributes, and GCC type attributes). As with the GCC implementation, these attributes must appertain to the declarator-id in a declaration, which means they must go either at the start of the declaration or immediately after the name being declared.

尝试

void* always_memset(void *b, int c, size_t len) [[gnu::optimize(0)]]

void* always_memset(void *b, int c, size_t len) __attribute__ ((optimize("0")));

这篇关于在铿锵,你如何使用每个功能优化的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆