ccls/src/libclangmm/TranslationUnit.cc

134 lines
4.0 KiB
C++
Raw Normal View History

2017-02-16 09:35:30 +00:00
#include "TranslationUnit.h"
#include "Tokens.h"
#include "Utility.h"
#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,
const std::string& filepath,
const std::vector<std::string>& arguments,
std::vector<CXUnsavedFile> unsaved_files,
unsigned flags) {
2017-02-22 08:52:00 +00:00
2017-02-16 09:35:30 +00:00
std::vector<const char*> args;
for (const std::string& a : arguments) {
2017-03-25 19:18:25 +00:00
args.push_back(a.c_str());
2017-02-16 09:35:30 +00:00
}
2017-02-17 09:57:44 +00:00
2017-02-22 08:52:00 +00:00
CXErrorCode error_code = clang_parseTranslationUnit2(
index.cx_index,
filepath.c_str(),
args.data(), args.size(),
unsaved_files.data(), unsaved_files.size(),
flags, &cx_tu);
2017-03-25 19:18:25 +00:00
switch (error_code) {
case CXError_Success:
did_fail = false;
2017-03-25 19:18:25 +00:00
break;
case CXError_Failure:
std::cerr << "libclang generic failure for " << filepath << std::endl;
did_fail = true;
2017-03-25 19:18:25 +00:00
break;
case CXError_Crashed:
std::cerr << "libclang crashed for " << filepath << std::endl;
did_fail = true;
2017-03-25 19:18:25 +00:00
break;
case CXError_InvalidArguments:
std::cerr << "libclang had invalid arguments for " << filepath << std::endl;
did_fail = true;
2017-03-25 19:18:25 +00:00
break;
case CXError_ASTReadError:
std::cerr << "libclang had ast read error for " << filepath << std::endl;
did_fail = true;
2017-03-25 19:18:25 +00:00
break;
}
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);
}
void TranslationUnit::ReparseTranslationUnit(std::vector<CXUnsavedFile>& unsaved, unsigned flags) {
int error_code = clang_reparseTranslationUnit(cx_tu, unsaved.size(), unsaved.data(), flags);
switch (error_code) {
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
}
}
CodeCompleteResults TranslationUnit::get_code_completions(
const std::string& buffer, unsigned line_number, unsigned column) {
2017-02-16 09:35:30 +00:00
CodeCompleteResults results(cx_tu, buffer, line_number, column);
return results;
}
2017-02-18 19:37:24 +00:00
/*
2017-02-17 09:57:44 +00:00
std::vector<Diagnostic> TranslationUnit::get_diagnostics() {
2017-02-16 09:35:30 +00:00
std::vector<Diagnostic> diagnostics;
2017-02-17 09:57:44 +00:00
for (unsigned c = 0; c < clang_getNumDiagnostics(cx_tu); c++) {
CXDiagnostic clang_diagnostic = clang_getDiagnostic(cx_tu, c);
2017-02-16 09:35:30 +00:00
diagnostics.emplace_back(Diagnostic(cx_tu, clang_diagnostic));
clang_disposeDiagnostic(clang_diagnostic);
}
return diagnostics;
}
2017-02-18 19:37:24 +00:00
*/
2017-02-16 09:35:30 +00:00
2017-02-18 19:37:24 +00:00
/*
2017-02-17 09:57:44 +00:00
std::unique_ptr<Tokens> TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) {
auto path = ToString(clang_getTranslationUnitSpelling(cx_tu));
2017-02-16 09:35:30 +00:00
SourceLocation start_location(cx_tu, path, start_offset);
SourceLocation end_location(cx_tu, path, end_offset);
SourceRange range(start_location, end_location);
return std::unique_ptr<Tokens>(new Tokens(cx_tu, range));
}
2017-02-17 09:57:44 +00:00
std::unique_ptr<Tokens> TranslationUnit::get_tokens(unsigned start_line, unsigned start_column, unsigned end_line, unsigned end_column) {
auto path = ToString(clang_getTranslationUnitSpelling(cx_tu));
2017-02-16 09:35:30 +00:00
SourceLocation start_location(cx_tu, path, start_line, start_column);
SourceLocation end_location(cx_tu, path, end_line, end_column);
SourceRange range(start_location, end_location);
return std::unique_ptr<Tokens>(new Tokens(cx_tu, range));
}
2017-02-18 19:37:24 +00:00
*/
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));
}
2017-02-18 19:37:24 +00:00
/*
2017-02-17 09:57:44 +00:00
Cursor TranslationUnit::get_cursor(std::string path, unsigned offset) {
2017-02-16 09:35:30 +00:00
SourceLocation location(cx_tu, path, offset);
return Cursor(clang_getCursor(cx_tu, location.cx_location));
}
2017-02-17 09:57:44 +00:00
Cursor TranslationUnit::get_cursor(std::string path, unsigned line, unsigned column) {
2017-02-16 09:35:30 +00:00
SourceLocation location(cx_tu, path, line, column);
return Cursor(clang_getCursor(cx_tu, location.cx_location));
}
2017-02-18 19:37:24 +00:00
*/
2017-02-17 09:57:44 +00:00
}