I have cloned the llvm-project repository.
Then I have generated a Visual Studio solution for the project using:
cmake -G "Visual Studio 16" -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS=ON -DLLVM_TARGETS_TO_BUILD=X86 -DCLANG_BUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=install ...\llvm-project\llvm
The important part is DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS, as suggested here:
https://llvm.discourse.group/t/how-to-create-pass-independently-on-windows/474/5
The solution and all the projects are generated. Then I build Clang itself and an example clang plugin that is already provided in the llvm-project source. The PrintFunctionNames seem to be the hello world of Clang plugins, so I have built that, according to the llvm documentation.
The build successfully ran, and now I have llvm/clang in the install dir, with the PrintFunctionNames plugin.
The problem is that I can not invoke the plugin. As far as I know, there is 3 ways to tell calng to use your plugin.
- The cc1 interface:
I can call my plugin with:
clang -cc1 -load PrintFunctionNames.dll -plugin print-fns test.cpp
This only works with clang and won’t work with clang++. Moreover, clang won’t find the standard headers, but that is expected behaviour with cc1 according to the documentation.
- The -Xclang interface:
I call my plugin with:
clang -Xclang -load -Xclang PrintFunctionNames.dll -Xclang -plugin -Xclang print-fns test.cpp
This works aswell, and since Xclang appends to the cc1, standard library headers are found too. However, calling clang++ instead of clang won’t work here either. Clang++ outputs an error: unable to find plugin 'print-fns'.
- The fplugin interface:
I have added the necessary code according to the documentation:
[https://clang.llvm.org/docs/ClangPlugins.html](https://clang.llvm.org/docs/ClangPlugins.html)
// Automatically run the plugin after the main AST action
PluginASTAction::ActionType getActionType() override {
return AddAfterMainAction;
}
After that, I can run the plugin with:
clang -fplugin=PrintFunctionNames
.dll test.cpp
This runs as expected, but once again, clang++ wont run the plugin. Moreover, no error messages are generated, clang++ just refuses to run the plugin.
I do not understand why clang seems to work fine with plugins, but clang++ refuses to run any. I have tried all the provided example plugins, none of them works with clang++, while all of them works as expected with clang.
I have tested both the latest version and llvm 11 too, I am using windows 10.
I would really appreciate any insights as to why clang++ would behave differently when it comes to plugins.