Adding CompilationDatabase support (still WIP)

This commit is contained in:
Strahinja Val Markovic 2013-01-18 17:22:36 -08:00
parent f9a516704b
commit c1119c001f
4 changed files with 177 additions and 3 deletions

View File

@ -25,13 +25,14 @@
#include <boost/thread/shared_mutex.hpp> #include <boost/thread/shared_mutex.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/utility.hpp>
namespace YouCompleteMe namespace YouCompleteMe
{ {
struct CompletionData; struct CompletionData;
class ClangResultsCache class ClangResultsCache : boost::noncopyable
{ {
public: public:

View File

@ -0,0 +1,115 @@
// 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/>.
#include "CompilationDatabase.h"
#include "ClangUtils.h"
#include "standard.h"
namespace YouCompleteMe
{
CompilationDatabase::CompilationDatabase(
const std::string& path_to_directory )
: is_loaded_( false )
{
CXCompilationDatabase_Error status;
compilation_database_ = clang_CompilationDatabase_fromDirectory(
path_to_directory.c_str(),
&status );
is_loaded_ = status == CXCompilationDatabase_NoError;
}
CompilationDatabase::~CompilationDatabase()
{
clang_CompilationDatabase_dispose( compilation_database_ );
}
bool CompilationDatabase::DatabaseSuccessfullyLoaded()
{
return is_loaded_;
}
std::vector< std::string > CompilationDatabase::FlagsForFile(
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() );
uint num_commands = clang_CompileCommands_getSize( commands );
if ( num_commands < 1 ) {
clang_CompileCommands_dispose( commands );
return flags;
}
// We always pick the first command offered
CXCompileCommand command = clang_CompileCommands_getCommand(
commands,
0);
uint num_flags = clang_CompileCommand_getNumArgs( command );
flags.reserve( num_flags );
for ( uint i = 0; i < num_flags; ++i )
{
flags.push_back( CXStringToString(
clang_CompileCommand_getArg( command, i ) ) );
}
clang_CompileCommands_dispose( commands );
return flags;
}
std::string CompilationDatabase::CompileCommandWorkingDirectoryForFile(
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() );
uint num_commands = clang_CompileCommands_getSize( commands );
if ( num_commands < 1 ) {
clang_CompileCommands_dispose( commands );
return path_to_directory;
}
// We always pick the first command offered
CXCompileCommand command = clang_CompileCommands_getCommand(
commands,
0);
path_to_directory = CXStringToString( clang_CompileCommand_getDirectory(
command ) );
return path_to_directory;
}
} // namespace YouCompleteMe

View File

@ -0,0 +1,49 @@
// 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 COMPILATIONDATABASE_H_ZT7MQXPG
#define COMPILATIONDATABASE_H_ZT7MQXPG
#include <vector>
#include <string>
#include <boost/utility.hpp>
#include <clang-c/CXCompilationDatabase.h>
namespace YouCompleteMe
{
class CompilationDatabase : boost::noncopyable
{
public:
CompilationDatabase( const std::string& path_to_directory );
~CompilationDatabase();
bool DatabaseSuccessfullyLoaded();
std::vector< std::string > FlagsForFile( const std::string &path_to_file );
std::string CompileCommandWorkingDirectoryForFile(
const std::string &path_to_file );
private:
bool is_loaded_;
CXCompilationDatabase compilation_database_;
};
} // namespace YouCompleteMe
#endif /* end of include guard: COMPILATIONDATABASE_H_ZT7MQXPG */

View File

@ -23,6 +23,7 @@
# include "CompletionData.h" # include "CompletionData.h"
# include "Diagnostic.h" # include "Diagnostic.h"
# include "UnsavedFile.h" # include "UnsavedFile.h"
# include "CompilationDatabase.h"
#endif // USE_CLANG_COMPLETER #endif // USE_CLANG_COMPLETER
#include <boost/python.hpp> #include <boost/python.hpp>
@ -124,5 +125,13 @@ BOOST_PYTHON_MODULE(ycm_core)
class_< std::vector< Diagnostic > >( "DiagnosticVec" ) class_< std::vector< Diagnostic > >( "DiagnosticVec" )
.def( vector_indexing_suite< std::vector< Diagnostic > >() ); .def( vector_indexing_suite< std::vector< Diagnostic > >() );
class_< CompilationDatabase, boost::noncopyable >(
"CompilationDatabase", init< std::string >() )
.def( "FlagsForFile", &CompilationDatabase::FlagsForFile )
.def( "DatabaseSuccessfullyLoaded",
&CompilationDatabase::DatabaseSuccessfullyLoaded )
.def( "CompileCommandWorkingDirectoryForFile",
&CompilationDatabase::CompileCommandWorkingDirectoryForFile );
#endif // USE_CLANG_COMPLETER #endif // USE_CLANG_COMPLETER
} }