2013-10-08 5 views
1

저장되지 않은 파일에 대해 코드 완료가 작동하지 않습니다 (libclang-c를 통해). 그러나 저장된 파일에는 효과적입니다.저장되지 않은 파일에 대해 Clang이 완료되지 않습니까?

코드 (모든) :

// show complete options 
void showComplete(CXTranslationUnit TU, char *src_filename, unsigned filesize, CXUnsavedFile *file, int line, int column) 
{ 
    fprintf(stderr, "TU=%p, file=%s complete at line=%i, column=%i\n", &TU, src_filename, line, column); 

    CXCodeCompleteResults *results = 
     clang_codeCompleteAt(TU, src_filename, line, column, file, (file == NULL ? 0 : 1), clang_defaultCodeCompleteOptions()); 

    if (results) { 
     fprintf(stderr, "results=[%p, count=%i]\n", results->Results, results->NumResults); 

     /* Sort the code-completion results based on the typed text. */ 
     clang_sortCodeCompletionResults(results->Results, results->NumResults); 

     for (int i = 0; i < results->NumResults; i++) 
     print_completion_result(results->Results + i, stdout); 
    } else { 
     fprintf(stderr, "no complete results\n"); 
    } 

    clang_disposeCodeCompleteResults(results); 
} 

int main(int argc, char** argv) 
{ 
    std::cout << "Starting ----" << std::endl; 

    CXIndex index = clang_createIndex(false, false); 
    char * filename = argv[1]; 

    fprintf(stderr, "\ncommand-line file: %s =============\n", filename); 
    CXTranslationUnit commandLineTu = clang_parseTranslationUnit(index, filename, 0, 0, 0, 0, CXTranslationUnit_None); 
    showComplete(commandLineTu, filename, 0, NULL, 1, 72); 
    clang_disposeTranslationUnit(commandLineTu); 

    // unsaved file tu 
    fprintf(stderr, "\nunsaved file =============\n"); 
    CXUnsavedFile *unsavedFile = new CXUnsavedFile; 
    unsavedFile->Filename = "temp.cpp"; 
    unsavedFile->Contents = "struct TempStruct { int a; bool b; }; int main() { TempStruct str; str. /*complete here*/ return 1; }"; 
    CXTranslationUnit in_memory_tu = clang_parseTranslationUnit(index, unsavedFile->Filename, NULL, 0, unsavedFile, 1, CXTranslationUnit_None); 
    int column = strchr(unsavedFile->Contents, '.') - unsavedFile->Contents + 1 + 1; // + 1 (length of '.') + 1 (next symbol) 
    showComplete(in_memory_tu, (char*)unsavedFile->Filename, strlen(unsavedFile->Contents), unsavedFile, 1, column); 

    clang_disposeTranslationUnit(in_memory_tu); 
    clang_disposeIndex(index); 

    return 0; 
} 

소스 파일 (저장된 파일은 저장되지 않은 파일 내용에 동일 정확히) :

struct TempStruct { int a; bool b; }; int main() { TempStruct str; str. /*complete here*/ return 1; } 

출력 :

 
Starting ---- 

command-line file: ../test/test_complete.cpp ============= 
TU=0x7fff5d75aaf0, file=../test/test_complete.cpp complete at line=1, column=72 
results=[0x7fec7b509510, count=5] 
FieldDecl:{ResultType int}{TypedText a} (35) FieldDecl:{ResultType bool}{TypedText b} (35) 
CXXMethod:{ResultType TempStruct &}{TypedText operator=}{LeftParen(}{Placeholder const tempStruct &}{RightParen)} (34) 
StructDecl:{TypedText TempStruct}{Text ::} (75) 
CXXDestructor:{ResultType void}{TypedText ~TempStruct}{LeftParen(}{RightParen)} (34) 

unsaved file ============= 
TU=0x7fff5d75aaf0, file=temp.cpp complete at line=1, column=72 
results=[0x7fec7b5043d0, count=0] 

어떤 생각을 뭐가 문제 야?

답변

0

unsavedFile-> Length를 설정하는 것을 잊어 버렸습니다. 소스 파일조차도 암시 적으로 길이를 설정해야하는 텍스트 파일입니다 (fe. unsavedFiles->Length = strlen(unsavedFile->Contents);)

관련 문제