2017-11-11 19:41:09 +00:00
|
|
|
#include "clang_translation_unit.h"
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-11-30 03:52:55 +00:00
|
|
|
#include "clang_utils.h"
|
2017-11-11 19:41:09 +00:00
|
|
|
#include "platform.h"
|
|
|
|
#include "utils.h"
|
2017-09-22 01:14:57 +00:00
|
|
|
|
2017-10-23 05:07:50 +00:00
|
|
|
#include <loguru.hpp>
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-02-22 08:52:00 +00:00
|
|
|
#include <cassert>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <fstream>
|
2017-03-25 19:18:25 +00:00
|
|
|
#include <iostream>
|
2017-11-30 03:52:55 +00:00
|
|
|
#include <mutex>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2017-11-30 03:52:55 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// We need to serialize requests to clang_parseTranslationUnit2FullArgv and
|
|
|
|
// clang_reparseTranslationUnit. See
|
|
|
|
// https://github.com/jacobdufault/cquery/issues/43#issuecomment-347614504.
|
2017-11-30 04:23:34 +00:00
|
|
|
//
|
|
|
|
// NOTE: This is disabled because it effectively serializes indexing, as a huge
|
|
|
|
// chunk of indexing time is spent inside of these functions.
|
|
|
|
//
|
|
|
|
// std::mutex g_parse_translation_unit_mutex;
|
|
|
|
// std::mutex g_reparse_translation_unit_mutex;
|
2017-11-30 03:52:55 +00:00
|
|
|
|
|
|
|
void EmitDiagnostics(std::string path, CXTranslationUnit tu) {
|
|
|
|
std::string output = "Fatal errors while trying to parse " + path + "\n";
|
|
|
|
|
|
|
|
size_t num_diagnostics = clang_getNumDiagnostics(tu);
|
|
|
|
for (unsigned i = 0; i < num_diagnostics; ++i) {
|
|
|
|
output += " - ";
|
|
|
|
|
|
|
|
CXDiagnostic diagnostic = clang_getDiagnostic(tu, i);
|
|
|
|
|
|
|
|
// Location.
|
|
|
|
CXFile file;
|
|
|
|
unsigned int line, column;
|
|
|
|
clang_getSpellingLocation(clang_getDiagnosticLocation(diagnostic), &file,
|
|
|
|
&line, &column, nullptr);
|
|
|
|
std::string path = FileName(file);
|
|
|
|
output += path + ":" + std::to_string(line - 1) + ":" +
|
|
|
|
std::to_string(column) + " ";
|
|
|
|
|
|
|
|
// Severity
|
|
|
|
switch (clang_getDiagnosticSeverity(diagnostic)) {
|
|
|
|
case CXDiagnostic_Ignored:
|
|
|
|
case CXDiagnostic_Note:
|
|
|
|
output += "[info]";
|
|
|
|
break;
|
|
|
|
case CXDiagnostic_Warning:
|
|
|
|
output += "[warning]";
|
|
|
|
break;
|
|
|
|
case CXDiagnostic_Error:
|
|
|
|
output += "[error]";
|
|
|
|
break;
|
|
|
|
case CXDiagnostic_Fatal:
|
|
|
|
output += "[fatal]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Content.
|
|
|
|
output += " " + ToString(clang_getDiagnosticSpelling(diagnostic));
|
|
|
|
|
|
|
|
clang_disposeDiagnostic(diagnostic);
|
|
|
|
|
|
|
|
output += "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << output;
|
|
|
|
std::cerr.flush();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-10-23 05:07:50 +00:00
|
|
|
// static
|
2017-11-11 19:41:09 +00:00
|
|
|
std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
|
|
|
|
ClangIndex* index,
|
2017-10-23 05:07:50 +00:00
|
|
|
const std::string& filepath,
|
|
|
|
const std::vector<std::string>& arguments,
|
|
|
|
std::vector<CXUnsavedFile> unsaved_files,
|
|
|
|
unsigned flags) {
|
2017-02-16 09:35:30 +00:00
|
|
|
std::vector<const char*> args;
|
2017-03-29 06:33:38 +00:00
|
|
|
for (const std::string& a : arguments)
|
2017-03-25 19:18:25 +00:00
|
|
|
args.push_back(a.c_str());
|
2017-03-29 06:33:38 +00:00
|
|
|
|
|
|
|
std::vector<std::string> platform_args = GetPlatformClangArguments();
|
|
|
|
for (const auto& arg : platform_args)
|
|
|
|
args.push_back(arg.c_str());
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-10-23 05:07:50 +00:00
|
|
|
CXTranslationUnit cx_tu;
|
2017-11-30 03:52:55 +00:00
|
|
|
CXErrorCode error_code;
|
|
|
|
{
|
2017-11-30 04:23:34 +00:00
|
|
|
// std::lock_guard<std::mutex> lock(g_parse_translation_unit_mutex);
|
2017-11-30 03:52:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_code != CXError_Success && cx_tu)
|
|
|
|
EmitDiagnostics(filepath, cx_tu);
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-03-25 19:18:25 +00:00
|
|
|
switch (error_code) {
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_Success:
|
2017-11-11 19:41:09 +00:00
|
|
|
return MakeUnique<ClangTranslationUnit>(cx_tu);
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_Failure:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang generic failure for " << filepath
|
|
|
|
<< " with args " << StringJoin(args);
|
|
|
|
return nullptr;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_Crashed:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang crashed for " << filepath << " with args "
|
|
|
|
<< StringJoin(args);
|
|
|
|
return nullptr;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_InvalidArguments:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang had invalid arguments for "
|
|
|
|
<< " with args " << StringJoin(args) << filepath;
|
|
|
|
return nullptr;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_ASTReadError:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang had ast read error for " << filepath
|
|
|
|
<< " with args " << StringJoin(args);
|
|
|
|
return nullptr;
|
2017-03-25 19:18:25 +00:00
|
|
|
}
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-10-23 05:07:50 +00:00
|
|
|
return nullptr;
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 05:07:50 +00:00
|
|
|
// static
|
2017-11-11 19:41:09 +00:00
|
|
|
std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Reparse(
|
|
|
|
std::unique_ptr<ClangTranslationUnit> tu,
|
2017-04-22 07:42:57 +00:00
|
|
|
std::vector<CXUnsavedFile>& unsaved) {
|
2017-11-30 03:52:55 +00:00
|
|
|
int error_code;
|
|
|
|
{
|
2017-11-30 04:23:34 +00:00
|
|
|
// std::lock_guard<std::mutex> lock(g_reparse_translation_unit_mutex);
|
2017-11-30 03:52:55 +00:00
|
|
|
error_code = clang_reparseTranslationUnit(
|
|
|
|
tu->cx_tu, (unsigned)unsaved.size(), unsaved.data(),
|
|
|
|
clang_defaultReparseOptions(tu->cx_tu));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_code != CXError_Success && tu->cx_tu)
|
|
|
|
EmitDiagnostics("<unknown>", tu->cx_tu);
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
switch (error_code) {
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_Success:
|
2017-10-23 05:07:50 +00:00
|
|
|
return tu;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_Failure:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang reparse generic failure";
|
|
|
|
return nullptr;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_Crashed:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang reparse crashed";
|
|
|
|
return nullptr;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_InvalidArguments:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang reparse had invalid arguments";
|
|
|
|
return nullptr;
|
2017-04-22 07:42:57 +00:00
|
|
|
case CXError_ASTReadError:
|
2017-10-23 05:07:50 +00:00
|
|
|
LOG_S(ERROR) << "libclang reparse had ast read error";
|
|
|
|
return nullptr;
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|
2017-10-23 05:07:50 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangTranslationUnit::ClangTranslationUnit(CXTranslationUnit tu) : cx_tu(tu) {}
|
2017-10-23 05:07:50 +00:00
|
|
|
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangTranslationUnit::~ClangTranslationUnit() {
|
2017-10-23 05:07:50 +00:00
|
|
|
clang_disposeTranslationUnit(cx_tu);
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|