<div dir="ltr"><div>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:</div><div><br></div><div>source code:</div><div>
<br></div><div> void func(int a, int b) {</div><div> }</div><div><br></div><div>user types:</div><div><br></div><div> func(/*cursor here*/</div><div><br></div><div>or he forgots the second argument type:</div><div>
<br></div><div> func(1, /*cursor here*/</div><div>and presses "show arguments"</div><div><br></div><div><br></div><div>is it possible to find according function(s) using Clang?</div><div><br></div><div>I've tried to find cursor and show info, but cursor kind is `CompoundStmt` and i can't find any function cursor for it.</div>
<div><br></div><div>source code:</div><div><br></div><div> CXIndex index2 = clang_createIndex(false, true);</div><div> </div><div> // unsaved file tu</div><div> fprintf(stderr, "\nunsaved file =============\n");</div>
<div> CXUnsavedFile *unsavedFile = new CXUnsavedFile;</div><div> unsavedFile->Filename = "./temp.cpp";</div><div> unsavedFile->Contents = "struct s { void func(int a) { return 1; }; }; void main() {s var; var.func(); }";</div>
<div> </div><div> unsavedFile->Length = strlen((char*)unsavedFile->Contents);</div><div> </div><div> CXTranslationUnit in_memory_tu = clang_parseTranslationUnit(index2, unsavedFile->Filename, 0, 0, unsavedFile, 1,</div>
<div> CXTranslationUnit_None | CXTranslationUnit_Incomplete);</div><div> int offset = atoi(argv[1]);</div><div> showArguments(in_memory_tu, (char *)unsavedFile->Filename, offset); //line=1, column=74</div>
<div> </div><div> clang_disposeTranslationUnit(in_memory_tu);</div><div> clang_disposeIndex(index2);</div><div> </div><div> // show arguments</div><div> void showArguments(CXTranslationUnit TU, char *src_filename, int offset)</div>
<div> {</div><div> fprintf(stderr, "TU=%p, file=%s offset==%i\n", &TU, src_filename, offset);</div><div> printSourceFileName("check TU filename", TU);</div><div> </div><div> CXFile file = clang_getFile(TU, src_filename);</div>
<div> CXSourceLocation sourceLocation = clang_getLocationForOffset(TU, file, offset);</div><div> CXCursor cursor = clang_getCursor(TU, sourceLocation);</div><div> </div><div> if (cursor.kind == CXCursor_InvalidCode || cursor.kind == CXCursor_InvalidFile) {</div>
<div> fprintf(stderr, "NULL sursor found at %i\n", offset);</div><div> } else {</div><div> CXCursorKind kind = clang_getCursorKind(cursor);</div><div> CXString kindSpelling = clang_getCursorKindSpelling(kind);</div>
<div> CXString spelling = clang_getCursorSpelling(cursor);</div><div> CXString name = clang_getCursorDisplayName(cursor);</div><div> </div><div> fprintf(stderr, "cursor found: kind=%i (%s) spelling=%s, name=%s\n",</div>
<div> kind, clang_getCString(kindSpelling), clang_getCString(spelling), clang_getCString(name));</div><div> </div><div> clang_disposeString(kindSpelling);</div><div> clang_disposeString(name);</div>
<div> clang_disposeString(spelling);</div><div> }</div><div> }</div><div><br></div><div>output:</div><div><br></div><div>> MBA-Anton:build asmirnov$ ./clang_complete_function 76 Starting ----</div><div>
> </div><div>> unsaved file ============= ./temp.cpp:1:31: error: void function</div><div>> 'func' should not return a value [-Wreturn-type] ./temp.cpp:1:47:</div><div>> error: 'main' must return 'int' ./temp.cpp:1:76: error: too few</div>
<div>> arguments to function call, single argument 'a' was not specified</div><div>> ./temp.cpp:1:12: note: 'func' declared here TU=0x7fff5dc11b58,</div><div>> file=./temp.cpp offset==76 check TU filename: [./temp.cpp] cursor</div>
<div>> found: kind=202 (CompoundStmt) spelling=, name=</div><div><br></div><div>Regards, Anton.</div></div>