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

Anton Smirnov dev at antonsmirnov.name
Thu Jan 9 11:36:03 PST 2014


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?


2014/1/10 Argyrios Kyrtzidis <akyrtzi at gmail.com>

> Hi Anton,
>
> On Jan 9, 2014, at 4:21 AM, Anton Smirnov <dev at antonsmirnov.name> wrote:
>
> > 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 ?
>
> >
> > 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.
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140110/ed0fc355/attachment.html>


More information about the cfe-dev mailing list