From a31f06416f1d27ca2ebd5e396f807f75a2e524c0 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 9 Sep 2015 12:24:44 +0200 Subject: [PATCH 1/2] Fixes a bug where not all of the similar types gets returned. --- src/Cursor.h | 1 + src/Token.h | 1 + src/Tokens.cc | 8 ++++-- src/TranslationUnit.cc | 61 +++++++++++++++--------------------------- src/TranslationUnit.h | 19 +++++++------ 5 files changed, 38 insertions(+), 52 deletions(-) diff --git a/src/Cursor.h b/src/Cursor.h index 7c01ca7..f567375 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -184,6 +184,7 @@ namespace clang { Cursor get_referenced() const; operator bool() const; bool operator==(const Cursor& rhs) const; + CXCursor cx_cursor; }; } // namespace clang diff --git a/src/Token.h b/src/Token.h index ed31d8a..d8f44db 100644 --- a/src/Token.h +++ b/src/Token.h @@ -28,6 +28,7 @@ namespace clang { bool has_type(); std::string get_type(); std::string get_brief_comments(); + CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; diff --git a/src/Tokens.cc b/src/Tokens.cc index f49b747..f207daa 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -1,7 +1,7 @@ #include "Tokens.h" #include "Utility.h" -#include -using namespace std; +#include //TODO: remove +using namespace std; //TODO: remove clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); @@ -9,6 +9,10 @@ clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu cx_cursors.resize(num_tokens); clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); for (unsigned i = 0; i < num_tokens; i++) { + if(cx_cursors[i].kind==CXCursor_DeclRefExpr) { //Temporary fix to a libclang bug + auto real_cursor=clang_getCursor(cx_tu, clang_getTokenLocation(cx_tu, cx_tokens[i])); + cx_cursors[i]=real_cursor; + } emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i])); } } diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 1118f37..3ef07d0 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -5,49 +5,41 @@ #include #include +#include //TODO: remove +using namespace std; //TODO: remove + clang::TranslationUnit:: ~TranslationUnit() { clang_disposeTranslationUnit(cx_tu); } clang::TranslationUnit:: -TranslationUnit(Index &index, - const std::string &filepath, - const std::vector &command_line_args) { +TranslationUnit(Index &index, const std::string &file_path, const std::vector &command_line_args) { std::map buffers; - std::ifstream ifs(filepath, std::ifstream::in); + std::ifstream ifs(file_path, std::ifstream::in); std::stringstream ss; ss << ifs.rdbuf(); - buffers[filepath]=ss.str(); - parse(index, filepath, command_line_args, buffers); + buffers[file_path]=ss.str(); + parse(index, file_path, command_line_args, buffers); } -clang::TranslationUnit:: -TranslationUnit(Index &index, - const std::string &filepath) { +clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path) { std::vector command_line_args; std::map buffers; - std::ifstream ifs(filepath, std::ifstream::in); + std::ifstream ifs(file_path, std::ifstream::in); std::stringstream ss; ss << ifs.rdbuf(); - buffers[filepath]=ss.str(); - parse(index, filepath, command_line_args, buffers); + buffers[file_path]=ss.str(); + parse(index, file_path, command_line_args, buffers); } -clang::TranslationUnit:: -TranslationUnit(clang::Index &index, - const std::string &filepath, - const std::vector &command_line_args, - const std::map &buffers, - unsigned flags) { - parse(index, filepath, command_line_args, buffers, flags); +clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path, const std::vector &command_line_args, + const std::map &buffers, unsigned flags) { + parse(index, file_path, command_line_args, buffers, flags); } -void clang::TranslationUnit::parse(Index &index, - const std::string &filepath, - const std::vector &command_line_args, - const std::map &buffers, - unsigned flags) { +void clang::TranslationUnit::parse(Index &index, const std::string &file_path, const std::vector &command_line_args, + const std::map &buffers, unsigned flags) { std::vector files; for (auto &buffer : buffers) { CXUnsavedFile file; @@ -60,19 +52,10 @@ void clang::TranslationUnit::parse(Index &index, for(auto &a: command_line_args) { args.push_back(a.c_str()); } - cx_tu = - clang_parseTranslationUnit(index.cx_index, - filepath.c_str(), - args.data(), - args.size(), - files.data(), - files.size(), - flags); + cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(), args.size(), files.data(), files.size(), flags); } -int clang::TranslationUnit:: -ReparseTranslationUnit(const std::map &buffers, - unsigned flags) { +int clang::TranslationUnit::ReparseTranslationUnit(const std::map &buffers, unsigned flags) { std::vector files; for (auto &buffer : buffers) { CXUnsavedFile file; @@ -81,17 +64,15 @@ ReparseTranslationUnit(const std::map &buffers, file.Length = buffer.second.size(); files.push_back(file); } - return clang_reparseTranslationUnit(cx_tu, - files.size(), - files.data(), - flags); + return clang_reparseTranslationUnit(cx_tu, files.size(), files.data(), flags); } unsigned clang::TranslationUnit::DefaultFlags() { return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; } -clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, unsigned line_number, unsigned column) { +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)); clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column); diff --git a/src/TranslationUnit.h b/src/TranslationUnit.h index e10d465..7cdd4dd 100644 --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -15,32 +15,31 @@ namespace clang { class TranslationUnit { public: TranslationUnit(Index &index, - const std::string &filepath, + const std::string &file_path, const std::vector &command_line_args); TranslationUnit(Index &index, - const std::string &filepath, + const std::string &file_path, const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - TranslationUnit(Index &index, - const std::string &filepath); + TranslationUnit(Index &index, const std::string &file_path); ~TranslationUnit(); int ReparseTranslationUnit(const std::map &buffers, unsigned flags=DefaultFlags()); static unsigned DefaultFlags(); void parse(Index &index, - const std::string &filepath, + const std::string &file_path, const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - clang::CodeCompleteResults get_code_completions(const std::map &buffers, unsigned line_number, unsigned column); - std::vector get_diagnostics(); - std::unique_ptr get_tokens(unsigned start_offset, unsigned end_offset); - clang::Cursor get_cursor(std::string path, unsigned offset); + clang::CodeCompleteResults get_code_completions(const std::map &buffers, unsigned line_number, unsigned column); + std::vector get_diagnostics(); + std::unique_ptr get_tokens(unsigned start_offset, unsigned end_offset); + clang::Cursor get_cursor(std::string path, unsigned offset); - CXTranslationUnit cx_tu; + CXTranslationUnit cx_tu; }; } // namespace clang #endif // TRANSLATIONUNIT_H_ From 192122d17b47f9e1d8124b3623ba17cd6fbd59e0 Mon Sep 17 00:00:00 2001 From: Ole Christian Eidheim Date: Sat, 12 Sep 2015 09:02:36 +0200 Subject: [PATCH 2/2] A bit shorter function signatures --- src/TranslationUnit.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 3ef07d0..098329d 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -33,12 +33,14 @@ clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_pa parse(index, file_path, command_line_args, buffers); } -clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path, const std::vector &command_line_args, +clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path, + const std::vector &command_line_args, const std::map &buffers, unsigned flags) { parse(index, file_path, command_line_args, buffers, flags); } -void clang::TranslationUnit::parse(Index &index, const std::string &file_path, const std::vector &command_line_args, +void clang::TranslationUnit::parse(Index &index, const std::string &file_path, + const std::vector &command_line_args, const std::map &buffers, unsigned flags) { std::vector files; for (auto &buffer : buffers) { @@ -52,10 +54,11 @@ void clang::TranslationUnit::parse(Index &index, const std::string &file_path, c 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); + cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(), + args.size(), files.data(), files.size(), flags); } -int clang::TranslationUnit::ReparseTranslationUnit(const std::map &buffers, unsigned flags) { +int clang::TranslationUnit::ReparseTranslationUnit(const std::map &buffers, unsigned flags) { std::vector files; for (auto &buffer : buffers) { CXUnsavedFile file;