Last active
December 12, 2015 03:18
-
-
Save marionette-of-u/4705748 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "clang-c/Index.h" | |
| #include <cstdlib> | |
| #include <iostream> | |
| #include <fstream> | |
| /* | |
| * Compile with: | |
| * g++ complete.cc -o complete -lclang -L/usr/lib/llvm | |
| * Run with: | |
| * LIBCLANG_TIMING=1 ./complete file.cc line column [clang args...] | |
| */ | |
| /* | |
| * test.cpp | |
| #include <string> | |
| namespace test{ | |
| using integer = int; | |
| using floating = double; | |
| } | |
| int main(){ | |
| hebereke | |
| std:: | |
| return 0; | |
| } | |
| */ | |
| int main() | |
| { | |
| const char *argv[] = { | |
| "dummy", | |
| "test.cpp", | |
| "10", "10" | |
| }; | |
| const int argc = sizeof(argv) / sizeof(*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_None); | |
| 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; | |
| } | |
| std::ofstream ofile("compl-result.txt"); | |
| 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); | |
| ofile << clang_getCString(out); | |
| } | |
| ofile << "\n"; | |
| } | |
| clang_disposeCodeCompleteResults(res); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment