diff --git a/cpp/ycm/ClangCompleter/ClangResultsCache.h b/cpp/ycm/ClangCompleter/ClangResultsCache.h index 6ee61d60..cd3ec4fc 100644 --- a/cpp/ycm/ClangCompleter/ClangResultsCache.h +++ b/cpp/ycm/ClangCompleter/ClangResultsCache.h @@ -25,13 +25,14 @@ #include #include #include +#include namespace YouCompleteMe { struct CompletionData; -class ClangResultsCache +class ClangResultsCache : boost::noncopyable { public: diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp new file mode 100644 index 00000000..94efa36b --- /dev/null +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp @@ -0,0 +1,115 @@ +// Copyright (C) 2011, 2012 Strahinja Val Markovic +// +// 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 . + +#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 + diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.h b/cpp/ycm/ClangCompleter/CompilationDatabase.h new file mode 100644 index 00000000..8e76a224 --- /dev/null +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.h @@ -0,0 +1,49 @@ +// Copyright (C) 2011, 2012 Strahinja Val Markovic +// +// 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 . + +#ifndef COMPILATIONDATABASE_H_ZT7MQXPG +#define COMPILATIONDATABASE_H_ZT7MQXPG + +#include +#include +#include +#include + +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 */ diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index dfc02f49..3ebb7aa7 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -23,6 +23,7 @@ # include "CompletionData.h" # include "Diagnostic.h" # include "UnsavedFile.h" +# include "CompilationDatabase.h" #endif // USE_CLANG_COMPLETER #include @@ -88,8 +89,8 @@ BOOST_PYTHON_MODULE(ycm_core) return_value_policy< reference_existing_object >() ) ) .def_readwrite( "length_", &UnsavedFile::length_ ); - class_< std::vector< UnsavedFile > >( "UnsavedFileVec" ) - .def( vector_indexing_suite< std::vector< UnsavedFile > >() ); + class_< std::vector< UnsavedFile > >( "UnsavedFileVec" ) + .def( vector_indexing_suite< std::vector< UnsavedFile > >() ); class_< ClangCompleter, boost::noncopyable >( "ClangCompleter" ) .def( "EnableThreading", &ClangCompleter::EnableThreading ) @@ -124,5 +125,13 @@ BOOST_PYTHON_MODULE(ycm_core) class_< std::vector< Diagnostic > >( "DiagnosticVec" ) .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 }