[cfe-dev] Inconsistent reporting of annotation attributes in libclang?

Rich Leigh rich.leigh at gmail.com
Wed Nov 26 09:50:13 PST 2014

I'm trying to write a python tool using libclang (v 3.5.1) as a parser, and
I'm running into a problem with the annotation attribute on templated

With -ast-dump I can see my annotation on both the vanilla structs and the
templated structs. Using the bindings/python/clang/cindex.py package
however, only vanilla structs have the annotation as a child node, and
templated structs seems to be missing it. I don't think it's the python
binding itself at fault however, it looks like clang_visitChildren never
presents the attribute, but I haven't checked that yet.

If for example testcase.cpp looks like this:

#define MYATTR __attribute__((annotate("hello")))

struct MYATTR Foo {

template <typename>
struct MYATTR Bar {

then using a simple python script like
https://github.com/sztomi/code-generator/blob/master/src/dump_ast.py I get
the following output:

  |  +--ANNOTATE_ATTR hello

On the other hand

clang -Xclang -ast-dump -fsyntax-only -Wall -Weverything /tmp/testcase.cpp

TranslationUnitDecl 0x3a5e550 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x3a5ea90 <<invalid sloc>> <invalid sloc> implicit __int128_t
|-TypedefDecl 0x3a5eaf0 <<invalid sloc>> <invalid sloc> implicit
__uint128_t 'unsigned __int128'
|-TypedefDecl 0x3a5eeb0 <<invalid sloc>> <invalid sloc> implicit
__builtin_va_list '__va_list_tag [1]'
|-CXXRecordDecl 0x3a5efb0 </tmp/testcase.cpp:3:1, line:4:1> line:3:15
struct Foo definition
| |-AnnotateAttr 0x3a5f060 <line:1:31, col:47> "hello"
| `-CXXRecordDecl 0x3a5f110 <line:3:1, col:15> col:15 implicit struct Foo
`-ClassTemplateDecl 0x3aa2360 <line:6:1, line:8:1> line:7:15 Bar
  |-TemplateTypeParmDecl 0x3a5f1a0 <line:6:11> col:11 typename
  `-CXXRecordDecl 0x3aa22d0 <line:7:1, line:8:1> line:7:15 struct Bar
    |-AnnotateAttr 0x3aa25e0 <line:1:31, col:47> "hello"
    `-CXXRecordDecl 0x3aa2630 <line:7:1, col:15> col:15 implicit struct Bar

clang --version
clang version 3.5.1 (https://github.com/llvm-mirror/clang.git
accae81be729aef49eada7c84fd17be68bd049e0) (
Target: x86_64-unknown-linux-gnu
Thread model: posix

I understand from http://clang.llvm.org/docs/Tooling.html#libclang the AST
provided by libclang isn't as detailed as the full internal AST, but my
expectation would be that I'd see the annotation on both structs. I'm not
sure if this is a bug in the python binding, libclang, or if I've just
missed something. Any ideas?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141126/4fce4a9d/attachment.html>

More information about the cfe-dev mailing list