2017-11-11 19:41:09 +00:00
|
|
|
#include "clang_translation_unit.h"
|
2017-04-22 07:39:55 +00:00
|
|
|
|
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-09-22 01:14:57 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
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-10-18 07:02:33 +00:00
|
|
|
CXErrorCode error_code = clang_parseTranslationUnit2FullArgv(
|
2017-05-25 02:04:19 +00:00
|
|
|
index->cx_index, filepath.c_str(), args.data(), (int)args.size(),
|
2017-05-21 23:22:00 +00:00
|
|
|
unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &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-09-22 01:14:57 +00:00
|
|
|
int error_code = clang_reparseTranslationUnit(
|
2017-10-23 05:07:50 +00:00
|
|
|
tu->cx_tu, (unsigned)unsaved.size(), unsaved.data(),
|
|
|
|
clang_defaultReparseOptions(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
|
|
|
}
|