Search code examples
compiler-constructionllvmllvm-clang

FunctionPass in LLVM for C++ input


I am trying to print all functions' names in my C/C++ code by parsing the code through LLVM. I referred the example given here http://llvm.org/docs/WritingAnLLVMPass.html#basic-code-required The example works fine for C code input but for C++ input, it displays following function names in a simple Hello World program

__cxx_global_var_init

main

GLOBAL_I_a

Then I found that LLVM has a certain issue with iostream as explained here: http://llvm.org/docs/FAQ.html#what-is-this-llvm-global-ctors-and-global-i-a-stuff-that-happens-when-i-include-iostream

I removed iostream from my code and then I got only "main" as output.

But for a multi-function C++ program that lets say contains the following functions: convertperm, findType and main (does not have iostream), I got the following output

_Z11convertpermSs

_Z8findtypeSs

main

Multi-function C programs work properly

Can anyone provide a fix to this problem?


Solution

  • Welcome to the wonderful world of C++ name mangling :)

    Your front-end (Clang, I'm assuming) has renamed these functions, a process called mangling. If you want to get the original function name back, you'll need to demangle the mangled names. You can read the linked article to learn more about why and how mangling and demangling are done.

    As far as I know there's no built-in C++ demangler in LLVM, but you can use an external one, for instance libstdc++'s abi::__cxa_demangle.