Avoid an extra clang reparse if not needed

This commit is contained in:
Strahinja Val Markovic 2012-08-13 21:06:44 -07:00
parent bb6244e090
commit a4d344aa36
2 changed files with 33 additions and 6 deletions

View File

@ -157,16 +157,22 @@ void ClangCompleter::UpdateTranslationUnit(
const std::vector< UnsavedFile > &unsaved_files, const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) const std::vector< std::string > &flags )
{ {
shared_ptr< TranslationUnit > unit = GetTranslationUnitForFile( filename, bool translation_unit_created;
unsaved_files, shared_ptr< TranslationUnit > unit = GetTranslationUnitForFile(
flags ); filename,
unsaved_files,
flags,
translation_unit_created );
if ( !unit ) if ( !unit )
return; return;
try try
{ {
// TODO: only do this if the unit was not just created // There's no point in reparsing a TU that was just created, it was just
unit->Reparse( unsaved_files ); // parsed in the TU constructor
if ( !translation_unit_created )
unit->Reparse( unsaved_files );
} }
catch ( ClangParseError& ) catch ( ClangParseError& )
@ -348,6 +354,16 @@ void ClangCompleter::CreateClangTask(
} }
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 );
}
// WARNING: It should not be possible to call this function from two separate // 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 // 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 // calls this function so there is no need for a mutex, but if that changes in
@ -356,7 +372,8 @@ void ClangCompleter::CreateClangTask(
shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile( shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
const std::string &filename, const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files, const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) const std::vector< std::string > &flags,
bool &translation_unit_created )
{ {
{ {
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ ); lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
@ -364,7 +381,10 @@ shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
filename_to_translation_unit_.find( filename ); 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; return it->second;
}
} }
shared_ptr< TranslationUnit > unit; shared_ptr< TranslationUnit > unit;
@ -385,6 +405,7 @@ shared_ptr< TranslationUnit > ClangCompleter::GetTranslationUnitForFile(
filename_to_translation_unit_[ filename ] = unit; filename_to_translation_unit_[ filename ] = unit;
} }
translation_unit_created = true;
return unit; return unit;
} }

View File

@ -132,6 +132,12 @@ private:
const std::vector< UnsavedFile > &unsaved_files, const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ); 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 );
std::vector< CompletionData > SortCandidatesForQuery( std::vector< CompletionData > SortCandidatesForQuery(
const std::string &query, const std::string &query,
const std::vector< CompletionData > &completion_datas ); const std::vector< CompletionData > &completion_datas );