diff --git a/src/clang_index.cc b/src/clang_index.cc index 291b7cd1..41c7b65b 100644 --- a/src/clang_index.cc +++ b/src/clang_index.cc @@ -1,13 +1,22 @@ #include "clang_index.h" +#include + ClangIndex::ClangIndex() : ClangIndex(1, 0) {} ClangIndex::ClangIndex(int exclude_declarations_from_pch, int display_diagnostics) { + // llvm::InitializeAllTargets (and possibly others) called by clang_createIndex + // transtively modifies/reads lib/Support/TargetRegistry.cpp FirstTarget. + // There will be a race condition if two threads call clang_createIndex + // concurrently. + static std::mutex mutex_; + std::lock_guard lock(mutex_); + cx_index = clang_createIndex(exclude_declarations_from_pch, display_diagnostics); } ClangIndex::~ClangIndex() { clang_disposeIndex(cx_index); -} \ No newline at end of file +} diff --git a/src/clang_translation_unit.cc b/src/clang_translation_unit.cc index 4eb90582..ff49a8e4 100644 --- a/src/clang_translation_unit.cc +++ b/src/clang_translation_unit.cc @@ -14,16 +14,6 @@ namespace { -// We need to serialize requests to clang_parseTranslationUnit2FullArgv and -// clang_reparseTranslationUnit. See -// https://github.com/jacobdufault/cquery/issues/43#issuecomment-347614504. -// -// NOTE: This is disabled because it effectively serializes indexing, as a huge -// chunk of indexing time is spent inside of these functions. -// -// std::mutex g_parse_translation_unit_mutex; -// std::mutex g_reparse_translation_unit_mutex; - void EmitDiagnostics(std::string path, std::vector args, CXTranslationUnit tu) { @@ -95,7 +85,6 @@ std::unique_ptr ClangTranslationUnit::Create( CXTranslationUnit cx_tu; CXErrorCode error_code; { - // std::lock_guard lock(g_parse_translation_unit_mutex); error_code = clang_parseTranslationUnit2FullArgv( index->cx_index, filepath.c_str(), args.data(), (int)args.size(), unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &cx_tu); @@ -134,7 +123,6 @@ std::unique_ptr ClangTranslationUnit::Reparse( std::vector& unsaved) { int error_code; { - // std::lock_guard lock(g_reparse_translation_unit_mutex); error_code = clang_reparseTranslationUnit( tu->cx_tu, (unsigned)unsaved.size(), unsaved.data(), clang_defaultReparseOptions(tu->cx_tu));