public
Last active

Hacking with libclang completion.

  • Download Gist
complete.cc
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <clang-c/Index.h>
#include <cstdlib>
#include <iostream>
 
/*
* Compile with:
* g++ complete.cc -o complete -lclang -L/usr/lib/llvm
* Run with:
* LIBCLANG_TIMING=1 ./complete file.cc line column [clang args...]
*/
 
int main(int argc, char** argv)
{
if (argc < 4) {
std::cout << argv[0] << " file.cc line colum [clang args...]"
<< std::endl;
return 1;
}
 
CXIndex idx = clang_createIndex(1, 0);
if (!idx) {
std::cerr << "createIndex failed" << std::endl;
return 2;
}
 
CXTranslationUnit u = clang_parseTranslationUnit(idx, argv[1], argv + 4,
argc - 4, 0, 0,
CXTranslationUnit_PrecompiledPreamble
| CXTranslationUnit_CXXPrecompiledPreamble);
 
if (!u) {
std::cerr << "parseTranslationUnit failed" << std::endl;
return 2;
}
 
clang_reparseTranslationUnit(u, 0, 0, 0);
 
int line = strtol(argv[2], 0, 10);
int column = strtol(argv[3], 0, 10);
CXCodeCompleteResults* res = clang_codeCompleteAt(u, argv[1],
line, column,
0, 0, 0);
if (!res) {
std::cerr << "Could not complete" << std::endl;
return 2;
}
 
for (unsigned i = 0; i < clang_codeCompleteGetNumDiagnostics(res); i++) {
const CXDiagnostic& diag = clang_codeCompleteGetDiagnostic(res, i);
const CXString& s = clang_getDiagnosticSpelling(diag);
std::cout << clang_getCString(s) << std::endl;
}
 
for (unsigned i = 0; i < res->NumResults; i++) {
const CXCompletionString& str = res->Results[i].CompletionString;
 
for (unsigned j = 0; j < clang_getNumCompletionChunks(str); j++) {
if (clang_getCompletionChunkKind(str, j) != CXCompletionChunk_TypedText)
continue;
 
const CXString& out = clang_getCompletionChunkText(str, j);
std::cout << clang_getCString(out);
}
 
std::cout << std::endl;
}
clang_disposeCodeCompleteResults(res);
 
return 0;
}

aha, this is the basic usage of clang's code completion. nice!!
maybe the c-index-test has more.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.