Commit 5e0c3927 authored by Ole Christian Eidheim's avatar Ole Christian Eidheim
Browse files

Cleanup of TranslationUnit-constructors, and removed unnecessary TranslationUnit::parse

parent 2b2f2ead
Pipeline #34398178 passed with stage
in 3 minutes and 3 seconds
......@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
project(clangmm)
add_compile_options(-std=c++11 -Wall -Wextra -Wno-unused-parameter)
add_compile_options(-std=c++11 -Wall -Wextra)
if(APPLE)
set(CMAKE_MACOSX_RPATH 1)
......
......@@ -49,7 +49,7 @@ clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool
};
VisitorData data{this, range.get_start().get_path(), invalid_tokens, {}};
auto translation_unit_cursor = clang_getTranslationUnitCursor(cx_tu);
clang_visitChildren(translation_unit_cursor, [](CXCursor cx_cursor, CXCursor cx_parent, CXClientData data_) {
clang_visitChildren(translation_unit_cursor, [](CXCursor cx_cursor, CXCursor /*cx_parent*/, CXClientData data_) {
auto data = static_cast<VisitorData *>(data_);
Cursor cursor(cx_cursor);
if(cursor.get_source_location().get_path() == data->path)
......@@ -58,7 +58,7 @@ clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool
}, &data);
for(auto &cursor : data.cursors) {
clang_visitChildren(cursor.cx_cursor, [](CXCursor cx_cursor, CXCursor cx_parent, CXClientData data_) {
clang_visitChildren(cursor.cx_cursor, [](CXCursor cx_cursor, CXCursor /*cx_parent*/, CXClientData data_) {
auto data = static_cast<VisitorData *>(data_);
if(clang_getCursorKind(cx_cursor) == CXCursor_FieldDecl) {
Cursor cursor(cx_cursor);
......
......@@ -5,66 +5,34 @@
#include <fstream>
#include <sstream>
#include <iostream> //TODO: remove
using namespace std; //TODO: remove
clangmm::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
clangmm::TranslationUnit::TranslationUnit(std::shared_ptr<Index> index_, const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::string &buffer, int flags) {
const std::string *buffer, int flags) : index(std::move(index_)) {
std::vector<const char *> args;
for(auto &a : command_line_args) {
args.push_back(a.c_str());
}
CXUnsavedFile files[1];
files[0].Filename = file_path.c_str();
files[0].Contents = buffer.c_str();
files[0].Length = buffer.size();
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
args.size(), files, 1, flags);
}
clangmm::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args,
int flags) {
std::vector<const char *> args;
for(auto &a : command_line_args) {
args.push_back(a.c_str());
std::vector<CXUnsavedFile> files;
if(buffer) {
files.emplace_back();
files.back().Filename = file_path.c_str();
files.back().Contents = buffer->c_str();
files.back().Length = buffer->size();
}
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
args.size(), NULL, 0, flags);
cx_tu = clang_parseTranslationUnit(index->cx_index, file_path.c_str(), args.data(),
args.size(), files.empty() ? nullptr : files.data(), files.size(), flags);
}
clangmm::TranslationUnit::~TranslationUnit() {
clang_disposeTranslationUnit(cx_tu);
}
void clangmm::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, int flags) {
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;
for(auto &a : command_line_args) {
args.push_back(a.c_str());
}
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
args.size(), files.data(), files.size(), flags);
}
int clangmm::TranslationUnit::reparse(const std::string &buffer, int flags) {
CXUnsavedFile files[1];
auto file_path = to_string(clang_getTranslationUnitSpelling(cx_tu));
CXUnsavedFile files[1];
files[0].Filename = file_path.c_str();
files[0].Contents = buffer.c_str();
files[0].Length = buffer.size();
......
......@@ -13,13 +13,12 @@
namespace clangmm {
class TranslationUnit {
std::shared_ptr<Index> index;
public:
TranslationUnit(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::string &buffer,
int flags = DefaultFlags());
TranslationUnit(Index &index, const std::string &file_path,
TranslationUnit(std::shared_ptr<Index> index_, const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::string *buffer = nullptr,
int flags = DefaultFlags());
~TranslationUnit();
......@@ -27,12 +26,6 @@ namespace clangmm {
static int DefaultFlags();
void parse(Index &index,
const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers,
int flags = DefaultFlags());
CodeCompleteResults get_code_completions(const std::string &buffer,
unsigned line_number, unsigned column);
......
......@@ -17,7 +17,7 @@ int main() {
arguments.emplace_back("-I/usr/lib64/clang/" + clang_version + "/include"); // For Fedora
}
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
clangmm::TranslationUnit tu(index, path, arguments);
std::string buffer = "#include <string>\n"
......
......@@ -25,7 +25,7 @@ int main() {
arguments.emplace_back("-I/usr/lib64/clang/" + clang_version + "/include"); // For Fedora
}
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
clangmm::TranslationUnit tu(index, path, arguments);
std::string buffer = "#include <string>\n"
......
......@@ -7,7 +7,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp");
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
std::vector<std::string> arguments;
auto clang_version_string = clangmm::to_string(clang_getClangVersion());
......
......@@ -10,7 +10,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main_error.cpp");
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
std::vector<std::string> arguments;
auto clang_version_string = clangmm::to_string(clang_getClangVersion());
......
#include "clangmm.h"
#include <cassert>
#include <memory>
#include <string>
int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp");
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
clangmm::TranslationUnit tu(index, path, {});
auto tokens = tu.get_tokens(0, 113);
......
......@@ -6,7 +6,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp");
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
clangmm::TranslationUnit tu(index, path, {});
......
......@@ -7,7 +7,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp");
clangmm::Index index(0, 0);
auto index = std::make_shared<clangmm::Index>(0, 0);
clangmm::TranslationUnit tu(index, path, {});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment