KDev-Clang - Support for Missing Includes

Milian Wolff mail at milianw.de
Tue Mar 4 11:37:15 UTC 2014


On Sunday 02 March 2014 21:52:39 Todd Nowacki wrote:
> Hi Milian,

Hey Todd,

sorry for the long delay.

> I'm currently working on implementing "missing include" support for
> kdev-clang for open academy, and I had a few questions.

Awesome!

> It seems like we can use mostly everything in the original
> cpp/codecompletion/missingincludemodel.cpp. The only trouble spot starts at
> line 190, with the usage of missingIncludeCompletionItems (which is
> impelemented in missingincludeitem.cpp)
> 
> It seems that we would want to replace a lot of the functionality in
> missingincludeitem.cpp with functions from the clang-c API.
> 
> Do you have any advice on what should be pulled in from
> missingincludemodel/missingincludeitem and what should be re-implemented?

Hm looking at that stuff, I think we don't even need most of the model. The 
model seems to start a parse job to update the duchain, but that is done very 
efficiently in clang code completion already anyways (see clang_codeCompleteAt 
call in context.cpp).

The oldcpp code in general is far from clean and easy to read, so maybe we 
should just take it for inspiration and do a clean-house implementation based 
on clang?

There are basically two usecases:

- add an include on the use of a forward-declaration
- add an include on the use of a non-declared class/function/...

I would say we should concentrate first on the former. After the clang 
information was updated via clang_codeCompleteAt, you should be able to get 
access to the CXCursor at the point where code completion was invoked. Then 
figure out somehow whether that is a use of a forward declaration (for both, 
I'm not sure how to do it, but I hope theres something in clang-c API for 
that).

Once you have found that this is the case, the next job is to find the proper 
include where the declaration is at. That is done via 
PersistentSymbolTable::self().declarations( id, declarationCount, 
declarations) in oldcpp's missingIncludeCompletionItems. There is some boiler-
plate code required around it to support the non-declared 
classes/functions/... by guessing the possible QualifiedIdentifier such that 
we can look for it in the symbol table. But that could/should be done in a 
second step I think.

Then once an include is found, we need to create code completion items, see 
the existing CompletionItem class in context.cpp for inspiration here, as well 
as the MissingIncludeCompletionItem. Generally, on execution, the #include 
should be added at a somewhat sorted place, see the existing code for 
inspiration again.

Generally, I hope you can make this work. I'd say its crucial here to write a 
unit test suite to ensure everything is working properly. This should be done 
before everything else. I.e. a simple "run code completion, compare resulting 
items, execute and see what happens" test. Then implement the above new 
completion and check whether that creates the proper includes etc. pp.

Good luck!

-- 
Milian Wolff
mail at milianw.de
http://milianw.de


More information about the KDevelop-devel mailing list