ccls/libclangmm/TranslationUnit.cc

143 lines
4.7 KiB
C++
Raw Normal View History

2017-02-16 09:35:30 +00:00
#include "TranslationUnit.h"
#include "SourceLocation.h"
#include "Tokens.h"
#include "Utility.h"
#include <fstream>
#include <sstream>
2017-02-22 08:52:00 +00:00
#include <cassert>
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
2017-02-17 09:57:44 +00:00
TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::string &buffer, unsigned flags) {
2017-02-16 09:35:30 +00:00
std::vector<const char*> args;
2017-02-17 09:57:44 +00:00
for (auto &a : command_line_args) {
2017-02-16 09:35:30 +00:00
args.push_back(a.c_str());
}
2017-02-17 09:57:44 +00:00
2017-02-16 09:35:30 +00:00
CXUnsavedFile files[1];
2017-02-17 09:57:44 +00:00
files[0].Filename = file_path.c_str();
files[0].Contents = buffer.c_str();
files[0].Length = buffer.size();
2017-02-22 08:52:00 +00:00
CXErrorCode error_code = clang_parseTranslationUnit2(
index.cx_index, file_path.c_str(), args.data(), args.size(), files, 1, flags, &cx_tu);
assert(!error_code);
2017-02-16 09:35:30 +00:00
}
2017-02-17 09:57:44 +00:00
TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
2017-02-22 08:52:00 +00:00
const std::vector<std::string> &command_line_args, unsigned flags) {
// TODO: only push defines for the time being. Might need to pass more flags.
2017-02-16 09:35:30 +00:00
std::vector<const char*> args;
2017-02-22 08:52:00 +00:00
for (const std::string& a : command_line_args) {
if (a.size() >= 2 && a[0] == '-' && a[1] == 'D')
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, file_path.c_str(), args.data(), args.size(), nullptr, 0, flags, &cx_tu);
assert(!error_code);
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-02-17 09:57:44 +00:00
void TranslationUnit::parse(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers, unsigned flags) {
2017-02-16 09:35:30 +00:00
std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) {
CXUnsavedFile file;
file.Filename = buffer.first.c_str();
file.Contents = buffer.second.c_str();
file.Length = buffer.second.size();
files.push_back(file);
}
std::vector<const char*> args;
2017-02-17 09:57:44 +00:00
for (auto &a : command_line_args) {
2017-02-16 09:35:30 +00:00
args.push_back(a.c_str());
}
2017-02-17 09:57:44 +00:00
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
args.size(), files.data(), files.size(), flags);
2017-02-16 09:35:30 +00:00
}
2017-02-17 09:57:44 +00:00
int TranslationUnit::ReparseTranslationUnit(const std::string &buffer, unsigned flags) {
2017-02-16 09:35:30 +00:00
CXUnsavedFile files[1];
2017-02-17 09:57:44 +00:00
auto file_path = ToString(clang_getTranslationUnitSpelling(cx_tu));
files[0].Filename = file_path.c_str();
files[0].Contents = buffer.c_str();
files[0].Length = buffer.size();
2017-02-16 09:35:30 +00:00
return clang_reparseTranslationUnit(cx_tu, 1, files, flags);
}
2017-02-17 09:57:44 +00:00
unsigned TranslationUnit::DefaultFlags() {
auto flags =
2017-02-16 09:35:30 +00:00
CXTranslationUnit_CacheCompletionResults |
CXTranslationUnit_PrecompiledPreamble |
CXTranslationUnit_Incomplete |
CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
#if CINDEX_VERSION_MAJOR>0 || (CINDEX_VERSION_MAJOR==0 && CINDEX_VERSION_MINOR>=35)
2017-02-17 09:57:44 +00:00
flags |= CXTranslationUnit_KeepGoing;
2017-02-16 09:35:30 +00:00
#endif
return flags;
}
2017-02-17 09:57:44 +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
}