Clean up c-index-test command line usage a bit by requiring a "-test-load-tu" option when testing clang_loadTranslationUnit(). Running c-index-test without the correct arguments now also prints a useful summary of its correct usage.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89115 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 3b0e4c8..c6fb51a 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -215,67 +215,80 @@
fprintf(file, "\n");
}
-void perform_code_completion(int argc, const char **argv) {
+int perform_code_completion(int argc, const char **argv) {
const char *input = argv[1];
char *filename = 0;
unsigned line;
unsigned column;
CXIndex CIdx;
+ int errorCode;
input += strlen("-code-completion-at=");
- if (parse_file_line_column(input, &filename, &line, &column))
- return;
+ if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
+ return errorCode;
CIdx = clang_createIndex(0, 0);
clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
filename, line, column, &print_completion_result, stdout);
clang_disposeIndex(CIdx);
free(filename);
+
+ return 0;
}
-/*
- * First sign of life:-)
- */
-int main(int argc, char **argv) {
- if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) {
- perform_code_completion(argc, (const char **)argv);
- return 0;
- }
-
-
- if (argc != 3) {
- printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
- return 0;
- }
- {
+int perform_test_load_tu(const char *file, const char *filter) {
CXIndex Idx;
CXTranslationUnit TU;
enum CXCursorKind K = CXCursor_NotImplemented;
-
- Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0,
+ enum CXCursorKind *ck = &K;
+ Idx = clang_createIndex(/* excludeDeclsFromPCH */
+ !strcmp(filter, "local") ? 1 : 0,
/* displayDiagnostics */ 1);
- TU = clang_createTranslationUnit(Idx, argv[1]);
-
+ TU = clang_createTranslationUnit(Idx, file);
+
if (!TU) {
- fprintf(stderr, "Unable to load translation unit!\n");
+ fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
return 1;
}
-
- if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
- clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
- clang_disposeTranslationUnit(TU);
- return 1;
- }
+
/* Perform some simple filtering. */
- if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
- else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
- else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
- else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
- else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
-
- clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
- clang_disposeTranslationUnit(TU);
- return 1;
+ if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
+ else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
+ else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
+ else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
+ else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
+ else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
+ else {
+ fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
+ return 1;
}
+
+ clang_loadTranslationUnit(TU, TranslationUnitVisitor, ck);
+ clang_disposeTranslationUnit(TU);
+ return 0;
+}
+
+static void print_usage(void) {
+ fprintf(stderr,
+ "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
+ " c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
+ " <symbol filter> options for -test-load-tu:\n%s",
+ " all - load all symbols, including those from PCH\n"
+ " local - load all symbols except those in PCH\n"
+ " category - only load ObjC categories (non-PCH)\n"
+ " interface - only load ObjC interfaces (non-PCH)\n"
+ " protocol - only load ObjC protocols (non-PCH)\n"
+ " function - only load functions (non-PCH)\n"
+ " typedef - only load typdefs (non-PCH)\n\n");
+}
+
+int main(int argc, const char **argv) {
+ if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
+ return perform_code_completion(argc, argv);
+ if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
+ return perform_test_load_tu(argv[2], argv[3]);
+
+ print_usage();
+ return 1;
}