Formatting the a part of the source with astyle

This commit is contained in:
Strahinja Val Markovic 2013-01-19 20:03:32 -08:00
parent 00db8fd8b1
commit 013a56c25c
14 changed files with 318 additions and 410 deletions

View File

@ -48,23 +48,19 @@ using boost::unique_future;
using boost::unique_lock;
using boost::unordered_map;
namespace YouCompleteMe
{
namespace YouCompleteMe {
extern const unsigned int MAX_ASYNC_THREADS;
extern const unsigned int MIN_ASYNC_THREADS;
namespace
{
namespace {
struct CompletionDataAndResult
{
struct CompletionDataAndResult {
CompletionDataAndResult( const CompletionData *completion_data,
const Result &result )
: completion_data_( completion_data ), result_( result ) {}
bool operator< ( const CompletionDataAndResult &other ) const
{
bool operator< ( const CompletionDataAndResult &other ) const {
return result_ < other.result_;
}
@ -80,14 +76,12 @@ ClangCompleter::ClangCompleter()
: candidate_repository_( CandidateRepository::Instance() ),
threading_enabled_( false ),
time_to_die_( false ),
clang_data_ready_( false )
{
clang_data_ready_( false ) {
clang_index_ = clang_createIndex( 0, 0 );
}
ClangCompleter::~ClangCompleter()
{
ClangCompleter::~ClangCompleter() {
// 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();
@ -109,23 +103,21 @@ ClangCompleter::~ClangCompleter()
// We need this mostly so that we can not use it in tests. Apparently the
// GoogleTest framework goes apeshit on us (on some platforms, in some
// occasions) if we enable threads by default.
void ClangCompleter::EnableThreading()
{
void ClangCompleter::EnableThreading() {
threading_enabled_ = true;
InitThreads();
}
std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile(
const std::string &filename )
{
const std::string &filename ) {
shared_ptr< TranslationUnit > unit;
{
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
unit = FindWithDefault(
filename_to_translation_unit_,
filename,
shared_ptr< TranslationUnit >() );
filename_to_translation_unit_,
filename,
shared_ptr< TranslationUnit >() );
}
if ( !unit )
@ -135,15 +127,14 @@ std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile(
}
bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename )
{
bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) {
shared_ptr< TranslationUnit > unit;
{
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
unit = FindWithDefault(
filename_to_translation_unit_,
filename,
shared_ptr< TranslationUnit >() );
filename_to_translation_unit_,
filename,
shared_ptr< TranslationUnit >() );
}
if ( !unit )
@ -154,30 +145,27 @@ bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename )
void ClangCompleter::UpdateTranslationUnit(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags )
{
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) {
bool translation_unit_created;
shared_ptr< TranslationUnit > unit = GetTranslationUnitForFile(
filename,
unsaved_files,
flags,
translation_unit_created );
filename,
unsaved_files,
flags,
translation_unit_created );
if ( !unit )
return;
try
{
try {
// 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 );
}
catch ( ClangParseError& )
{
catch ( ClangParseError & ) {
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
// If unit->Reparse fails, then the underlying TranslationUnit object is not
@ -189,10 +177,9 @@ void ClangCompleter::UpdateTranslationUnit(
Future< void > ClangCompleter::UpdateTranslationUnitAsync(
std::string filename,
std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags )
{
std::string filename,
std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags ) {
function< void() > functor =
bind( &ClangCompleter::UpdateTranslationUnit,
boost::ref( *this ),
@ -214,15 +201,15 @@ Future< void > ClangCompleter::UpdateTranslationUnitAsync(
std::vector< CompletionData >
ClangCompleter::CandidatesForLocationInFile(
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags )
{
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) {
shared_ptr< TranslationUnit > unit = GetTranslationUnitForFile( filename,
unsaved_files,
flags );
if ( !unit )
return std::vector< CompletionData >();
@ -234,13 +221,12 @@ ClangCompleter::CandidatesForLocationInFile(
Future< AsyncCompletions >
ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
const std::string &query,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags )
{
const std::string &query,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) {
// TODO: throw exception when threading is not enabled and this is called
if ( !threading_enabled_ )
return Future< AsyncCompletions >();
@ -248,8 +234,8 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
bool skip_clang_result_cache = ShouldSkipClangResultCache( query,
line,
column );
if ( skip_clang_result_cache )
{
if ( skip_clang_result_cache ) {
// The clang thread is busy, return nothing
if ( UpdatingTranslationUnit( filename ) )
return Future< AsyncCompletions >();
@ -271,8 +257,7 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
unique_future< AsyncCompletions > future;
CreateSortingTask( query, future );
if ( skip_clang_result_cache )
{
if ( skip_clang_result_cache ) {
CreateClangTask( filename, line, column, unsaved_files, flags );
}
@ -282,8 +267,7 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
bool ClangCompleter::ShouldSkipClangResultCache( const std::string &query,
int line,
int column )
{
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'.
@ -292,20 +276,19 @@ bool ClangCompleter::ShouldSkipClangResultCache( const std::string &query,
// 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 );
.NewPositionDifferentFromStoredPosition( line, column );
}
// 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 )
{
const std::string &query,
unique_future< AsyncCompletions > &future ) {
// Careful! The code in this function may burn your eyes.
function< CompletionDatas( const CompletionDatas& ) >
sort_candidates_for_query_functor =
function< CompletionDatas( const CompletionDatas & ) >
sort_candidates_for_query_functor =
bind( &ClangCompleter::SortCandidatesForQuery,
boost::ref( *this ),
query,
@ -327,39 +310,38 @@ void ClangCompleter::CreateSortingTask(
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 );
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 =
bind( &ClangCompleter::CandidatesForLocationInFile,
boost::ref( *this ),
boost::move( filename ),
line,
column,
boost::move( unsaved_files ),
boost::move( flags ) );
function< CompletionDatas() > candidates_for_location_functor =
bind( &ClangCompleter::CandidatesForLocationInFile,
boost::ref( *this ),
boost::move( filename ),
line,
column,
boost::move( unsaved_files ),
boost::move( flags ) );
shared_ptr< ClangPackagedTask > clang_packaged_task =
make_shared< ClangPackagedTask >();
shared_ptr< ClangPackagedTask > clang_packaged_task =
make_shared< ClangPackagedTask >();
clang_packaged_task->completions_task_ = packaged_task< AsyncCompletions >(
bind( ReturnValueAsShared< std::vector< CompletionData > >,
candidates_for_location_functor ) );
clang_packaged_task->completions_task_ =
packaged_task< AsyncCompletions >(
bind( ReturnValueAsShared< std::vector< CompletionData > >,
candidates_for_location_functor ) );
clang_task_.Set( clang_packaged_task );
clang_task_.Set( clang_packaged_task );
}
boost::shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags )
{
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 );
}
@ -371,18 +353,16 @@ boost::shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
// the future a mutex will be needed to make sure that two threads don't try to
// create the same translation unit.
shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool &translation_unit_created )
{
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool &translation_unit_created ) {
{
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
TranslationUnitForFilename::iterator it =
filename_to_translation_unit_.find( filename );
if ( it != filename_to_translation_unit_.end() )
{
if ( it != filename_to_translation_unit_.end() ) {
translation_unit_created = false;
return it->second;
}
@ -390,14 +370,12 @@ shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
shared_ptr< TranslationUnit > unit;
try
{
try {
unit = make_shared< TranslationUnit >(
filename, unsaved_files, flags, clang_index_ );
filename, unsaved_files, flags, clang_index_ );
}
catch ( ClangParseError& )
{
catch ( ClangParseError & ) {
return unit;
}
@ -412,25 +390,24 @@ shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery(
const std::string &query,
const std::vector< CompletionData > &completion_datas )
{
const std::string &query,
const std::vector< CompletionData > &completion_datas ) {
Bitset query_bitset = LetterBitsetFromString( query );
std::vector< const Candidate* > repository_candidates =
std::vector< const Candidate * > repository_candidates =
candidate_repository_.GetCandidatesForStrings( completion_datas );
std::vector< CompletionDataAndResult > data_and_results;
for ( uint i = 0; i < repository_candidates.size(); ++i )
{
const Candidate* candidate = repository_candidates[ i ];
for ( uint i = 0; i < repository_candidates.size(); ++i ) {
const Candidate *candidate = repository_candidates[ i ];
if ( !candidate->MatchesQueryBitset( query_bitset ) )
continue;
Result result = candidate->QueryMatchResult( query );
if ( result.IsSubsequence() )
{
if ( result.IsSubsequence() ) {
CompletionDataAndResult data_and_result( &completion_datas[ i ], result );
data_and_results.push_back( data_and_result );
}
@ -441,8 +418,7 @@ std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery(
std::vector< CompletionData > sorted_completion_datas;
sorted_completion_datas.reserve( data_and_results.size() );
foreach ( const CompletionDataAndResult& data_and_result, data_and_results )
{
foreach ( const CompletionDataAndResult & data_and_result, data_and_results ) {
sorted_completion_datas.push_back( *data_and_result.completion_data_ );
}
@ -450,14 +426,12 @@ std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery(
}
void ClangCompleter::InitThreads()
{
void ClangCompleter::InitThreads() {
int threads_to_create =
std::max( MIN_ASYNC_THREADS,
std::min( MAX_ASYNC_THREADS, thread::hardware_concurrency() ) );
std::min( MAX_ASYNC_THREADS, thread::hardware_concurrency() ) );
for ( int i = 0; i < threads_to_create; ++i )
{
for ( int i = 0; i < threads_to_create; ++i ) {
sorting_threads_.create_thread( bind( &ClangCompleter::SortingThreadMain,
boost::ref( *this ) ) );
}
@ -467,15 +441,13 @@ void ClangCompleter::InitThreads()
}
void ClangCompleter::ClangThreadMain()
{
while ( true )
{
try
{
void ClangCompleter::ClangThreadMain() {
while ( true ) {
try {
shared_ptr< ClangPackagedTask > task = clang_task_.Get();
bool has_completions_task = task->completions_task_.valid();
if ( has_completions_task )
task->completions_task_();
else
@ -496,28 +468,25 @@ void ClangCompleter::ClangThreadMain()
clang_data_ready_condition_variable_.notify_all();
}
catch ( thread_interrupted& )
{
catch ( thread_interrupted & ) {
shared_lock< shared_mutex > lock( time_to_die_mutex_ );
if ( time_to_die_ )
return;
// else do nothing and re-enter the loop
}
}
}
void ClangCompleter::SortingThreadMain()
{
while ( true )
{
try
{
void ClangCompleter::SortingThreadMain() {
while ( true ) {
try {
{
unique_lock< mutex > lock( clang_data_ready_mutex_ );
while ( !clang_data_ready_ )
{
while ( !clang_data_ready_ ) {
clang_data_ready_condition_variable_.wait( lock );
}
}
@ -528,11 +497,12 @@ void ClangCompleter::SortingThreadMain()
( *task )();
}
catch ( thread_interrupted& )
{
catch ( thread_interrupted & ) {
shared_lock< shared_mutex > lock( time_to_die_mutex_ );
if ( time_to_die_ )
return;
// else do nothing and re-enter the loop
}
}

View File

@ -33,8 +33,7 @@
typedef void *CXIndex;
typedef struct CXTranslationUnitImpl *CXTranslationUnit;
namespace YouCompleteMe
{
namespace YouCompleteMe {
class CandidateRepository;
class TranslationUnit;
@ -44,17 +43,16 @@ typedef std::vector< CompletionData > CompletionDatas;
typedef boost::shared_ptr< CompletionDatas > AsyncCompletions;
typedef boost::unordered_map< std::string,
boost::shared_ptr<
std::vector< std::string > > > FlagsForFile;
typedef boost::unordered_map < std::string,
boost::shared_ptr <
std::vector< std::string > > > FlagsForFile;
typedef boost::unordered_map< std::string,
boost::shared_ptr< TranslationUnit > > TranslationUnitForFilename;
typedef boost::unordered_map < std::string,
boost::shared_ptr< TranslationUnit > > TranslationUnitForFilename;
// TODO: document that all filename parameters must be absolute paths
class ClangCompleter : boost::noncopyable
{
class ClangCompleter : boost::noncopyable {
public:
ClangCompleter();
~ClangCompleter();
@ -67,49 +65,48 @@ public:
// Public because of unit tests (gtest is not very thread-friendly)
void UpdateTranslationUnit(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
// NOTE: params are taken by value on purpose! With a C++11 compiler we can
// avoid internal copies if params are taken by value (move ctors FTW)
Future< void > UpdateTranslationUnitAsync(
std::string filename,
std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags );
std::string filename,
std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags );
// Public because of unit tests (gtest is not very thread-friendly)
std::vector< CompletionData > CandidatesForLocationInFile(
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
Future< AsyncCompletions > CandidatesForQueryAndLocationInFileAsync(
const std::string &query,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
const std::string &query,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
private:
// This is basically a union. Only one of the two tasks is set to something
// valid, the other task is invalid. Which one is valid depends on the caller.
struct ClangPackagedTask
{
struct ClangPackagedTask {
boost::packaged_task< AsyncCompletions > completions_task_;
boost::packaged_task< void > parsing_task_;
};
typedef ConcurrentLatestValue<
boost::shared_ptr<
boost::packaged_task< AsyncCompletions > > > LatestSortingTask;
typedef ConcurrentLatestValue <
boost::shared_ptr <
boost::packaged_task< AsyncCompletions > > > LatestSortingTask;
typedef ConcurrentLatestValue<
boost::shared_ptr< ClangPackagedTask > > LatestClangTask;
typedef ConcurrentLatestValue <
boost::shared_ptr< ClangPackagedTask > > LatestClangTask;
bool ShouldSkipClangResultCache( const std::string &query,
int line,
@ -121,26 +118,26 @@ private:
// NOTE: params are taken by value on purpose! With a C++11 compiler we can
// avoid internal copies if params are taken by value (move ctors FTW)
void CreateClangTask(
std::string filename,
int line,
int column,
std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags );
std::string filename,
int line,
int column,
std::vector< UnsavedFile > unsaved_files,
std::vector< std::string > flags );
boost::shared_ptr< TranslationUnit > GetTranslationUnitForFile(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
boost::shared_ptr< TranslationUnit > GetTranslationUnitForFile(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool &translation_unit_created );
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool &translation_unit_created );
std::vector< CompletionData > SortCandidatesForQuery(
const std::string &query,
const std::vector< CompletionData > &completion_datas );
const std::string &query,
const std::vector< CompletionData > &completion_datas );
void InitThreads();

View File

@ -22,19 +22,17 @@ using boost::shared_mutex;
using boost::shared_lock;
using boost::unique_lock;
namespace YouCompleteMe
{
namespace YouCompleteMe {
bool ClangResultsCache::NewPositionDifferentFromStoredPosition( int new_line,
int new_colum )
const
{
const {
shared_lock< shared_mutex > reader_lock( access_mutex_ );
return line_ != new_line || column_ != new_colum;
}
void ClangResultsCache::ResetWithNewLineAndColumn( int new_line, int new_colum )
{
void ClangResultsCache::ResetWithNewLineAndColumn( int new_line,
int new_colum ) {
unique_lock< shared_mutex > reader_lock( access_mutex_ );
line_ = new_line;

View File

@ -27,24 +27,22 @@
#include <boost/config.hpp>
#include <boost/utility.hpp>
namespace YouCompleteMe
{
namespace YouCompleteMe {
struct CompletionData;
class ClangResultsCache : boost::noncopyable
{
class ClangResultsCache : boost::noncopyable {
public:
ClangResultsCache() : line_( -1 ), column_( -1 ) {}
bool NewPositionDifferentFromStoredPosition( int new_line, int new_colum )
const;
const;
void ResetWithNewLineAndColumn( int new_line, int new_colum );
void SetCompletionDatas( const std::vector< CompletionData > new_completions )
{
void SetCompletionDatas(
const std::vector< CompletionData > new_completions ) {
completion_datas_ = new_completions;
}
@ -54,8 +52,7 @@ public:
# pragma clang diagnostic ignored "-Wc++98-compat"
# endif //#ifdef __clang__
void SetCompletionDatas( std::vector< CompletionData >&& new_completions )
{
void SetCompletionDatas( std::vector< CompletionData > && new_completions ) {
completion_datas_ = new_completions;
}
@ -66,9 +63,8 @@ public:
template< typename T >
T OperateOnCompletionDatas(
boost::function< T( const std::vector< CompletionData >& ) > operation )
const
{
boost::function< T( const std::vector< CompletionData >& ) > operation )
const {
boost::shared_lock< boost::shared_mutex > reader_lock( access_mutex_ );
return operation( completion_datas_ );
}

View File

@ -26,12 +26,11 @@
#include <boost/unordered_map.hpp>
using boost::unordered_map;
namespace YouCompleteMe
{
namespace YouCompleteMe {
std::string CXStringToString( CXString text )
{
std::string CXStringToString( CXString text ) {
std::string final_string;
if ( !text.data )
return final_string;
@ -40,30 +39,31 @@ std::string CXStringToString( CXString text )
return final_string;
}
namespace
{
namespace {
// NOTE: The passed in pointer should never be NULL!
std::string FullDiagnosticText( CXDiagnostic cxdiagnostic )
{
std::string full_text = CXStringToString( clang_formatDiagnostic(
cxdiagnostic,
clang_defaultDiagnosticDisplayOptions() ) );
std::string FullDiagnosticText( CXDiagnostic cxdiagnostic ) {
std::string full_text = CXStringToString(
clang_formatDiagnostic(
cxdiagnostic,
clang_defaultDiagnosticDisplayOptions() ) );
// Note: clang docs say that a CXDiagnosticSet retrieved with
// clang_getChildDiagnostics do NOT need to be released with
// clang_diposeDiagnosticSet
CXDiagnosticSet diag_set = clang_getChildDiagnostics( cxdiagnostic );
if ( !diag_set )
return full_text;
uint num_child_diagnostics = clang_getNumDiagnosticsInSet( diag_set );
if ( !num_child_diagnostics )
return full_text;
for ( uint i = 0; i < num_child_diagnostics; ++i )
{
for ( uint i = 0; i < num_child_diagnostics; ++i ) {
CXDiagnostic diagnostic = clang_getDiagnosticInSet( diag_set, i );
if ( !diagnostic )
continue;
@ -74,10 +74,8 @@ std::string FullDiagnosticText( CXDiagnostic cxdiagnostic )
}
char DiagnosticSeverityToType( CXDiagnosticSeverity severity )
{
switch ( severity )
{
char DiagnosticSeverityToType( CXDiagnosticSeverity severity ) {
switch ( severity ) {
case CXDiagnostic_Ignored:
case CXDiagnostic_Note:
return 'I';
@ -98,23 +96,22 @@ char DiagnosticSeverityToType( CXDiagnosticSeverity severity )
// 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 )
{
bool CompletionStringAvailable( CXCompletionString completion_string ) {
if ( !completion_string )
return false;
return clang_getCompletionAvailability( completion_string ) ==
CXAvailability_Available;
CXAvailability_Available;
}
} // unnamed namespace
std::vector< CXUnsavedFile > ToCXUnsavedFiles(
const std::vector< UnsavedFile > &unsaved_files )
{
const std::vector< UnsavedFile > &unsaved_files ) {
std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() );
for ( uint i = 0; i < unsaved_files.size(); ++i )
{
for ( uint i = 0; i < unsaved_files.size(); ++i ) {
X_VERIFY( unsaved_files[ i ].filename_ );
X_VERIFY( unsaved_files[ i ].contents_ );
X_VERIFY( unsaved_files[ i ].length_ );
@ -128,17 +125,16 @@ std::vector< CXUnsavedFile > ToCXUnsavedFiles(
std::vector< CompletionData > ToCompletionDataVector(
CXCodeCompleteResults *results )
{
CXCodeCompleteResults *results ) {
std::vector< CompletionData > completions;
if ( !results || !results->Results )
return completions;
completions.reserve( results->NumResults );
unordered_map< std::string, uint > seen_data;
for ( uint i = 0; i < results->NumResults; ++i )
{
for ( uint i = 0; i < results->NumResults; ++i ) {
CXCompletionResult completion_result = results->Results[ i ];
if ( !CompletionStringAvailable( completion_result.CompletionString ) )
@ -149,21 +145,19 @@ std::vector< CompletionData > ToCompletionDataVector(
data.original_string_,
completions.size() );
if ( index == completions.size() )
{
if ( index == completions.size() ) {
completions.push_back( boost::move( data ) );
}
else
{
else {
// If we have already seen this completion, then this is an overload of a
// function we have seen. We add the signature of the overload to the
// detailed information.
completions[ index ].detailed_info_
.append( data.return_type_ )
.append( " " )
.append( data.everything_except_return_type_ )
.append( "\n" );
.append( data.return_type_ )
.append( " " )
.append( data.everything_except_return_type_ )
.append( "\n" );
}
}
@ -171,14 +165,14 @@ std::vector< CompletionData > ToCompletionDataVector(
}
Diagnostic CXDiagnosticToDiagnostic( CXDiagnostic cxdiagnostic )
{
Diagnostic CXDiagnosticToDiagnostic( CXDiagnostic cxdiagnostic ) {
Diagnostic diagnostic;
if ( !cxdiagnostic )
return diagnostic;
diagnostic.kind_ = DiagnosticSeverityToType(
clang_getDiagnosticSeverity( cxdiagnostic ) );
clang_getDiagnosticSeverity( cxdiagnostic ) );
// If this is an "ignored" diagnostic, there's no point in continuing since we
// won't display those to the user
@ -196,7 +190,7 @@ Diagnostic CXDiagnosticToDiagnostic( CXDiagnostic cxdiagnostic )
diagnostic.filename_ = CXStringToString( clang_getFileName( file ) );
diagnostic.text_ = CXStringToString(
clang_getDiagnosticSpelling( cxdiagnostic ) );
clang_getDiagnosticSpelling( cxdiagnostic ) );
diagnostic.long_formatted_text_ = FullDiagnosticText( cxdiagnostic );
clang_disposeDiagnostic( cxdiagnostic );

View File

@ -25,16 +25,15 @@
#include <vector>
#include <clang-c/Index.h>
namespace YouCompleteMe
{
namespace YouCompleteMe {
std::string CXStringToString( CXString text );
std::vector< CompletionData > ToCompletionDataVector(
CXCodeCompleteResults *results );
CXCodeCompleteResults *results );
std::vector< CXUnsavedFile > ToCXUnsavedFiles(
const std::vector< UnsavedFile > &unsaved_files );
const std::vector< UnsavedFile > &unsaved_files );
Diagnostic CXDiagnosticToDiagnostic( CXDiagnostic cxdiagnostic );

View File

@ -19,46 +19,43 @@
#include "ClangUtils.h"
#include "standard.h"
namespace YouCompleteMe
{
namespace YouCompleteMe {
CompilationDatabase::CompilationDatabase(
const std::string& path_to_directory )
: is_loaded_( false )
{
const std::string &path_to_directory )
: is_loaded_( false ) {
CXCompilationDatabase_Error status;
compilation_database_ = clang_CompilationDatabase_fromDirectory(
path_to_directory.c_str(),
&status );
path_to_directory.c_str(),
&status );
is_loaded_ = status == CXCompilationDatabase_NoError;
}
CompilationDatabase::~CompilationDatabase()
{
CompilationDatabase::~CompilationDatabase() {
clang_CompilationDatabase_dispose( compilation_database_ );
}
bool CompilationDatabase::DatabaseSuccessfullyLoaded()
{
bool CompilationDatabase::DatabaseSuccessfullyLoaded() {
return is_loaded_;
}
std::vector< std::string > CompilationDatabase::FlagsForFile(
const std::string &path_to_file )
{
const std::string &path_to_file ) {
std::vector< std::string > flags;
if ( !is_loaded_ )
return flags;
CXCompileCommands commands =
clang_CompilationDatabase_getCompileCommands(
compilation_database_,
path_to_file.c_str() );
clang_CompilationDatabase_getCompileCommands(
compilation_database_,
path_to_file.c_str() );
uint num_commands = clang_CompileCommands_getSize( commands );
if ( num_commands < 1 ) {
clang_CompileCommands_dispose( commands );
return flags;
@ -66,15 +63,15 @@ std::vector< std::string > CompilationDatabase::FlagsForFile(
// We always pick the first command offered
CXCompileCommand command = clang_CompileCommands_getCommand(
commands,
0);
commands,
0 );
uint num_flags = clang_CompileCommand_getNumArgs( command );
flags.reserve( num_flags );
for ( uint i = 0; i < num_flags; ++i )
{
for ( uint i = 0; i < num_flags; ++i ) {
flags.push_back( CXStringToString(
clang_CompileCommand_getArg( command, i ) ) );
clang_CompileCommand_getArg( command, i ) ) );
}
clang_CompileCommands_dispose( commands );
@ -83,18 +80,19 @@ std::vector< std::string > CompilationDatabase::FlagsForFile(
std::string CompilationDatabase::CompileCommandWorkingDirectoryForFile(
const std::string &path_to_file )
{
const std::string &path_to_file ) {
std::string path_to_directory;
if ( !is_loaded_ )
return path_to_directory;
CXCompileCommands commands =
clang_CompilationDatabase_getCompileCommands(
compilation_database_,
path_to_file.c_str() );
clang_CompilationDatabase_getCompileCommands(
compilation_database_,
path_to_file.c_str() );
uint num_commands = clang_CompileCommands_getSize( commands );
if ( num_commands < 1 ) {
clang_CompileCommands_dispose( commands );
return path_to_directory;
@ -102,11 +100,11 @@ std::string CompilationDatabase::CompileCommandWorkingDirectoryForFile(
// We always pick the first command offered
CXCompileCommand command = clang_CompileCommands_getCommand(
commands,
0);
commands,
0 );
path_to_directory = CXStringToString( clang_CompileCommand_getDirectory(
command ) );
command ) );
return path_to_directory;
}

View File

@ -23,13 +23,11 @@
#include <boost/utility.hpp>
#include <clang-c/CXCompilationDatabase.h>
namespace YouCompleteMe
{
namespace YouCompleteMe {
class CompilationDatabase : boost::noncopyable
{
class CompilationDatabase : boost::noncopyable {
public:
CompilationDatabase( const std::string& path_to_directory );
CompilationDatabase( const std::string &path_to_directory );
~CompilationDatabase();
bool DatabaseSuccessfullyLoaded();
@ -37,7 +35,7 @@ public:
std::vector< std::string > FlagsForFile( const std::string &path_to_file );
std::string CompileCommandWorkingDirectoryForFile(
const std::string &path_to_file );
const std::string &path_to_file );
private:
bool is_loaded_;

View File

@ -21,15 +21,12 @@
#include <boost/algorithm/string/erase.hpp>
#include <boost/move/move.hpp>
namespace
{
namespace {
char CursorKindToVimKind( CXCursorKind kind )
{
char CursorKindToVimKind( CXCursorKind kind ) {
// TODO: actually it appears that Vim will show returned kinds even when they
// do not match the "approved" list, so let's use that
switch ( kind )
{
switch ( kind ) {
case CXCursor_UnexposedDecl:
case CXCursor_StructDecl:
case CXCursor_UnionDecl:
@ -58,8 +55,7 @@ char CursorKindToVimKind( CXCursorKind kind )
}
bool IsMainCompletionTextInfo( CXCompletionChunkKind kind )
{
bool IsMainCompletionTextInfo( CXCompletionChunkKind kind ) {
return
kind == CXCompletionChunk_Optional ||
kind == CXCompletionChunk_TypedText ||
@ -83,19 +79,19 @@ bool IsMainCompletionTextInfo( CXCompletionChunkKind kind )
std::string ChunkToString( CXCompletionString completion_string,
uint chunk_num )
{
uint chunk_num ) {
if ( !completion_string )
return std::string();
return YouCompleteMe::CXStringToString(
clang_getCompletionChunkText( completion_string, chunk_num ) );
clang_getCompletionChunkText( completion_string, chunk_num ) );
}
std::string OptionalChunkToString( CXCompletionString completion_string,
uint chunk_num )
{
uint chunk_num ) {
std::string final_string;
if ( !completion_string )
return final_string;
@ -106,21 +102,18 @@ std::string OptionalChunkToString( CXCompletionString completion_string,
return final_string;
uint optional_num_chunks = clang_getNumCompletionChunks(
optional_completion_string );
optional_completion_string );
for ( uint j = 0; j < optional_num_chunks; ++j )
{
for ( uint j = 0; j < optional_num_chunks; ++j ) {
CXCompletionChunkKind kind = clang_getCompletionChunkKind(
optional_completion_string, j );
optional_completion_string, j );
if ( kind == CXCompletionChunk_Optional )
{
if ( kind == CXCompletionChunk_Optional ) {
final_string.append( OptionalChunkToString( optional_completion_string,
j ) );
}
else
{
else {
final_string.append( ChunkToString( optional_completion_string, j ) );
}
}
@ -134,8 +127,7 @@ std::string OptionalChunkToString( CXCompletionString completion_string,
// the parameter BUT if this code is compiled in C++11 mode a move constructor
// can be called on the passed-in value. This is not possible if we accept the
// param by const ref.
std::string RemoveTwoConsecutiveUnderscores( std::string text )
{
std::string RemoveTwoConsecutiveUnderscores( std::string text ) {
boost::erase_all( text, "__" );
return text;
}
@ -143,11 +135,9 @@ std::string RemoveTwoConsecutiveUnderscores( std::string text )
} // unnamed namespace
namespace YouCompleteMe
{
namespace YouCompleteMe {
CompletionData::CompletionData( const CXCompletionResult &completion_result )
{
CompletionData::CompletionData( const CXCompletionResult &completion_result ) {
CXCompletionString completion_string = completion_result.CompletionString;
if ( !completion_string )
@ -157,8 +147,7 @@ CompletionData::CompletionData( const CXCompletionResult &completion_result )
bool saw_left_paren = false;
bool saw_function_params = false;
for ( uint j = 0; j < num_chunks; ++j )
{
for ( uint j = 0; j < num_chunks; ++j ) {
ExtractDataFromChunk( completion_string,
j,
saw_left_paren,
@ -175,54 +164,47 @@ CompletionData::CompletionData( const CXCompletionResult &completion_result )
// compiler-reserved.
everything_except_return_type_ =
RemoveTwoConsecutiveUnderscores(
boost::move( everything_except_return_type_ ) );
boost::move( everything_except_return_type_ ) );
detailed_info_.append( return_type_ )
.append( " " )
.append( everything_except_return_type_ )
.append( "\n" );
.append( " " )
.append( everything_except_return_type_ )
.append( "\n" );
}
void CompletionData::ExtractDataFromChunk( CXCompletionString completion_string,
uint chunk_num,
bool &saw_left_paren,
bool &saw_function_params )
{
bool &saw_function_params ) {
CXCompletionChunkKind kind = clang_getCompletionChunkKind(
completion_string, chunk_num );
completion_string, chunk_num );
if ( IsMainCompletionTextInfo( kind ) )
{
if ( kind == CXCompletionChunk_LeftParen )
{
if ( IsMainCompletionTextInfo( kind ) ) {
if ( kind == CXCompletionChunk_LeftParen ) {
saw_left_paren = true;
}
else if ( saw_left_paren &&
!saw_function_params &&
kind != CXCompletionChunk_RightParen &&
kind != CXCompletionChunk_Informative )
{
kind != CXCompletionChunk_Informative ) {
saw_function_params = true;
everything_except_return_type_.append( " " );
}
else if ( saw_function_params && kind == CXCompletionChunk_RightParen )
{
else if ( saw_function_params && kind == CXCompletionChunk_RightParen ) {
everything_except_return_type_.append( " " );
}
if ( kind == CXCompletionChunk_Optional )
{
if ( kind == CXCompletionChunk_Optional ) {
everything_except_return_type_.append(
OptionalChunkToString( completion_string, chunk_num ) );
OptionalChunkToString( completion_string, chunk_num ) );
}
else
{
else {
everything_except_return_type_.append(
ChunkToString( completion_string, chunk_num ) );
ChunkToString( completion_string, chunk_num ) );
}
}

View File

@ -22,8 +22,7 @@
#include <string>
#include <clang-c/Index.h>
namespace YouCompleteMe
{
namespace YouCompleteMe {
// This class holds pieces of information about a single completion coming from
// clang. These pieces are shown in Vim's UI in different ways.
@ -37,16 +36,14 @@ namespace YouCompleteMe
//
// The user can also enable a "preview" window that will show extra information
// about a completion at the top of the buffer.
struct CompletionData
{
struct CompletionData {
CompletionData() {}
CompletionData( const CXCompletionResult &completion_result );
// What should actually be inserted into the buffer. For a function like
// "int foo(int x)", this is just "foo". Same for a data member like "foo_":
// we insert just "foo_".
std::string TextToInsertInBuffer()
{
std::string TextToInsertInBuffer() {
return original_string_;
}
@ -55,29 +52,25 @@ struct CompletionData
// the completion is, say, a data member. So for a function like "int foo(int
// x)", this would be "foo(int x)". For a data member like "count_", it would
// be just "count_".
std::string MainCompletionText()
{
std::string MainCompletionText() {
return everything_except_return_type_;
}
// This is extra info shown in the pop-up completion menu, after the
// completion text and the kind. Currently we put the return type of the
// function here, if any.
std::string ExtraMenuInfo()
{
std::string ExtraMenuInfo() {
return return_type_;
}
// This is used to show extra information in vim's preview window. This is the
// window that vim usually shows at the top of the buffer. This should be used
// for extra information about the completion.
std::string DetailedInfoForPreviewWindow()
{
std::string DetailedInfoForPreviewWindow() {
return detailed_info_;
}
bool operator== ( const CompletionData &other ) const
{
bool operator== ( const CompletionData &other ) const {
return
kind_ == other.kind_ &&
everything_except_return_type_ == other.everything_except_return_type_ &&

View File

@ -21,13 +21,10 @@
#include "standard.h"
#include <string>
namespace YouCompleteMe
{
namespace YouCompleteMe {
struct Diagnostic
{
bool operator== ( const Diagnostic &other ) const
{
struct Diagnostic {
bool operator== ( const Diagnostic &other ) const {
return
line_number_ == other.line_number_ &&
column_number_ == other.column_number_ &&

View File

@ -28,36 +28,33 @@ using boost::unique_lock;
using boost::mutex;
using boost::try_to_lock_t;
namespace YouCompleteMe
{
namespace YouCompleteMe {
TranslationUnit::TranslationUnit(
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
CXIndex clang_index )
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
CXIndex clang_index )
: filename_( filename ),
clang_translation_unit_( NULL )
{
std::vector< const char* > pointer_flags;
clang_translation_unit_( NULL ) {
std::vector< const char * > pointer_flags;
pointer_flags.reserve( flags.size() );
foreach ( const std::string &flag, flags )
{
foreach ( const std::string & flag, flags ) {
pointer_flags.push_back( flag.c_str() );
}
std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles(
unsaved_files );
std::vector< CXUnsavedFile > cxunsaved_files =
ToCXUnsavedFiles( unsaved_files );
clang_translation_unit_ = clang_parseTranslationUnit(
clang_index,
filename.c_str(),
&pointer_flags[ 0 ],
pointer_flags.size(),
&cxunsaved_files[ 0 ],
cxunsaved_files.size(),
clang_defaultEditingTranslationUnitOptions() );
clang_index,
filename.c_str(),
&pointer_flags[ 0 ],
pointer_flags.size(),
&cxunsaved_files[ 0 ],
cxunsaved_files.size(),
clang_defaultEditingTranslationUnitOptions() );
if ( !clang_translation_unit_ )
boost_throw( ClangParseError() );
@ -68,15 +65,13 @@ TranslationUnit::TranslationUnit(
}
TranslationUnit::~TranslationUnit()
{
TranslationUnit::~TranslationUnit() {
if ( clang_translation_unit_ )
clang_disposeTranslationUnit( clang_translation_unit_ );
}
std::vector< Diagnostic > TranslationUnit::LatestDiagnostics()
{
std::vector< Diagnostic > TranslationUnit::LatestDiagnostics() {
std::vector< Diagnostic > diagnostics;
unique_lock< mutex > lock( diagnostics_mutex_ );
@ -95,34 +90,32 @@ std::vector< Diagnostic > TranslationUnit::LatestDiagnostics()
}
bool TranslationUnit::IsCurrentlyUpdating() const
{
bool TranslationUnit::IsCurrentlyUpdating() const {
unique_lock< mutex > lock( clang_access_mutex_, try_to_lock_t() );
return !lock.owns_lock();
}
void TranslationUnit::Reparse( const std::vector< UnsavedFile > &unsaved_files )
{
std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles(
unsaved_files );
void TranslationUnit::Reparse(
const std::vector< UnsavedFile > &unsaved_files ) {
std::vector< CXUnsavedFile > cxunsaved_files =
ToCXUnsavedFiles( unsaved_files );
Reparse( cxunsaved_files );
}
std::vector< CompletionData > TranslationUnit::CandidatesForLocation(
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files )
{
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files ) {
unique_lock< mutex > lock( clang_access_mutex_ );
if ( !clang_translation_unit_ )
return std::vector< CompletionData >();
std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles(
unsaved_files );
std::vector< CXUnsavedFile > cxunsaved_files =
ToCXUnsavedFiles( unsaved_files );
// 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.
@ -153,21 +146,19 @@ std::vector< CompletionData > TranslationUnit::CandidatesForLocation(
// non-const pointer to clang. This function (and clang too) will not modify the
// param though.
void TranslationUnit::Reparse(
std::vector< CXUnsavedFile > &unsaved_files )
{
std::vector< CXUnsavedFile > &unsaved_files ) {
unique_lock< mutex > lock( clang_access_mutex_ );
if ( !clang_translation_unit_ )
return;
int failure = clang_reparseTranslationUnit(
clang_translation_unit_,
unsaved_files.size(),
&unsaved_files[ 0 ],
clang_defaultEditingTranslationUnitOptions() );
clang_translation_unit_,
unsaved_files.size(),
&unsaved_files[ 0 ],
clang_defaultEditingTranslationUnitOptions() );
if ( failure )
{
if ( failure ) {
clang_disposeTranslationUnit( clang_translation_unit_ );
clang_translation_unit_ = NULL;
boost_throw( ClangParseError() );
@ -178,17 +169,16 @@ void TranslationUnit::Reparse(
// Should only be called while holding the clang_access_mutex_
void TranslationUnit::UpdateLatestDiagnostics()
{
void TranslationUnit::UpdateLatestDiagnostics() {
unique_lock< mutex > lock( diagnostics_mutex_ );
latest_diagnostics_.clear();
uint num_diagnostics = clang_getNumDiagnostics( clang_translation_unit_ );
latest_diagnostics_.reserve( num_diagnostics );
for ( uint i = 0; i < num_diagnostics; ++i )
{
Diagnostic diagnostic = CXDiagnosticToDiagnostic(
for ( uint i = 0; i < num_diagnostics; ++i ) {
Diagnostic diagnostic =
CXDiagnosticToDiagnostic(
clang_getDiagnostic( clang_translation_unit_, i ) );
if ( diagnostic.kind_ != 'I' )

View File

@ -33,14 +33,12 @@ typedef void *CXIndex;
typedef struct CXTranslationUnitImpl *CXTranslationUnit;
struct CXUnsavedFile;
namespace YouCompleteMe
{
namespace YouCompleteMe {
struct CompletionData;
typedef boost::shared_ptr< std::vector< CompletionData > > AsyncCompletions;
class TranslationUnit : boost::noncopyable
{
class TranslationUnit : boost::noncopyable {
public:
TranslationUnit(
@ -58,9 +56,9 @@ public:
void Reparse( const std::vector< UnsavedFile > &unsaved_files );
std::vector< CompletionData > CandidatesForLocation(
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files );
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files );
private:

View File

@ -20,8 +20,7 @@
#include <cstddef>
struct UnsavedFile
{
struct UnsavedFile {
UnsavedFile() : filename_( NULL ), contents_( NULL ), length_( 0 ) {}
const char *filename_;
@ -32,8 +31,7 @@ struct UnsavedFile
// methods. I have no clue why, but it won't compile without it.
// TODO: report this problem on the Boost bug tracker, the default equality
// operator should be more than adequate here
bool operator== ( const UnsavedFile &other ) const
{
bool operator== ( const UnsavedFile &other ) const {
return
filename_ == other.filename_ &&
contents_ == other.contents_ &&