ccls/src/clang_translation_unit.cc

89 lines
2.7 KiB
C++
Raw Normal View History

#include "clang_translation_unit.h"
2017-04-22 07:39:55 +00:00
#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>
2017-09-22 01:14:57 +00:00
#include <sstream>
// 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;
2017-10-18 07:02:33 +00:00
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);
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) {
2017-09-22 01:14:57 +00:00
int error_code = clang_reparseTranslationUnit(
tu->cx_tu, (unsigned)unsaved.size(), unsaved.data(),
clang_defaultReparseOptions(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
}