libclang's code completion APIs give unexpected results for Objective-C

Here I have a very small .m file where I have typed “NSL” and would expect the top code completion suggestion to be “NSLog()”:

https://gist.github.com/bolinfest/11236613

Here is my C code that calls libclang and uses it to get suggested code completions:

https://gist.github.com/bolinfest/11236626

Here are the suggestions I get when I run my C code (the command that I ran is ./main.o Hello.m 5 6):

https://gist.github.com/bolinfest/11236652

Some surprising results:

(1) libclang gives me over 5000 suggestions.
(2) NSLog() is in the list, but its priority is 50. There are all sorts of other suggestions that seem less relevant, but have a lower priority (counterintuitively, this means higher probability of being clicked on: http://clang.llvm.org/doxygen/group__CINDEX__CODE__COMPLET.html#ga46e843acdf63d9a7a0c7341a2d222c49). Search for “priority=40” in the gist to see examples of results with a lower priority that seem less relevant.
(3) The order in which things appear in the results appears to be random. I see that clang has a convenience function to sort results alphabetically, but not by priority.

Are there additional arguments that I need to pass to libclang to tune things so that the results I get from clang_codeCompleteAt() are more in line with what I expect?

Thank you,
Michael

In case anyone else runs into this issue, this Stack Overflow post has a lot of helpful information: http://stackoverflow.com/questions/15875800/why-isnt-libclang-returning-meaningful-completion-results.

In short, the line/column information that you pass should point to the start of the token, and it is the caller’s responsibility to filter by the partially complete token.

Also, clang has a flag (-code-completion-at) that makes it convenient to test this API without writing any code; however, it only appears to work with -cc1, not with -ObjC.

Clarification: I misunderstood what -cc1 meant (http://stackoverflow.com/questions/8991662/can-u-tell-me-what-does-the-clang-cc1-option-do cleared that up).

The following is a convenient way to test out the code completion API without writing any code:

clang -cc1 -fsyntax-only -code-completion-at=PATH_TO_M_FILE:LINE:COL PATH_TO_M_FILE