How to get a list of all global declarations of a C/C++ program using Clang?

喜欢而已 提交于 2020-01-11 05:19:51

问题


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.



来源:https://stackoverflow.com/questions/32024115/how-to-get-a-list-of-all-global-declarations-of-a-c-c-program-using-clang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!