Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this C++ code compile when using clang -std=gnu++11?

Tags:

c++

c++11

clang

This compiles when using clang -std=gnu++11 -c test.cpp:

void test() {
    [[random text here]]
    if (0) {
    }
}

But this gives error main.cpp:3:1: error: expected statement:

void test() {
    [[random text here]]
}

If I compile with clang -std=gnu++11 -S -emit-llvm main.cpp and look at the LLVM code it looks like the [[...]] line has no effect:

define void @_Z5testv() nounwind uwtable ssp {
  ret void
}

Any ideas why? bug or some C++11 syntax or GNU extension syntax?

Im using clang from Xcode 4.4.1 (Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn).

like image 468
Mattias Wadman Avatar asked Aug 31 '12 09:08

Mattias Wadman


1 Answers

This is using C++11's attribute syntax. "random text here" is therefore assumed to be an attribute. By the C++11 specification, an attribute can modify many statements and declarations.

Attributes can be statements, but they have to actually be statements. Meaning they end in a ; just like many other C++ statements.

The set of attributes supported by an implementation is implementation-defined (and Clang doesn't support any. Indeed, it apparently isn't supposed to support attribute syntax at all, according to the website). Attributes not implemented by a particular implementation should be ignored, which is why it has no effect.

like image 156
Nicol Bolas Avatar answered Oct 24 '22 14:10

Nicol Bolas