Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

llvm

clang

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?

like image 513
samvv Avatar asked Aug 15 '15 11:08

samvv


1 Answers

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.

like image 115
Maxim Egorushkin Avatar answered Oct 22 '22 22:10

Maxim Egorushkin