diff --git a/src/command_line.cc b/src/command_line.cc index 80db274a..e65f5af9 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -3227,6 +3227,11 @@ int main(int argc, char** argv) { bool print_help = true; + if (HasOption(options, "--clang-sanity-check")) { + print_help = false; + ClangSanityCheck(); + } + if (HasOption(options, "--log-stdin-stdout-to-stderr")) g_log_stdin_stdout_to_stderr = true; @@ -3272,6 +3277,9 @@ int main(int argc, char** argv) { Print stdin and stdout messages to stderr. This is a aid for developing new language clients, as it makes it easier to figure out how the client is interacting with cquery. + --clang-sanity-check + Run a simple index test. Verifies basic clang functionality. + Needs to be executed from the cquery root checkout directory. Configuration: When opening up a directory, cquery will look for a compile_commands.json diff --git a/src/indexer.cc b/src/indexer.cc index 6b321b51..cd88ab0a 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1716,3 +1716,53 @@ void IndexInit() { clang_enableStackTraces(); clang_toggleCrashRecovery(1); } + +void ClangSanityCheck() { + std::vector args = {"clang", "tests/vars/class_member.cc"}; + unsigned opts = 0; + CXIndex index = clang_createIndex(0, 1); + CXTranslationUnit tu; + clang_parseTranslationUnit2FullArgv(index, nullptr, args.data(), args.size(), + nullptr, 0, opts, &tu); + assert(tu); + + IndexerCallbacks callback = {0}; + callback.abortQuery = [](CXClientData client_data, void* reserved) { + return 0; + }; + callback.diagnostic = [](CXClientData client_data, + CXDiagnosticSet diagnostics, void* reserved) {}; + callback.enteredMainFile = [](CXClientData client_data, CXFile mainFile, + void* reserved) -> CXIdxClientFile { + return nullptr; + }; + callback.ppIncludedFile = + [](CXClientData client_data, + const CXIdxIncludedFileInfo* file) -> CXIdxClientFile { + return nullptr; + }; + callback.importedASTFile = + [](CXClientData client_data, + const CXIdxImportedASTFileInfo*) -> CXIdxClientASTFile { + return nullptr; + }; + callback.startedTranslationUnit = [](CXClientData client_data, + void* reserved) -> CXIdxClientContainer { + return nullptr; + }; + callback.indexDeclaration = [](CXClientData client_data, + const CXIdxDeclInfo* decl) {}; + callback.indexEntityReference = [](CXClientData client_data, + const CXIdxEntityRefInfo* ref) {}; + + const unsigned kIndexOpts = 0; + CXIndexAction index_action = clang_IndexAction_create(index); + int index_param = 0; + clang_toggleCrashRecovery(0); + clang_indexTranslationUnit(index_action, &index_param, &callback, + sizeof(IndexerCallbacks), kIndexOpts, tu); + clang_IndexAction_dispose(index_action); + + clang_disposeTranslationUnit(tu); + clang_disposeIndex(index); +} diff --git a/src/indexer.h b/src/indexer.h index 3230eb38..66029ad3 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -555,3 +555,5 @@ std::vector> ParseWithTu( const std::vector& file_contents); void IndexInit(); + +void ClangSanityCheck();