How to get a list of all global declarations of a

2019-02-17 06:45发布

问题:

I'm trying to write a program that lists all of the publicly exported variables and functions of a C or C++ program by using Clang.

I followed part 05 of this tutorial, but it doesn't work for current version of clang. Above that, I got some hints that CompilerInstance can make the code shorter, but I'm not entirely sure how to use it.

How would you implement this functionality? Can you give me any pointers into the right direction? For example: is there a large hash table of globally declared variables or do I have to traverse the AST?

回答1:

publicly exported - do you mean symbols with external linkage?

You can use nm on object files, shared libraries and executables with --extern-only --defined-only --demangle options to display defined symbols with external linkage. Symbol type field tells you what kind of symbol it is, global variables normally have types B, b, C, D, d, G, g, R, r, S, s and W, w for static data members of templates declared in header files and function-scope statics.



回答2:

I've found a way. The result is AutoFFI, an FFI bindings generator built on top of Clang. I'll post the details on how it is done if I have the time.



标签: c++ c clang llvm