From 5e0c39279e07d5e78973b4dd36160d245d30a580 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 26 Oct 2018 08:44:02 +0200 Subject: [PATCH] Cleanup of TranslationUnit-constructors, and removed unnecessary TranslationUnit::parse --- CMakeLists.txt | 2 +- src/tokens.cc | 4 +-- src/translation_unit.cc | 54 ++++++----------------------- src/translation_unit.h | 15 +++----- tests/code_complete_results_test.cc | 2 +- tests/completion_string_test.cc | 2 +- tests/cursor_test.cc | 2 +- tests/diagnostics_test.cc | 2 +- tests/source_location_test.cc | 3 +- tests/token_test.cc | 2 +- tests/translation_unit_test.cc | 2 +- 11 files changed, 26 insertions(+), 64 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71c9f59..b72f621 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/tokens.cc b/src/tokens.cc index e15b71a..faf76ca 100644 --- a/src/tokens.cc +++ b/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, {}}; 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(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(data_); if(clang_getCursorKind(cx_cursor) == CXCursor_FieldDecl) { Cursor cursor(cx_cursor); diff --git a/src/translation_unit.cc b/src/translation_unit.cc index 8a82d6b..f1ab0db 100644 --- a/src/translation_unit.cc +++ b/src/translation_unit.cc @@ -5,66 +5,34 @@ #include #include -#include //TODO: remove -using namespace std; //TODO: remove - -clangmm::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path, +clangmm::TranslationUnit::TranslationUnit(std::shared_ptr index_, const std::string &file_path, const std::vector &command_line_args, - const std::string &buffer, int flags) { + const std::string *buffer, int flags) : index(std::move(index_)) { std::vector 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 &command_line_args, - int flags) { - std::vector args; - for(auto &a : command_line_args) { - args.push_back(a.c_str()); + std::vector 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 &command_line_args, - const std::map &buffers, int flags) { - std::vector 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 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(); diff --git a/src/translation_unit.h b/src/translation_unit.h index 5475ff7..33f72ec 100644 --- a/src/translation_unit.h +++ b/src/translation_unit.h @@ -13,13 +13,12 @@ namespace clangmm { class TranslationUnit { + std::shared_ptr index; + public: - TranslationUnit(Index &index, const std::string &file_path, - const std::vector &command_line_args, - const std::string &buffer, - int flags = DefaultFlags()); - TranslationUnit(Index &index, const std::string &file_path, + TranslationUnit(std::shared_ptr index_, const std::string &file_path, const std::vector &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 &command_line_args, - const std::map &buffers, - int flags = DefaultFlags()); - CodeCompleteResults get_code_completions(const std::string &buffer, unsigned line_number, unsigned column); diff --git a/tests/code_complete_results_test.cc b/tests/code_complete_results_test.cc index 681efba..b824f53 100644 --- a/tests/code_complete_results_test.cc +++ b/tests/code_complete_results_test.cc @@ -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(0, 0); clangmm::TranslationUnit tu(index, path, arguments); std::string buffer = "#include \n" diff --git a/tests/completion_string_test.cc b/tests/completion_string_test.cc index 9a2db93..58245ba 100644 --- a/tests/completion_string_test.cc +++ b/tests/completion_string_test.cc @@ -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(0, 0); clangmm::TranslationUnit tu(index, path, arguments); std::string buffer = "#include \n" diff --git a/tests/cursor_test.cc b/tests/cursor_test.cc index c89acf9..3849a01 100644 --- a/tests/cursor_test.cc +++ b/tests/cursor_test.cc @@ -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(0, 0); std::vector arguments; auto clang_version_string = clangmm::to_string(clang_getClangVersion()); diff --git a/tests/diagnostics_test.cc b/tests/diagnostics_test.cc index 4df561d..c82971d 100644 --- a/tests/diagnostics_test.cc +++ b/tests/diagnostics_test.cc @@ -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(0, 0); std::vector arguments; auto clang_version_string = clangmm::to_string(clang_getClangVersion()); diff --git a/tests/source_location_test.cc b/tests/source_location_test.cc index 499cddf..accdb9a 100644 --- a/tests/source_location_test.cc +++ b/tests/source_location_test.cc @@ -1,12 +1,13 @@ #include "clangmm.h" #include +#include #include 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(0, 0); clangmm::TranslationUnit tu(index, path, {}); auto tokens = tu.get_tokens(0, 113); diff --git a/tests/token_test.cc b/tests/token_test.cc index 950c5b4..d269235 100644 --- a/tests/token_test.cc +++ b/tests/token_test.cc @@ -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(0, 0); clangmm::TranslationUnit tu(index, path, {}); diff --git a/tests/translation_unit_test.cc b/tests/translation_unit_test.cc index cd0a550..4525e7e 100644 --- a/tests/translation_unit_test.cc +++ b/tests/translation_unit_test.cc @@ -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(0, 0); clangmm::TranslationUnit tu(index, path, {});