Skip to content

Instantly share code, notes, and snippets.

@bolinfest
Created April 23, 2014 23:56
Show Gist options
  • Save bolinfest/11236626 to your computer and use it in GitHub Desktop.
Save bolinfest/11236626 to your computer and use it in GitHub Desktop.
main.c
#include <clang-c/Index.h>
#include <stdio.h>
#include <stdlib.h>
void printCodeCompletionSuggestions(CXTranslationUnit tu, char* source, unsigned line, unsigned column) {
struct CXUnsavedFile *unsavedFiles = NULL;
unsigned numUnsavedFiles = 0;
unsigned options = clang_defaultCodeCompleteOptions();
CXCodeCompleteResults *results = clang_codeCompleteAt(tu, source, line, column, unsavedFiles, numUnsavedFiles, options);
unsigned numResults = results->NumResults;
printf("Num completions: %d\n", numResults);
CXCompletionResult *result = results->Results;
for (unsigned i = 0; i < numResults; i++) {
CXCompletionString completionString = result[i].CompletionString;
unsigned priority = clang_getCompletionPriority(completionString);
// With the cursor at the end of this line:
//
// NSL
//
// The priority of NSLog is 50, which seems much higher than it should be.
if (priority > 50) {
continue;
}
unsigned numChunks = clang_getNumCompletionChunks(completionString);
unsigned numAnnotations = clang_getCompletionNumAnnotations(completionString);
printf("Result %d (priority=%d, chunks=%d, annotations=%d)\n", i, priority, numChunks, numAnnotations);
for (unsigned chunkNumber = 0; chunkNumber < numChunks; chunkNumber++) {
CXString chunk = clang_getCompletionChunkText(completionString, chunkNumber);
enum CXCompletionChunkKind kind = clang_getCompletionChunkKind(completionString, chunkNumber);
printf("%s kind=%d\n", clang_getCString(chunk), kind);
clang_disposeString(chunk);
}
printf("\n");
}
clang_disposeCodeCompleteResults(results);
}
int main(int argc, char **argv) {
char* source = argv[1];
unsigned line = atoi(argv[2]);
unsigned column = atoi(argv[3]);
CXIndex index = clang_createIndex(
/* excludeDeclarationsFromPCH */ 0,
/* displayDiagnostics */ 0);
const char *args[] = {
"-isysroot",
"/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk",
};
int numArgs = sizeof(args) / sizeof(*args);
CXTranslationUnit tu = clang_parseTranslationUnit(index, source, args, numArgs, NULL, 0, CXTranslationUnit_None);
printCodeCompletionSuggestions(tu, source, line, column);
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment