[cfe-dev] libclang and plugins

Manuel Klimek klimek at google.com
Mon Sep 29 01:57:59 PDT 2014


On Fri Sep 26 2014 at 8:39:37 PM M.Sc. Stefan Kemnitz <
kemnitz.stefan at gmail.com> wrote:

> 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?
>

I think the right solution is to add a way to load plugins when running
with libclang.
I see 2 options:
1. add a call to libclang to allow loading of plugins
+ less complexity in the driver
- libclang tools need to get explicit support for this
2. make the driver allow loading clang plugins
+ "just works" with any editor integration
- more complexity in the driver

I'm leaning towards (2). If you want to give implementing it a try, I'd be
happy to review patches...

Cheers,
/Manuel


>
>
> Greetings Stefan Kemnitz
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140929/11d9517b/attachment.html>


More information about the cfe-dev mailing list