Note: I am using LLVM and CLANG 2.9
I want to start with someone basic. Like printing out all the function or class names. Or maybe all variables in a struct that is a type int. Anyways to get started i look at
llvm/tools/clang/examples. I see clang-interpreter, in main.cpp theres nothing including AST and it seems like not what i want. The example PrintFunctionNames looks exactly what i want but theres no main(). I read it and it seems like HandleTopLevelDecl is the key function. I see instructions on how to use this on linux/mac but i rather do it on windows since i like debugging in VS.
Moving on i looked at clang-wpa.cpp which seems to kickstart an analyzer/debugger/something but i cant build it! I have missing files and such. One such error is “error C2039: ‘LAST_INHERITABLE’ : is not a member of ‘clang::attr’”. Others i mention in a post here http://stackoverflow.com/questions/6326453/ Is the examples outdated? i am afraid to grab the main branch for more troubles. Does the example work on linux? and does it work on 2.9 tag or do i need to use a branch? (which?)
Its not the prettiest code ever, but I wrote something that gets function names, function calls and function pointers. I just modified the PrintFunctions example.
https://gist.github.com/1027771 or see the attachment
With PrintFunctionsConsumer you just implement the functions you want to use. You can also make a constructor/destructor like normal.
PrintFunctionNames.cpp (2.99 KB)
I had the idea that the missing headers may be somewhere in the build folder and sure enough there they were.
I was able to build the wpa and PrintFunctionNames example. Looking in the readme of PrintFunctionNames i saw
clang -cc1 -load …/…/Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns some-input-file.c
so i ran
clang -cc1 -load clangPlugin.dll -plugin printfns a.c
error: unable to find plugin ‘print-fns’
I suspect it involves visual studios and exporting with its DLL. I included the ‘exports’ file and it didnt help. It said “_ZN4llvm8Registry*” which is weird. I suspect i am suppose to export “FrontendPluginRegistry::Add”. So i threw __declspec(dllexport) in front of it which it complained about being static. I remove static and it compile fine but i still get that unable to find plugin error. If i use an incorrect dll file name it says it cant find the module so i know the dll name is correct. I just dont know how to call it/export it properly
Also thanks for that edit retval, i’ll want to run it but the problem above prevents me from doing so.