[cfe-dev] Is it possible to get function parameters for offset with Clang?

Anton Smirnov dev at antonsmirnov.name
Thu Jan 9 04:21:06 PST 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140109/b08b6eb9/attachment.html>


More information about the cfe-dev mailing list