1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// Copyright (C) 2011-2018 ycmd contributors
//
// This file is part of ycmd.
//
// ycmd 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.
//
// ycmd 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 ycmd. If not, see <http://www.gnu.org/licenses/>.
#include "ClangCompleter.h"
#include "Candidate.h"
#include "ClangUtils.h"
#include "CompletionData.h"
#include "Result.h"
#include "TranslationUnit.h"
#include "Utils.h"
#include <clang-c/Index.h>
#include <memory>
using std::shared_ptr;
namespace YouCompleteMe {
ClangCompleter::ClangCompleter()
: clang_index_( clang_createIndex( 0, 0 ) ),
translation_unit_store_( clang_index_ ) {
// 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 );
}
ClangCompleter::~ClangCompleter() {
// We need to destroy all TUs before calling clang_disposeIndex because
// the translation units need to be destroyed before the index is destroyed.
// 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 daemon threads and will all be killed before the main thread exits.
translation_unit_store_.RemoveAll();
clang_disposeIndex( clang_index_ );
}
bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) {
shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename );
if ( !unit ) {
return false;
}
// 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.
return unit->IsCurrentlyUpdating();
}
std::vector< Diagnostic > ClangCompleter::UpdateTranslationUnit(
const std::string &translation_unit,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) {
bool translation_unit_created;
shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate(
translation_unit,
unsaved_files,
flags,
translation_unit_created );
try {
return unit->Reparse( unsaved_files );
} catch ( const ClangParseError & ) {
// 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.
translation_unit_store_.Remove( translation_unit );
throw;
}
}
std::vector< CompletionData >
ClangCompleter::CandidatesForLocationInFile(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->CandidatesForLocation( filename,
line,
column,
unsaved_files );
}
Location ClangCompleter::GetDeclarationLocation(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->GetDeclarationLocation( filename,
line,
column,
unsaved_files,
reparse );
}
Location ClangCompleter::GetDefinitionLocation(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->GetDefinitionLocation( filename,
line,
column,
unsaved_files,
reparse );
}
Location ClangCompleter::GetDefinitionOrDeclarationLocation(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->GetDefinitionOrDeclarationLocation( filename,
line,
column,
unsaved_files,
reparse );
}
std::string ClangCompleter::GetTypeAtLocation(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->GetTypeAtLocation( filename,
line,
column,
unsaved_files,
reparse );
}
std::string ClangCompleter::GetEnclosingFunctionAtLocation(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->GetEnclosingFunctionAtLocation( filename,
line,
column,
unsaved_files,
reparse );
}
std::vector< FixIt >
ClangCompleter::GetFixItsForLocationInFile(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
return unit->GetFixItsForLocationInFile( filename,
line,
column,
unsaved_files,
reparse );
}
DocumentationData ClangCompleter::GetDocsForLocationInFile(
const std::string &translation_unit,
const std::string &filename,
int line,
int column,
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags,
bool reparse ) {
shared_ptr< TranslationUnit > unit =
translation_unit_store_.GetOrCreate( translation_unit,
unsaved_files,
flags );
Location location( unit->GetDeclarationLocation( filename,
line,
column,
unsaved_files,
reparse ) );
// By default, libclang ignores comments from system headers and, in
// particular, headers included with the -isystem flag. If the declaration is
// found in such header, get the documentation directly from the corresponding
// translation unit. Comments in the main file of a translation unit are not
// ignored.
if ( unit->LocationIsInSystemHeader( location ) ) {
unit = translation_unit_store_.GetOrCreate( location.filename_,
unsaved_files,
flags );
return unit->GetDocsForLocation( location, unsaved_files, reparse );
}
// This translation unit has already been parsed when getting the
// declaration's location.
return unit->GetDocsForLocation( location, unsaved_files, false );
}
void ClangCompleter::DeleteCachesForFile( const std::string &filename ) {
translation_unit_store_.Remove( filename );
}
} // namespace YouCompleteMe
|