Is it possible to get function parameters for offset with Clang?

I’m working on code complete and i’d like to let the user see function parameters when he types function name or even already typed first parameters:

source code:

void func(int a, int b) {
}

user types:

func(/cursor here/

or he forgots the second argument type:

func(1, /cursor here/
and presses “show arguments”

is it possible to find according function(s) using Clang?

I’ve tried to find cursor and show info, but cursor kind is CompoundStmt and i can’t find any function cursor for it.

source code:

CXIndex index2 = clang_createIndex(false, true);

// unsaved file tu
fprintf(stderr, “\nunsaved file =============\n”);
CXUnsavedFile *unsavedFile = new CXUnsavedFile;
unsavedFile->Filename = “./temp.cpp”;
unsavedFile->Contents = “struct s { void func(int a) { return 1; }; }; void main() {s var; var.func(); }”;

unsavedFile->Length = strlen((char*)unsavedFile->Contents);

CXTranslationUnit in_memory_tu = clang_parseTranslationUnit(index2, unsavedFile->Filename, 0, 0, unsavedFile, 1,
CXTranslationUnit_None | CXTranslationUnit_Incomplete);
int offset = atoi(argv[1]);
showArguments(in_memory_tu, (char *)unsavedFile->Filename, offset); //line=1, column=74

clang_disposeTranslationUnit(in_memory_tu);
clang_disposeIndex(index2);

// show arguments
void showArguments(CXTranslationUnit TU, char *src_filename, int offset)
{
fprintf(stderr, “TU=%p, file=%s offset==%i\n”, &TU, src_filename, offset);
printSourceFileName(“check TU filename”, TU);

CXFile file = clang_getFile(TU, src_filename);
CXSourceLocation sourceLocation = clang_getLocationForOffset(TU, file, offset);
CXCursor cursor = clang_getCursor(TU, sourceLocation);

if (cursor.kind == CXCursor_InvalidCode || cursor.kind == CXCursor_InvalidFile) {
fprintf(stderr, “NULL sursor found at %i\n”, offset);
} else {
CXCursorKind kind = clang_getCursorKind(cursor);
CXString kindSpelling = clang_getCursorKindSpelling(kind);
CXString spelling = clang_getCursorSpelling(cursor);
CXString name = clang_getCursorDisplayName(cursor);

fprintf(stderr, “cursor found: kind=%i (%s) spelling=%s, name=%s\n”,
kind, clang_getCString(kindSpelling), clang_getCString(spelling), clang_getCString(name));

clang_disposeString(kindSpelling);
clang_disposeString(name);
clang_disposeString(spelling);
}
}

output:

MBA-Anton:build asmirnov$ ./clang_complete_function 76 Starting ----

unsaved file ============= ./temp.cpp:1:31: error: void function
‘func’ should not return a value [-Wreturn-type] ./temp.cpp:1:47:
error: ‘main’ must return ‘int’ ./temp.cpp:1:76: error: too few
arguments to function call, single argument ‘a’ was not specified
./temp.cpp:1:12: note: ‘func’ declared here TU=0x7fff5dc11b58,
file=./temp.cpp offset==76 check TU filename: [./temp.cpp] cursor
found: kind=202 (CompoundStmt) spelling=, name=

Regards, Anton.

Hi Anton,

I'm working on code complete and i'd like to let the user see function parameters when he types function name or even already typed first parameters:

source code:

    void func(int a, int b) {
    }

user types:

    func(/*cursor here*/

or he forgots the second argument type:

    func(1, /*cursor here*/
and presses "show arguments"

is it possible to find according function(s) using Clang?

I've tried to find cursor and show info, but cursor kind is `CompoundStmt` and i can't find any function cursor for it.

This would be a really cool feature! If you are interested on working on it I’d suggest that the way to go is by enhancing our code completion machinery.
Basically, we should have Sema completion also report the function call context (which function is getting called, which is the index of the parameter we are completing in, etc.), add it to the code-completion data and expose it through libclang.

What do you think ?

Hello, Argyrios.

I’m android developer and unfortunately i am not skilled enough to develop compiler using c/cpp.
So AFAIK there is no such possibility now?

That has never stopped me before :). Have some faith.

Not sure that is good idea as it’s completely different language, design approaches and tools.
Let it be developed by professionals to be really great tool that i almost love.

The thing is that with open source projects, if you want something get done, you will most likely have do it yourself.

If you contribute with the code there will be professionals (or developers who know the code base) to review your changes. This will also help you become a better programmer.

I do know the feeling that I can better spend my time somewhere else where I know what I'm doing.

I will think about it. I hope i will have much more free time in few month…

Just to know: my dev way: Borland Delphi → Borland C++ Builder → .net via C# → J2EE → Android and
it’s not a problem for me to learn smth - it’s a question of priorities

Priority is a completely different thing.