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-07-12 01:41:32 -04:00
|
|
|
#include "Candidate.h"
|
2012-07-20 00:17:39 -04:00
|
|
|
#include "CompletionData.h"
|
2012-07-08 14:54:57 -04:00
|
|
|
#include "standard.h"
|
2012-07-12 01:41:32 -04:00
|
|
|
#include "CandidateRepository.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>
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
// TODO: remove all explicit uses of the boost:: prefix by adding explicit using
|
|
|
|
// directives for the stuff we need
|
2012-07-22 18:19:28 -04:00
|
|
|
namespace fs = boost::filesystem;
|
2012-07-15 23:49:56 -04:00
|
|
|
using boost::packaged_task;
|
|
|
|
using boost::bind;
|
|
|
|
using boost::unique_future;
|
|
|
|
using boost::make_shared;
|
|
|
|
using boost::shared_ptr;
|
|
|
|
using boost::bind;
|
|
|
|
using boost::thread;
|
2012-07-26 23:50:56 -04:00
|
|
|
using boost::lock_guard;
|
2012-08-01 20:18:35 -04:00
|
|
|
using boost::unique_lock;
|
2012-07-26 23:50:56 -04:00
|
|
|
using boost::mutex;
|
2012-07-27 00:43:20 -04:00
|
|
|
using boost::unordered_map;
|
2012-08-01 23:09:01 -04:00
|
|
|
using boost::try_to_lock_t;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
|
|
|
namespace YouCompleteMe
|
|
|
|
{
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
typedef boost::function< std::vector< CompletionData >() >
|
|
|
|
FunctionReturnsCompletionDataVector;
|
|
|
|
|
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
|
|
|
std::vector< CXUnsavedFile > ToCXUnsavedFiles(
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files )
|
|
|
|
{
|
|
|
|
std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() );
|
|
|
|
for ( uint i = 0; i < unsaved_files.size(); ++i )
|
|
|
|
{
|
2012-07-10 18:26:07 -04:00
|
|
|
// TODO: assert non-null
|
|
|
|
clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_;
|
|
|
|
clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_;
|
|
|
|
clang_unsaved_files[ i ].Length = unsaved_files[ i ].length_;
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return clang_unsaved_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string CXStringToString( CXString text )
|
|
|
|
{
|
|
|
|
std::string final_string( clang_getCString( text ) );
|
|
|
|
clang_disposeString( text );
|
|
|
|
return final_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string ChunkToString( CXCompletionString completion_string, int chunk_num )
|
|
|
|
{
|
|
|
|
return CXStringToString(
|
|
|
|
clang_getCompletionChunkText( completion_string, chunk_num ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-16 23:51:21 -04:00
|
|
|
// Returns true when the provided completion string is available to the user;
|
|
|
|
// unavailable completion strings refer to entities that are private/protected,
|
|
|
|
// deprecated etc.
|
|
|
|
bool CompletionStringAvailable( CXCompletionString completion_string )
|
|
|
|
{
|
|
|
|
return clang_getCompletionAvailability( completion_string ) ==
|
|
|
|
CXAvailability_Available;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
bool IsChunkKindForExtraMenuInfo( CXCompletionChunkKind kind )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-07-20 00:17:39 -04:00
|
|
|
return
|
|
|
|
kind == CXCompletionChunk_Optional ||
|
|
|
|
kind == CXCompletionChunk_TypedText ||
|
|
|
|
kind == CXCompletionChunk_Placeholder ||
|
|
|
|
kind == CXCompletionChunk_LeftParen ||
|
|
|
|
kind == CXCompletionChunk_RightParen ||
|
|
|
|
kind == CXCompletionChunk_RightBracket ||
|
|
|
|
kind == CXCompletionChunk_LeftBracket ||
|
|
|
|
kind == CXCompletionChunk_LeftBrace ||
|
|
|
|
kind == CXCompletionChunk_RightBrace ||
|
|
|
|
kind == CXCompletionChunk_RightAngle ||
|
|
|
|
kind == CXCompletionChunk_LeftAngle ||
|
|
|
|
kind == CXCompletionChunk_Comma ||
|
|
|
|
kind == CXCompletionChunk_ResultType ||
|
|
|
|
kind == CXCompletionChunk_Colon ||
|
|
|
|
kind == CXCompletionChunk_SemiColon ||
|
|
|
|
kind == CXCompletionChunk_Equal ||
|
|
|
|
kind == CXCompletionChunk_HorizontalSpace;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char CursorKindToVimKind( CXCursorKind kind )
|
|
|
|
{
|
2012-07-21 15:06:18 -04:00
|
|
|
// TODO: actually it appears that Vim will show returned kinds even when they
|
|
|
|
// do not match the "approved" list, so let's use that
|
2012-07-20 00:17:39 -04:00
|
|
|
switch ( kind )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-07-20 00:17:39 -04:00
|
|
|
case CXCursor_UnexposedDecl:
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
case CXCursor_TypedefDecl:
|
|
|
|
return 't';
|
|
|
|
|
|
|
|
case CXCursor_FieldDecl:
|
|
|
|
return 'm';
|
|
|
|
|
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
case CXCursor_FunctionTemplate:
|
|
|
|
return 'f';
|
|
|
|
|
|
|
|
case CXCursor_VarDecl:
|
|
|
|
return 'v';
|
|
|
|
|
|
|
|
case CXCursor_MacroDefinition:
|
|
|
|
return 'd';
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 'u'; // for 'unknown', 'unsupported'... whatever you like
|
|
|
|
}
|
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-07-16 23:51:21 -04:00
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
char DiagnosticSeverityToType( CXDiagnosticSeverity severity )
|
|
|
|
{
|
|
|
|
switch ( severity )
|
|
|
|
{
|
|
|
|
case CXDiagnostic_Ignored:
|
|
|
|
case CXDiagnostic_Note:
|
|
|
|
return 'I';
|
|
|
|
|
|
|
|
case CXDiagnostic_Warning:
|
|
|
|
return 'W';
|
|
|
|
|
|
|
|
case CXDiagnostic_Error:
|
|
|
|
case CXDiagnostic_Fatal:
|
|
|
|
return 'E';
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 'E';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: this should be a constructor
|
2012-07-20 00:17:39 -04:00
|
|
|
CompletionData CompletionResultToCompletionData(
|
|
|
|
const CXCompletionResult &completion_result )
|
|
|
|
{
|
|
|
|
CompletionData data;
|
|
|
|
CXCompletionString completion_string = completion_result.CompletionString;
|
|
|
|
|
|
|
|
uint num_chunks = clang_getNumCompletionChunks( completion_string );
|
|
|
|
for ( uint j = 0; j < num_chunks; ++j )
|
|
|
|
{
|
|
|
|
CXCompletionChunkKind kind = clang_getCompletionChunkKind(
|
|
|
|
completion_string, j );
|
|
|
|
|
|
|
|
if ( IsChunkKindForExtraMenuInfo( kind ) )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
2012-07-20 00:17:39 -04:00
|
|
|
data.extra_menu_info_.append( ChunkToString( completion_string, j ) );
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
// by default, there's no space after the return type
|
|
|
|
if ( kind == CXCompletionChunk_ResultType )
|
|
|
|
data.extra_menu_info_.append( " " );
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
2012-07-20 00:17:39 -04:00
|
|
|
|
|
|
|
if ( kind == CXCompletionChunk_TypedText )
|
|
|
|
data.original_string_ = ChunkToString( completion_string, j );
|
|
|
|
|
|
|
|
if ( kind == CXCompletionChunk_Informative )
|
|
|
|
data.detailed_info_ = ChunkToString( completion_string, j );
|
|
|
|
}
|
|
|
|
|
|
|
|
data.kind_ = CursorKindToVimKind( completion_result.CursorKind );
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector< CompletionData > ToCompletionDataVector(
|
|
|
|
CXCodeCompleteResults *results )
|
|
|
|
{
|
|
|
|
std::vector< CompletionData > completions;
|
|
|
|
completions.reserve( results->NumResults );
|
|
|
|
|
2012-07-27 00:43:20 -04:00
|
|
|
unordered_map< std::string, uint > seen_data;
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
for ( uint i = 0; i < results->NumResults; ++i )
|
|
|
|
{
|
|
|
|
CXCompletionResult completion_result = results->Results[ i ];
|
|
|
|
|
|
|
|
if ( !CompletionStringAvailable( completion_result.CompletionString ) )
|
|
|
|
continue;
|
|
|
|
|
2012-07-27 00:43:20 -04:00
|
|
|
CompletionData data = CompletionResultToCompletionData( completion_result );
|
|
|
|
uint index = GetValueElseInsert( seen_data,
|
|
|
|
data.original_string_,
|
|
|
|
completions.size() );
|
|
|
|
|
|
|
|
if ( index == completions.size() )
|
|
|
|
{
|
|
|
|
completions.push_back( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
completions[ index ].detailed_info_
|
|
|
|
.append( "\n" )
|
|
|
|
.append( data.extra_menu_info_ );
|
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return completions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
Diagnostic CXDiagnosticToDiagnostic( CXDiagnostic cxdiagnostic )
|
|
|
|
{
|
|
|
|
Diagnostic diagnostic;
|
|
|
|
diagnostic.kind_ = DiagnosticSeverityToType(
|
|
|
|
clang_getDiagnosticSeverity( cxdiagnostic ) );
|
|
|
|
|
|
|
|
// If this is an "ignored" diagnostic, there's no point in continuing since we
|
|
|
|
// won't display those to the user
|
|
|
|
if ( diagnostic.kind_ == 'I' )
|
|
|
|
return diagnostic;
|
|
|
|
|
|
|
|
CXSourceLocation location = clang_getDiagnosticLocation( cxdiagnostic );
|
|
|
|
CXFile file;
|
|
|
|
uint unused_offset;
|
|
|
|
clang_getSpellingLocation( location,
|
|
|
|
&file,
|
|
|
|
&diagnostic.line_number_,
|
|
|
|
&diagnostic.column_number_,
|
|
|
|
&unused_offset );
|
|
|
|
diagnostic.filename_ = CXStringToString( clang_getFileName( file ) );
|
|
|
|
diagnostic.text_ = CXStringToString(
|
|
|
|
clang_getDiagnosticSpelling( cxdiagnostic ) );
|
|
|
|
|
|
|
|
clang_disposeDiagnostic( cxdiagnostic );
|
|
|
|
return diagnostic;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 ),
|
|
|
|
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
|
|
|
{
|
|
|
|
foreach ( const TranslationUnitForFilename::value_type &filename_unit,
|
|
|
|
filename_to_translation_unit_ )
|
|
|
|
{
|
|
|
|
clang_disposeTranslationUnit( filename_unit.second );
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_disposeIndex( clang_index_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
// We need this mostly so that we can not use it in tests. Apparently the
|
|
|
|
// GoogleTest framework goes apeshit on us if we enable threads by default.
|
|
|
|
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-01 20:18:35 -04:00
|
|
|
std::vector< Diagnostic > diagnostics;
|
2012-08-01 23:09:01 -04:00
|
|
|
unique_lock< mutex > lock( clang_access_mutex_, try_to_lock_t() );
|
2012-08-01 20:18:35 -04:00
|
|
|
if ( !lock.owns_lock() )
|
|
|
|
return diagnostics;
|
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
CXTranslationUnit unit = FindWithDefault( filename_to_translation_unit_,
|
|
|
|
filename,
|
|
|
|
NULL );
|
|
|
|
if ( !unit )
|
|
|
|
return diagnostics;
|
|
|
|
|
|
|
|
uint num_diagnostics = clang_getNumDiagnostics( unit );
|
|
|
|
diagnostics.reserve( num_diagnostics );
|
|
|
|
|
|
|
|
for ( uint i = 0; i < num_diagnostics; ++i )
|
|
|
|
{
|
|
|
|
Diagnostic diagnostic = CXDiagnosticToDiagnostic(
|
|
|
|
clang_getDiagnostic( unit, i ) );
|
|
|
|
|
|
|
|
if ( diagnostic.kind_ != 'I' )
|
|
|
|
diagnostics.push_back( diagnostic );
|
|
|
|
}
|
|
|
|
|
|
|
|
return diagnostics;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
bool ClangCompleter::UpdatingTranslationUnit()
|
|
|
|
{
|
2012-08-01 23:09:01 -04:00
|
|
|
unique_lock< mutex > lock( clang_access_mutex_, try_to_lock_t() );
|
|
|
|
return !lock.owns_lock();
|
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
|
|
|
{
|
|
|
|
TranslationUnitForFilename::iterator it =
|
|
|
|
filename_to_translation_unit_.find( filename );
|
|
|
|
|
|
|
|
if ( it != filename_to_translation_unit_.end() )
|
|
|
|
{
|
|
|
|
std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles(
|
|
|
|
unsaved_files );
|
|
|
|
|
|
|
|
clang_reparseTranslationUnit(
|
|
|
|
it->second,
|
|
|
|
cxunsaved_files.size(),
|
|
|
|
&cxunsaved_files[ 0 ],
|
|
|
|
clang_defaultEditingTranslationUnitOptions() );
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename_to_translation_unit_[ filename ] =
|
2012-08-01 01:01:41 -04:00
|
|
|
CreateTranslationUnit( filename, unsaved_files, flags );
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
void ClangCompleter::UpdateTranslationUnitAsync(
|
|
|
|
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
|
|
|
{
|
|
|
|
boost::function< void() > functor =
|
|
|
|
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
|
|
|
|
|
|
|
boost::lock_guard< boost::mutex > lock( file_parse_task_mutex_ );
|
|
|
|
|
|
|
|
// Only ever set the task when it's NULL; if it's not, that means that the
|
|
|
|
// clang thread is working on it
|
2012-07-30 22:42:41 -04:00
|
|
|
if ( file_parse_task_ )
|
|
|
|
return;
|
|
|
|
|
|
|
|
file_parse_task_ = make_shared< packaged_task< void > >( functor );
|
|
|
|
file_parse_task_condition_variable_.notify_all();
|
2012-07-26 23:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 00:17:39 -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
|
|
|
{
|
|
|
|
std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles(
|
|
|
|
unsaved_files );
|
|
|
|
|
2012-07-10 18:26:07 -04:00
|
|
|
// codeCompleteAt reparses the TU if the underlying source file has changed on
|
|
|
|
// disk since the last time the TU was updated and there are no unsaved files.
|
|
|
|
// If there are unsaved files, then codeCompleteAt will parse the in-memory
|
|
|
|
// file contents we are giving it. In short, it is NEVER a good idea to call
|
|
|
|
// clang_reparseTranslationUnit right before a call to clang_codeCompleteAt.
|
2012-07-15 23:49:56 -04:00
|
|
|
// This only makes clang reparse the whole file TWICE, which has a huge impact
|
2012-07-10 18:26:07 -04:00
|
|
|
// on latency. At the time of writing, it seems that most users of libclang
|
|
|
|
// in the open-source world don't realize this (I checked). Some don't even
|
|
|
|
// call reparse*, but parse* which is even less efficient.
|
2012-07-08 14:54:57 -04:00
|
|
|
|
|
|
|
CXCodeCompleteResults *results =
|
2012-08-01 01:01:41 -04:00
|
|
|
clang_codeCompleteAt( GetTranslationUnitForFile( filename,
|
|
|
|
unsaved_files,
|
|
|
|
flags ),
|
2012-07-08 14:54:57 -04:00
|
|
|
filename.c_str(),
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
&cxunsaved_files[ 0 ],
|
|
|
|
cxunsaved_files.size(),
|
|
|
|
clang_defaultCodeCompleteOptions());
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionData > candidates = ToCompletionDataVector( results );
|
2012-07-08 14:54:57 -04:00
|
|
|
clang_disposeCodeCompleteResults( results );
|
2012-07-15 23:49:56 -04:00
|
|
|
return candidates;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
Future< AsyncCompletions >
|
|
|
|
ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
|
2012-08-01 01:01:41 -04:00
|
|
|
std::string query,
|
|
|
|
std::string filename,
|
2012-07-15 23:49:56 -04:00
|
|
|
int line,
|
|
|
|
int column,
|
2012-08-01 01:01:41 -04:00
|
|
|
std::vector< UnsavedFile > unsaved_files,
|
|
|
|
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
|
|
|
|
|
|
|
if ( query.empty() )
|
|
|
|
{
|
2012-07-26 23:50:56 -04:00
|
|
|
// The clang thread is busy, return nothing
|
|
|
|
if ( UpdatingTranslationUnit() )
|
|
|
|
return Future< AsyncCompletions >();
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
|
|
|
boost::lock_guard< boost::mutex > lock( clang_data_ready_mutex_ );
|
|
|
|
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-07-16 23:23:17 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
FunctionReturnsCompletionDataVector sort_candidates_for_query_functor =
|
2012-07-16 23:23:17 -04:00
|
|
|
bind( &ClangCompleter::SortCandidatesForQuery,
|
|
|
|
boost::ref( *this ),
|
|
|
|
query,
|
|
|
|
boost::cref( latest_clang_results_ ) );
|
|
|
|
|
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-07-16 23:23:17 -04:00
|
|
|
sort_candidates_for_query_functor ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
unique_future< AsyncCompletions > future = task->get_future();
|
2012-07-15 23:49:56 -04:00
|
|
|
sorting_task_.Set( task );
|
|
|
|
|
|
|
|
if ( query.empty() )
|
|
|
|
{
|
2012-07-20 00:17:39 -04:00
|
|
|
FunctionReturnsCompletionDataVector
|
|
|
|
candidates_for_location_in_file_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-07-20 00:17:39 -04:00
|
|
|
shared_ptr< packaged_task< AsyncCompletions > > task =
|
|
|
|
make_shared< packaged_task< AsyncCompletions > >(
|
|
|
|
bind( ReturnValueAsShared< std::vector< CompletionData > >,
|
2012-07-16 23:51:21 -04:00
|
|
|
candidates_for_location_in_file_functor ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
clang_completions_task_.Set( task );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
return Future< AsyncCompletions >( boost::move( future ) );
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
CXTranslationUnit ClangCompleter::CreateTranslationUnit(
|
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-01 01:01:41 -04:00
|
|
|
std::vector< const char* > pointer_flags;
|
|
|
|
pointer_flags.reserve( flags.size() );
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2012-08-01 01:01:41 -04:00
|
|
|
foreach ( const std::string &flag, flags )
|
2012-07-22 18:19:28 -04:00
|
|
|
{
|
2012-08-01 01:01:41 -04:00
|
|
|
pointer_flags.push_back( flag.c_str() );
|
2012-07-22 18:19:28 -04:00
|
|
|
}
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles(
|
|
|
|
unsaved_files );
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
CXTranslationUnit unit = clang_parseTranslationUnit(
|
2012-07-08 14:54:57 -04:00
|
|
|
clang_index_,
|
|
|
|
filename.c_str(),
|
2012-08-01 01:01:41 -04:00
|
|
|
&pointer_flags[ 0 ],
|
|
|
|
pointer_flags.size(),
|
2012-07-08 14:54:57 -04:00
|
|
|
&cxunsaved_files[ 0 ],
|
|
|
|
cxunsaved_files.size(),
|
2012-07-10 18:26:07 -04:00
|
|
|
clang_defaultEditingTranslationUnitOptions() );
|
2012-07-26 23:50:56 -04:00
|
|
|
|
|
|
|
// Only with a reparse is the preable precompiled. I do not know why...
|
|
|
|
// TODO: report this bug on the clang tracker
|
|
|
|
clang_reparseTranslationUnit(
|
|
|
|
unit,
|
|
|
|
cxunsaved_files.size(),
|
|
|
|
&cxunsaved_files[ 0 ],
|
|
|
|
clang_defaultEditingTranslationUnitOptions() );
|
|
|
|
|
|
|
|
return unit;
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
CXTranslationUnit 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,
|
|
|
|
const std::vector< std::string > &flags )
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
|
|
|
TranslationUnitForFilename::iterator it =
|
|
|
|
filename_to_translation_unit_.find( filename );
|
|
|
|
|
|
|
|
if ( it != filename_to_translation_unit_.end() )
|
|
|
|
return it->second;
|
|
|
|
|
2012-08-01 01:01:41 -04:00
|
|
|
CXTranslationUnit unit = CreateTranslationUnit( filename,
|
|
|
|
unsaved_files,
|
|
|
|
flags );
|
2012-07-08 14:54:57 -04:00
|
|
|
filename_to_translation_unit_[ filename ] = unit;
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
sorting_threads_.create_thread(
|
|
|
|
bind( &ClangCompleter::SortingThreadMain,
|
2012-07-26 23:50:56 -04:00
|
|
|
boost::ref( *this ) ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
clang_completions_thread_ = boost::thread(
|
|
|
|
&ClangCompleter::ClangCompletionsThreadMain,
|
|
|
|
boost::ref( *this ) );
|
|
|
|
|
|
|
|
file_parse_thread_ = boost::thread(
|
|
|
|
&ClangCompleter::FileParseThreadMain,
|
|
|
|
boost::ref( *this ) );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
void ClangCompleter::FileParseThreadMain()
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
2012-07-26 23:50:56 -04:00
|
|
|
{
|
|
|
|
boost::unique_lock< boost::mutex > lock( file_parse_task_mutex_ );
|
|
|
|
|
|
|
|
while ( !file_parse_task_ )
|
|
|
|
{
|
|
|
|
file_parse_task_condition_variable_.wait( lock );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:09:01 -04:00
|
|
|
{
|
|
|
|
unique_lock< mutex > lock( clang_access_mutex_ );
|
|
|
|
( *file_parse_task_ )();
|
|
|
|
}
|
2012-07-26 23:50:56 -04:00
|
|
|
|
|
|
|
lock_guard< mutex > lock( file_parse_task_mutex_ );
|
|
|
|
file_parse_task_ = VoidTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ClangCompleter::ClangCompletionsThreadMain()
|
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
// TODO: this should be a separate func, much like the file_parse_task_ part
|
|
|
|
shared_ptr< packaged_task< AsyncCompletions > > task =
|
|
|
|
clang_completions_task_.Get();
|
|
|
|
|
|
|
|
// If the file parse thread is accessing clang by parsing a file, then drop
|
|
|
|
// the current completion request
|
|
|
|
{
|
|
|
|
lock_guard< mutex > lock( file_parse_task_mutex_ );
|
|
|
|
if ( file_parse_task_ )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:09:01 -04:00
|
|
|
{
|
|
|
|
unique_lock< mutex > lock( clang_access_mutex_ );
|
|
|
|
( *task )();
|
|
|
|
}
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
unique_future< AsyncCompletions > future = task->get_future();
|
2012-07-15 23:49:56 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
boost::unique_lock< boost::shared_mutex > writer_lock(
|
|
|
|
latest_clang_results_shared_mutex_ );
|
|
|
|
latest_clang_results_ = *future.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::lock_guard< boost::mutex > lock( clang_data_ready_mutex_ );
|
|
|
|
clang_data_ready_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_data_ready_condition_variable_.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
void ClangCompleter::SortingThreadMain()
|
2012-07-15 23:49:56 -04:00
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::unique_lock< boost::mutex > lock( clang_data_ready_mutex_ );
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
{
|
|
|
|
boost::shared_lock< boost::shared_mutex > reader_lock(
|
|
|
|
latest_clang_results_shared_mutex_ );
|
|
|
|
|
|
|
|
( *task )();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
catch ( boost::thread_interrupted& )
|
|
|
|
{
|
|
|
|
// Do nothing and re-enter the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
} // namespace YouCompleteMe
|