ccls/src/libclangmm/TranslationUnit.cc

93 lines
2.9 KiB
C++
Raw Normal View History

2017-02-16 09:35:30 +00:00
#include "TranslationUnit.h"
2017-04-22 07:39:55 +00:00
2017-02-16 09:35:30 +00:00
#include "Utility.h"
2017-03-29 06:33:38 +00:00
#include "../platform.h"
2017-04-22 07:39:55 +00:00
2017-02-16 09:35:30 +00:00
#include <fstream>
#include <sstream>
2017-02-22 08:52:00 +00:00
#include <cassert>
2017-03-25 19:18:25 +00:00
#include <iostream>
2017-02-16 09:35:30 +00:00
2017-02-17 09:57:44 +00:00
namespace clang {
2017-02-16 09:35:30 +00:00
TranslationUnit::TranslationUnit(Index& index,
2017-04-22 07:42:57 +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
//std::cerr << "Parsing " << filepath << " with args " << StringJoin(args) << std::endl;
2017-04-03 01:34:15 +00:00
//CXErrorCode error_code = clang_parseTranslationUnit2FullArgv(
CXErrorCode error_code = clang_parseTranslationUnit2(
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:
did_fail = false;
break;
case CXError_Failure:
std::cerr << "libclang generic failure for " << filepath << " with args " << StringJoin(args) << std::endl;
2017-04-22 07:42:57 +00:00
did_fail = true;
break;
case CXError_Crashed:
std::cerr << "libclang crashed for " << filepath << " with args " << StringJoin(args) << std::endl;
2017-04-22 07:42:57 +00:00
did_fail = true;
break;
case CXError_InvalidArguments:
std::cerr << "libclang had invalid arguments for " << " with args " << StringJoin(args) << filepath
2017-04-22 07:42:57 +00:00
<< std::endl;
did_fail = true;
break;
case CXError_ASTReadError:
std::cerr << "libclang had ast read error for " << filepath << " with args " << StringJoin(args) << std::endl;
2017-04-22 07:42:57 +00:00
did_fail = true;
break;
2017-03-25 19:18:25 +00:00
}
2017-02-16 09:35:30 +00:00
}
2017-02-17 09:57:44 +00:00
TranslationUnit::~TranslationUnit() {
2017-02-16 09:35:30 +00:00
clang_disposeTranslationUnit(cx_tu);
}
2017-04-22 07:42:57 +00:00
void TranslationUnit::ReparseTranslationUnit(
std::vector<CXUnsavedFile>& unsaved) {
int error_code =
clang_reparseTranslationUnit(cx_tu, (unsigned)unsaved.size(), unsaved.data(),
2017-04-22 07:42:57 +00:00
clang_defaultReparseOptions(cx_tu));
switch (error_code) {
2017-04-22 07:42:57 +00:00
case CXError_Success:
did_fail = false;
break;
case CXError_Failure:
std::cerr << "libclang reparse generic failure" << std::endl;
did_fail = true;
break;
case CXError_Crashed:
std::cerr << "libclang reparse crashed " << std::endl;
did_fail = true;
break;
case CXError_InvalidArguments:
std::cerr << "libclang reparse had invalid arguments" << std::endl;
did_fail = true;
break;
case CXError_ASTReadError:
std::cerr << "libclang reparse had ast read error" << std::endl;
did_fail = true;
break;
2017-02-16 09:35:30 +00:00
}
}
2017-02-17 09:57:44 +00:00
Cursor TranslationUnit::document_cursor() const {
return Cursor(clang_getTranslationUnitCursor(cx_tu));
}
}