From c948f2694318f33106a9f402dbf8666ee3fad596 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 27 Nov 2015 09:01:14 +0100 Subject: [PATCH] Now possible to perform clang processing without copy of buffer from Glib::ustring. --- src/CodeCompleteResults.cc | 20 ++++++++++++++++++++ src/CodeCompleteResults.h | 6 +++++- src/TranslationUnit.cc | 35 +++++++++++++++++++++++++++++++++++ src/TranslationUnit.h | 18 ++++++++++++++++-- 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index 8fdef7b..3d6a931 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -4,6 +4,26 @@ #include "Utility.h" clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu, + const std::string &buffer, + unsigned line_num, unsigned column) { + CXUnsavedFile files[1]; + auto file_path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); + files[0].Filename = file_path.c_str(); + files[0].Contents = buffer.c_str(); + files[0].Length = buffer.size(); + + cx_results = clang_codeCompleteAt(cx_tu, + file_path.c_str(), + line_num, + column, + files, + 1, + clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments); + if(cx_results!=NULL) + clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults); +} + +clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, unsigned line_num, unsigned column) { diff --git a/src/CodeCompleteResults.h b/src/CodeCompleteResults.h index ceb366c..af0f536 100644 --- a/src/CodeCompleteResults.h +++ b/src/CodeCompleteResults.h @@ -8,7 +8,11 @@ namespace clang { class CodeCompleteResults { friend class TranslationUnit; - CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, + + CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer, + unsigned line_num, unsigned column); + //TODO: remove + CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_path, const std::map &buffers, unsigned line_num, unsigned column); public: diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 64e3af3..af8140c 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -33,6 +33,23 @@ clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_pa parse(index, file_path, command_line_args, buffers); } +clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path, + const std::vector &command_line_args, + const std::string &buffer, unsigned flags) { + 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); +} + clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path, const std::vector &command_line_args, const std::map &buffers, unsigned flags) { @@ -58,6 +75,18 @@ void clang::TranslationUnit::parse(Index &index, const std::string &file_path, args.size(), files.data(), files.size(), flags); } +int clang::TranslationUnit::ReparseTranslationUnit(const std::string &buffer, unsigned flags) { + CXUnsavedFile files[1]; + + auto file_path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); + + files[0].Filename=file_path.c_str(); + files[0].Contents=buffer.c_str(); + files[0].Length=buffer.size(); + + return clang_reparseTranslationUnit(cx_tu, 1, files, flags); +} + int clang::TranslationUnit::ReparseTranslationUnit(const std::map &buffers, unsigned flags) { std::vector files; for (auto &buffer : buffers) { @@ -74,6 +103,12 @@ unsigned clang::TranslationUnit::DefaultFlags() { return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; } +clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::string &buffer, + unsigned line_number, unsigned column) { + clang::CodeCompleteResults results(cx_tu, buffer, line_number, column); + return results; +} + clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, unsigned line_number, unsigned column) { auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); diff --git a/src/TranslationUnit.h b/src/TranslationUnit.h index bd8dc58..27ced5a 100644 --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -14,9 +14,16 @@ namespace clang { class TranslationUnit { public: + //TODO: remove TranslationUnit(Index &index, const std::string &file_path, const std::vector &command_line_args); + TranslationUnit(Index &index, + const std::string &file_path, + const std::vector &command_line_args, + const std::string &buffer, + unsigned flags=DefaultFlags()); + //TODO: remove TranslationUnit(Index &index, const std::string &file_path, const std::vector &command_line_args, @@ -24,8 +31,12 @@ namespace clang { unsigned flags=DefaultFlags()); TranslationUnit(Index &index, const std::string &file_path); ~TranslationUnit(); + + int ReparseTranslationUnit(const std::string &buffer, unsigned flags=DefaultFlags()); + //TODO: remove int ReparseTranslationUnit(const std::map &buffers, unsigned flags=DefaultFlags()); + static unsigned DefaultFlags(); void parse(Index &index, @@ -33,8 +44,11 @@ namespace clang { const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - - clang::CodeCompleteResults get_code_completions(const std::map &buffers, + + clang::CodeCompleteResults get_code_completions(const std::string &buffer, + unsigned line_number, unsigned column); + //TODO: remove + clang::CodeCompleteResults get_code_completions(const std::map &buffers, unsigned line_number, unsigned column); std::vector get_diagnostics();