问题描述
表明铛后的应该保留所有的AST解析属性
This answer suggests that clang post revision 165082 should retain all parsed attributes in the AST.
我先带这意味着所有的属性将被保留,但是这不会出现这样的情况:
I first took this to mean that all attributes would be retained, but this does not appear to be the case:
$ clang++ -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
$ cat att.cpp
void f [[noreturn, foo]] () {}
$ clang++ att.cpp -Xclang -ast-dump -fsyntax-only -std=c++11
att.cpp:1:20: warning: unknown attribute 'foo' ignored [-Wattributes]
void f [[noreturn, foo]] () {}
^
att.cpp:1:30: warning: function declared 'noreturn' should not return [-Winvalid-noreturn]
void f [[noreturn, foo]] () {}
^
TranslationUnitDecl 0x102021cd0 <<invalid sloc>>
|-TypedefDecl 0x102022210 <<invalid sloc>> __int128_t '__int128'
|-TypedefDecl 0x102022270 <<invalid sloc>> __uint128_t 'unsigned __int128'
|-TypedefDecl 0x102022630 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'
`-FunctionDecl 0x1020226d0 <att.cpp:1:1, col:30> f 'void (void)'
|-CompoundStmt 0x1020227b0 <col:29, col:30>
`-CXX11NoReturnAttr 0x102022770 <col:10>
2 warnings generated.
在上面,注意属性'富'的确被忽略,是不是在AST present,而不是属性不返回的。
In the above, note that attribute 'foo' has indeed been ignored, and is not present in the AST, as opposed to the attribute 'noreturn'.
要归功于'富'在一些点在AST保留,或将所有的属性必须是实际的编译器(在Attr.td等定义的一部分,作为的)在AST保留?
Will attribute 'foo' be retained in the AST at some point, or will all attributes have to be part of the actual compiler (defined in Attr.td etc., as described in the Clang Internals Manual) to be retained in the AST?
推荐答案
的属性只保留在AST,如果他们已经通过铛,这是大多数海湾合作委员会attribs的那铿锵本身定义的那些闻名。然而,你可以(在某种程度上)使用从提示的。这使您可以定义任何新的属性,然后在下面的方式AST对其进行处理:
比如你拿了code线从上面的链接
The attributes are only retained in the AST if they are already known by Clang, which are most of the GCC attribs and the ones that clang defines itself. However you can(sort of) add your own attribs using the hints from this link. This enables you to define any new attribute and then process it in the ast in the following manner:For example you took the line of code from the above link
__attribute__((annotate("async")) uint c;
然后在你的RecursiveASTVisitor实例,你可以做如下:
Then in your RecursiveASTVisitor instantiation you can do as follows:
bool MyRecursiveASTVisitor::VisitVarDecl(VarDecl* v)
{
v->dump();
if(v->hasAttrs()){
clang::AttrVec vec = v->getAttrs();
printf("%s\n",vec[0]->getSpelling());
printf("%s\n", Lexer::getSourceText(
CharSourceRange::getTokenRange(
vec[0]->getRange()),
compiler.getSourceManager(),
langOpts).str().c_str());
}
return true;
}
第一个printf只打印注解,因为这是原来的属性,要获取的是感兴趣的值来使用我们从词法分析器作为字符串中的实际令牌。
The first printf only prints the "annotate" since that is the original attribute, to get the value which is of interest to use we get the actual token from the lexer as a string.
由于我们并没有创建一个新的属性,我们只得到追加的属性类型,但我们可以进一步挖掘和区分我们新创建的属性。不是优雅的一个新创建的属性(尽管可能需要改变铛code本身),但仍然有效。
Since we did not create a new attribute we only get the append attribute type but we can dig further and distinguish our newly created attribute. Not as elegant as a newly created attribute ( which though possible needs changing clang code itself) but still works.
这篇关于如何访问通过铛工具解析的C ++ 11的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!