<div dir="ltr">Anyone? Should i create bug?</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/10/8 Anton Smirnov <span dir="ltr"><<a href="mailto:dev@antonsmirnov.name" target="_blank">dev@antonsmirnov.name</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello.<div><br></div><div>I've found code complete is not working (via libclang-c) for unsaved files (but works great for saved files).</div>
<div><br></div><div>Code (not all):</div><div><br></div><div><div>
// show complete options</div><div>void showComplete(CXTranslationUnit TU, char *src_filename, unsigned filesize, CXUnsavedFile *file, int line, int column)</div><div>{</div><div>    fprintf(stderr, "TU=%p, file=%s complete at line=%i, column=%i\n", &TU, src_filename, line, column);</div>

<div><br></div><div>    CXCodeCompleteResults *results =</div><div>        clang_codeCompleteAt(TU, src_filename, line, column, file, (file == NULL ? 0 : 1), clang_defaultCodeCompleteOptions());</div><div><br></div><div>
    if (results) {</div>
<div>        fprintf(stderr, "results=[%p, count=%i]\n", results->Results, results->NumResults);</div><div><br></div><div>        /* Sort the code-completion results based on the typed text. */</div><div>
      clang_sortCodeCompletionResults(results->Results, results->NumResults);</div>
<div><br></div><div>      for (int i = 0; i < results->NumResults; i++)</div><div>        print_completion_result(results->Results + i, stdout);</div><div>    } else {</div><div>        fprintf(stderr, "no complete results\n");</div>

<div>    }</div><div><br></div><div>    clang_disposeCodeCompleteResults(results);</div><div>}</div><div><br></div><div>int main(int argc, char** argv)</div><div>{</div><div>    std::cout << "Starting ----" << std::endl;</div>

<div><br></div><div>    CXIndex index = clang_createIndex(false, false);</div><div>    char * filename = argv[1];</div><div><br></div><div>    fprintf(stderr, "\ncommand-line file: %s =============\n", filename);</div>

<div>    CXTranslationUnit commandLineTu = clang_parseTranslationUnit(index, filename, 0, 0, 0, 0, CXTranslationUnit_None);</div><div>    showComplete(commandLineTu, filename, 0, NULL, 1, 72);</div><div>    clang_disposeTranslationUnit(commandLineTu);</div>

<div><br></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 TempStruct { int a; bool b; }; int main() { TempStruct str; str. /*complete here*/ return 1; }";</div><div>    CXTranslationUnit in_memory_tu = clang_parseTranslationUnit(index, unsavedFile->Filename, NULL, 0, unsavedFile, 1, CXTranslationUnit_None);</div>

<div>    int column = strchr(unsavedFile->Contents, '.') - unsavedFile->Contents + 1 + 1; // + 1 (length of '.') + 1 (next symbol)</div><div>    showComplete(in_memory_tu, (char*)unsavedFile->Filename, strlen(unsavedFile->Contents), unsavedFile, 1, column);</div>

<div><br></div><div>    clang_disposeTranslationUnit(in_memory_tu);</div><div>    clang_disposeIndex(index);</div><div><br></div><div>    return 0;</div><div>}</div></div><div><br></div><div>Source file (saved file, is exactly equals to unsaved file content):</div>

<div>struct TempStruct { int a; bool b; }; int main() { TempStruct str; str. /*complete here*/ return 1; }<br></div><div><br></div><div>Output:</div><div><div>Starting ----</div><div><br></div><div>command-line file: ../test/test_complete.cpp =============</div>

<div>TU=0x7fff5d75aaf0, file=../test/test_complete.cpp complete at line=1, column=72</div><div>results=[0x7fec7b509510, count=5]</div><div>FieldDecl:{ResultType int}{TypedText a} (35)</div><div>FieldDecl:{ResultType bool}{TypedText b} (35)</div>

<div>CXXMethod:{ResultType TempStruct &}{TypedText operator=}{LeftParen (}{Placeholder const TempStruct &}{RightParen )} (34)</div><div>StructDecl:{TypedText TempStruct}{Text ::} (75)</div><div>CXXDestructor:{ResultType void}{TypedText ~TempStruct}{LeftParen (}{RightParen )} (34)</div>

<div><br></div><div>unsaved file =============</div><div>TU=0x7fff5d75aaf0, file=temp.cpp complete at line=1, column=72</div><div>results=[0x7fec7b5043d0, count=0]</div><div><br></div></div><div><br></div><div>Any thoughts what's wrong?</div>
<span class="HOEnZb"><font color="#888888">
<div><br></div><div>Anton</div></font></span></div>
</blockquote></div><br></div>