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 {
|
|
|
|
|
2017-12-01 17:36:32 +00:00
|
|
|
void EmitDiagnostics(std::string path,
|
|
|
|
std::vector<const char*> args,
|
|
|
|
CXTranslationUnit tu) {
|
2017-11-30 03:52:55 +00:00
|
|
|
std::string output = "Fatal errors while trying to parse " + path + "\n";
|
2017-12-01 17:36:32 +00:00
|
|
|
output +=
|
2018-01-11 02:43:01 +00:00
|
|
|
"Args: " +
|
|
|
|
StringJoinMap(args, [](const char* arg) { return std::string(arg); }) +
|
2017-12-01 17:36:32 +00:00
|
|
|
"\n";
|
2017-11-30 03:52:55 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:04:39 +00:00
|
|
|
LOG_S(WARNING) << output;
|
2017-11-30 03:52:55 +00:00
|
|
|
}
|
|
|
|
} // 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,
|
2018-01-18 01:52:35 +00:00
|
|
|
std::vector<CXUnsavedFile>& unsaved_files,
|
2017-10-23 05:07:50 +00:00
|
|
|
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;
|
|
|
|
{
|
|
|
|
error_code = clang_parseTranslationUnit2FullArgv(
|
2017-11-27 06:17:51 +00:00
|
|
|
index->cx_index, nullptr, args.data(), (int)args.size(),
|
2017-11-30 03:52:55 +00:00
|
|
|
unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &cx_tu);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_code != CXError_Success && cx_tu)
|
2017-12-01 17:36:32 +00:00
|
|
|
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:
|
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;
|
|
|
|
{
|
|
|
|
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)
|
2017-12-01 17:36:32 +00:00
|
|
|
EmitDiagnostics("<unknown>", {}, tu->cx_tu);
|
2017-11-30 03:52:55 +00:00
|
|
|
|
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
|
|
|
}
|