2013-01-18 20:22:36 -05: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/>.
|
|
|
|
|
|
|
|
#include "CompilationDatabase.h"
|
|
|
|
#include "ClangUtils.h"
|
|
|
|
#include "standard.h"
|
2013-10-11 22:27:04 -04:00
|
|
|
#include "ReleaseGil.h"
|
2013-01-18 20:22:36 -05:00
|
|
|
|
2013-01-22 22:40:05 -05:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2013-01-23 20:23:51 -05:00
|
|
|
#include <boost/make_shared.hpp>
|
2013-01-22 22:40:05 -05:00
|
|
|
#include <boost/type_traits/remove_pointer.hpp>
|
2013-09-30 20:30:46 -04:00
|
|
|
#include <boost/thread/locks.hpp>
|
2013-01-22 22:40:05 -05:00
|
|
|
|
2013-09-30 20:30:46 -04:00
|
|
|
using boost::lock_guard;
|
2013-10-14 13:22:57 -04:00
|
|
|
using boost::unique_lock;
|
|
|
|
using boost::try_to_lock_t;
|
2013-01-22 22:40:05 -05:00
|
|
|
using boost::remove_pointer;
|
2013-01-23 20:23:51 -05:00
|
|
|
using boost::shared_ptr;
|
2013-09-30 20:30:46 -04:00
|
|
|
using boost::mutex;
|
2013-01-22 22:40:05 -05:00
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
namespace YouCompleteMe {
|
2013-01-23 20:23:51 -05:00
|
|
|
|
2013-01-22 22:42:44 -05:00
|
|
|
typedef shared_ptr <
|
|
|
|
remove_pointer< CXCompileCommands >::type > CompileCommandsWrap;
|
2013-01-18 20:22:36 -05:00
|
|
|
|
2013-01-23 20:23:51 -05:00
|
|
|
|
2013-01-18 20:22:36 -05:00
|
|
|
CompilationDatabase::CompilationDatabase(
|
2013-01-19 23:03:32 -05:00
|
|
|
const std::string &path_to_directory )
|
2013-09-30 20:30:46 -04:00
|
|
|
: is_loaded_( false ) {
|
2013-01-18 20:22:36 -05:00
|
|
|
CXCompilationDatabase_Error status;
|
|
|
|
compilation_database_ = clang_CompilationDatabase_fromDirectory(
|
2013-01-19 23:03:32 -05:00
|
|
|
path_to_directory.c_str(),
|
|
|
|
&status );
|
2013-01-18 20:22:36 -05:00
|
|
|
is_loaded_ = status == CXCompilationDatabase_NoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
CompilationDatabase::~CompilationDatabase() {
|
2013-01-18 20:22:36 -05:00
|
|
|
clang_CompilationDatabase_dispose( compilation_database_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-19 23:03:32 -05:00
|
|
|
bool CompilationDatabase::DatabaseSuccessfullyLoaded() {
|
2013-01-18 20:22:36 -05:00
|
|
|
return is_loaded_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:22:57 -04:00
|
|
|
bool CompilationDatabase::AlreadyGettingFlags() {
|
|
|
|
unique_lock< mutex > lock( compilation_database_mutex_, try_to_lock_t() );
|
|
|
|
return !lock.owns_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-23 20:23:51 -05:00
|
|
|
CompilationInfoForFile CompilationDatabase::GetCompilationInfoForFile(
|
2013-02-03 00:49:55 -05:00
|
|
|
const std::string &path_to_file ) {
|
2013-10-11 22:27:04 -04:00
|
|
|
ReleaseGil unlock;
|
2013-01-23 20:23:51 -05:00
|
|
|
CompilationInfoForFile info;
|
2013-01-19 23:03:32 -05:00
|
|
|
|
2013-01-18 20:22:36 -05:00
|
|
|
if ( !is_loaded_ )
|
2013-01-23 20:23:51 -05:00
|
|
|
return info;
|
|
|
|
|
2013-09-30 20:30:46 -04:00
|
|
|
lock_guard< mutex > lock( compilation_database_mutex_ );
|
2013-01-18 20:22:36 -05:00
|
|
|
|
2013-01-22 22:40:05 -05:00
|
|
|
CompileCommandsWrap commands(
|
2013-01-19 23:03:32 -05:00
|
|
|
clang_CompilationDatabase_getCompileCommands(
|
|
|
|
compilation_database_,
|
2013-01-22 22:40:05 -05:00
|
|
|
path_to_file.c_str() ), clang_CompileCommands_dispose );
|
2013-01-18 20:22:36 -05:00
|
|
|
|
2013-01-22 22:40:05 -05:00
|
|
|
uint num_commands = clang_CompileCommands_getSize( commands.get() );
|
2013-01-19 23:03:32 -05:00
|
|
|
|
2013-01-18 20:22:36 -05:00
|
|
|
if ( num_commands < 1 ) {
|
2013-01-23 20:23:51 -05:00
|
|
|
return info;
|
2013-01-18 20:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We always pick the first command offered
|
|
|
|
CXCompileCommand command = clang_CompileCommands_getCommand(
|
2013-01-22 22:40:05 -05:00
|
|
|
commands.get(),
|
2013-01-19 23:03:32 -05:00
|
|
|
0 );
|
2013-01-18 20:22:36 -05:00
|
|
|
|
2013-01-23 20:23:51 -05:00
|
|
|
info.compiler_working_dir_ = CXStringToString(
|
2013-02-03 00:49:55 -05:00
|
|
|
clang_CompileCommand_getDirectory( command ) );
|
2013-01-23 20:23:51 -05:00
|
|
|
|
2013-01-18 20:22:36 -05:00
|
|
|
uint num_flags = clang_CompileCommand_getNumArgs( command );
|
2013-01-23 20:23:51 -05:00
|
|
|
info.compiler_flags_.reserve( num_flags );
|
2013-01-19 23:03:32 -05:00
|
|
|
|
|
|
|
for ( uint i = 0; i < num_flags; ++i ) {
|
2013-01-23 20:23:51 -05:00
|
|
|
info.compiler_flags_.push_back(
|
2013-02-03 00:49:55 -05:00
|
|
|
CXStringToString( clang_CompileCommand_getArg( command, i ) ) );
|
2013-01-18 20:22:36 -05:00
|
|
|
}
|
|
|
|
|
2013-01-23 20:23:51 -05:00
|
|
|
return info;
|
2013-01-18 20:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace YouCompleteMe
|
|
|
|
|