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()”:

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

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

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: 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,

In case anyone else runs into this issue, this Stack Overflow post has a lot of helpful information:

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