mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-29 11:01:57 +00:00
Cleanup clang::TranslationUnit API so callers have to handle failures.
This commit is contained in:
parent
9429dff63a
commit
3e9cffcc62
@ -283,10 +283,10 @@ void BuildDetailString(CXCompletionString completion_string,
|
||||
}
|
||||
}
|
||||
|
||||
void EnsureDocumentParsed(ClangCompleteManager* manager,
|
||||
std::shared_ptr<CompletionSession> session,
|
||||
std::unique_ptr<clang::TranslationUnit>* tu,
|
||||
clang::Index* index) {
|
||||
void TryEnsureDocumentParsed(ClangCompleteManager* manager,
|
||||
std::shared_ptr<CompletionSession> session,
|
||||
std::unique_ptr<clang::TranslationUnit>* tu,
|
||||
clang::Index* index) {
|
||||
// Nothing to do. We already have a translation unit.
|
||||
if (*tu)
|
||||
return;
|
||||
@ -306,15 +306,14 @@ void EnsureDocumentParsed(ClangCompleteManager* manager,
|
||||
|
||||
LOG_S(INFO) << "Creating completion session with arguments "
|
||||
<< StringJoin(args);
|
||||
*tu = MakeUnique<clang::TranslationUnit>(index, session->file.filename, args,
|
||||
unsaved, Flags());
|
||||
*tu = clang::TranslationUnit::Create(index, session->file.filename, args,
|
||||
unsaved, Flags());
|
||||
|
||||
// Build diagnostics.
|
||||
if (manager->config_->diagnosticsOnParse && !(*tu)->did_fail) {
|
||||
// If we're emitting diagnostics, do an immedaite reparse, otherwise we will
|
||||
if (manager->config_->diagnosticsOnParse && *tu) {
|
||||
// If we're emitting diagnostics, do an immediate reparse, otherwise we will
|
||||
// emit stale/bad diagnostics.
|
||||
clang_reparseTranslationUnit((*tu)->cx_tu, unsaved.size(), unsaved.data(),
|
||||
clang_defaultReparseOptions((*tu)->cx_tu));
|
||||
*tu = clang::TranslationUnit::Reparse(std::move(*tu), unsaved);
|
||||
|
||||
NonElidedVector<lsDiagnostic> ls_diagnostics;
|
||||
unsigned num_diagnostics = clang_getNumDiagnostics((*tu)->cx_tu);
|
||||
@ -350,8 +349,8 @@ void CompletionParseMain(ClangCompleteManager* completion_manager) {
|
||||
}
|
||||
|
||||
std::unique_ptr<clang::TranslationUnit> parsing;
|
||||
EnsureDocumentParsed(completion_manager, session, &parsing,
|
||||
&session->index);
|
||||
TryEnsureDocumentParsed(completion_manager, session, &parsing,
|
||||
&session->index);
|
||||
|
||||
// Activate new translation unit.
|
||||
// tu_last_parsed_at is only read by this thread, so it doesn't need to be
|
||||
@ -374,9 +373,14 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
|
||||
|
||||
std::lock_guard<std::mutex> lock(session->tu_lock);
|
||||
Timer timer;
|
||||
EnsureDocumentParsed(completion_manager, session, &session->tu,
|
||||
&session->index);
|
||||
timer.ResetAndPrint("[complete] EnsureDocumentParsed");
|
||||
TryEnsureDocumentParsed(completion_manager, session, &session->tu,
|
||||
&session->index);
|
||||
timer.ResetAndPrint("[complete] TryEnsureDocumentParsed");
|
||||
|
||||
// It is possible we failed to create the document despite
|
||||
// |TryEnsureDocumentParsed|.
|
||||
if (!session->tu)
|
||||
continue;
|
||||
|
||||
std::vector<CXUnsavedFile> unsaved =
|
||||
completion_manager->working_files_->AsUnsavedFiles();
|
||||
@ -458,10 +462,14 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
|
||||
// faster than reparsing the document.
|
||||
|
||||
timer.Reset();
|
||||
clang_reparseTranslationUnit(
|
||||
session->tu->cx_tu, unsaved.size(), unsaved.data(),
|
||||
clang_defaultReparseOptions(session->tu->cx_tu));
|
||||
session->tu =
|
||||
clang::TranslationUnit::Reparse(std::move(session->tu), unsaved);
|
||||
timer.ResetAndPrint("[complete] clang_reparseTranslationUnit");
|
||||
if (!session->tu) {
|
||||
LOG_S(ERROR) << "Reparsing translation unit for diagnostics failed for "
|
||||
<< path;
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t num_diagnostics = clang_getNumDiagnostics(session->tu->cx_tu);
|
||||
NonElidedVector<lsDiagnostic> ls_diagnostics;
|
||||
|
@ -1619,21 +1619,19 @@ std::vector<std::unique_ptr<IndexFile>> Parse(
|
||||
unsaved_files.push_back(unsaved);
|
||||
}
|
||||
|
||||
clang::TranslationUnit tu(index, file, args, unsaved_files,
|
||||
std::unique_ptr<clang::TranslationUnit> tu = clang::TranslationUnit::Create(
|
||||
index, file, args, unsaved_files,
|
||||
CXTranslationUnit_KeepGoing |
|
||||
CXTranslationUnit_DetailedPreprocessingRecord);
|
||||
if (tu.did_fail) {
|
||||
std::cerr << "!! Failed creating translation unit for " << file
|
||||
<< std::endl;
|
||||
if (!tu)
|
||||
return {};
|
||||
}
|
||||
|
||||
perf->index_parse = timer.ElapsedMicrosecondsAndReset();
|
||||
|
||||
if (dump_ast)
|
||||
Dump(tu.document_cursor());
|
||||
Dump(tu->document_cursor());
|
||||
|
||||
return ParseWithTu(file_consumer_shared, perf, &tu, index, file, args,
|
||||
return ParseWithTu(file_consumer_shared, perf, tu.get(), index, file, args,
|
||||
unsaved_files);
|
||||
}
|
||||
|
||||
|
@ -4,20 +4,22 @@
|
||||
#include "../utils.h"
|
||||
#include "Utility.h"
|
||||
|
||||
#include <loguru.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace clang {
|
||||
|
||||
TranslationUnit::TranslationUnit(Index* index,
|
||||
const std::string& filepath,
|
||||
const std::vector<std::string>& arguments,
|
||||
std::vector<CXUnsavedFile> unsaved_files,
|
||||
unsigned flags) {
|
||||
// static
|
||||
std::unique_ptr<TranslationUnit> TranslationUnit::Create(
|
||||
Index* index,
|
||||
const std::string& filepath,
|
||||
const std::vector<std::string>& arguments,
|
||||
std::vector<CXUnsavedFile> unsaved_files,
|
||||
unsigned flags) {
|
||||
std::vector<const char*> args;
|
||||
for (const std::string& a : arguments)
|
||||
args.push_back(a.c_str());
|
||||
@ -26,76 +28,68 @@ TranslationUnit::TranslationUnit(Index* index,
|
||||
for (const auto& arg : platform_args)
|
||||
args.push_back(arg.c_str());
|
||||
|
||||
// std::cerr << "Parsing " << filepath << " with args " << StringJoin(args) <<
|
||||
// std::endl;
|
||||
|
||||
// cx_tu = clang_createTranslationUnitFromSourceFile(
|
||||
// index->cx_index, filepath.c_str(), args.size(), args.data(),
|
||||
// (unsigned)unsaved_files.size(), unsaved_files.data());
|
||||
|
||||
CXTranslationUnit cx_tu;
|
||||
CXErrorCode error_code = clang_parseTranslationUnit2FullArgv(
|
||||
index->cx_index, filepath.c_str(), args.data(), (int)args.size(),
|
||||
unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &cx_tu);
|
||||
|
||||
switch (error_code) {
|
||||
case CXError_Success:
|
||||
did_fail = false;
|
||||
break;
|
||||
return MakeUnique<TranslationUnit>(cx_tu);
|
||||
case CXError_Failure:
|
||||
std::cerr << "libclang generic failure for " << filepath << " with args "
|
||||
<< StringJoin(args) << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
LOG_S(ERROR) << "libclang generic failure for " << filepath
|
||||
<< " with args " << StringJoin(args);
|
||||
return nullptr;
|
||||
case CXError_Crashed:
|
||||
std::cerr << "libclang crashed for " << filepath << " with args "
|
||||
<< StringJoin(args) << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
LOG_S(ERROR) << "libclang crashed for " << filepath << " with args "
|
||||
<< StringJoin(args);
|
||||
return nullptr;
|
||||
case CXError_InvalidArguments:
|
||||
std::cerr << "libclang had invalid arguments for "
|
||||
<< " with args " << StringJoin(args) << filepath << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
LOG_S(ERROR) << "libclang had invalid arguments for "
|
||||
<< " with args " << StringJoin(args) << filepath;
|
||||
return nullptr;
|
||||
case CXError_ASTReadError:
|
||||
std::cerr << "libclang had ast read error for " << filepath
|
||||
<< " with args " << StringJoin(args) << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
LOG_S(ERROR) << "libclang had ast read error for " << filepath
|
||||
<< " with args " << StringJoin(args);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// static
|
||||
std::unique_ptr<TranslationUnit> TranslationUnit::Reparse(
|
||||
std::unique_ptr<TranslationUnit> tu,
|
||||
std::vector<CXUnsavedFile>& unsaved) {
|
||||
int error_code = clang_reparseTranslationUnit(
|
||||
tu->cx_tu, (unsigned)unsaved.size(), unsaved.data(),
|
||||
clang_defaultReparseOptions(tu->cx_tu));
|
||||
switch (error_code) {
|
||||
case CXError_Success:
|
||||
return tu;
|
||||
case CXError_Failure:
|
||||
LOG_S(ERROR) << "libclang reparse generic failure";
|
||||
return nullptr;
|
||||
case CXError_Crashed:
|
||||
LOG_S(ERROR) << "libclang reparse crashed";
|
||||
return nullptr;
|
||||
case CXError_InvalidArguments:
|
||||
LOG_S(ERROR) << "libclang reparse had invalid arguments";
|
||||
return nullptr;
|
||||
case CXError_ASTReadError:
|
||||
LOG_S(ERROR) << "libclang reparse had ast read error";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
TranslationUnit::TranslationUnit(CXTranslationUnit tu) : cx_tu(tu) {}
|
||||
|
||||
TranslationUnit::~TranslationUnit() {
|
||||
clang_disposeTranslationUnit(cx_tu);
|
||||
}
|
||||
|
||||
void TranslationUnit::ReparseTranslationUnit(
|
||||
std::vector<CXUnsavedFile>& unsaved) {
|
||||
int error_code = clang_reparseTranslationUnit(
|
||||
cx_tu, (unsigned)unsaved.size(), unsaved.data(),
|
||||
clang_defaultReparseOptions(cx_tu));
|
||||
switch (error_code) {
|
||||
case CXError_Success:
|
||||
did_fail = false;
|
||||
break;
|
||||
case CXError_Failure:
|
||||
std::cerr << "libclang reparse generic failure" << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
case CXError_Crashed:
|
||||
std::cerr << "libclang reparse crashed " << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
case CXError_InvalidArguments:
|
||||
std::cerr << "libclang reparse had invalid arguments" << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
case CXError_ASTReadError:
|
||||
std::cerr << "libclang reparse had ast read error" << std::endl;
|
||||
did_fail = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Cursor TranslationUnit::document_cursor() const {
|
||||
return Cursor(clang_getTranslationUnitCursor(cx_tu));
|
||||
}
|
||||
|
@ -3,9 +3,9 @@
|
||||
#include "Cursor.h"
|
||||
#include "Index.h"
|
||||
|
||||
|
||||
#include <clang-c/Index.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -13,17 +13,20 @@ namespace clang {
|
||||
|
||||
class TranslationUnit {
|
||||
public:
|
||||
TranslationUnit(Index* index,
|
||||
const std::string& filepath,
|
||||
const std::vector<std::string>& arguments,
|
||||
std::vector<CXUnsavedFile> unsaved_files,
|
||||
unsigned flags);
|
||||
static std::unique_ptr<TranslationUnit> Create(
|
||||
Index* index,
|
||||
const std::string& filepath,
|
||||
const std::vector<std::string>& arguments,
|
||||
std::vector<CXUnsavedFile> unsaved_files,
|
||||
unsigned flags);
|
||||
|
||||
static std::unique_ptr<TranslationUnit> Reparse(
|
||||
std::unique_ptr<TranslationUnit> tu,
|
||||
std::vector<CXUnsavedFile>& unsaved);
|
||||
|
||||
explicit TranslationUnit(CXTranslationUnit tu);
|
||||
~TranslationUnit();
|
||||
|
||||
bool did_fail = false;
|
||||
|
||||
void ReparseTranslationUnit(std::vector<CXUnsavedFile>& unsaved);
|
||||
|
||||
Cursor document_cursor() const;
|
||||
|
||||
CXTranslationUnit cx_tu;
|
||||
|
Loading…
Reference in New Issue
Block a user