[cfe-dev] libclang and plugins

M.Sc. Stefan Kemnitz kemnitz.stefan at gmail.com
Fri Sep 26 06:50:45 PDT 2014


Hi,

i am working on a plugin for clang that will show me some custom
diagnostics.

I extended the "Hello World"-plugin PrintFunctionNames so that it emits an
errors if it finds a function declaration ( its just a toy plugin )
Everything works fine if i execute it from command line with clang.

-------------------------------------------------------------------------------------
$ clang -Xclang -load -Xclang <pluginpath>/PrintFunctionNames.so -Xclang
-add-plugin -Xclang print-fns main.cpp
top-level-decl: "foo"
/home/incubus/test/clang-plugins/main.cpp:3:1: error: use of undeclared
identifier 'foo'
void foo()
^
/home/incubus/test/clang-plugins/main.cpp:12:5: error: use of undeclared
identifier 'bar'
    bar();
    ^
top-level-decl: "main"
/home/incubus/test/clang-plugins/main.cpp:8:1: error: use of undeclared
identifier 'main'
int main(int argc, char** argv){
^
3 errors generated.
---------------------------------------------------------------------------------------

the first and third error are from my plugin and the second is from clang
itself.

Now i want to do the same thing through libclang.
I am using the vim plugin YouCompleteMe to send all of the flags that were
passed to clang, to libclang.

I followed the path of execution to
clang_parseTranslationUnit -> clang_parseTranslationUnit2 ->
clang_parseTranslationUnit_impl ->
 ASTUnit::LoadFromCommandLine -> ASTUnit::LoadFromCompilerInvocation ->
ASTUnit::Parse

were the parsing process is started.

in FrontendAction::BeginSourceFile  the method
FrontendAction::CreateWrappedASTConsumer is called which creates
ASTConsumers from the plugins.
I found out that the Consumer is not created because the
FrontendPluginRegistry does not contain any plugins.
This is due to the fact that the plugin was never loaded.

I searched the codebase for the loading routines and found out that the
only loading is in clang::ExecuteCompilerInvocation.
I know that clang_parseTranslationUnit is supposed to Parse the translation
unit not to execute the whole compiler.

My question is:

Is there any way to load the plugin earlier so that it can be executed
while parsing?
Or if it is not possible. Is there some other way to add custom error
messages that will be emitted when running clang_parseTranslationUnit?


Greetings Stefan Kemnitz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140926/b960bb85/attachment.html>


More information about the cfe-dev mailing list