Browse Source

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

merge-requests/37/head
eidheim 7 years ago
parent
commit
5e0c39279e
  1. 2
      CMakeLists.txt
  2. 4
      src/tokens.cc
  3. 54
      src/translation_unit.cc
  4. 15
      src/translation_unit.h
  5. 2
      tests/code_complete_results_test.cc
  6. 2
      tests/completion_string_test.cc
  7. 2
      tests/cursor_test.cc
  8. 2
      tests/diagnostics_test.cc
  9. 3
      tests/source_location_test.cc
  10. 2
      tests/token_test.cc
  11. 2
      tests/translation_unit_test.cc

2
CMakeLists.txt

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
project(clangmm) project(clangmm)
add_compile_options(-std=c++11 -Wall -Wextra -Wno-unused-parameter) add_compile_options(-std=c++11 -Wall -Wextra)
if(APPLE) if(APPLE)
set(CMAKE_MACOSX_RPATH 1) set(CMAKE_MACOSX_RPATH 1)

4
src/tokens.cc

@ -49,7 +49,7 @@ clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool
}; };
VisitorData data{this, range.get_start().get_path(), invalid_tokens, {}}; VisitorData data{this, range.get_start().get_path(), invalid_tokens, {}};
auto translation_unit_cursor = clang_getTranslationUnitCursor(cx_tu); 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_); auto data = static_cast<VisitorData *>(data_);
Cursor cursor(cx_cursor); Cursor cursor(cx_cursor);
if(cursor.get_source_location().get_path() == data->path) if(cursor.get_source_location().get_path() == data->path)
@ -58,7 +58,7 @@ clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool
}, &data); }, &data);
for(auto &cursor : data.cursors) { 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_); auto data = static_cast<VisitorData *>(data_);
if(clang_getCursorKind(cx_cursor) == CXCursor_FieldDecl) { if(clang_getCursorKind(cx_cursor) == CXCursor_FieldDecl) {
Cursor cursor(cx_cursor); Cursor cursor(cx_cursor);

54
src/translation_unit.cc

@ -5,66 +5,34 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> //TODO: remove clangmm::TranslationUnit::TranslationUnit(std::shared_ptr<Index> index_, const std::string &file_path,
using namespace std; //TODO: remove
clangmm::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args, 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; std::vector<const char *> args;
for(auto &a : command_line_args) { for(auto &a : command_line_args) {
args.push_back(a.c_str()); args.push_back(a.c_str());
} }
CXUnsavedFile files[1]; std::vector<CXUnsavedFile> files;
files[0].Filename = file_path.c_str(); if(buffer) {
files[0].Contents = buffer.c_str(); files.emplace_back();
files[0].Length = buffer.size(); files.back().Filename = file_path.c_str();
files.back().Contents = buffer->c_str();
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(), files.back().Length = buffer->size();
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());
} }
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(), cx_tu = clang_parseTranslationUnit(index->cx_index, file_path.c_str(), args.data(),
args.size(), NULL, 0, flags); args.size(), files.empty() ? nullptr : files.data(), files.size(), flags);
} }
clangmm::TranslationUnit::~TranslationUnit() { clangmm::TranslationUnit::~TranslationUnit() {
clang_disposeTranslationUnit(cx_tu); 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) { int clangmm::TranslationUnit::reparse(const std::string &buffer, int flags) {
CXUnsavedFile files[1];
auto file_path = to_string(clang_getTranslationUnitSpelling(cx_tu)); auto file_path = to_string(clang_getTranslationUnitSpelling(cx_tu));
CXUnsavedFile files[1];
files[0].Filename = file_path.c_str(); files[0].Filename = file_path.c_str();
files[0].Contents = buffer.c_str(); files[0].Contents = buffer.c_str();
files[0].Length = buffer.size(); files[0].Length = buffer.size();

15
src/translation_unit.h

@ -13,13 +13,12 @@
namespace clangmm { namespace clangmm {
class TranslationUnit { class TranslationUnit {
std::shared_ptr<Index> index;
public: public:
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,
int flags = DefaultFlags());
TranslationUnit(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args, const std::vector<std::string> &command_line_args,
const std::string *buffer = nullptr,
int flags = DefaultFlags()); int flags = DefaultFlags());
~TranslationUnit(); ~TranslationUnit();
@ -27,12 +26,6 @@ namespace clangmm {
static int DefaultFlags(); 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, CodeCompleteResults get_code_completions(const std::string &buffer,
unsigned line_number, unsigned column); unsigned line_number, unsigned column);

2
tests/code_complete_results_test.cc

@ -17,7 +17,7 @@ int main() {
arguments.emplace_back("-I/usr/lib64/clang/" + clang_version + "/include"); // For Fedora 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); clangmm::TranslationUnit tu(index, path, arguments);
std::string buffer = "#include <string>\n" std::string buffer = "#include <string>\n"

2
tests/completion_string_test.cc

@ -25,7 +25,7 @@ int main() {
arguments.emplace_back("-I/usr/lib64/clang/" + clang_version + "/include"); // For Fedora 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); clangmm::TranslationUnit tu(index, path, arguments);
std::string buffer = "#include <string>\n" std::string buffer = "#include <string>\n"

2
tests/cursor_test.cc

@ -7,7 +7,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp"); 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; std::vector<std::string> arguments;
auto clang_version_string = clangmm::to_string(clang_getClangVersion()); auto clang_version_string = clangmm::to_string(clang_getClangVersion());

2
tests/diagnostics_test.cc

@ -10,7 +10,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main_error.cpp"); 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; std::vector<std::string> arguments;
auto clang_version_string = clangmm::to_string(clang_getClangVersion()); auto clang_version_string = clangmm::to_string(clang_getClangVersion());

3
tests/source_location_test.cc

@ -1,12 +1,13 @@
#include "clangmm.h" #include "clangmm.h"
#include <cassert> #include <cassert>
#include <memory>
#include <string> #include <string>
int main() { int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp"); 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, {}); clangmm::TranslationUnit tu(index, path, {});
auto tokens = tu.get_tokens(0, 113); auto tokens = tu.get_tokens(0, 113);

2
tests/token_test.cc

@ -6,7 +6,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp"); 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, {}); clangmm::TranslationUnit tu(index, path, {});

2
tests/translation_unit_test.cc

@ -7,7 +7,7 @@ int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;
std::string path(tests_path + "/case/main.cpp"); 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, {}); clangmm::TranslationUnit tu(index, path, {});

Loading…
Cancel
Save