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/>.
|
|
|
|
|
|
|
|
#ifndef CLANGCOMPLETE_H_WLKDU0ZV
|
|
|
|
#define CLANGCOMPLETE_H_WLKDU0ZV
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
#include "ConcurrentLatestValue.h"
|
|
|
|
#include "Future.h"
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
#include <boost/utility.hpp>
|
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
typedef void *CXIndex;
|
|
|
|
typedef struct CXTranslationUnitImpl *CXTranslationUnit;
|
|
|
|
|
|
|
|
namespace YouCompleteMe
|
|
|
|
{
|
|
|
|
|
2012-07-12 01:41:32 -04:00
|
|
|
class CandidateRepository;
|
2012-07-20 00:17:39 -04:00
|
|
|
struct CompletionData;
|
2012-07-12 01:41:32 -04:00
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
// TODO: put this in a separated header file
|
2012-07-08 14:54:57 -04:00
|
|
|
struct UnsavedFile
|
|
|
|
{
|
2012-07-10 18:26:07 -04:00
|
|
|
UnsavedFile() : filename_( NULL ), contents_( NULL ), length_( 0 ) {}
|
|
|
|
|
|
|
|
const char *filename_;
|
|
|
|
const char *contents_;
|
|
|
|
unsigned long length_;
|
|
|
|
|
|
|
|
// We need this to be able to export this struct to Python via Boost.Python's
|
|
|
|
// 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
|
2012-07-12 23:54:08 -04:00
|
|
|
bool operator== ( const UnsavedFile &other ) const
|
2012-07-10 18:26:07 -04:00
|
|
|
{
|
2012-07-12 23:54:08 -04:00
|
|
|
return
|
|
|
|
filename_ == other.filename_ &&
|
|
|
|
contents_ == other.contents_ &&
|
|
|
|
length_ == other.length_;
|
2012-07-10 18:26:07 -04:00
|
|
|
}
|
2012-07-08 14:54:57 -04:00
|
|
|
};
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
typedef boost::shared_ptr< std::vector< CompletionData > > AsyncCompletions;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
|
|
|
typedef boost::unordered_map< std::string, std::vector< std::string > >
|
|
|
|
FlagsForFile;
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
typedef boost::unordered_map< std::string, CXTranslationUnit >
|
|
|
|
TranslationUnitForFilename;
|
|
|
|
|
|
|
|
|
2012-07-11 02:28:58 -04:00
|
|
|
class ClangCompleter : boost::noncopyable
|
2012-07-08 14:54:57 -04:00
|
|
|
{
|
|
|
|
public:
|
2012-07-11 02:28:58 -04:00
|
|
|
ClangCompleter();
|
|
|
|
~ClangCompleter();
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
void EnableThreading();
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
void SetGlobalCompileFlags( const std::vector< std::string > &flags );
|
|
|
|
|
|
|
|
void SetFileCompileFlags( const std::string &filename,
|
|
|
|
const std::vector< std::string > &flags );
|
|
|
|
|
|
|
|
void UpdateTranslationUnit( const std::string &filename,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files );
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionData > CandidatesForLocationInFile(
|
2012-07-15 23:49:56 -04:00
|
|
|
const std::string &filename,
|
|
|
|
int line,
|
|
|
|
int column,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files );
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
Future< AsyncCompletions > CandidatesForQueryAndLocationInFileAsync(
|
2012-07-12 01:41:32 -04:00
|
|
|
const std::string &query,
|
2012-07-08 14:54:57 -04:00
|
|
|
const std::string &filename,
|
|
|
|
int line,
|
|
|
|
int column,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files );
|
|
|
|
|
|
|
|
private:
|
2012-07-15 23:49:56 -04:00
|
|
|
typedef ConcurrentLatestValue<
|
|
|
|
boost::shared_ptr<
|
2012-07-20 00:17:39 -04:00
|
|
|
boost::packaged_task< AsyncCompletions > > > LatestTask;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
|
|
|
// caller takes ownership of translation unit
|
|
|
|
CXTranslationUnit CreateTranslationUnit(
|
|
|
|
const std::string &filename,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files );
|
|
|
|
|
|
|
|
std::vector< const char* > ClangFlagsForFilename(
|
|
|
|
const std::string &filename );
|
|
|
|
|
|
|
|
CXTranslationUnit GetTranslationUnitForFile(
|
|
|
|
const std::string &filename,
|
|
|
|
const std::vector< UnsavedFile > &unsaved_files );
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionData > 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
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
void InitThreads();
|
|
|
|
|
|
|
|
void ClangThreadMain( LatestTask &clang_task );
|
|
|
|
|
|
|
|
void SortingThreadMain( LatestTask &sorting_task );
|
|
|
|
|
2012-07-12 01:41:32 -04:00
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
// PRIVATE MEMBER VARIABLES
|
|
|
|
/////////////////////////////
|
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
CXIndex clang_index_;
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
FlagsForFile flags_for_file_;
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
TranslationUnitForFilename filename_to_translation_unit_;
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-08 14:54:57 -04:00
|
|
|
std::vector< std::string > global_flags_;
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-12 01:41:32 -04:00
|
|
|
CandidateRepository &candidate_repository_;
|
2012-07-08 14:54:57 -04:00
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
mutable LatestTask clang_task_;
|
|
|
|
|
|
|
|
mutable LatestTask sorting_task_;
|
|
|
|
|
|
|
|
bool threading_enabled_;
|
|
|
|
|
|
|
|
// TODO: use boost.atomic for clang_data_ready_
|
|
|
|
bool clang_data_ready_;
|
|
|
|
boost::mutex clang_data_ready_mutex_;
|
|
|
|
boost::condition_variable clang_data_ready_condition_variable_;
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
std::vector< CompletionData > latest_clang_results_;
|
2012-07-15 23:49:56 -04:00
|
|
|
boost::shared_mutex latest_clang_results_shared_mutex_;
|
|
|
|
|
|
|
|
// Unfortunately clang is not thread-safe so we can only ever use one thread
|
|
|
|
// to access it. So this one background thread will be the only thread that
|
|
|
|
// can access libclang.
|
|
|
|
boost::thread clang_thread_;
|
|
|
|
|
|
|
|
boost::thread_group sorting_threads_;
|
2012-07-08 14:54:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace YouCompleteMe
|
|
|
|
|
|
|
|
#endif /* end of include guard: CLANGCOMPLETE_H_WLKDU0ZV */
|