2012-07-08 14:54:57 -04:00
|
|
|
// Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
//
|
|
|
|
// This file is part of YouCompleteMe.
|
|
|
|
//
|
|
|
|
// YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
#include "ClangCompleter.h"
|
2012-08-13 23:51:04 -04:00
|
|
|
#include "exceptions.h"
|
2012-09-06 14:37:22 -04:00
|
|
|
#include "Result.h"
|
2012-07-12 01:41:32 -04:00
|
|
|
#include "Candidate.h"
|
2012-08-11 22:27:15 -04:00
|
|
|
#include "TranslationUnit.h"
|
2012-07-08 14:54:57 -04:00
|
|
|
#include "standard.h"
|
2012-07-12 01:41:32 -04:00
|
|
|
#include "CandidateRepository.h"
|
2012-08-12 22:01:39 -04:00
|
|
|
#include "CompletionData.h"
|
2012-07-15 23:49:56 -04:00
|
|
|
#include "ConcurrentLatestValue.h"
|
2012-07-22 18:19:28 -04:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "ClangUtils.h"
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
#include <clang-c/Index.h>
|
2012-07-15 23:49:56 -04:00
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
|
|
|
|
using boost::bind;
|
2012-08-11 23:13:12 -04:00
|
|
|
using boost::cref;
|
|
|
|
using boost::function;
|
|
|
|
using boost::lock_guard;
|
2012-07-15 23:49:56 -04:00
|
|
|
using boost::make_shared;
|
2012-08-11 23:13:12 -04:00
|
|
|
using boost::mutex;
|
|
|
|
using boost::packaged_task;
|
|
|
|
using boost::ref;
|
|
|
|
using boost::shared_lock;
|
|
|
|
using boost::shared_mutex;
|
2012-07-15 23:49:56 -04:00
|
|
|
using boost::shared_ptr;
|
|
|
|
using boost::thread;
|
2012-08-11 23:13:12 -04:00
|
|
|
using boost::thread_interrupted;
|
|
|
|
using boost::try_to_lock_t;
|
|
|
|
using boost::unique_future;
|
2012-08-01 20:18:35 -04:00
|
|
|
using boost::unique_lock;
|
2012-07-27 00:43:20 -04:00
|
|
|
using boost::unordered_map;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
|
|
|
namespace YouCompleteMe
|
|
|
|
{
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
extern const unsigned int MAX_ASYNC_THREADS;
|
|
|
|
extern const unsigned int MIN_ASYNC_THREADS;
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
struct CompletionDataAndResult
|
|
|
|
{
|
|
|
|
CompletionDataAndResult( const CompletionData *completion_data,
|
|
|
|
const Result &result )
|
|
|
|
: completion_data_( completion_data ), result_( result ) {}
|
|
|
|
|
|
|
|
bool operator< ( const CompletionDataAndResult &other ) const
|
|
|
|
{
|
|
|
|
return result_ < other.result_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CompletionData *completion_data_;
|
|
|
|
Result result_;
|
|
|
|
};
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
ClangCompleter::ClangCompleter()
|
2012-07-15 23:49:56 -04:00
|
|
|
: candidate_repository_( CandidateRepository::Instance() ),
|
|
|
|
threading_enabled_( false ),
|
2012-08-11 22:27:15 -04:00
|
|
|
time_to_die_( false ),
|
2012-07-15 23:49:56 -04:00
|
|
|
clang_data_ready_( false )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
|
|
|
clang_index_ = clang_createIndex( 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
ClangCompleter::~ClangCompleter()
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
// We need to clear this before calling clang_disposeIndex because the
|
|
|
|
// translation units need to be destroyed before the index is destroyed.
|
|
|
|
filename_to_translation_unit_.clear();
|
|
|
|
clang_disposeIndex( clang_index_ );
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
unique_lock< shared_mutex > lock( time_to_die_mutex_ );
|
|
|
|
time_to_die_ = true;
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
sorting_threads_.interrupt_all();
|
|
|
|
sorting_threads_.join_all();
|
|
|
|
|
|
|
|
clang_thread_.interrupt();
|
|
|
|
clang_thread_.join();
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
// We need this mostly so that we can not use it in tests. Apparently the
|
2012-08-11 23:13:12 -04:00
|
|
|
// GoogleTest framework goes apeshit on us (on some platforms, in some
|
|
|
|
// occasions) if we enable threads by default.
|
2012-07-15 23:49:56 -04:00
|
|
|
void ClangCompleter::EnableThreading()
|
|
|
|
{
|
|
|
|
threading_enabled_ = true;
|
|
|
|
InitThreads();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile(
|
|
|
|
const std::string &filename )
|
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_ptr< TranslationUnit > unit;
|
2012-07-28 18:27:30 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
|
|
|
|
unit = FindWithDefault(
|
|
|
|
filename_to_translation_unit_,
|
|
|
|
filename,
|
|
|
|
shared_ptr< TranslationUnit >() );
|
2012-07-28 18:27:30 -04:00
|
|
|
}
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
if ( !unit )
|
|
|
|
return std::vector< Diagnostic >();
|
|
|
|
|
|
|
|
return unit->LatestDiagnostics();
|
2012-07-28 18:27:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename )
|
2012-07-26 23:50:56 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_ptr< TranslationUnit > unit;
|
|
|
|
{
|
|
|
|
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
|
|
|
|
unit = FindWithDefault(
|
|
|
|
filename_to_translation_unit_,
|
|
|
|
filename,
|
|
|
|
shared_ptr< TranslationUnit >() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !unit )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return unit->IsCurrentlyUpdating();
|
2012-07-26 23:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
void ClangCompleter::UpdateTranslationUnit(
|
2012-07-08 14:54:57 -04:00
|
|
|
const std::string &filename,
|
2012-08-01 01:01:41 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-08-14 00:06:44 -04:00
|
|
|
bool translation_unit_created;
|
|
|
|
shared_ptr< TranslationUnit > unit = GetTranslationUnitForFile(
|
|
|
|
filename,
|
|
|
|
unsaved_files,
|
|
|
|
flags,
|
|
|
|
translation_unit_created );
|
|
|
|
|
2012-08-13 23:51:04 -04:00
|
|
|
if ( !unit )
|
|
|
|
return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-08-14 00:06:44 -04:00
|
|
|
// There's no point in reparsing a TU that was just created, it was just
|
|
|
|
// parsed in the TU constructor
|
|
|
|
if ( !translation_unit_created )
|
|
|
|
unit->Reparse( unsaved_files );
|
2012-08-13 23:51:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
catch ( ClangParseError& )
|
|
|
|
{
|
|
|
|
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
|
|
|
|
|
|
|
|
// If unit->Reparse fails, then the underlying TranslationUnit object is not
|
|
|
|
// valid anymore and needs to be destroyed and removed from the filename ->
|
|
|
|
// TU map.
|
|
|
|
Erase( filename_to_translation_unit_, filename );
|
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-12 17:36:36 -04:00
|
|
|
Future< void > ClangCompleter::UpdateTranslationUnitAsync(
|
2012-07-26 23:50:56 -04:00
|
|
|
std::string filename,
|
2012-08-01 01:01:41 -04:00
|
|
|
std::vector< UnsavedFile > unsaved_files,
|
|
|
|
std::vector< std::string > flags )
|
2012-07-26 23:50:56 -04:00
|
|
|
{
|
2012-08-11 23:13:12 -04:00
|
|
|
function< void() > functor =
|
2012-07-26 23:50:56 -04:00
|
|
|
bind( &ClangCompleter::UpdateTranslationUnit,
|
|
|
|
boost::ref( *this ),
|
|
|
|
boost::move( filename ),
|
2012-08-01 01:01:41 -04:00
|
|
|
boost::move( unsaved_files ),
|
|
|
|
boost::move( flags ) );
|
2012-07-26 23:50:56 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_ptr< ClangPackagedTask > clang_packaged_task =
|
|
|
|
make_shared< ClangPackagedTask >();
|
2012-08-11 23:13:12 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
clang_packaged_task->parsing_task_ = packaged_task< void >( functor );
|
2012-08-12 17:36:36 -04:00
|
|
|
unique_future< void > future =
|
|
|
|
clang_packaged_task->parsing_task_.get_future();
|
2012-08-11 22:27:15 -04:00
|
|
|
clang_task_.Set( clang_packaged_task );
|
2012-08-12 17:36:36 -04:00
|
|
|
|
|
|
|
return Future< void >( boost::move( future ) );
|
2012-07-26 23:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
std::vector< CompletionData >
|
|
|
|
ClangCompleter::CandidatesForLocationInFile(
|
2012-07-08 14:54:57 -04:00
|
|
|
const std::string &filename,
|
|
|
|
int line,
|
|
|
|
int column,
|
2012-08-01 01:01:41 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_ptr< TranslationUnit > unit = GetTranslationUnitForFile( filename,
|
|
|
|
unsaved_files,
|
|
|
|
flags );
|
2012-08-13 23:51:04 -04:00
|
|
|
if ( !unit )
|
|
|
|
return std::vector< CompletionData >();
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
return unit->CandidatesForLocation( line,
|
|
|
|
column,
|
|
|
|
unsaved_files );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
Future< AsyncCompletions >
|
|
|
|
ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
|
2012-08-12 22:01:39 -04:00
|
|
|
const std::string &query,
|
|
|
|
const std::string &filename,
|
2012-07-15 23:49:56 -04:00
|
|
|
int line,
|
|
|
|
int column,
|
2012-08-12 22:01:39 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags )
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
|
|
|
// TODO: throw exception when threading is not enabled and this is called
|
|
|
|
if ( !threading_enabled_ )
|
2012-07-20 00:17:39 -04:00
|
|
|
return Future< AsyncCompletions >();
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-08-12 22:01:39 -04:00
|
|
|
bool skip_clang_result_cache = ShouldSkipClangResultCache( query,
|
|
|
|
line,
|
|
|
|
column );
|
|
|
|
if ( skip_clang_result_cache )
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
2012-07-26 23:50:56 -04:00
|
|
|
// The clang thread is busy, return nothing
|
2012-08-11 22:27:15 -04:00
|
|
|
if ( UpdatingTranslationUnit( filename ) )
|
2012-07-26 23:50:56 -04:00
|
|
|
return Future< AsyncCompletions >();
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
2012-08-11 23:13:12 -04:00
|
|
|
lock_guard< mutex > lock( clang_data_ready_mutex_ );
|
2012-07-15 23:49:56 -04:00
|
|
|
clang_data_ready_ = false;
|
|
|
|
}
|
2012-07-16 23:23:17 -04:00
|
|
|
|
|
|
|
// Needed to "reset" the sorting threads to the start of their loop. This
|
|
|
|
// way any threads blocking on a read in sorting_task_.Get() are reset to
|
|
|
|
// wait on the clang_data_ready_condition_variable_.
|
2012-07-15 23:49:56 -04:00
|
|
|
sorting_threads_.interrupt_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the sorting task needs to be set before the clang task (if any) just in
|
|
|
|
// case the clang task finishes (and therefore notifies a sorting thread to
|
|
|
|
// consume a sorting task) before the sorting task is set
|
2012-08-13 13:13:51 -04:00
|
|
|
unique_future< AsyncCompletions > future;
|
|
|
|
CreateSortingTask( query, future );
|
2012-08-12 22:01:39 -04:00
|
|
|
|
|
|
|
if ( skip_clang_result_cache )
|
|
|
|
{
|
|
|
|
CreateClangTask( filename, line, column, unsaved_files, flags );
|
|
|
|
}
|
|
|
|
|
|
|
|
return Future< AsyncCompletions >( boost::move( future ) );
|
|
|
|
}
|
|
|
|
|
2012-07-16 23:23:17 -04:00
|
|
|
|
2012-08-12 22:01:39 -04:00
|
|
|
bool ClangCompleter::ShouldSkipClangResultCache( const std::string &query,
|
|
|
|
int line,
|
|
|
|
int column )
|
|
|
|
{
|
|
|
|
// We need query.empty() in addition to the second check because if we don't
|
|
|
|
// have it, then we have a problem in the following situation:
|
|
|
|
// The user has a variable 'foo' of type 'A' and a variable 'bar' of type 'B'.
|
|
|
|
// He then types in 'foo.' and we store the clang results and also return
|
|
|
|
// them. The user then deletes that code and types in 'bar.'. Without the
|
|
|
|
// query.empty() check we would return the results from the cache here (it's
|
|
|
|
// the same line and column!) which would be incorrect.
|
|
|
|
return query.empty() || latest_clang_results_
|
|
|
|
.NewPositionDifferentFromStoredPosition( line, column );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-13 13:13:51 -04:00
|
|
|
// Copy-ctor for unique_future is private in C++03 mode so we need to take it as
|
|
|
|
// an out param
|
|
|
|
void ClangCompleter::CreateSortingTask(
|
|
|
|
const std::string &query,
|
|
|
|
unique_future< AsyncCompletions > &future )
|
2012-08-12 22:01:39 -04:00
|
|
|
{
|
|
|
|
// Careful! The code in this function may burn your eyes.
|
|
|
|
|
|
|
|
function< CompletionDatas( const CompletionDatas& ) >
|
|
|
|
sort_candidates_for_query_functor =
|
2012-07-16 23:23:17 -04:00
|
|
|
bind( &ClangCompleter::SortCandidatesForQuery,
|
|
|
|
boost::ref( *this ),
|
|
|
|
query,
|
2012-08-12 22:01:39 -04:00
|
|
|
_1 );
|
|
|
|
|
|
|
|
function< CompletionDatas() > operate_on_completion_data_functor =
|
|
|
|
bind( &ClangResultsCache::OperateOnCompletionDatas< CompletionDatas >,
|
|
|
|
boost::cref( latest_clang_results_ ),
|
|
|
|
boost::move( sort_candidates_for_query_functor ) );
|
2012-07-16 23:23:17 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
shared_ptr< packaged_task< AsyncCompletions > > task =
|
|
|
|
make_shared< packaged_task< AsyncCompletions > >(
|
|
|
|
bind( ReturnValueAsShared< std::vector< CompletionData > >,
|
2012-08-12 22:01:39 -04:00
|
|
|
boost::move( operate_on_completion_data_functor ) ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-08-13 13:13:51 -04:00
|
|
|
future = task->get_future();
|
2012-07-15 23:49:56 -04:00
|
|
|
sorting_task_.Set( task );
|
2012-08-12 22:01:39 -04:00
|
|
|
}
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-08-12 22:01:39 -04:00
|
|
|
|
|
|
|
void ClangCompleter::CreateClangTask(
|
|
|
|
std::string filename,
|
|
|
|
int line,
|
|
|
|
int column,
|
|
|
|
std::vector< UnsavedFile > unsaved_files,
|
|
|
|
std::vector< std::string > flags )
|
|
|
|
{
|
|
|
|
latest_clang_results_.ResetWithNewLineAndColumn( line, column );
|
|
|
|
|
|
|
|
function< CompletionDatas() > candidates_for_location_functor =
|
2012-07-16 23:23:17 -04:00
|
|
|
bind( &ClangCompleter::CandidatesForLocationInFile,
|
|
|
|
boost::ref( *this ),
|
2012-08-01 01:01:41 -04:00
|
|
|
boost::move( filename ),
|
2012-07-16 23:23:17 -04:00
|
|
|
line,
|
|
|
|
column,
|
2012-08-01 01:01:41 -04:00
|
|
|
boost::move( unsaved_files ),
|
|
|
|
boost::move( flags ) );
|
2012-07-16 23:23:17 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_ptr< ClangPackagedTask > clang_packaged_task =
|
|
|
|
make_shared< ClangPackagedTask >();
|
2012-08-11 23:13:12 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
clang_packaged_task->completions_task_ = packaged_task< AsyncCompletions >(
|
2012-07-20 00:17:39 -04:00
|
|
|
bind( ReturnValueAsShared< std::vector< CompletionData > >,
|
2012-08-11 22:27:15 -04:00
|
|
|
candidates_for_location_functor ) );
|
|
|
|
|
|
|
|
clang_task_.Set( clang_packaged_task );
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-14 00:06:44 -04:00
|
|
|
boost::shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
|
|
|
|
const std::string &filename,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags )
|
|
|
|
{
|
|
|
|
bool dont_care;
|
|
|
|
return GetTranslationUnitForFile( filename, unsaved_files, flags, dont_care );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
// WARNING: It should not be possible to call this function from two separate
|
|
|
|
// threads at the same time. Currently only one thread (the clang thread) ever
|
|
|
|
// calls this function so there is no need for a mutex, but if that changes in
|
|
|
|
// the future a mutex will be needed to make sure that two threads don't try to
|
|
|
|
// create the same translation unit.
|
2012-08-11 23:13:12 -04:00
|
|
|
shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
|
2012-07-08 14:54:57 -04:00
|
|
|
const std::string &filename,
|
2012-08-01 01:01:41 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
2012-08-14 00:06:44 -04:00
|
|
|
const std::vector< std::string > &flags,
|
|
|
|
bool &translation_unit_created )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-07-22 18:19:28 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
|
|
|
|
TranslationUnitForFilename::iterator it =
|
|
|
|
filename_to_translation_unit_.find( filename );
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
if ( it != filename_to_translation_unit_.end() )
|
2012-08-14 00:06:44 -04:00
|
|
|
{
|
|
|
|
translation_unit_created = false;
|
2012-08-11 22:27:15 -04:00
|
|
|
return it->second;
|
2012-08-14 00:06:44 -04:00
|
|
|
}
|
2012-08-11 22:27:15 -04:00
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-08-13 23:51:04 -04:00
|
|
|
shared_ptr< TranslationUnit > unit;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
unit = make_shared< TranslationUnit >(
|
|
|
|
filename, unsaved_files, flags, clang_index_ );
|
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-08-13 23:51:04 -04:00
|
|
|
catch ( ClangParseError& )
|
|
|
|
{
|
|
|
|
return unit;
|
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
{
|
|
|
|
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
|
|
|
|
filename_to_translation_unit_[ filename ] = unit;
|
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-08-14 00:06:44 -04:00
|
|
|
translation_unit_created = true;
|
2012-07-08 14:54:57 -04:00
|
|
|
return unit;
|
|
|
|
}
|
|
|
|
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery(
|
2012-07-12 01:41:32 -04:00
|
|
|
const std::string &query,
|
2012-07-20 00:17:39 -04:00
|
|
|
const std::vector< CompletionData > &completion_datas )
|
2012-07-12 01:41:32 -04:00
|
|
|
{
|
|
|
|
Bitset query_bitset = LetterBitsetFromString( query );
|
|
|
|
|
|
|
|
std::vector< const Candidate* > repository_candidates =
|
2012-07-20 00:17:39 -04:00
|
|
|
candidate_repository_.GetCandidatesForStrings( completion_datas );
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionDataAndResult > data_and_results;
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
for ( uint i = 0; i < repository_candidates.size(); ++i )
|
2012-07-12 01:41:32 -04:00
|
|
|
{
|
2012-07-20 00:17:39 -04:00
|
|
|
const Candidate* candidate = repository_candidates[ i ];
|
2012-07-12 01:41:32 -04:00
|
|
|
if ( !candidate->MatchesQueryBitset( query_bitset ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Result result = candidate->QueryMatchResult( query );
|
|
|
|
if ( result.IsSubsequence() )
|
2012-07-20 00:17:39 -04:00
|
|
|
{
|
|
|
|
CompletionDataAndResult data_and_result( &completion_datas[ i ], result );
|
|
|
|
data_and_results.push_back( data_and_result );
|
|
|
|
}
|
2012-07-12 01:41:32 -04:00
|
|
|
}
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::sort( data_and_results.begin(), data_and_results.end() );
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionData > sorted_completion_datas;
|
|
|
|
sorted_completion_datas.reserve( data_and_results.size() );
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
foreach ( const CompletionDataAndResult& data_and_result, data_and_results )
|
2012-07-12 01:41:32 -04:00
|
|
|
{
|
2012-07-20 00:17:39 -04:00
|
|
|
sorted_completion_datas.push_back( *data_and_result.completion_data_ );
|
2012-07-12 01:41:32 -04:00
|
|
|
}
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
return sorted_completion_datas;
|
2012-07-12 01:41:32 -04:00
|
|
|
}
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
|
|
|
|
void ClangCompleter::InitThreads()
|
|
|
|
{
|
|
|
|
int threads_to_create =
|
|
|
|
std::max( MIN_ASYNC_THREADS,
|
|
|
|
std::min( MAX_ASYNC_THREADS, thread::hardware_concurrency() ) );
|
|
|
|
|
|
|
|
for ( int i = 0; i < threads_to_create; ++i )
|
|
|
|
{
|
2012-08-11 23:13:12 -04:00
|
|
|
sorting_threads_.create_thread( bind( &ClangCompleter::SortingThreadMain,
|
|
|
|
boost::ref( *this ) ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
2012-08-11 23:13:12 -04:00
|
|
|
clang_thread_ = thread( &ClangCompleter::ClangThreadMain,
|
|
|
|
boost::ref( *this ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
void ClangCompleter::ClangThreadMain()
|
2012-07-26 23:50:56 -04:00
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
try
|
2012-07-26 23:50:56 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_ptr< ClangPackagedTask > task = clang_task_.Get();
|
|
|
|
|
|
|
|
bool has_completions_task = task->completions_task_.valid();
|
|
|
|
if ( has_completions_task )
|
|
|
|
task->completions_task_();
|
|
|
|
else
|
|
|
|
task->parsing_task_();
|
|
|
|
|
|
|
|
if ( !has_completions_task )
|
2012-07-26 23:50:56 -04:00
|
|
|
continue;
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
unique_future< AsyncCompletions > future =
|
|
|
|
task->completions_task_.get_future();
|
2012-08-12 22:01:39 -04:00
|
|
|
latest_clang_results_.SetCompletionDatas( *future.get() );
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
{
|
2012-08-11 23:13:12 -04:00
|
|
|
lock_guard< mutex > lock( clang_data_ready_mutex_ );
|
2012-08-11 22:27:15 -04:00
|
|
|
clang_data_ready_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_data_ready_condition_variable_.notify_all();
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
2012-08-11 23:13:12 -04:00
|
|
|
catch ( thread_interrupted& )
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_lock< shared_mutex > lock( time_to_die_mutex_ );
|
|
|
|
if ( time_to_die_ )
|
|
|
|
return;
|
|
|
|
// else do nothing and re-enter the loop
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
void ClangCompleter::SortingThreadMain()
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
unique_lock< mutex > lock( clang_data_ready_mutex_ );
|
2012-07-15 23:49:56 -04:00
|
|
|
|
|
|
|
while ( !clang_data_ready_ )
|
|
|
|
{
|
|
|
|
clang_data_ready_condition_variable_.wait( lock );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
shared_ptr< packaged_task< AsyncCompletions > > task =
|
|
|
|
sorting_task_.Get();
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-08-12 22:01:39 -04:00
|
|
|
( *task )();
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
2012-08-11 23:13:12 -04:00
|
|
|
catch ( thread_interrupted& )
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
2012-08-11 22:27:15 -04:00
|
|
|
shared_lock< shared_mutex > lock( time_to_die_mutex_ );
|
|
|
|
if ( time_to_die_ )
|
|
|
|
return;
|
|
|
|
// else do nothing and re-enter the loop
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
} // namespace YouCompleteMe
|