tweekmonster / deoplete-clang2

MIT License
90 stars 16 forks source link

`libclang` versus `clang -cc1` #2

Open phcerdan opened 7 years ago

phcerdan commented 7 years ago

Hi there, thanks for the plugin. I am trying to search a little bit about what is the difference between using libclang or using the clang front end clang -cc1? And only this FAQ.

I've found libclang unreliable with heavy templated code in c++, and happy to explore alternatives.

Any clarification, or link to information would be appreciated. Thanks!

tweekmonster commented 7 years ago

Hello! clang is a driver. It knows what to do to compile a program, including calling itself.

clang -cc1 is a frontend for specific features. To a user, it's useful for analyzing specific files, but you're limited to commands that are useful to the driver. You can see its flags with clang -cc1 --help | less

libclang is a library that can be used by programs to access its functions via FFI. A Python script could call the functions directly instead of requiring a Python implementation of Clang. This is useful if you want to analyze files in a way that's not possible using clang -cc1. For example, it's much easier to walk through a file's AST to find a specific function using libclang vs using clang -cc1 -ast-dump.

I've only gone ankle deep in C++, so I couldn't say how to improve completions for it. All I can tell you is that accurate completions boils down to parsing and presenting completions that are contextual for the current position in a file. But, that depends on how well clang understands the current source and the files it requires, which in turn depends on you. From my experience, bad/missing completions happen when generated headers are missing or not defining variables with -D.

If you're looking for another option, you could try the nvim-langserver-shim created by the venerable @tjdevries. There's a C++ langserver provided by Microsoft, but it looks like you'll need VS Code running.

phcerdan commented 7 years ago

Thanks a lot for the explanation @tweekmonster. So, what is the advantage of using clang -cc1 in a completer, more flexibility?

I think my only problem with libclang is that I would like completion in headers with templates. And not really supported. neoinclude sounds that might help, but I haven't tried it yet.

tweekmonster commented 7 years ago

So, what is the advantage of using clang -cc1 in a completer, more flexibility?

For this plugin, it's simply faster for as-you-type completions. The completions provided by clang -cc1 were the same that were provided by deoplete-clang through libclang's Python bindings. I'm not sure why deoplete-clang was so slow, but I'm guessing there's a penalty for conversions to Python types, which is exaggerated by the sheer amount of completions.

It also helps that using clang -cc1 was much easier to implement.

I think my only problem with libclang is that I would like completion in headers with templates.

Maybe I could help if you post an examples, and explain what does and doesn't work.

neoinclude sounds that might help, but I haven't tried it yet.

This reminded me of a feature I wanted to add. 58079e52aa29357b7a272a6edf0cc5abb7344100

phcerdan commented 7 years ago

I will try to post a minimal example of what isn't working in headers next weekend. Thanks!