2013-09-30 16:40:25 -04:00
|
|
|
// Copyright (C) 2011-2013 Strahinja Val Markovic <val@markovic.io>
|
2012-07-08 14:54:57 -04:00
|
|
|
//
|
|
|
|
// 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-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>
|
2013-09-30 16:40:25 -04:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
using boost::shared_ptr;
|
2012-07-27 00:43:20 -04:00
|
|
|
using boost::unordered_map;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
namespace YouCompleteMe {
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
ClangCompleter::ClangCompleter()
|
2013-08-12 18:44:12 -04:00
|
|
|
: clang_index_( clang_createIndex( 0, 0 ) ),
|
2013-09-30 16:40:25 -04:00
|
|
|
translation_unit_store_( clang_index_ ) {
|
2013-01-26 13:46:26 -05:00
|
|
|
// The libclang docs don't say what is the default value for crash recovery.
|
|
|
|
// I'm pretty sure it's turned on by default, but I'm not going to take any
|
|
|
|
// chances.
|
|
|
|
clang_toggleCrashRecovery( true );
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
ClangCompleter::~ClangCompleter() {
|
2013-08-12 18:44:12 -04:00
|
|
|
// We need to destroy all TUs before calling clang_disposeIndex because
|
|
|
|
// the translation units need to be destroyed before the index is destroyed.
|
2013-09-30 16:40:25 -04:00
|
|
|
// Technically, a thread could still be holding onto a shared_ptr<TU> object
|
|
|
|
// when we destroy the clang index, but since we're shutting down, we don't
|
|
|
|
// really care.
|
|
|
|
// In practice, this situation shouldn't happen because the server threads are
|
|
|
|
// Python deamon threads and will all be killed before the main thread exits.
|
2013-08-12 18:44:12 -04:00
|
|
|
translation_unit_store_.RemoveAll();
|
2013-02-16 15:24:00 -05:00
|
|
|
clang_disposeIndex( clang_index_ );
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile(
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::string &filename ) {
|
2013-08-12 18:44:12 -04:00
|
|
|
shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) {
|
2013-08-12 18:44:12 -04:00
|
|
|
shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename );
|
2012-08-11 22:27:15 -04:00
|
|
|
|
|
|
|
if ( !unit )
|
|
|
|
return false;
|
|
|
|
|
2013-01-24 21:37:44 -05:00
|
|
|
// Thankfully, an invalid, sentinel TU always returns true for
|
|
|
|
// IsCurrentlyUpdating, so no caller will try to rely on the TU object, even
|
|
|
|
// if unit is currently pointing to a sentinel.
|
2012-08-11 22:27:15 -04:00
|
|
|
return unit->IsCurrentlyUpdating();
|
2012-07-26 23:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
void ClangCompleter::UpdateTranslationUnit(
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::string &filename,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags ) {
|
2012-08-14 00:06:44 -04:00
|
|
|
bool translation_unit_created;
|
2013-08-12 18:44:12 -04:00
|
|
|
shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate(
|
|
|
|
filename,
|
|
|
|
unsaved_files,
|
|
|
|
flags,
|
|
|
|
translation_unit_created );
|
2012-08-14 00:06:44 -04:00
|
|
|
|
2012-08-13 23:51:04 -04:00
|
|
|
if ( !unit )
|
|
|
|
return;
|
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
catch ( ClangParseError & ) {
|
2012-08-13 23:51:04 -04:00
|
|
|
// 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.
|
2013-08-12 18:44:12 -04:00
|
|
|
translation_unit_store_.Remove( filename );
|
2012-08-13 23:51:04 -04:00
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-11 22:27:15 -04:00
|
|
|
std::vector< CompletionData >
|
|
|
|
ClangCompleter::CandidatesForLocationInFile(
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::string &filename,
|
2013-01-19 23:03:32 -05:00
|
|
|
int line,
|
|
|
|
int column,
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags ) {
|
2013-08-12 18:44:12 -04:00
|
|
|
shared_ptr< TranslationUnit > unit =
|
|
|
|
translation_unit_store_.GetOrCreate( filename, unsaved_files, flags );
|
2013-01-19 23:03:32 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
Location ClangCompleter::GetDeclarationLocation(
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::string &filename,
|
2013-04-01 22:09:37 -04:00
|
|
|
int line,
|
|
|
|
int column,
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags ) {
|
2013-08-12 18:44:12 -04:00
|
|
|
shared_ptr< TranslationUnit > unit =
|
|
|
|
translation_unit_store_.GetOrCreate( filename, unsaved_files, flags );
|
2013-04-29 22:24:28 -04:00
|
|
|
|
|
|
|
if ( !unit ) {
|
2013-04-03 14:58:37 -04:00
|
|
|
return Location();
|
|
|
|
}
|
2013-04-29 22:24:28 -04:00
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
return unit->GetDeclarationLocation( line, column, unsaved_files );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Location ClangCompleter::GetDefinitionLocation(
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::string &filename,
|
2013-04-01 22:09:37 -04:00
|
|
|
int line,
|
|
|
|
int column,
|
2013-10-01 13:12:50 -04:00
|
|
|
const std::vector< UnsavedFile > &unsaved_files,
|
|
|
|
const std::vector< std::string > &flags ) {
|
2013-08-12 18:44:12 -04:00
|
|
|
shared_ptr< TranslationUnit > unit =
|
|
|
|
translation_unit_store_.GetOrCreate( filename, unsaved_files, flags );
|
2013-04-29 22:24:28 -04:00
|
|
|
|
|
|
|
if ( !unit ) {
|
2013-04-03 14:58:37 -04:00
|
|
|
return Location();
|
|
|
|
}
|
2013-04-29 22:24:28 -04:00
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
return unit->GetDefinitionLocation( line, column, unsaved_files );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-01 13:12:50 -04:00
|
|
|
void ClangCompleter::DeleteCachesForFile( const std::string &filename ) {
|
2013-09-30 16:40:25 -04:00
|
|
|
translation_unit_store_.Remove( filename );
|
2012-07-15 23:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
} // namespace YouCompleteMe
|