Clang: Do not optimize a specific function
<p>For a long time i used <code>gcc</code> to compile C code. Sometimes i had to use the <code>optimize("O0")</code> attribute to disable optimizations for a specific function. Now i like to do this with <code>clang</code>.</p>

<p>Assume the following code:</p>

<pre><code>#include &lt;stdio.h&gt;

void __attribute__((optimize("O0"))) blabla(void) {
}

int main(void) {
    blabla();
    return 0;
}
</code></pre>

<p>If i compile it with <code>clang</code> i get this error:</p>

<pre><code>test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes]
void __attribute__((optimize("O0"))) blabla(void) {
                    ^
1 warning generated.
</code></pre>

<p>Then i used google (and also) stackoverflow to find out what attribute is required for <code>clang</code>, because many of them are not in the standard (as soon as i know).</p>

<p>I found this thread:
<a href="https://stackoverflow.com/questions/26266820/in-clang-how-do-you-use-per-function-optimization-attributes">In clang, how do you use per-function optimization attributes?</a></p>

<p>If i try the attribute <code>optimize("0")</code> i get this error:</p>

<pre><code>test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes]
void __attribute__((optimize("0"))) blabla(void) {
                    ^
1 warning generated.
</code></pre>

<p>And if i try the attribute <code>optnone</code> i get this error:</p>

<pre><code>test2.c:3:21: warning: unknown attribute 'optnone' ignored [-Wattributes]
void __attribute__((optnone)) blabla(void) {
                    ^
1 warning generated.
</code></pre>

<p>I also tried to move the attribute after the function name, but it doesn't work (for some reason there is a warning about GCC?!):</p>

<pre><code>test2.c:3:34: warning: GCC does not allow optnone attribute in this position on a function definition [-Wgcc-compat]
void blabla(void) __attribute__((optnone)) {
                                 ^
test2.c:3:34: warning: unknown attribute 'optnone' ignored [-Wattributes]
2 warnings generated.
</code></pre>

<p>Another test with the following code:</p>

<pre><code>#include &lt;stdio.h&gt;

[[clang::optnone]]
void blabla(void) {
}

int main(void) {
    blabla();
    return 0;
}
</code></pre>

<p>It produces:</p>

<pre><code>user@ubuntu:/tmp/optxx$ clang test2.c
test2.c:3:1: error: expected identifier or '('
[[clang::optnone]]
^
test2.c:3:2: error: expected expression
[[clang::optnone]]
 ^
test2.c:8:5: warning: implicit declaration of function 'blabla' is invalid in C99 [-Wimplicit-function-declaration]
    blabla();
    ^
1 warning and 2 errors generated.
</code></pre>

<p>Probably i do something wrong, but i cannot see what.</p>

<p>-edit-</p>

<p><code>clang</code> version:</p>

<pre><code>user@ubuntu:/tmp/optxx$ clang -v
Ubuntu clang version 3.3-16ubuntu1 (branches/release_33) (based on LLVM 3.3)
Target: x86_64-pc-linux-gnu
Thread model: posix
</code></pre>