ccls/src/clang_translation_unit.cc

159 lines
4.5 KiB
C++
Raw Normal View History

#include "clang_translation_unit.h"
2017-04-22 07:39:55 +00:00
#include "clang_utils.h"
#include "platform.h"
#include "utils.h"
2017-09-22 01:14:57 +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>
#include <mutex>
2017-09-22 01:14:57 +00:00
#include <sstream>
namespace {
void EmitDiagnostics(std::string path,
std::vector<const char*> args,
CXTranslationUnit tu) {
std::string output = "Fatal errors while trying to parse " + path + "\n";
output +=
"Args: " +
StringJoinMap(args, [](const char* arg) { return std::string(arg); }) +
"\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";
}
LOG_S(WARNING) << output;
}
} // namespace
// static
std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
ClangIndex* index,
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
CXTranslationUnit cx_tu;
CXErrorCode error_code;
{
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, args, 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:
return MakeUnique<ClangTranslationUnit>(cx_tu);
2017-04-22 07:42:57 +00:00
case CXError_Failure:
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:
LOG_S(ERROR) << "libclang crashed for " << filepath << " with args "
<< StringJoin(args);
return nullptr;
2017-04-22 07:42:57 +00:00
case CXError_InvalidArguments:
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:
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
return nullptr;
2017-02-16 09:35:30 +00:00
}
// static
std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Reparse(
std::unique_ptr<ClangTranslationUnit> tu,
2017-04-22 07:42:57 +00:00
std::vector<CXUnsavedFile>& unsaved) {
int error_code;
{
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);
switch (error_code) {
2017-04-22 07:42:57 +00:00
case CXError_Success:
return tu;
2017-04-22 07:42:57 +00:00
case CXError_Failure:
LOG_S(ERROR) << "libclang reparse generic failure";
return nullptr;
2017-04-22 07:42:57 +00:00
case CXError_Crashed:
LOG_S(ERROR) << "libclang reparse crashed";
return nullptr;
2017-04-22 07:42:57 +00:00
case CXError_InvalidArguments:
LOG_S(ERROR) << "libclang reparse had invalid arguments";
return nullptr;
2017-04-22 07:42:57 +00:00
case CXError_ASTReadError:
LOG_S(ERROR) << "libclang reparse had ast read error";
return nullptr;
2017-02-16 09:35:30 +00:00
}
return nullptr;
}
ClangTranslationUnit::ClangTranslationUnit(CXTranslationUnit tu) : cx_tu(tu) {}
ClangTranslationUnit::~ClangTranslationUnit() {
clang_disposeTranslationUnit(cx_tu);
2017-02-16 09:35:30 +00:00
}