From e2c8256b26c2d9b3bb59f6d44351955d0b51f7e8 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 14 Jul 2015 23:31:30 +0200 Subject: [PATCH 01/21] Big cleanup, got rid of several circular includes and class forward declarations. Still some work left, but I'll wait with this. Main reason behind the cleanup was compilation troubles. --- src/CMakeLists.txt | 1 - src/CodeCompleteResults.cc | 4 +- src/CodeCompleteResults.h | 8 +- src/CompilationDatabase.h | 3 +- src/CompileCommand.h | 2 +- src/CompileCommands.h | 3 +- src/CompletionString.h | 8 +- src/Cursor.cc | 4 +- src/Cursor.h | 10 +-- src/Diagnostic.cc | 20 ++--- src/Diagnostic.h | 14 +--- src/Index.h | 4 - src/SourceLocation.cc | 16 ++-- src/SourceLocation.h | 16 ++-- src/SourceRange.cc | 22 ++++- src/SourceRange.h | 23 +++-- src/Token.cc | 16 ++-- src/Token.h | 19 +++-- src/Tokens.cc | 125 ++++++++++++++++++++++++++-- src/Tokens.h | 16 +++- src/TranslationUnit.cc | 2 +- src/TranslationUnit.h | 16 +--- tests/CodeCompleteResults_H_Test.cc | 2 +- tests/CompletionString_H_Test.cc | 2 +- tests/Cursor_H_Test.cc | 4 +- tests/Diagnostics_Test.cc | 6 +- tests/SourceLocation_H_Test.cc | 8 +- tests/Token_H_Test.cc | 8 +- 28 files changed, 243 insertions(+), 139 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1a1ae1c..b30f052 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,6 @@ message("Searcing for libclang") find_package(LibClang REQUIRED) set(header_files - clangmm.h CodeCompleteResults.h CompilationDatabase.h CompileCommand.h diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index 6a281e8..ea1b0f6 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -3,7 +3,7 @@ #include clang::CodeCompleteResults:: -CodeCompleteResults(clang::TranslationUnit *tu, +CodeCompleteResults(CXTranslationUnit &tu, const std::string &file_name, const std::map &buffers, int line_num, @@ -16,7 +16,7 @@ CodeCompleteResults(clang::TranslationUnit *tu, file.Length = buffer.second.size(); files.push_back(file); } - results_ = clang_codeCompleteAt(tu->tu_, + results_ = clang_codeCompleteAt(tu, file_name.c_str(), line_num, column, diff --git a/src/CodeCompleteResults.h b/src/CodeCompleteResults.h index 8446a74..2269f68 100644 --- a/src/CodeCompleteResults.h +++ b/src/CodeCompleteResults.h @@ -1,14 +1,13 @@ #ifndef CODECOMPLETERESULTS_H_ #define CODECOMPLETERESULTS_H_ #include -#include "TranslationUnit.h" +#include +#include "CompletionString.h" namespace clang { - class CompletionString; - class CodeCompleteResults { public: - CodeCompleteResults(TranslationUnit *tu, + CodeCompleteResults(CXTranslationUnit &tu, const std::string &file_name, const std::map &buffers, int line_num, @@ -17,7 +16,6 @@ namespace clang { CompletionString get(int index); int size(); - private: CXCodeCompleteResults *results_; }; } // namespace clang diff --git a/src/CompilationDatabase.h b/src/CompilationDatabase.h index 8d66a57..7d35e56 100644 --- a/src/CompilationDatabase.h +++ b/src/CompilationDatabase.h @@ -11,9 +11,8 @@ namespace clang { explicit CompilationDatabase(const std::string &project_path); CompilationDatabase(); ~CompilationDatabase(); - private: + CXCompilationDatabase db_; - friend CompileCommands; }; } diff --git a/src/CompileCommand.h b/src/CompileCommand.h index f56dac8..cf3892e 100644 --- a/src/CompileCommand.h +++ b/src/CompileCommand.h @@ -9,7 +9,7 @@ namespace clang { CompileCommand(int nth, CompileCommands *commands); std::string get_command(); std::vector get_command_as_args(); - private: + CXCompileCommand command_; }; } diff --git a/src/CompileCommands.h b/src/CompileCommands.h index f56d859..7c4cb8d 100644 --- a/src/CompileCommands.h +++ b/src/CompileCommands.h @@ -12,9 +12,8 @@ namespace clang { CompileCommands(const std::string &filename, CompilationDatabase *db); std::vector get_commands(); ~CompileCommands(); - private: + CXCompileCommands commands_; - friend class CompileCommand; }; } #endif // COMPILECOMMANDS_H_ diff --git a/src/CompletionString.h b/src/CompletionString.h index eee7013..23f9ea2 100644 --- a/src/CompletionString.h +++ b/src/CompletionString.h @@ -1,7 +1,8 @@ #ifndef COMPLETIONSTRING_H_ #define COMPLETIONSTRING_H_ #include -#include "CodeCompleteResults.h" +#include +#include namespace clang { enum CompletionChunkKind { @@ -27,14 +28,13 @@ namespace clang { class CompletionString { public: + explicit CompletionString(const CXCompletionString &str); bool available(); std::vector get_chunks(); std::string get_brief_comments(); int get_num_chunks(); - private: - explicit CompletionString(const CXCompletionString &str); + CXCompletionString str_; - friend CodeCompleteResults; }; } // namespace clang #endif // COMPLETIONSTRING_H_ diff --git a/src/Cursor.cc b/src/Cursor.cc index e6ab0b7..7f9b3c8 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -5,6 +5,6 @@ const clang::CursorKind clang::Cursor::kind() { } clang::Cursor:: -Cursor(clang::TranslationUnit *tu, clang::SourceLocation *source_location) { - cursor_ = clang_getCursor(tu->tu_, source_location->location_); +Cursor(CXTranslationUnit &tu, clang::SourceLocation *source_location) { + cursor_ = clang_getCursor(tu, source_location->location_); } diff --git a/src/Cursor.h b/src/Cursor.h index 5740226..d7326ff 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -1,9 +1,11 @@ #ifndef CURSOR_H_ #define CURSOR_H_ -#include "TranslationUnit.h" +#include #include "SourceLocation.h" namespace clang { + class SourceLocation; + enum class CursorKind { UnexposedDecl = 1, StructDecl = 2, @@ -175,13 +177,9 @@ namespace clang { class Cursor { public: Cursor() {} - Cursor(TranslationUnit *tu, SourceLocation *source_location); + Cursor(CXTranslationUnit &tu, SourceLocation *source_location); const CursorKind kind(); - private: CXCursor cursor_; - friend SourceRange; - friend SourceLocation; - friend Tokens; }; } // namespace clang #endif // CURSOR_H_ diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 5712527..fa446e7 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -2,29 +2,19 @@ #include "SourceLocation.h" #include "Tokens.h" -clang::Diagnostic::Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic) { +clang::Diagnostic::Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic) { severity=clang_getDiagnosticSeverity(clang_diagnostic); severity_spelling=get_severity_spelling(severity); spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic)); clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic)); - std::string tmp_path; - unsigned line, column, offset; - location.get_location_info(&tmp_path, &line, &column, &offset); - path=tmp_path; - start_location.line=line; - start_location.column=column; - start_location.offset=offset; clang::SourceRange range(&location, &location); - clang::Tokens tokens(&tu, &range); + clang::Tokens tokens(tu, &range); if(tokens.size()==1) { auto& token=tokens[0]; - clang::SourceRange range=token.get_source_range(&tu); - clang::SourceLocation location(&range, false); - location.get_location_info(NULL, &line, &column, &offset); - end_location.line=line; - end_location.column=column; - end_location.offset=offset; + clang::SourceRange range=token.get_source_range(); + auto end_location=clang::SourceLocation(&range, false); + this->range=range.get_range_data(location, end_location); } } diff --git a/src/Diagnostic.h b/src/Diagnostic.h index c858d33..773b986 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -3,25 +3,19 @@ #include #include #include -#include "TranslationUnit.h" +#include "SourceRange.h" -namespace clang { +namespace clang { class Diagnostic { public: - class LocationData { - public: - unsigned line, column, offset; - }; - - Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic); + Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic); static const std::string get_severity_spelling(unsigned severity); unsigned severity; std::string severity_spelling; std::string spelling; - std::string path; - LocationData start_location, end_location; + RangeData range; }; } diff --git a/src/Index.h b/src/Index.h index f93feab..6e71e7b 100644 --- a/src/Index.h +++ b/src/Index.h @@ -1,16 +1,12 @@ #ifndef INDEX_H_ #define INDEX_H_ - #include namespace clang { - class TranslationUnit; class Index { public: Index(int excludeDeclarationsFromPCH, int displayDiagnostics); - private: CXIndex index_; - friend TranslationUnit; }; } // namespace clang #endif // INDEX_H_ diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index c68d32f..13506ae 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -4,13 +4,13 @@ // SourceLocation // // // // // // // // // clang::SourceLocation:: -SourceLocation(clang::TranslationUnit *tu, +SourceLocation(CXTranslationUnit &tu, const std::string &filename, int line_number, int line_offset) { - CXFile file = clang_getFile(tu->tu_, + CXFile file = clang_getFile(tu, filename.c_str()); - location_ = clang_getLocation(tu->tu_, + location_ = clang_getLocation(tu, file, line_number, line_offset); @@ -28,19 +28,19 @@ SourceLocation(clang::SourceRange *range, bool start) { } clang::SourceLocation:: -SourceLocation(TranslationUnit *tu, +SourceLocation(CXTranslationUnit &tu, Token *token) { - location_ = clang_getTokenLocation(tu->tu_, + location_ = clang_getTokenLocation(tu, token->token_); } clang::SourceLocation:: -SourceLocation(clang::TranslationUnit *tu, +SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset) { - CXFile file = clang_getFile(tu->tu_, + CXFile file = clang_getFile(tu, filepath.c_str()); - location_ = clang_getLocationForOffset(tu->tu_, + location_ = clang_getLocationForOffset(tu, file, offset); } diff --git a/src/SourceLocation.h b/src/SourceLocation.h index 286ddbb..81394ec 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -1,23 +1,28 @@ #ifndef SOURCELOCATION_H_ #define SOURCELOCATION_H_ -#include "TranslationUnit.h" +#include #include "Token.h" #include "Cursor.h" +#include namespace clang { + class Token; + class SourceRange; + class Cursor; + class SourceLocation { public: - SourceLocation(TranslationUnit* tu, + SourceLocation(CXTranslationUnit &tu, const std::string &filename, int line_number, int column); - SourceLocation(TranslationUnit *tu, + SourceLocation(CXTranslationUnit &tu, Token *token); SourceLocation(SourceRange *range, bool start); - SourceLocation(TranslationUnit *tu, + SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); @@ -30,10 +35,7 @@ namespace clang { unsigned *column, unsigned *offset); - private: CXSourceLocation location_; - friend SourceRange; - friend Cursor; }; } // namespace clang diff --git a/src/SourceRange.cc b/src/SourceRange.cc index 87581d4..8de63a6 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -1,8 +1,8 @@ #include "SourceRange.h" clang::SourceRange:: -SourceRange(clang::TranslationUnit *tu, clang::Token *token) { - range_ = clang_getTokenExtent(tu->tu_, token->token_); +SourceRange(clang::Token *token) { + range_ = clang_getTokenExtent(token->tu, token->token_); } clang::SourceRange:: @@ -13,3 +13,21 @@ SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) { clang::SourceRange::SourceRange(Cursor *cursor) { range_ = clang_getCursorExtent(cursor->cursor_); } + +clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start, clang::SourceLocation &end) { + std::string path; + unsigned start_offset, end_offset; + start.get_location_info(&path, NULL, NULL, &start_offset); + end.get_location_info(NULL, NULL, NULL, &end_offset); + RangeData range_data; + range_data.path=path; + range_data.start_offset=start_offset; + range_data.end_offset=end_offset; + return range_data; +} + +clang::RangeData clang::SourceRange::get_range_data() { + clang::SourceLocation start(this, true); + clang::SourceLocation end(this, false); + return get_range_data(start, end); +} \ No newline at end of file diff --git a/src/SourceRange.h b/src/SourceRange.h index d1f772b..be74888 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -1,23 +1,32 @@ #ifndef SOURCERANGE_H_ #define SOURCERANGE_H_ -#include "TranslationUnit.h" +#include #include "Token.h" #include "Cursor.h" +#include "SourceLocation.h" +#include namespace clang { + class Token; + class SourceLocation; + class Cursor; + + class RangeData { + public: + std::string path; + unsigned start_offset, end_offset; + }; + class SourceRange { public: SourceRange() {} - SourceRange(TranslationUnit *tu, Token *token); + SourceRange(Token *token); SourceRange(SourceLocation *start, SourceLocation *end); explicit SourceRange(Cursor *cursor); - - private: + static RangeData get_range_data(SourceLocation &start, SourceLocation &end); + RangeData get_range_data(); CXSourceRange range_; - friend Tokens; - friend SourceLocation; - friend Diagnostic; }; } // namespace clang #endif // SOURCERANGE_H_ diff --git a/src/Token.cc b/src/Token.cc index 455da1a..6ac0945 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -5,25 +5,23 @@ // // // // // // clang::Token instansiates an token -clang::Token::Token(const CXToken &token) : - token_(token) { +clang::Token::Token(CXTranslationUnit &tu, const CXToken &token) : + tu(tu), token_(token) { } // returns gets an source location for this token objekt // based on the translationunit given -clang::SourceLocation clang::Token:: - get_source_location(clang::TranslationUnit *tu) { +clang::SourceLocation clang::Token::get_source_location() { return SourceLocation(tu, this); } // returns a sourcerange that covers this token -clang::SourceRange clang::Token:: - get_source_range(clang::TranslationUnit *tu) { - return SourceRange(tu, this); +clang::SourceRange clang::Token::get_source_range() { + return SourceRange(this); } // returns a string description of this tokens kind -std::string clang::Token::get_token_spelling(clang::TranslationUnit *tu) { - CXString s = clang_getTokenSpelling(tu->tu_, token_); +std::string clang::Token::get_token_spelling() { + CXString s = clang_getTokenSpelling(tu, token_); return std::string(clang_getCString(s)); } diff --git a/src/Token.h b/src/Token.h index dd8efe4..627b6ef 100644 --- a/src/Token.h +++ b/src/Token.h @@ -1,10 +1,14 @@ #ifndef TOKEN_H_ #define TOKEN_H_ +#include #include "SourceLocation.h" #include "SourceRange.h" -#include "TranslationUnit.h" +#include namespace clang { + class SourceLocation; + class SourceRange; + enum TokenKind { Token_Punctuation, Token_Keyword, @@ -15,17 +19,14 @@ namespace clang { class Token { public: + explicit Token(CXTranslationUnit &tu, const CXToken &token); const TokenKind kind(); - std::string get_token_spelling(TranslationUnit *tu); - SourceLocation get_source_location(TranslationUnit *tu); - SourceRange get_source_range(TranslationUnit *tu); + std::string get_token_spelling(); + SourceLocation get_source_location(); + SourceRange get_source_range(); std::string type; - private: - explicit Token(const CXToken &token); - friend SourceRange; - friend SourceLocation; - friend Tokens; const CXToken& token_; + CXTranslationUnit &tu; }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 571d0db..9b2b1e8 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -2,24 +2,24 @@ #include using namespace std; -clang::Tokens::Tokens(clang::TranslationUnit *tu, clang::SourceRange *range): tu(*tu) { - clang_tokenize(tu->tu_, +clang::Tokens::Tokens(CXTranslationUnit &tu, clang::SourceRange *range): tu(tu) { + clang_tokenize(tu, range->range_, &tokens_, &num_tokens_); for (int i = 0; i < num_tokens_; i++) { - push_back(clang::Token(tokens_[i])); + emplace_back(tu, tokens_[i]); } } clang::Tokens::~Tokens() { - clang_disposeTokens(tu.tu_, tokens_, size()); + clang_disposeTokens(tu, tokens_, size()); } -void clang::Tokens::update_types(clang::TranslationUnit *tu) { +void clang::Tokens::update_types() { clang_cursors.clear(); clang_cursors.reserve(size()); - clang_annotateTokens(tu->tu_, tokens_, size(), clang_cursors.data()); + clang_annotateTokens(tu, tokens_, size(), clang_cursors.data()); for(size_t c=0;c > &ranges) { + for(size_t c=0;c #include "SourceRange.h" #include "Token.h" +#include +#include namespace clang { class Tokens : public std::vector { public: - Tokens(TranslationUnit *tu, SourceRange *range); + Tokens(CXTranslationUnit &tu, SourceRange *range); ~Tokens(); - void update_types(clang::TranslationUnit *tu); + void update_types(); std::string get_brief_comments(size_t cursor_id); + CXCursor find_referenced(); + bool equalCursors(CXCursor a, CXCursor b); + void rename(CXCursor &referenced, std::unordered_map > &ranges); + private: CXToken *tokens_; unsigned num_tokens_; std::vector clang_cursors; - TranslationUnit& tu; + CXTranslationUnit& tu; + + static CXChildVisitResult clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData); }; } // namespace clang #endif // TOKENS_H_ diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index de1c2aa..497c62d 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -101,7 +101,7 @@ void clang::TranslationUnit::update_diagnostics() { diagnostics.clear(); for(unsigned c=0;c diagnostics; - private: + void parse(Index *index, const std::string &filepath, const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - friend Token; - friend Tokens; - friend SourceLocation; - friend SourceRange; - friend Cursor; - friend CodeCompleteResults; CXTranslationUnit tu_; }; } // namespace clang diff --git a/tests/CodeCompleteResults_H_Test.cc b/tests/CodeCompleteResults_H_Test.cc index c696727..a5b4ad1 100644 --- a/tests/CodeCompleteResults_H_Test.cc +++ b/tests/CodeCompleteResults_H_Test.cc @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) { // ] - clang::CodeCompleteResults results(&tu, path, buffers, 4, 5); + clang::CodeCompleteResults results(tu.tu_, path, buffers, 4, 5); bool substr_found=false; for(int c=0;c Date: Wed, 15 Jul 2015 00:45:45 +0200 Subject: [PATCH 02/21] clangmm.h now gets installed again when doing make install. --- src/CMakeLists.txt | 1 + src/Diagnostic.cc | 2 +- src/Tokens.h | 2 -- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b30f052..478c921 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ message("Searcing for libclang") find_package(LibClang REQUIRED) set(header_files + clangmm.h CodeCompleteResults.h CompilationDatabase.h CompileCommand.h diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index fa446e7..c501da4 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -14,7 +14,7 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnos auto& token=tokens[0]; clang::SourceRange range=token.get_source_range(); auto end_location=clang::SourceLocation(&range, false); - this->range=range.get_range_data(location, end_location); + this->range=SourceRange::get_range_data(location, end_location); } } diff --git a/src/Tokens.h b/src/Tokens.h index c8e7d3a..bc0b392 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -22,8 +22,6 @@ namespace clang { unsigned num_tokens_; std::vector clang_cursors; CXTranslationUnit& tu; - - static CXChildVisitResult clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData); }; } // namespace clang #endif // TOKENS_H_ From de4ca5281843e00ff1e1afa5c48a1e077119c721 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 14:32:33 +0200 Subject: [PATCH 03/21] Major cleanup. Think I got rid of all the forward declarations. The library is much easier to use now while having the same features. --- src/CodeCompleteResults.cc | 14 +- src/CodeCompleteResults.h | 4 +- src/CompilationDatabase.cc | 4 +- src/CompilationDatabase.h | 2 +- src/CompileCommand.cc | 12 +- src/CompileCommand.h | 4 +- src/CompileCommands.cc | 16 +-- src/CompileCommands.h | 4 +- src/CompletionString.cc | 14 +- src/CompletionString.h | 4 +- src/Cursor.cc | 22 ++- src/Cursor.h | 16 ++- src/Diagnostic.cc | 16 +-- src/Diagnostic.h | 2 +- src/Index.cc | 2 +- src/Index.h | 2 +- src/SourceLocation.cc | 24 +--- src/SourceLocation.h | 22 +-- src/SourceRange.cc | 18 +-- src/SourceRange.h | 18 +-- src/Token.cc | 94 +++++++++++-- src/Token.h | 13 +- src/Tokens.cc | 208 +++++----------------------- src/Tokens.h | 18 +-- src/TranslationUnit.cc | 55 +++++--- src/TranslationUnit.h | 25 ++-- tests/CodeCompleteResults_H_Test.cc | 4 +- tests/CompletionString_H_Test.cc | 5 +- tests/Cursor_H_Test.cc | 8 +- tests/Diagnostics_Test.cc | 12 +- tests/SourceLocation_H_Test.cc | 28 ++-- tests/Token_H_Test.cc | 16 +-- tests/TranslationUnit_Test.cc | 4 +- 33 files changed, 308 insertions(+), 402 deletions(-) diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index ea1b0f6..da916f8 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -3,7 +3,7 @@ #include clang::CodeCompleteResults:: -CodeCompleteResults(CXTranslationUnit &tu, +CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, int line_num, @@ -16,24 +16,24 @@ CodeCompleteResults(CXTranslationUnit &tu, file.Length = buffer.second.size(); files.push_back(file); } - results_ = clang_codeCompleteAt(tu, + cx_results = clang_codeCompleteAt(cx_tu, file_name.c_str(), line_num, column, files.data(), files.size(), clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments); - clang_sortCodeCompletionResults(results_->Results, results_->NumResults); + clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults); } clang::CodeCompleteResults::~CodeCompleteResults() { - delete[] results_->Results; - delete results_; + delete[] cx_results->Results; + delete cx_results; } int clang::CodeCompleteResults:: size() { - return results_->NumResults; + return cx_results->NumResults; } clang::CompletionString clang::CodeCompleteResults:: @@ -41,5 +41,5 @@ get(int i) { if (i >= size()) { throw std::invalid_argument("clang::CodeCompleteResults::get(int i): i>=size()"); } - return CompletionString(results_->Results[i].CompletionString); + return CompletionString(cx_results->Results[i].CompletionString); } diff --git a/src/CodeCompleteResults.h b/src/CodeCompleteResults.h index 2269f68..dda4d89 100644 --- a/src/CodeCompleteResults.h +++ b/src/CodeCompleteResults.h @@ -7,7 +7,7 @@ namespace clang { class CodeCompleteResults { public: - CodeCompleteResults(CXTranslationUnit &tu, + CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, int line_num, @@ -16,7 +16,7 @@ namespace clang { CompletionString get(int index); int size(); - CXCodeCompleteResults *results_; + CXCodeCompleteResults *cx_results; }; } // namespace clang #endif // CODECOMPLETERESULTS_H_ diff --git a/src/CompilationDatabase.cc b/src/CompilationDatabase.cc index 183570f..cdcc099 100644 --- a/src/CompilationDatabase.cc +++ b/src/CompilationDatabase.cc @@ -4,7 +4,7 @@ clang::CompilationDatabase:: CompilationDatabase(const std::string &project_path) { CXCompilationDatabase_Error error; - db_ = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error); + cx_db = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error); if(error) { //TODO: compile_commands.json is missing, create it? } @@ -12,5 +12,5 @@ CompilationDatabase(const std::string &project_path) { clang::CompilationDatabase:: ~CompilationDatabase() { - clang_CompilationDatabase_dispose(db_); + clang_CompilationDatabase_dispose(cx_db); } diff --git a/src/CompilationDatabase.h b/src/CompilationDatabase.h index 7d35e56..860f7bf 100644 --- a/src/CompilationDatabase.h +++ b/src/CompilationDatabase.h @@ -12,7 +12,7 @@ namespace clang { CompilationDatabase(); ~CompilationDatabase(); - CXCompilationDatabase db_; + CXCompilationDatabase cx_db; }; } diff --git a/src/CompileCommand.cc b/src/CompileCommand.cc index 0582e8d..4ba97b7 100644 --- a/src/CompileCommand.cc +++ b/src/CompileCommand.cc @@ -2,26 +2,26 @@ #include "CompileCommands.h" clang::CompileCommand:: -CompileCommand(int nth, clang::CompileCommands *commands) { - command_ = clang_CompileCommands_getCommand(commands->commands_, nth); +CompileCommand(int nth, clang::CompileCommands &commands) { + cx_command = clang_CompileCommands_getCommand(commands.cx_commands, nth); } std::string clang::CompileCommand:: get_command() { std::string res; - unsigned N = clang_CompileCommand_getNumArgs(command_); + unsigned N = clang_CompileCommand_getNumArgs(cx_command); for (int i = 0; i < N; i++) { - res += clang_getCString(clang_CompileCommand_getArg(command_, i)); + res += clang_getCString(clang_CompileCommand_getArg(cx_command, i)); } return res; } std::vector clang::CompileCommand:: get_command_as_args() { - unsigned N = clang_CompileCommand_getNumArgs(command_); + unsigned N = clang_CompileCommand_getNumArgs(cx_command); std::vector res(N); for (int i = 0; i < N; i++) { - res[i] = clang_getCString(clang_CompileCommand_getArg(command_, i)); + res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i)); } return res; } diff --git a/src/CompileCommand.h b/src/CompileCommand.h index cf3892e..51f64a3 100644 --- a/src/CompileCommand.h +++ b/src/CompileCommand.h @@ -6,11 +6,11 @@ namespace clang { class CompileCommand { public: - CompileCommand(int nth, CompileCommands *commands); + CompileCommand(int nth, CompileCommands &commands); std::string get_command(); std::vector get_command_as_args(); - CXCompileCommand command_; + CXCompileCommand cx_command; }; } #endif // COMPILECOMMAND_H_ diff --git a/src/CompileCommands.cc b/src/CompileCommands.cc index 42a7095..26ea944 100644 --- a/src/CompileCommands.cc +++ b/src/CompileCommands.cc @@ -1,24 +1,24 @@ #include "CompileCommands.h" clang::CompileCommands:: -CompileCommands(const std::string &filename, CompilationDatabase *db) { - commands_ = - clang_CompilationDatabase_getCompileCommands(db->db_, filename.c_str()); - if(clang_CompileCommands_getSize(commands_)==0) - commands_ = clang_CompilationDatabase_getAllCompileCommands(db->db_); +CompileCommands(const std::string &filename, CompilationDatabase &db) { + cx_commands = + clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str()); + if(clang_CompileCommands_getSize(cx_commands)==0) + cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db); } clang::CompileCommands:: ~CompileCommands() { - clang_CompileCommands_dispose(commands_); + clang_CompileCommands_dispose(cx_commands); } std::vector clang::CompileCommands:: get_commands() { - unsigned N = clang_CompileCommands_getSize(commands_); + unsigned N = clang_CompileCommands_getSize(cx_commands); std::vector res; for (unsigned i = 0; i < N; i++) { - res.emplace_back(clang::CompileCommand(i, this)); + res.emplace_back(clang::CompileCommand(i, *this)); } return res; } diff --git a/src/CompileCommands.h b/src/CompileCommands.h index 7c4cb8d..f822d18 100644 --- a/src/CompileCommands.h +++ b/src/CompileCommands.h @@ -9,11 +9,11 @@ namespace clang { class CompileCommands { public: - CompileCommands(const std::string &filename, CompilationDatabase *db); + CompileCommands(const std::string &filename, CompilationDatabase &db); std::vector get_commands(); ~CompileCommands(); - CXCompileCommands commands_; + CXCompileCommands cx_commands; }; } #endif // COMPILECOMMANDS_H_ diff --git a/src/CompletionString.cc b/src/CompletionString.cc index 68cc0f4..1c0cb88 100644 --- a/src/CompletionString.cc +++ b/src/CompletionString.cc @@ -1,23 +1,21 @@ #include "CompletionString.h" clang::CompletionString:: -CompletionString(const CXCompletionString &str) { - str_ = str; -} +CompletionString(const CXCompletionString &cx_str) : cx_str(cx_str) {} bool clang::CompletionString::available() { - return clang_getCompletionAvailability(str_) == CXAvailability_Available; + return clang_getCompletionAvailability(cx_str) == CXAvailability_Available; } int clang::CompletionString::get_num_chunks() { - return clang_getNumCompletionChunks(str_); + return clang_getNumCompletionChunks(cx_str); } std::vector clang::CompletionString::get_chunks() { std::vector res; for (size_t i = 0; i < get_num_chunks(); i++) { - auto cxstr=clang_getCompletionChunkText(str_, i); - res.emplace_back(clang_getCString(cxstr), static_cast (clang_getCompletionChunkKind(str_, i))); + auto cxstr=clang_getCompletionChunkText(cx_str, i); + res.emplace_back(clang_getCString(cxstr), static_cast (clang_getCompletionChunkKind(cx_str, i))); clang_disposeString(cxstr); } return res; @@ -25,7 +23,7 @@ std::vector clang::CompletionString::get_chunks() { std::string clang::CompletionString::get_brief_comments() { std::string brief_comments; - auto cxstr=clang_getCompletionBriefComment(str_); + auto cxstr=clang_getCompletionBriefComment(cx_str); if(cxstr.data!=NULL) { brief_comments=clang_getCString(cxstr); clang_disposeString(cxstr); diff --git a/src/CompletionString.h b/src/CompletionString.h index 23f9ea2..43d953b 100644 --- a/src/CompletionString.h +++ b/src/CompletionString.h @@ -28,13 +28,13 @@ namespace clang { class CompletionString { public: - explicit CompletionString(const CXCompletionString &str); + explicit CompletionString(const CXCompletionString &cx_str); bool available(); std::vector get_chunks(); std::string get_brief_comments(); int get_num_chunks(); - CXCompletionString str_; + CXCompletionString cx_str; }; } // namespace clang #endif // COMPLETIONSTRING_H_ diff --git a/src/Cursor.cc b/src/Cursor.cc index 7f9b3c8..0f5f56c 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -1,10 +1,24 @@ #include "Cursor.h" -const clang::CursorKind clang::Cursor::kind() { - return (CursorKind) clang_getCursorKind(this->cursor_); +const clang::CursorKind clang::Cursor::get_kind() { + return (CursorKind) clang_getCursorKind(this->cx_cursor); } clang::Cursor:: -Cursor(CXTranslationUnit &tu, clang::SourceLocation *source_location) { - cursor_ = clang_getCursor(tu, source_location->location_); +Cursor(CXTranslationUnit &cx_tu, clang::SourceLocation &source_location) { + cx_cursor = clang_getCursor(cx_tu, source_location.cx_location); } + +clang::SourceLocation clang::Cursor::get_source_location() const { + return SourceLocation(clang_getCursorLocation(cx_cursor)); +} + +clang::SourceRange clang::Cursor::get_source_range() const { + return SourceRange(clang_getCursorExtent(cx_cursor)); +} + +bool clang::Cursor::operator==(const Cursor& rhs) const { + auto lhs_rd=get_source_range().get_range_data(); + auto rhs_rd=rhs.get_source_range().get_range_data(); + return lhs_rd.path==rhs_rd.path && lhs_rd.start_offset==rhs_rd.start_offset && lhs_rd.end_offset==rhs_rd.end_offset; +} \ No newline at end of file diff --git a/src/Cursor.h b/src/Cursor.h index d7326ff..05824d9 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -2,10 +2,9 @@ #define CURSOR_H_ #include #include "SourceLocation.h" +#include "SourceRange.h" -namespace clang { - class SourceLocation; - +namespace clang { enum class CursorKind { UnexposedDecl = 1, StructDecl = 2, @@ -176,10 +175,13 @@ namespace clang { class Cursor { public: - Cursor() {} - Cursor(CXTranslationUnit &tu, SourceLocation *source_location); - const CursorKind kind(); - CXCursor cursor_; + Cursor(CXCursor cx_cursor) : cx_cursor(cx_cursor) {} + Cursor(CXTranslationUnit &cx_tu, SourceLocation &source_location); + const CursorKind get_kind(); + SourceLocation get_source_location() const; + SourceRange get_source_range() const; + bool operator==(const Cursor& rhs) const; + CXCursor cx_cursor; }; } // namespace clang #endif // CURSOR_H_ diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index c501da4..2d2fda6 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -2,19 +2,19 @@ #include "SourceLocation.h" #include "Tokens.h" -clang::Diagnostic::Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic) { - severity=clang_getDiagnosticSeverity(clang_diagnostic); +clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) { + severity=clang_getDiagnosticSeverity(cx_diagnostic); severity_spelling=get_severity_spelling(severity); - spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic)); - clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic)); + spelling=clang_getCString(clang_getDiagnosticSpelling(cx_diagnostic)); + clang::SourceLocation location(clang_getDiagnosticLocation(cx_diagnostic)); - clang::SourceRange range(&location, &location); - clang::Tokens tokens(tu, &range); + clang::SourceRange range(location, location); + clang::Tokens tokens(cx_tu, range); if(tokens.size()==1) { auto& token=tokens[0]; clang::SourceRange range=token.get_source_range(); - auto end_location=clang::SourceLocation(&range, false); - this->range=SourceRange::get_range_data(location, end_location); + auto locations=range.get_source_locations(); + this->range=SourceRange::get_range_data(location, locations.second); } } diff --git a/src/Diagnostic.h b/src/Diagnostic.h index 773b986..a01e776 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -8,7 +8,7 @@ namespace clang { class Diagnostic { public: - Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic); + Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic); static const std::string get_severity_spelling(unsigned severity); diff --git a/src/Index.cc b/src/Index.cc index 89c1313..8208c7d 100644 --- a/src/Index.cc +++ b/src/Index.cc @@ -2,6 +2,6 @@ clang::Index:: Index(int excludeDeclarationsFromPCH, int displayDiagnostics) { - index_ = clang_createIndex(excludeDeclarationsFromPCH, + cx_index = clang_createIndex(excludeDeclarationsFromPCH, displayDiagnostics); } diff --git a/src/Index.h b/src/Index.h index 6e71e7b..5ac32d0 100644 --- a/src/Index.h +++ b/src/Index.h @@ -6,7 +6,7 @@ namespace clang { class Index { public: Index(int excludeDeclarationsFromPCH, int displayDiagnostics); - CXIndex index_; + CXIndex cx_index; }; } // namespace clang #endif // INDEX_H_ diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 13506ae..976eb01 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -10,28 +10,10 @@ SourceLocation(CXTranslationUnit &tu, int line_offset) { CXFile file = clang_getFile(tu, filename.c_str()); - location_ = clang_getLocation(tu, + cx_location = clang_getLocation(tu, file, line_number, line_offset); - } - -clang::SourceLocation:: -SourceLocation(Cursor *cursor) { - location_ = clang_getCursorLocation(cursor->cursor_); -} - -clang::SourceLocation:: -SourceLocation(clang::SourceRange *range, bool start) { - location_ = start ? clang_getRangeStart(range->range_) : - clang_getRangeEnd(range->range_); -} - -clang::SourceLocation:: -SourceLocation(CXTranslationUnit &tu, - Token *token) { - location_ = clang_getTokenLocation(tu, - token->token_); } clang::SourceLocation:: @@ -40,7 +22,7 @@ SourceLocation(CXTranslationUnit &tu, int offset) { CXFile file = clang_getFile(tu, filepath.c_str()); - location_ = clang_getLocationForOffset(tu, + cx_location = clang_getLocationForOffset(tu, file, offset); } @@ -51,7 +33,7 @@ get_location_info(std::string* path, unsigned *column, unsigned *offset) { CXFile file; - clang_getExpansionLocation(location_, &file, line, column, offset); + clang_getExpansionLocation(cx_location, &file, line, column, offset); if (path != NULL && file!=NULL) { path->operator=(((clang_getCString((clang_getFileName(file)))))); } diff --git a/src/SourceLocation.h b/src/SourceLocation.h index 81394ec..ad25c24 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -1,41 +1,29 @@ #ifndef SOURCELOCATION_H_ #define SOURCELOCATION_H_ #include -#include "Token.h" -#include "Cursor.h" #include namespace clang { - class Token; - class SourceRange; - class Cursor; class SourceLocation { public: - SourceLocation(CXTranslationUnit &tu, + SourceLocation(CXSourceLocation cx_location) : cx_location(cx_location) {} + + SourceLocation(CXTranslationUnit &cx_tu, const std::string &filename, int line_number, int column); - SourceLocation(CXTranslationUnit &tu, - Token *token); - - SourceLocation(SourceRange *range, bool start); - SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); - SourceLocation(CXSourceLocation location) {location_=location;} - - explicit SourceLocation(Cursor *cursor); - - void get_location_info(std::string* path, + void get_location_info(std::string *path, unsigned *line, unsigned *column, unsigned *offset); - CXSourceLocation location_; + CXSourceLocation cx_location; }; } // namespace clang diff --git a/src/SourceRange.cc b/src/SourceRange.cc index 8de63a6..ae3bd24 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -1,17 +1,12 @@ #include "SourceRange.h" clang::SourceRange:: -SourceRange(clang::Token *token) { - range_ = clang_getTokenExtent(token->tu, token->token_); +SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { + cx_range = clang_getRange(start.cx_location, end.cx_location); } -clang::SourceRange:: -SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) { - range_ = clang_getRange(start->location_, end->location_); -} - -clang::SourceRange::SourceRange(Cursor *cursor) { - range_ = clang_getCursorExtent(cursor->cursor_); +std::pair clang::SourceRange::get_source_locations() { + return std::pair(clang_getRangeStart(cx_range), clang_getRangeEnd(cx_range)); } clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start, clang::SourceLocation &end) { @@ -27,7 +22,6 @@ clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start } clang::RangeData clang::SourceRange::get_range_data() { - clang::SourceLocation start(this, true); - clang::SourceLocation end(this, false); - return get_range_data(start, end); + auto locations=get_source_locations(); + return get_range_data(locations.first, locations.second); } \ No newline at end of file diff --git a/src/SourceRange.h b/src/SourceRange.h index be74888..6ca89a3 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -1,16 +1,10 @@ #ifndef SOURCERANGE_H_ #define SOURCERANGE_H_ #include -#include "Token.h" -#include "Cursor.h" #include "SourceLocation.h" #include -namespace clang { - class Token; - class SourceLocation; - class Cursor; - +namespace clang { class RangeData { public: std::string path; @@ -19,14 +13,12 @@ namespace clang { class SourceRange { public: - SourceRange() {} - SourceRange(Token *token); - SourceRange(SourceLocation *start, - SourceLocation *end); - explicit SourceRange(Cursor *cursor); + SourceRange(CXSourceRange cx_range) : cx_range(cx_range) {} + SourceRange(SourceLocation &start, SourceLocation &end); + std::pair get_source_locations(); static RangeData get_range_data(SourceLocation &start, SourceLocation &end); RangeData get_range_data(); - CXSourceRange range_; + CXSourceRange cx_range; }; } // namespace clang #endif // SOURCERANGE_H_ diff --git a/src/Token.cc b/src/Token.cc index 6ac0945..57bb38e 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -4,27 +4,103 @@ // Token // // // // // // -// clang::Token instansiates an token -clang::Token::Token(CXTranslationUnit &tu, const CXToken &token) : - tu(tu), token_(token) { -} - // returns gets an source location for this token objekt // based on the translationunit given clang::SourceLocation clang::Token::get_source_location() { - return SourceLocation(tu, this); + return SourceLocation(clang_getTokenLocation(cx_tu, cx_token)); } // returns a sourcerange that covers this token clang::SourceRange clang::Token::get_source_range() { - return SourceRange(this); + return SourceRange(clang_getTokenExtent(cx_tu, cx_token)); } // returns a string description of this tokens kind std::string clang::Token::get_token_spelling() { - CXString s = clang_getTokenSpelling(tu, token_); + CXString s = clang_getTokenSpelling(cx_tu, cx_token); return std::string(clang_getCString(s)); } const clang::TokenKind clang::Token::kind() { - return (TokenKind) clang_getTokenKind(token_); + return (TokenKind) clang_getTokenKind(cx_token); +} + +bool clang::Token::has_type() { + return !clang_Cursor_isNull(clang_getCursorReferenced(cx_cursor)); +} + +std::string clang::Token::get_type() { + std::string spelling; + auto referenced=clang_getCursorReferenced(cx_cursor); + if(!clang_Cursor_isNull(referenced)) { + auto type=clang_getCursorType(referenced); + auto cxstr=clang_getTypeSpelling(type); + spelling=clang_getCString(cxstr); + clang_disposeString(cxstr); + std::string auto_end=""; + //TODO fix const auto + if((spelling.size()>=4 && spelling.substr(0, 4)=="auto")) { + auto_end=spelling.substr(4); + auto type=clang_getCanonicalType(clang_getCursorType(cx_cursor)); + auto cxstr=clang_getTypeSpelling(type); + spelling=clang_getCString(cxstr); + clang_disposeString(cxstr); + if(spelling.find(" ")==std::string::npos) + spelling+=auto_end; + } + } + return spelling; +} + +std::string clang::Token::get_brief_comments() { + std::string comment_string; + auto referenced=clang_getCursorReferenced(cx_cursor); + auto comment=clang_Cursor_getParsedComment(referenced); + if(clang_Comment_getKind(comment)==CXComment_FullComment) { + size_t para_c=0; + for(unsigned c=0;c=2) + break; + for(unsigned c=0;c0) + comment_string.pop_back(); + if(clang_InlineCommandComment_getNumArgs(grandchild_comment)==0) + comment_string+=clang_getCString(cxstr); + clang_disposeString(cxstr); + for(unsigned arg_c=0;arg_c0) + comment_string+=" "; + comment_string+=clang_getCString(cxstr); + clang_disposeString(cxstr); + } + } + } + } + } + /*cout << " " << clang_Comment_getKind(child_comment) << ", children: " << clang_Comment_getNumChildren(child_comment) << endl; + auto cxstr=clang_FullComment_getAsHTML(child_comment); + cout << " " << clang_getCString(cxstr) << endl; + clang_disposeString(cxstr);*/ + } + while(comment_string.size()>0 && (comment_string.back()=='\n' || comment_string.back()==' ')) + comment_string.pop_back(); + } + + return comment_string; } diff --git a/src/Token.h b/src/Token.h index 627b6ef..cf7be82 100644 --- a/src/Token.h +++ b/src/Token.h @@ -3,6 +3,7 @@ #include #include "SourceLocation.h" #include "SourceRange.h" +#include "Cursor.h" #include namespace clang { @@ -19,14 +20,18 @@ namespace clang { class Token { public: - explicit Token(CXTranslationUnit &tu, const CXToken &token); + explicit Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor) {}; const TokenKind kind(); std::string get_token_spelling(); SourceLocation get_source_location(); SourceRange get_source_range(); - std::string type; - const CXToken& token_; - CXTranslationUnit &tu; + clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} + bool has_type(); + std::string get_type(); + std::string get_brief_comments(); + CXTranslationUnit &cx_tu; + CXToken& cx_token; + CXCursor& cx_cursor; }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 9b2b1e8..b1c5893 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -2,98 +2,34 @@ #include using namespace std; -clang::Tokens::Tokens(CXTranslationUnit &tu, clang::SourceRange *range): tu(tu) { - clang_tokenize(tu, - range->range_, - &tokens_, - &num_tokens_); - for (int i = 0; i < num_tokens_; i++) { - emplace_back(tu, tokens_[i]); +clang::Tokens::Tokens(CXTranslationUnit &cx_tu, SourceRange &range): cx_tu(cx_tu) { + + clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); + cx_cursors.clear(); + cx_cursors.reserve(num_tokens); + clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); + for (int i = 0; i < num_tokens; i++) { + emplace_back(cx_tu, cx_tokens[i], cx_cursors[i]); } } clang::Tokens::~Tokens() { - clang_disposeTokens(tu, tokens_, size()); + clang_disposeTokens(cx_tu, cx_tokens, size()); } -void clang::Tokens::update_types() { - clang_cursors.clear(); - clang_cursors.reserve(size()); - clang_annotateTokens(tu, tokens_, size(), clang_cursors.data()); - - for(size_t c=0;c=4 && spelling.substr(0, 4)=="auto")) { - auto_end=spelling.substr(4); - auto type=clang_getCanonicalType(clang_getCursorType(clang_cursors[c])); - auto cxstr=clang_getTypeSpelling(type); - spelling=clang_getCString(cxstr); - clang_disposeString(cxstr); - if(spelling.find(" ")==std::string::npos) - spelling+=auto_end; - } - - (*this)[c].type=spelling; - //std::cout << clang_getCString(clang_getTypeSpelling(type)) << ": " << type.kind << endl; - ////auto cursor=clang_getTypeDeclaration(type); - ////tks[c].type=clang_getCString(clang_getCursorSpelling(cursor)); - ////auto type=clang_getCursorType(referenced); - - } - //Testing: - /*if(tks[c].get_token_spelling(tu)=="text_view") { - cout << tks[c].get_token_spelling(tu) << endl; - auto kind=clang_getCursorKind(cursors[c].cursor_); - cout << " " << kind << endl; - cout << " Decl: " << clang_isDeclaration(kind) << endl; - cout << " Attr: " << clang_isAttribute(kind) << endl; - cout << " Ref: " << clang_isReference(kind) << endl; - cout << " Expr: " << clang_isExpression(kind) << endl; - auto referenced=clang_getCursorReferenced(cursors[c].cursor_); - if(!clang_Cursor_isNull(referenced)) { - cout << " " << clang_getCursorKind(referenced) << endl; - - clang::Cursor referenced_cursor; - referenced_cursor.cursor_=referenced; - auto range=clang::SourceRange(&referenced_cursor); - - auto location=clang::SourceLocation(&range, true); - std::string path; - unsigned line, column, offset; - location.get_location_info(&path, &line, &column, &offset); - cout << " start: " << path << ", " << line << ", " << column << endl; - - location=clang::SourceLocation(&range, false); - location.get_location_info(&path, &line, &column, &offset); - cout << " start: " << path << ", " << line << ", " << column << endl; - - auto type=clang_getCursorType(referenced); - cout << " " << clang_getCString(clang_getTypeSpelling(type)) << endl; - } - }*/ - } -} +/*CXCursor clang::Tokens::find_referenced() { + cursors.clear(); + cursors.reserve(size()); + clang_annotateTokens(tu, tokens.data(), size(), cursors.data()); -CXCursor clang::Tokens::find_referenced() { - clang_cursors.clear(); - clang_cursors.reserve(size()); - clang_annotateTokens(tu, tokens_, size(), clang_cursors.data()); - - auto kind=clang_getCursorKind(clang_cursors[0]); + auto kind=clang_getCursorKind(cursors[0]); cout << " " << kind << endl; cout << " Decl: " << clang_isDeclaration(kind) << endl; cout << " Attr: " << clang_isAttribute(kind) << endl; cout << " Ref: " << clang_isReference(kind) << endl; cout << " Expr: " << clang_isExpression(kind) << endl; - auto referenced=clang_getCursorReferenced(clang_cursors[0]); + auto referenced=clang_getCursorReferenced(cursors[0]); kind=clang_getCursorKind(referenced); cout << " " << kind << endl; @@ -109,59 +45,31 @@ CXCursor clang::Tokens::find_referenced() { cout << " " << clang_getCString(clang_getCursorSpelling(referenced)) << endl; return referenced; -} - -bool clang::Tokens::equalCursors(CXCursor a, CXCursor b) { - clang::Cursor cursor_a; - cursor_a.cursor_=a; - auto range=clang::SourceRange(&cursor_a); - auto location=clang::SourceLocation(&range, true); - std::string path_a; - unsigned offset_start_a, offset_end_a; - location.get_location_info(&path_a, NULL, NULL, &offset_start_a); - location=clang::SourceLocation(&range, false); - location.get_location_info(NULL, NULL, NULL, &offset_end_a); - - clang::Cursor cursor_b; - cursor_b.cursor_=b; - range=clang::SourceRange(&cursor_b); - location=clang::SourceLocation(&range, true); - std::string path_b; - unsigned offset_start_b, offset_end_b; - location.get_location_info(&path_b, NULL, NULL, &offset_start_b); - location=clang::SourceLocation(&range, false); - location.get_location_info(NULL, NULL, NULL, &offset_end_b); - - return path_a==path_b && offset_start_a==offset_start_b && offset_end_a==offset_end_b; -} +}*/ -void clang::Tokens::rename(CXCursor& referenced, std::unordered_map > &ranges) { +void clang::Tokens::rename(CXCursor &referenced) { for(size_t c=0;c=2) - break; - for(unsigned c=0;c0) - comment_string.pop_back(); - if(clang_InlineCommandComment_getNumArgs(grandchild_comment)==0) - comment_string+=clang_getCString(cxstr); - clang_disposeString(cxstr); - for(unsigned arg_c=0;arg_c0) - comment_string+=" "; - comment_string+=clang_getCString(cxstr); - clang_disposeString(cxstr); - } - } - } - } - } - /*cout << " " << clang_Comment_getKind(child_comment) << ", children: " << clang_Comment_getNumChildren(child_comment) << endl; - auto cxstr=clang_FullComment_getAsHTML(child_comment); - cout << " " << clang_getCString(cxstr) << endl; - clang_disposeString(cxstr);*/ - } - while(comment_string.size()>0 && (comment_string.back()=='\n' || comment_string.back()==' ')) - comment_string.pop_back(); - } - - return comment_string; -} \ No newline at end of file diff --git a/src/Tokens.h b/src/Tokens.h index bc0b392..ca60a35 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -9,19 +9,15 @@ namespace clang { class Tokens : public std::vector { public: - Tokens(CXTranslationUnit &tu, SourceRange *range); + Tokens(CXTranslationUnit &cx_tu, SourceRange &range); ~Tokens(); - void update_types(); - std::string get_brief_comments(size_t cursor_id); - CXCursor find_referenced(); - bool equalCursors(CXCursor a, CXCursor b); - void rename(CXCursor &referenced, std::unordered_map > &ranges); - + void rename(CXCursor &referenced); + //std::unordered_map > private: - CXToken *tokens_; - unsigned num_tokens_; - std::vector clang_cursors; - CXTranslationUnit& tu; + CXToken *cx_tokens; + unsigned num_tokens; + std::vector cx_cursors; + CXTranslationUnit& cx_tu; }; } // namespace clang #endif // TOKENS_H_ diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 497c62d..0fd841d 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -6,17 +6,11 @@ clang::TranslationUnit:: ~TranslationUnit() { - clang_disposeTranslationUnit(tu_); -} - -clang::TranslationUnit& clang::TranslationUnit:: -operator=(const clang::TranslationUnit &tu) { - tu_ = tu.tu_; - return *this; + clang_disposeTranslationUnit(cx_tu); } clang::TranslationUnit:: -TranslationUnit(Index *index, +TranslationUnit(Index &index, const std::string &filepath, const std::vector &command_line_args) { std::map buffers; @@ -28,7 +22,7 @@ TranslationUnit(Index *index, } clang::TranslationUnit:: -TranslationUnit(Index *index, +TranslationUnit(Index &index, const std::string &filepath) { std::vector command_line_args; std::map buffers; @@ -40,7 +34,7 @@ TranslationUnit(Index *index, } clang::TranslationUnit:: -TranslationUnit(clang::Index *index, +TranslationUnit(clang::Index &index, const std::string &filepath, const std::vector &command_line_args, const std::map &buffers, @@ -48,7 +42,7 @@ TranslationUnit(clang::Index *index, parse(index, filepath, command_line_args, buffers, flags); } -void clang::TranslationUnit::parse(Index *index, +void clang::TranslationUnit::parse(Index &index, const std::string &filepath, const std::vector &command_line_args, const std::map &buffers, @@ -65,8 +59,8 @@ void clang::TranslationUnit::parse(Index *index, for(auto &a: command_line_args) { args.push_back(a.c_str()); } - tu_ = - clang_parseTranslationUnit(index->index_, + cx_tu = + clang_parseTranslationUnit(index.cx_index, filepath.c_str(), args.data(), args.size(), @@ -76,8 +70,7 @@ void clang::TranslationUnit::parse(Index *index, } int clang::TranslationUnit:: -ReparseTranslationUnit(const std::string &file_path, - const std::map &buffers, +ReparseTranslationUnit(const std::map &buffers, unsigned flags) { std::vector files; for (auto &buffer : buffers) { @@ -87,7 +80,7 @@ ReparseTranslationUnit(const std::string &file_path, file.Length = buffer.second.size(); files.push_back(file); } - return clang_reparseTranslationUnit(tu_, + return clang_reparseTranslationUnit(cx_tu, files.size(), files.data(), flags); @@ -97,11 +90,31 @@ unsigned clang::TranslationUnit::DefaultFlags() { return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; } -void clang::TranslationUnit::update_diagnostics() { - diagnostics.clear(); - for(unsigned c=0;c &buffers, int line_number, int column) { + auto cxstr=clang_getTranslationUnitSpelling(cx_tu); + std::string path=clang_getCString(cxstr); + clang_disposeString(cxstr); + + clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column); + return results; +} + +std::vector clang::TranslationUnit::get_diagnostics() { + std::vector diagnostics; + for(unsigned c=0;c clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) { + auto cxstr=clang_getTranslationUnitSpelling(cx_tu); + std::string path=clang_getCString(cxstr); + clang_disposeString(cxstr); + clang::SourceLocation start_location(cx_tu, path, start_offset); + clang::SourceLocation end_location(cx_tu, path, end_offset); + clang::SourceRange range(start_location, end_location); + return std::unique_ptr(new Tokens(cx_tu, range)); } diff --git a/src/TranslationUnit.h b/src/TranslationUnit.h index c8eace5..9429031 100644 --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -7,37 +7,38 @@ #include #include "Index.h" #include "Diagnostic.h" +#include "Tokens.h" +#include "CodeCompleteResults.h" namespace clang { class TranslationUnit { public: - TranslationUnit(Index *index, + TranslationUnit(Index &index, const std::string &filepath, const std::vector &command_line_args); - TranslationUnit(Index *index, + TranslationUnit(Index &index, const std::string &filepath, const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - TranslationUnit(Index *index, + TranslationUnit(Index &index, const std::string &filepath); ~TranslationUnit(); - TranslationUnit& operator=(const TranslationUnit &tu); - int ReparseTranslationUnit(const std::string &file_path, - const std::map - &buffers, + int ReparseTranslationUnit(const std::map &buffers, unsigned flags=DefaultFlags()); static unsigned DefaultFlags(); - void update_diagnostics(); - std::vector diagnostics; - - void parse(Index *index, + void parse(Index &index, const std::string &filepath, const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - CXTranslationUnit tu_; + + clang::CodeCompleteResults get_code_completions(const std::map &buffers, int line_number, int column); + std::vector get_diagnostics(); + std::unique_ptr get_tokens(unsigned start_offset, unsigned end_offset); + + CXTranslationUnit cx_tu; }; } // namespace clang #endif // TRANSLATIONUNIT_H_ diff --git a/tests/CodeCompleteResults_H_Test.cc b/tests/CodeCompleteResults_H_Test.cc index a5b4ad1..d2e880f 100644 --- a/tests/CodeCompleteResults_H_Test.cc +++ b/tests/CodeCompleteResults_H_Test.cc @@ -9,7 +9,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) { std::string path("./case/main.cpp"); clang::Index index(0, 0); - clang::TranslationUnit tu(&index, path); + clang::TranslationUnit tu(index, path); // ReparseTranslationUnit takes a map with filepath as key // and buffer as value @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) { // ] - clang::CodeCompleteResults results(tu.tu_, path, buffers, 4, 5); + auto results=tu.get_code_completions(buffers, 4, 5); bool substr_found=false; for(int c=0;c args; - clang::TranslationUnit tu(&index, path, args, map_buffers); + clang::TranslationUnit tu(index, path, args, map_buffers); - tu.update_diagnostics(); - BOOST_CHECK(tu.diagnostics.size()==1); - BOOST_CHECK(tu.diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'"); - BOOST_CHECK(tu.diagnostics[0].range.path=="./case/main_error.cpp"); - BOOST_CHECK(tu.diagnostics[0].severity==3); + auto diagnostics=tu.get_diagnostics(); + BOOST_CHECK(diagnostics.size()==1); + BOOST_CHECK(diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'"); + BOOST_CHECK(diagnostics[0].range.path=="./case/main_error.cpp"); + BOOST_CHECK(diagnostics[0].severity==3); } diff --git a/tests/SourceLocation_H_Test.cc b/tests/SourceLocation_H_Test.cc index 56e2ac3..3b11c52 100644 --- a/tests/SourceLocation_H_Test.cc +++ b/tests/SourceLocation_H_Test.cc @@ -7,30 +7,26 @@ BOOST_AUTO_TEST_CASE(source_location) { clang::Index index(0, 0); - clang::TranslationUnit tu(&index, path); - clang::SourceLocation start(tu.tu_, path, 0); - clang::SourceLocation end(tu.tu_, path, 7, 1); - clang::SourceRange range(&start, &end); - clang::Tokens tokens(tu.tu_, &range); + clang::TranslationUnit tu(index, path); + auto tokens=tu.get_tokens(0, 113); - clang::SourceRange token_range = tokens[28].get_source_range(); + clang::SourceRange token_range = (*tokens)[28].get_source_range(); unsigned token_start_line, token_start_column, token_start_offset, token_end_line, token_end_column, token_end_offset; std::string token_start_path, token_end_path; - clang::SourceLocation token_start(&token_range, true); - clang::SourceLocation token_end(&token_range, false); + auto locations=token_range.get_source_locations(); - token_start.get_location_info(&token_start_path, - &token_start_line, - &token_start_column, - &token_start_offset); + locations.first.get_location_info(&token_start_path, + &token_start_line, + &token_start_column, + &token_start_offset); - token_end.get_location_info(&token_end_path, - &token_end_line, - &token_end_column, - &token_end_offset); + locations.second.get_location_info(&token_end_path, + &token_end_line, + &token_end_column, + &token_end_offset); BOOST_CHECK(token_start_path == path); BOOST_CHECK(token_start_line == 6); diff --git a/tests/Token_H_Test.cc b/tests/Token_H_Test.cc index 2e64e94..38198c2 100644 --- a/tests/Token_H_Test.cc +++ b/tests/Token_H_Test.cc @@ -7,17 +7,13 @@ BOOST_AUTO_TEST_CASE(token) { clang::Index index(0, 0); - clang::TranslationUnit tu(&index, path); - clang::SourceLocation start(tu.tu_, path, 0); - clang::SourceLocation end(tu.tu_, path, 7, 1); + clang::TranslationUnit tu(index, path); + + auto tokens=tu.get_tokens(0, 113); - clang::SourceRange range(&start, &end); + BOOST_CHECK(tokens->size() == 32); + BOOST_CHECK((*tokens)[1].kind() == clang::TokenKind::Token_Identifier); - clang::Tokens tokens(tu.tu_, &range); - - BOOST_CHECK(tokens.size() == 32); - BOOST_CHECK(tokens[1].kind() == clang::TokenKind::Token_Identifier); - - std::string str = tokens[28].get_token_spelling(); + std::string str = (*tokens)[28].get_token_spelling(); BOOST_CHECK(str == "return"); } diff --git a/tests/TranslationUnit_Test.cc b/tests/TranslationUnit_Test.cc index 408ab9b..acd9fbd 100644 --- a/tests/TranslationUnit_Test.cc +++ b/tests/TranslationUnit_Test.cc @@ -7,7 +7,7 @@ BOOST_AUTO_TEST_CASE(translation_unit) { std::string path("./case/main.cpp"); clang::Index index(0, 0); - clang::TranslationUnit tu(&index, path); + clang::TranslationUnit tu(index, path); // ReparseTranslationUnit takes a map with filepath as key // and buffer as value @@ -20,5 +20,5 @@ BOOST_AUTO_TEST_CASE(translation_unit) { file.append("}"); buffers[path] = file; - BOOST_CHECK(tu.ReparseTranslationUnit(path, buffers) == 0); + BOOST_CHECK(tu.ReparseTranslationUnit(buffers) == 0); } From f81a2e7744c711b6a12905768afdd281a186f651 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 14:34:13 +0200 Subject: [PATCH 04/21] 2 more forward declarations gone. --- src/Token.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Token.h b/src/Token.h index cf7be82..c77d2f4 100644 --- a/src/Token.h +++ b/src/Token.h @@ -7,9 +7,6 @@ #include namespace clang { - class SourceLocation; - class SourceRange; - enum TokenKind { Token_Punctuation, Token_Keyword, From 9714ebfc93dc46ab489ea55d65eed82f9b0228aa Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 14:47:28 +0200 Subject: [PATCH 05/21] Last forward declaration gone. --- src/CompilationDatabase.h | 1 - src/CompileCommand.cc | 5 ----- src/CompileCommand.h | 4 ++-- src/CompileCommands.cc | 2 +- src/Cursor.h | 2 +- src/SourceLocation.h | 2 +- src/SourceRange.h | 2 +- 7 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/CompilationDatabase.h b/src/CompilationDatabase.h index 860f7bf..0527fdf 100644 --- a/src/CompilationDatabase.h +++ b/src/CompilationDatabase.h @@ -5,7 +5,6 @@ #include namespace clang { - class CompileCommands; class CompilationDatabase { public: explicit CompilationDatabase(const std::string &project_path); diff --git a/src/CompileCommand.cc b/src/CompileCommand.cc index 4ba97b7..3d98579 100644 --- a/src/CompileCommand.cc +++ b/src/CompileCommand.cc @@ -1,11 +1,6 @@ #include "CompileCommand.h" #include "CompileCommands.h" -clang::CompileCommand:: -CompileCommand(int nth, clang::CompileCommands &commands) { - cx_command = clang_CompileCommands_getCommand(commands.cx_commands, nth); -} - std::string clang::CompileCommand:: get_command() { std::string res; diff --git a/src/CompileCommand.h b/src/CompileCommand.h index 51f64a3..ba87d7d 100644 --- a/src/CompileCommand.h +++ b/src/CompileCommand.h @@ -1,12 +1,12 @@ #ifndef COMPILECOMMAND_H_ #define COMPILECOMMAND_H_ -#include "CompilationDatabase.h" +#include #include namespace clang { class CompileCommand { public: - CompileCommand(int nth, CompileCommands &commands); + CompileCommand(const CXCompileCommand& cx_command) : cx_command(cx_command) {}; std::string get_command(); std::vector get_command_as_args(); diff --git a/src/CompileCommands.cc b/src/CompileCommands.cc index 26ea944..2b5494b 100644 --- a/src/CompileCommands.cc +++ b/src/CompileCommands.cc @@ -18,7 +18,7 @@ get_commands() { unsigned N = clang_CompileCommands_getSize(cx_commands); std::vector res; for (unsigned i = 0; i < N; i++) { - res.emplace_back(clang::CompileCommand(i, *this)); + res.emplace_back(clang_CompileCommands_getCommand(cx_commands, i)); } return res; } diff --git a/src/Cursor.h b/src/Cursor.h index 05824d9..4b5fc10 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -175,7 +175,7 @@ namespace clang { class Cursor { public: - Cursor(CXCursor cx_cursor) : cx_cursor(cx_cursor) {} + Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {} Cursor(CXTranslationUnit &cx_tu, SourceLocation &source_location); const CursorKind get_kind(); SourceLocation get_source_location() const; diff --git a/src/SourceLocation.h b/src/SourceLocation.h index ad25c24..0837dc1 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -7,7 +7,7 @@ namespace clang { class SourceLocation { public: - SourceLocation(CXSourceLocation cx_location) : cx_location(cx_location) {} + SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {} SourceLocation(CXTranslationUnit &cx_tu, const std::string &filename, diff --git a/src/SourceRange.h b/src/SourceRange.h index 6ca89a3..7a85504 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -13,7 +13,7 @@ namespace clang { class SourceRange { public: - SourceRange(CXSourceRange cx_range) : cx_range(cx_range) {} + SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {} SourceRange(SourceLocation &start, SourceLocation &end); std::pair get_source_locations(); static RangeData get_range_data(SourceLocation &start, SourceLocation &end); From 1276a723f298ee30a72c7bb3a33ba615fb0daa3f Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 14:58:43 +0200 Subject: [PATCH 06/21] Now storing the token ranges for optimisation. --- src/Diagnostic.cc | 3 +-- src/Token.h | 7 +++++-- tests/SourceLocation_H_Test.cc | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 2d2fda6..bc9bc8b 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -12,8 +12,7 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos clang::Tokens tokens(cx_tu, range); if(tokens.size()==1) { auto& token=tokens[0]; - clang::SourceRange range=token.get_source_range(); - auto locations=range.get_source_locations(); + auto locations=token.source_range.get_source_locations(); this->range=SourceRange::get_range_data(location, locations.second); } } diff --git a/src/Token.h b/src/Token.h index c77d2f4..d88817f 100644 --- a/src/Token.h +++ b/src/Token.h @@ -17,11 +17,11 @@ namespace clang { class Token { public: - explicit Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor) {}; + Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): + cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), source_range(get_source_range()) {}; const TokenKind kind(); std::string get_token_spelling(); SourceLocation get_source_location(); - SourceRange get_source_range(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); @@ -29,6 +29,9 @@ namespace clang { CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; + SourceRange source_range; + private: + SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/tests/SourceLocation_H_Test.cc b/tests/SourceLocation_H_Test.cc index 3b11c52..ab6d1c0 100644 --- a/tests/SourceLocation_H_Test.cc +++ b/tests/SourceLocation_H_Test.cc @@ -10,7 +10,7 @@ BOOST_AUTO_TEST_CASE(source_location) { clang::TranslationUnit tu(index, path); auto tokens=tu.get_tokens(0, 113); - clang::SourceRange token_range = (*tokens)[28].get_source_range(); + clang::SourceRange token_range = (*tokens)[28].source_range; unsigned token_start_line, token_start_column, token_start_offset, token_end_line, token_end_column, token_end_offset; From 2b01d6bda3c65d7de07d3914701288ba5d383883 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 15:07:32 +0200 Subject: [PATCH 07/21] Added missing header. --- src/CompileCommand.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CompileCommand.h b/src/CompileCommand.h index ba87d7d..582d2b4 100644 --- a/src/CompileCommand.h +++ b/src/CompileCommand.h @@ -2,6 +2,7 @@ #define COMPILECOMMAND_H_ #include #include +#include namespace clang { class CompileCommand { From 352c105a818fe83fe58dbac2406bae37662444af Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 15:30:52 +0200 Subject: [PATCH 08/21] fixed has_type(). --- src/Token.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Token.cc b/src/Token.cc index 57bb38e..8cd376e 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -25,7 +25,14 @@ const clang::TokenKind clang::Token::kind() { } bool clang::Token::has_type() { - return !clang_Cursor_isNull(clang_getCursorReferenced(cx_cursor)); + auto referenced=clang_getCursorReferenced(cx_cursor); + if(clang_Cursor_isNull(referenced)) + return false; + auto type=clang_getCursorType(referenced); + auto cxstr=clang_getTypeSpelling(type); + std::string spelling=clang_getCString(cxstr); + clang_disposeString(cxstr); + return spelling!=""; } std::string clang::Token::get_type() { From d041071ebafcfbc1baafef0c09f24cf882ea0550 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 19:06:02 +0200 Subject: [PATCH 09/21] Can now find similar tokens for refactoring purposes. Also some more cleanup. --- src/Cursor.cc | 15 ++++--- src/Cursor.h | 1 - src/Diagnostic.cc | 4 +- src/Token.cc | 1 + src/Tokens.cc | 93 ++++++------------------------------------ src/Tokens.h | 3 +- src/TranslationUnit.cc | 5 +++ src/TranslationUnit.h | 2 + tests/Cursor_H_Test.cc | 3 +- 9 files changed, 32 insertions(+), 95 deletions(-) diff --git a/src/Cursor.cc b/src/Cursor.cc index 0f5f56c..cb9f455 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -4,11 +4,6 @@ const clang::CursorKind clang::Cursor::get_kind() { return (CursorKind) clang_getCursorKind(this->cx_cursor); } -clang::Cursor:: -Cursor(CXTranslationUnit &cx_tu, clang::SourceLocation &source_location) { - cx_cursor = clang_getCursor(cx_tu, source_location.cx_location); -} - clang::SourceLocation clang::Cursor::get_source_location() const { return SourceLocation(clang_getCursorLocation(cx_cursor)); } @@ -18,7 +13,11 @@ clang::SourceRange clang::Cursor::get_source_range() const { } bool clang::Cursor::operator==(const Cursor& rhs) const { - auto lhs_rd=get_source_range().get_range_data(); - auto rhs_rd=rhs.get_source_range().get_range_data(); - return lhs_rd.path==rhs_rd.path && lhs_rd.start_offset==rhs_rd.start_offset && lhs_rd.end_offset==rhs_rd.end_offset; + auto cxstr=clang_getCursorUSR(cx_cursor); + std::string lhs_str=clang_getCString(cxstr); + clang_disposeString(cxstr); + cxstr=clang_getCursorUSR(rhs.cx_cursor); + std::string rhs_str=clang_getCString(cxstr); + clang_disposeString(cxstr); + return lhs_str==rhs_str; } \ No newline at end of file diff --git a/src/Cursor.h b/src/Cursor.h index 4b5fc10..4f34455 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -176,7 +176,6 @@ namespace clang { class Cursor { public: Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {} - Cursor(CXTranslationUnit &cx_tu, SourceLocation &source_location); const CursorKind get_kind(); SourceLocation get_source_location() const; SourceRange get_source_range() const; diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index bc9bc8b..f2f4f52 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -5,7 +5,9 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) { severity=clang_getDiagnosticSeverity(cx_diagnostic); severity_spelling=get_severity_spelling(severity); - spelling=clang_getCString(clang_getDiagnosticSpelling(cx_diagnostic)); + auto cxstr=clang_getDiagnosticSpelling(cx_diagnostic); + spelling=clang_getCString(cxstr); + clang_disposeString(cxstr); clang::SourceLocation location(clang_getDiagnosticLocation(cx_diagnostic)); clang::SourceRange range(location, location); diff --git a/src/Token.cc b/src/Token.cc index 8cd376e..30f2c54 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -24,6 +24,7 @@ const clang::TokenKind clang::Token::kind() { return (TokenKind) clang_getTokenKind(cx_token); } +//TODO: Is there a way to optimise this? bool clang::Token::has_type() { auto referenced=clang_getCursorReferenced(cx_cursor); if(clang_Cursor_isNull(referenced)) diff --git a/src/Tokens.cc b/src/Tokens.cc index b1c5893..820bd6b 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -17,87 +17,18 @@ clang::Tokens::~Tokens() { clang_disposeTokens(cx_tu, cx_tokens, size()); } -/*CXCursor clang::Tokens::find_referenced() { - cursors.clear(); - cursors.reserve(size()); - clang_annotateTokens(tu, tokens.data(), size(), cursors.data()); - - auto kind=clang_getCursorKind(cursors[0]); - cout << " " << kind << endl; - cout << " Decl: " << clang_isDeclaration(kind) << endl; - cout << " Attr: " << clang_isAttribute(kind) << endl; - cout << " Ref: " << clang_isReference(kind) << endl; - cout << " Expr: " << clang_isExpression(kind) << endl; - - auto referenced=clang_getCursorReferenced(cursors[0]); - - kind=clang_getCursorKind(referenced); - cout << " " << kind << endl; - cout << " Decl: " << clang_isDeclaration(kind) << endl; - cout << " Attr: " << clang_isAttribute(kind) << endl; - cout << " Ref: " << clang_isReference(kind) << endl; - cout << " Expr: " << clang_isExpression(kind) << endl; - - //TODO: To find similar function declarations, these must be equal (if cursor is declaration): - //TODO: How do we know it is a function? clang_getCursorKind(cursor)==CXCursor_CXXMethod? - cout << " " << clang_getCString(clang_getTypeSpelling(clang_getCursorType(referenced))) << endl; - cout << " " << clang_getCString(clang_getTypeSpelling(clang_getCursorType(clang_getCursorSemanticParent(referenced)))) << endl; - cout << " " << clang_getCString(clang_getCursorSpelling(referenced)) << endl; - - return referenced; -}*/ - -void clang::Tokens::rename(CXCursor &referenced) { - for(size_t c=0;c > clang::Tokens::get_similar_token_offsets(clang::Token& token) { + std::vector > offsets; + auto referenced=clang_getCursorReferenced(token.get_cursor().cx_cursor); + for(auto &a_token: *this) { + auto a_referenced=clang_getCursorReferenced(a_token.get_cursor().cx_cursor); + if(Cursor(referenced)==Cursor(a_referenced) && token.get_token_spelling()==a_token.get_token_spelling()) { + auto range_data=a_token.source_range.get_range_data(); + offsets.emplace_back(range_data.start_offset, range_data.end_offset); } } - //clang_visitChildren(clang_getTranslationUnitCursor(tu.tu_), clang::Tokens::clang_visitor, &referenced); + return offsets; } - -/*CXChildVisitResult clang::Tokens::clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData) { - CXCursor* referenced=(CXCursor*)clientData; - auto a_referenced=clang_getCursorReferenced(cursor); - if(clang_equalCursors(a_referenced, *referenced)) { - cout << "found" << endl; - clang::Cursor a_cursor; - a_cursor.cursor_=cursor; - auto range=clang::SourceRange(&a_cursor); - - auto location=clang::SourceLocation(range, true); - std::string path; - unsigned line, column, offset; - location.get_location_info(&path, &line, &column, &offset); - cout << " start: " << path << ", " << line << ", " << column << endl; - - location=clang::SourceLocation(range, false); - location.get_location_info(&path, &line, &column, &offset); - cout << " end: " << path << ", " << line << ", " << column << endl; - } - - return CXChildVisit_Recurse; -}*/ diff --git a/src/Tokens.h b/src/Tokens.h index ca60a35..81693d1 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -11,8 +11,7 @@ namespace clang { public: Tokens(CXTranslationUnit &cx_tu, SourceRange &range); ~Tokens(); - void rename(CXCursor &referenced); - //std::unordered_map > + std::vector > get_similar_token_offsets(clang::Token& token); private: CXToken *cx_tokens; unsigned num_tokens; diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 0fd841d..37f8104 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -118,3 +118,8 @@ std::unique_ptr clang::TranslationUnit::get_tokens(unsigned start clang::SourceRange range(start_location, end_location); return std::unique_ptr(new Tokens(cx_tu, range)); } + +clang::Cursor clang::TranslationUnit::get_cursor(std::string path, unsigned offset) { + clang::SourceLocation location(cx_tu, path, offset); + return Cursor(clang_getCursor(cx_tu, location.cx_location)); +} diff --git a/src/TranslationUnit.h b/src/TranslationUnit.h index 9429031..8bf2563 100644 --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -9,6 +9,7 @@ #include "Diagnostic.h" #include "Tokens.h" #include "CodeCompleteResults.h" +#include "Cursor.h" namespace clang { class TranslationUnit { @@ -37,6 +38,7 @@ namespace clang { clang::CodeCompleteResults get_code_completions(const std::map &buffers, int line_number, int 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; }; diff --git a/tests/Cursor_H_Test.cc b/tests/Cursor_H_Test.cc index a364960..0edc1c3 100644 --- a/tests/Cursor_H_Test.cc +++ b/tests/Cursor_H_Test.cc @@ -12,8 +12,7 @@ BOOST_AUTO_TEST_CASE(cursor) { // ] - clang::SourceLocation location(tu.cx_tu, path, 6, 4); - clang::Cursor cursor(tu.cx_tu, location); + auto cursor=tu.get_cursor(path, 103); BOOST_CHECK(cursor.get_kind() == clang::CursorKind::ReturnStmt); } From aababceb5ea41736e239d24f61fed9b3956580f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Wed, 15 Jul 2015 20:38:05 +0200 Subject: [PATCH 10/21] #9 Work in progress --- src/docs/windows-install.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/docs/windows-install.md diff --git a/src/docs/windows-install.md b/src/docs/windows-install.md new file mode 100644 index 0000000..156f22e --- /dev/null +++ b/src/docs/windows-install.md @@ -0,0 +1,13 @@ +# Windows Install Guide # +## Requirements ## + +```bash +* chocolatey [website](http://chocolatey.org) +``` + +##Preperation ## +First you install some dependencies + +```bash +PS: choco install cmake make nmake +``` From a92970fd690a3b7d548a93abb68b78d904b015d7 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 11:13:02 +0200 Subject: [PATCH 11/21] Smaller fixes and cleanups. --- src/Cursor.cc | 22 ++++++++++++++++------ src/Cursor.h | 3 +++ src/Tokens.cc | 13 +++++++------ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Cursor.cc b/src/Cursor.cc index cb9f455..4a7c992 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -12,12 +12,22 @@ clang::SourceRange clang::Cursor::get_source_range() const { return SourceRange(clang_getCursorExtent(cx_cursor)); } -bool clang::Cursor::operator==(const Cursor& rhs) const { +std::string clang::Cursor::get_usr() const { auto cxstr=clang_getCursorUSR(cx_cursor); - std::string lhs_str=clang_getCString(cxstr); - clang_disposeString(cxstr); - cxstr=clang_getCursorUSR(rhs.cx_cursor); - std::string rhs_str=clang_getCString(cxstr); + std::string USR=clang_getCString(cxstr); clang_disposeString(cxstr); - return lhs_str==rhs_str; + return USR; +} + +std::string clang::Cursor::get_referenced_usr() const { + auto referenced=clang_getCursorReferenced(cx_cursor); + if(!clang_Cursor_isNull(referenced)) { + return Cursor(referenced).get_usr(); + } + else + return ""; +} + +bool clang::Cursor::operator==(const Cursor& rhs) const { + return get_usr()==rhs.get_usr(); } \ No newline at end of file diff --git a/src/Cursor.h b/src/Cursor.h index 4f34455..ae85534 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -3,6 +3,7 @@ #include #include "SourceLocation.h" #include "SourceRange.h" +#include namespace clang { enum class CursorKind { @@ -179,6 +180,8 @@ namespace clang { const CursorKind get_kind(); SourceLocation get_source_location() const; SourceRange get_source_range() const; + std::string get_usr() const; + std::string get_referenced_usr() const; bool operator==(const Cursor& rhs) const; CXCursor cx_cursor; }; diff --git a/src/Tokens.cc b/src/Tokens.cc index 820bd6b..390948c 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -22,12 +22,13 @@ clang::Tokens::~Tokens() { //Similar tokens defined as tokens with equal referenced cursors. std::vector > clang::Tokens::get_similar_token_offsets(clang::Token& token) { std::vector > offsets; - auto referenced=clang_getCursorReferenced(token.get_cursor().cx_cursor); - for(auto &a_token: *this) { - auto a_referenced=clang_getCursorReferenced(a_token.get_cursor().cx_cursor); - if(Cursor(referenced)==Cursor(a_referenced) && token.get_token_spelling()==a_token.get_token_spelling()) { - auto range_data=a_token.source_range.get_range_data(); - offsets.emplace_back(range_data.start_offset, range_data.end_offset); + auto referenced_usr=token.get_cursor().get_referenced_usr(); + if(referenced_usr!="") { + for(auto &a_token: *this) { + if(referenced_usr==a_token.get_cursor().get_referenced_usr() && token.get_token_spelling()==a_token.get_token_spelling()) { + auto range_data=a_token.source_range.get_range_data(); + offsets.emplace_back(range_data.start_offset, range_data.end_offset); + } } } return offsets; From 9c85713a62c70b060440c3bbcb3c44a3de031686 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 19:10:19 +0200 Subject: [PATCH 12/21] Cleanup. --- src/Diagnostic.cc | 11 +++++---- src/Diagnostic.h | 3 ++- src/SourceLocation.cc | 41 ++++++++++++++-------------------- src/SourceLocation.h | 9 ++------ src/SourceRange.cc | 25 +++++---------------- src/SourceRange.h | 11 ++------- src/Token.h | 6 ++--- src/Tokens.cc | 5 ++--- src/Tokens.h | 2 +- tests/Diagnostics_Test.cc | 1 - tests/SourceLocation_H_Test.cc | 28 +++-------------------- 11 files changed, 43 insertions(+), 99 deletions(-) diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index f2f4f52..6b73f9b 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -8,14 +8,13 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos auto cxstr=clang_getDiagnosticSpelling(cx_diagnostic); spelling=clang_getCString(cxstr); clang_disposeString(cxstr); - clang::SourceLocation location(clang_getDiagnosticLocation(cx_diagnostic)); + clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); - clang::SourceRange range(location, location); - clang::Tokens tokens(cx_tu, range); + unsigned start_offset; + start_location.get_data(&path, NULL, NULL, &start_offset); + clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location)); if(tokens.size()==1) { - auto& token=tokens[0]; - auto locations=token.source_range.get_source_locations(); - this->range=SourceRange::get_range_data(location, locations.second); + offsets=std::pair(start_offset, tokens[0].offsets.second); } } diff --git a/src/Diagnostic.h b/src/Diagnostic.h index a01e776..9874186 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -15,7 +15,8 @@ namespace clang { unsigned severity; std::string severity_spelling; std::string spelling; - RangeData range; + std::string path; + std::pair offsets; }; } diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 976eb01..29047f0 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -8,34 +8,27 @@ SourceLocation(CXTranslationUnit &tu, const std::string &filename, int line_number, int line_offset) { - CXFile file = clang_getFile(tu, - filename.c_str()); - cx_location = clang_getLocation(tu, - file, - line_number, - line_offset); + CXFile file = clang_getFile(tu, filename.c_str()); + cx_location = clang_getLocation(tu, file, line_number, line_offset); } clang::SourceLocation:: -SourceLocation(CXTranslationUnit &tu, - const std::string &filepath, - int offset) { - CXFile file = clang_getFile(tu, - filepath.c_str()); - cx_location = clang_getLocationForOffset(tu, - file, - offset); +SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset) { + CXFile file = clang_getFile(tu, filepath.c_str()); + cx_location = clang_getLocationForOffset(tu, file, offset); } -void clang::SourceLocation:: -get_location_info(std::string* path, - unsigned *line, - unsigned *column, - unsigned *offset) { - CXFile file; - clang_getExpansionLocation(cx_location, &file, line, column, offset); - if (path != NULL && file!=NULL) { - path->operator=(((clang_getCString((clang_getFileName(file)))))); - } +void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) { + if(path==nullptr) + clang_getExpansionLocation(cx_location, NULL, line, column, offset); + else { + CXFile file; + clang_getExpansionLocation(cx_location, &file, line, column, offset); + if (file!=NULL) { + auto cxstr=clang_getFileName(file); + *path=clang_getCString(cxstr); + clang_disposeString(cxstr); + } + } } diff --git a/src/SourceLocation.h b/src/SourceLocation.h index 0837dc1..f041f82 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -14,14 +14,9 @@ namespace clang { int line_number, int column); - SourceLocation(CXTranslationUnit &tu, - const std::string &filepath, - int offset); + SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); - void get_location_info(std::string *path, - unsigned *line, - unsigned *column, - unsigned *offset); + void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset); CXSourceLocation cx_location; }; diff --git a/src/SourceRange.cc b/src/SourceRange.cc index ae3bd24..7e6e7ea 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -5,23 +5,10 @@ SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { cx_range = clang_getRange(start.cx_location, end.cx_location); } -std::pair clang::SourceRange::get_source_locations() { - return std::pair(clang_getRangeStart(cx_range), clang_getRangeEnd(cx_range)); -} - -clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start, clang::SourceLocation &end) { - std::string path; - unsigned start_offset, end_offset; - start.get_location_info(&path, NULL, NULL, &start_offset); - end.get_location_info(NULL, NULL, NULL, &end_offset); - RangeData range_data; - range_data.path=path; - range_data.start_offset=start_offset; - range_data.end_offset=end_offset; - return range_data; -} - -clang::RangeData clang::SourceRange::get_range_data() { - auto locations=get_source_locations(); - return get_range_data(locations.first, locations.second); +std::pair clang::SourceRange::get_offsets() { + SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range)); + std::pair offsets; + start.get_data(NULL, NULL, NULL, &offsets.first); + end.get_data(NULL, NULL, NULL, &offsets.second); + return offsets; } \ No newline at end of file diff --git a/src/SourceRange.h b/src/SourceRange.h index 7a85504..c39eb18 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -3,21 +3,14 @@ #include #include "SourceLocation.h" #include +#include namespace clang { - class RangeData { - public: - std::string path; - unsigned start_offset, end_offset; - }; - class SourceRange { public: SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {} SourceRange(SourceLocation &start, SourceLocation &end); - std::pair get_source_locations(); - static RangeData get_range_data(SourceLocation &start, SourceLocation &end); - RangeData get_range_data(); + std::pair get_offsets(); CXSourceRange cx_range; }; } // namespace clang diff --git a/src/Token.h b/src/Token.h index d88817f..9fcb7e3 100644 --- a/src/Token.h +++ b/src/Token.h @@ -18,20 +18,20 @@ namespace clang { class Token { public: Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): - cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), source_range(get_source_range()) {}; + cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {}; const TokenKind kind(); std::string get_token_spelling(); SourceLocation get_source_location(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); + SourceRange get_source_range(); std::string get_brief_comments(); CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; - SourceRange source_range; + std::pair offsets; private: - SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 390948c..2d3f0e1 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -2,7 +2,7 @@ #include using namespace std; -clang::Tokens::Tokens(CXTranslationUnit &cx_tu, SourceRange &range): cx_tu(cx_tu) { +clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); cx_cursors.clear(); @@ -26,8 +26,7 @@ std::vector > clang::Tokens::get_similar_token_off if(referenced_usr!="") { for(auto &a_token: *this) { if(referenced_usr==a_token.get_cursor().get_referenced_usr() && token.get_token_spelling()==a_token.get_token_spelling()) { - auto range_data=a_token.source_range.get_range_data(); - offsets.emplace_back(range_data.start_offset, range_data.end_offset); + offsets.emplace_back(a_token.offsets); } } } diff --git a/src/Tokens.h b/src/Tokens.h index 81693d1..054f510 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -9,7 +9,7 @@ namespace clang { class Tokens : public std::vector { public: - Tokens(CXTranslationUnit &cx_tu, SourceRange &range); + Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); ~Tokens(); std::vector > get_similar_token_offsets(clang::Token& token); private: diff --git a/tests/Diagnostics_Test.cc b/tests/Diagnostics_Test.cc index 70972eb..c56b6a6 100644 --- a/tests/Diagnostics_Test.cc +++ b/tests/Diagnostics_Test.cc @@ -23,6 +23,5 @@ BOOST_AUTO_TEST_CASE(diagnostics_test) { auto diagnostics=tu.get_diagnostics(); BOOST_CHECK(diagnostics.size()==1); BOOST_CHECK(diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'"); - BOOST_CHECK(diagnostics[0].range.path=="./case/main_error.cpp"); BOOST_CHECK(diagnostics[0].severity==3); } diff --git a/tests/SourceLocation_H_Test.cc b/tests/SourceLocation_H_Test.cc index ab6d1c0..6722b4a 100644 --- a/tests/SourceLocation_H_Test.cc +++ b/tests/SourceLocation_H_Test.cc @@ -10,30 +10,8 @@ BOOST_AUTO_TEST_CASE(source_location) { clang::TranslationUnit tu(index, path); auto tokens=tu.get_tokens(0, 113); - clang::SourceRange token_range = (*tokens)[28].source_range; + auto offsets=(*tokens)[28].offsets; - unsigned token_start_line, token_start_column, token_start_offset, - token_end_line, token_end_column, token_end_offset; - std::string token_start_path, token_end_path; - - auto locations=token_range.get_source_locations(); - - locations.first.get_location_info(&token_start_path, - &token_start_line, - &token_start_column, - &token_start_offset); - - locations.second.get_location_info(&token_end_path, - &token_end_line, - &token_end_column, - &token_end_offset); - - BOOST_CHECK(token_start_path == path); - BOOST_CHECK(token_start_line == 6); - BOOST_CHECK(token_start_column == 3); - BOOST_CHECK(token_start_offset == 103); - BOOST_CHECK(token_end_path == path); - BOOST_CHECK(token_end_line == 6); - BOOST_CHECK(token_end_column == 9); - BOOST_CHECK(token_end_offset == 109); + BOOST_CHECK(offsets.first == 103); + BOOST_CHECK(offsets.second == 109); } From ec06c9716f181e1d27e5a00b537062ca8a927759 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 19:44:40 +0200 Subject: [PATCH 13/21] Got rid of warning messages while compiling, and some minor cleanups. --- src/CompileCommand.cc | 4 ++-- src/CompletionString.cc | 4 ++-- src/CompletionString.h | 2 +- src/Cursor.cc | 13 ++++++------- src/Cursor.h | 3 ++- src/Token.cc | 4 ++-- src/Token.h | 6 +++--- src/Tokens.cc | 11 ++++++----- tests/Token_H_Test.cc | 4 ++-- 9 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/CompileCommand.cc b/src/CompileCommand.cc index 3d98579..0cb8971 100644 --- a/src/CompileCommand.cc +++ b/src/CompileCommand.cc @@ -5,7 +5,7 @@ std::string clang::CompileCommand:: get_command() { std::string res; unsigned N = clang_CompileCommand_getNumArgs(cx_command); - for (int i = 0; i < N; i++) { + for (unsigned i = 0; i < N; i++) { res += clang_getCString(clang_CompileCommand_getArg(cx_command, i)); } return res; @@ -15,7 +15,7 @@ std::vector clang::CompileCommand:: get_command_as_args() { unsigned N = clang_CompileCommand_getNumArgs(cx_command); std::vector res(N); - for (int i = 0; i < N; i++) { + for (unsigned i = 0; i < N; i++) { res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i)); } return res; diff --git a/src/CompletionString.cc b/src/CompletionString.cc index 1c0cb88..2d291c1 100644 --- a/src/CompletionString.cc +++ b/src/CompletionString.cc @@ -7,13 +7,13 @@ bool clang::CompletionString::available() { return clang_getCompletionAvailability(cx_str) == CXAvailability_Available; } -int clang::CompletionString::get_num_chunks() { +unsigned clang::CompletionString::get_num_chunks() { return clang_getNumCompletionChunks(cx_str); } std::vector clang::CompletionString::get_chunks() { std::vector res; - for (size_t i = 0; i < get_num_chunks(); i++) { + for (unsigned i = 0; i < get_num_chunks(); i++) { auto cxstr=clang_getCompletionChunkText(cx_str, i); res.emplace_back(clang_getCString(cxstr), static_cast (clang_getCompletionChunkKind(cx_str, i))); clang_disposeString(cxstr); diff --git a/src/CompletionString.h b/src/CompletionString.h index 43d953b..a9d9830 100644 --- a/src/CompletionString.h +++ b/src/CompletionString.h @@ -32,7 +32,7 @@ namespace clang { bool available(); std::vector get_chunks(); std::string get_brief_comments(); - int get_num_chunks(); + unsigned get_num_chunks(); CXCompletionString cx_str; }; diff --git a/src/Cursor.cc b/src/Cursor.cc index 4a7c992..d5be2a2 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -19,13 +19,12 @@ std::string clang::Cursor::get_usr() const { return USR; } -std::string clang::Cursor::get_referenced_usr() const { - auto referenced=clang_getCursorReferenced(cx_cursor); - if(!clang_Cursor_isNull(referenced)) { - return Cursor(referenced).get_usr(); - } - else - return ""; +clang::Cursor clang::Cursor::get_referenced() const { + return Cursor(clang_getCursorReferenced(cx_cursor)); +} + +clang::Cursor::operator bool() const { + return !clang_Cursor_isNull(cx_cursor); } bool clang::Cursor::operator==(const Cursor& rhs) const { diff --git a/src/Cursor.h b/src/Cursor.h index ae85534..7c01ca7 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -181,7 +181,8 @@ namespace clang { SourceLocation get_source_location() const; SourceRange get_source_range() const; std::string get_usr() const; - std::string get_referenced_usr() const; + Cursor get_referenced() const; + operator bool() const; bool operator==(const Cursor& rhs) const; CXCursor cx_cursor; }; diff --git a/src/Token.cc b/src/Token.cc index 30f2c54..310a130 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -15,12 +15,12 @@ clang::SourceRange clang::Token::get_source_range() { return SourceRange(clang_getTokenExtent(cx_tu, cx_token)); } // returns a string description of this tokens kind -std::string clang::Token::get_token_spelling() { +std::string clang::Token::get_spelling() { CXString s = clang_getTokenSpelling(cx_tu, cx_token); return std::string(clang_getCString(s)); } -const clang::TokenKind clang::Token::kind() { +const clang::TokenKind clang::Token::get_kind() { return (TokenKind) clang_getTokenKind(cx_token); } diff --git a/src/Token.h b/src/Token.h index 9fcb7e3..6bb4b64 100644 --- a/src/Token.h +++ b/src/Token.h @@ -19,19 +19,19 @@ namespace clang { public: Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {}; - const TokenKind kind(); - std::string get_token_spelling(); + const TokenKind get_kind(); + std::string get_spelling(); SourceLocation get_source_location(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); - SourceRange get_source_range(); std::string get_brief_comments(); CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; std::pair offsets; private: + SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 2d3f0e1..a597557 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -3,12 +3,11 @@ using namespace std; clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { - clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); cx_cursors.clear(); cx_cursors.reserve(num_tokens); clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); - for (int i = 0; i < num_tokens; i++) { + for (unsigned i = 0; i < num_tokens; i++) { emplace_back(cx_tu, cx_tokens[i], cx_cursors[i]); } } @@ -22,10 +21,12 @@ clang::Tokens::~Tokens() { //Similar tokens defined as tokens with equal referenced cursors. std::vector > clang::Tokens::get_similar_token_offsets(clang::Token& token) { std::vector > offsets; - auto referenced_usr=token.get_cursor().get_referenced_usr(); - if(referenced_usr!="") { + auto referenced=token.get_cursor().get_referenced(); + if(referenced) { + auto referenced_usr=referenced.get_usr(); for(auto &a_token: *this) { - if(referenced_usr==a_token.get_cursor().get_referenced_usr() && token.get_token_spelling()==a_token.get_token_spelling()) { + auto a_referenced=a_token.get_cursor().get_referenced(); + if(a_referenced && referenced_usr==a_referenced.get_usr() && token.get_spelling()==a_token.get_spelling()) { offsets.emplace_back(a_token.offsets); } } diff --git a/tests/Token_H_Test.cc b/tests/Token_H_Test.cc index 38198c2..6325062 100644 --- a/tests/Token_H_Test.cc +++ b/tests/Token_H_Test.cc @@ -12,8 +12,8 @@ BOOST_AUTO_TEST_CASE(token) { auto tokens=tu.get_tokens(0, 113); BOOST_CHECK(tokens->size() == 32); - BOOST_CHECK((*tokens)[1].kind() == clang::TokenKind::Token_Identifier); + BOOST_CHECK((*tokens)[1].get_kind() == clang::TokenKind::Token_Identifier); - std::string str = (*tokens)[28].get_token_spelling(); + std::string str = (*tokens)[28].get_spelling(); BOOST_CHECK(str == "return"); } From 8137e7a9e6038b4acf7fb192fd97b06f05fdd930 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 20:31:21 +0200 Subject: [PATCH 14/21] More cleanup. --- src/CodeCompleteResults.cc | 12 +++++------- src/CodeCompleteResults.h | 13 ++++++------- src/Diagnostic.cc | 4 ++-- src/Diagnostic.h | 4 ++-- src/SourceLocation.cc | 23 ++++++++++++----------- src/SourceLocation.h | 16 ++++++++-------- src/SourceRange.cc | 4 ++-- src/Token.h | 3 +-- src/Tokens.h | 4 +++- src/TranslationUnit.cc | 2 +- src/TranslationUnit.h | 2 +- 11 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index da916f8..e6aea17 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -3,11 +3,9 @@ #include clang::CodeCompleteResults:: -CodeCompleteResults(CXTranslationUnit &cx_tu, - const std::string &file_name, +CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, - int line_num, - int column) { + unsigned line_num, unsigned column) { std::vector files; for (auto &buffer : buffers) { CXUnsavedFile file; @@ -31,15 +29,15 @@ clang::CodeCompleteResults::~CodeCompleteResults() { delete cx_results; } -int clang::CodeCompleteResults:: +unsigned clang::CodeCompleteResults:: size() { return cx_results->NumResults; } clang::CompletionString clang::CodeCompleteResults:: -get(int i) { +get(unsigned i) { if (i >= size()) { - throw std::invalid_argument("clang::CodeCompleteResults::get(int i): i>=size()"); + throw std::invalid_argument("clang::CodeCompleteResults::get(unsigned i): i>=size()"); } return CompletionString(cx_results->Results[i].CompletionString); } diff --git a/src/CodeCompleteResults.h b/src/CodeCompleteResults.h index dda4d89..e6c8a17 100644 --- a/src/CodeCompleteResults.h +++ b/src/CodeCompleteResults.h @@ -6,15 +6,14 @@ namespace clang { class CodeCompleteResults { - public: - CodeCompleteResults(CXTranslationUnit &cx_tu, - const std::string &file_name, + friend class TranslationUnit; + CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, - int line_num, - int column); + unsigned line_num, unsigned column); + public: ~CodeCompleteResults(); - CompletionString get(int index); - int size(); + CompletionString get(unsigned index); + unsigned size(); CXCodeCompleteResults *cx_results; }; diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 6b73f9b..1ff3cfe 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -10,8 +10,8 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos clang_disposeString(cxstr); clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); - unsigned start_offset; - start_location.get_data(&path, NULL, NULL, &start_offset); + path=start_location.get_path(); + unsigned start_offset=start_location.get_offset(); clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location)); if(tokens.size()==1) { offsets=std::pair(start_offset, tokens[0].offsets.second); diff --git a/src/Diagnostic.h b/src/Diagnostic.h index 9874186..42d3785 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -7,9 +7,9 @@ namespace clang { class Diagnostic { - public: + friend class TranslationUnit; Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic); - + public: static const std::string get_severity_spelling(unsigned severity); unsigned severity; diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 29047f0..62a2d6c 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -3,21 +3,22 @@ // // // // // // // // // SourceLocation // // // // // // // // // -clang::SourceLocation:: -SourceLocation(CXTranslationUnit &tu, - const std::string &filename, - int line_number, - int line_offset) { - CXFile file = clang_getFile(tu, filename.c_str()); - cx_location = clang_getLocation(tu, file, line_number, line_offset); -} - -clang::SourceLocation:: -SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset) { +clang::SourceLocation::SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset) { CXFile file = clang_getFile(tu, filepath.c_str()); cx_location = clang_getLocationForOffset(tu, file, offset); } +std::string clang::SourceLocation::get_path() { + std::string path; + get_data(&path, NULL, NULL, NULL); + return path; +} +unsigned clang::SourceLocation::get_offset() { + unsigned offset; + get_data(NULL, NULL, NULL, &offset); + return offset; +} + void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) { if(path==nullptr) clang_getExpansionLocation(cx_location, NULL, line, column, offset); diff --git a/src/SourceLocation.h b/src/SourceLocation.h index f041f82..9f29b99 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -6,19 +6,19 @@ namespace clang { class SourceLocation { + friend class TranslationUnit; + SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset); public: SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {} - - SourceLocation(CXTranslationUnit &cx_tu, - const std::string &filename, - int line_number, - int column); - - SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); - void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset); + public: + std::string get_path(); + unsigned get_offset(); CXSourceLocation cx_location; + + private: + void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset); }; } // namespace clang diff --git a/src/SourceRange.cc b/src/SourceRange.cc index 7e6e7ea..5c97323 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -8,7 +8,7 @@ SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { std::pair clang::SourceRange::get_offsets() { SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range)); std::pair offsets; - start.get_data(NULL, NULL, NULL, &offsets.first); - end.get_data(NULL, NULL, NULL, &offsets.second); + offsets.first=start.get_offset(); + offsets.second=end.get_offset(); return offsets; } \ No newline at end of file diff --git a/src/Token.h b/src/Token.h index 6bb4b64..77b4e89 100644 --- a/src/Token.h +++ b/src/Token.h @@ -22,6 +22,7 @@ namespace clang { const TokenKind get_kind(); std::string get_spelling(); SourceLocation get_source_location(); + SourceRange get_source_range(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); @@ -30,8 +31,6 @@ namespace clang { CXToken& cx_token; CXCursor& cx_cursor; std::pair offsets; - private: - SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.h b/src/Tokens.h index 054f510..ae35873 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -8,8 +8,10 @@ namespace clang { class Tokens : public std::vector { - public: + friend class TranslationUnit; + friend class Diagnostic; Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); + public: ~Tokens(); std::vector > get_similar_token_offsets(clang::Token& token); private: diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 37f8104..edd2bde 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -90,7 +90,7 @@ unsigned clang::TranslationUnit::DefaultFlags() { return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; } -clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, int line_number, int column) { +clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, unsigned line_number, unsigned column) { auto cxstr=clang_getTranslationUnitSpelling(cx_tu); std::string path=clang_getCString(cxstr); clang_disposeString(cxstr); diff --git a/src/TranslationUnit.h b/src/TranslationUnit.h index 8bf2563..e10d465 100644 --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -35,7 +35,7 @@ namespace clang { const std::map &buffers, unsigned flags=DefaultFlags()); - clang::CodeCompleteResults get_code_completions(const std::map &buffers, int line_number, int column); + 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); From 4b206c20482cb6caa1b1f2e3855a5f458908a4a2 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 20:35:11 +0200 Subject: [PATCH 15/21] Got rid of a compilation warning. --- tests/CodeCompleteResults_H_Test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CodeCompleteResults_H_Test.cc b/tests/CodeCompleteResults_H_Test.cc index d2e880f..bb75e07 100644 --- a/tests/CodeCompleteResults_H_Test.cc +++ b/tests/CodeCompleteResults_H_Test.cc @@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) { auto results=tu.get_code_completions(buffers, 4, 5); bool substr_found=false; - for(int c=0;c Date: Fri, 17 Jul 2015 14:55:16 +0200 Subject: [PATCH 16/21] Added one more friend class. --- src/Token.h | 3 ++- src/Tokens.cc | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Token.h b/src/Token.h index 77b4e89..15ded6c 100644 --- a/src/Token.h +++ b/src/Token.h @@ -16,9 +16,10 @@ namespace clang { }; class Token { - public: + friend class Tokens; Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {}; + public: const TokenKind get_kind(); std::string get_spelling(); SourceLocation get_source_location(); diff --git a/src/Tokens.cc b/src/Tokens.cc index a597557..50d9ebe 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -8,7 +8,7 @@ clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu cx_cursors.reserve(num_tokens); clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); for (unsigned i = 0; i < num_tokens; i++) { - emplace_back(cx_tu, cx_tokens[i], cx_cursors[i]); + emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i])); } } From ff1d7fe09b116e67c236fbe2f50a51003cb2cf39 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 21 Jul 2015 13:11:01 +0200 Subject: [PATCH 17/21] Added Tokens::get_cxx_methods(). --- src/Token.cc | 1 + src/Tokens.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ src/Tokens.h | 1 + 3 files changed, 44 insertions(+) diff --git a/src/Token.cc b/src/Token.cc index 310a130..ce3da9b 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -59,6 +59,7 @@ std::string clang::Token::get_type() { return spelling; } +//TODO: use clang_Cursor_getBriefCommentText std::string clang::Token::get_brief_comments() { std::string comment_string; auto referenced=clang_getCursorReferenced(cx_cursor); diff --git a/src/Tokens.cc b/src/Tokens.cc index 50d9ebe..b62b982 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -33,3 +33,45 @@ std::vector > clang::Tokens::get_similar_token_off } return offsets; } + +std::vector > clang::Tokens::get_cxx_methods() { + std::vector > methods; + long last_offset=-1; + for(auto &token: *this) { + if(token.get_kind()==clang::Token_Identifier) { + auto cursor=token.get_cursor(); + auto kind=cursor.get_kind(); + if(kind==clang::CursorKind::CXXMethod || kind==clang::CursorKind::Constructor || kind==clang::CursorKind::Destructor) { + auto offset=cursor.get_source_location().get_offset(); + if(offset!=last_offset) { + std::string method; + CXString cxstr; + if(kind==clang::CursorKind::CXXMethod) { + auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor)); + auto cxstr=clang_getTypeSpelling(type); + method+=clang_getCString(cxstr); + clang_disposeString(cxstr); + auto pos=method.find(" "); + if(pos!=std::string::npos) + method.erase(pos, 1); + method+=" "; + } + + clang::Cursor parent(clang_getCursorSemanticParent(cursor.cx_cursor)); + cxstr=clang_getCursorDisplayName(parent.cx_cursor); + method+=clang_getCString(cxstr); + clang_disposeString(cxstr); + + method+="::"; + + cxstr=clang_getCursorDisplayName(cursor.cx_cursor); + method+=clang_getCString(cxstr); + clang_disposeString(cxstr); + methods.emplace_back(method, offset); + } + last_offset=offset; + } + } + } + return methods; +} diff --git a/src/Tokens.h b/src/Tokens.h index ae35873..96366cd 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -14,6 +14,7 @@ namespace clang { public: ~Tokens(); std::vector > get_similar_token_offsets(clang::Token& token); + std::vector > get_cxx_methods(); private: CXToken *cx_tokens; unsigned num_tokens; From 483d9bf9d3765e4ad736ee8417f6bf5f5fef0b5f Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 22 Jul 2015 17:49:26 +0200 Subject: [PATCH 18/21] Simpler Token::get_brief_comments. --- src/Token.cc | 55 ++++++---------------------------------------------- 1 file changed, 6 insertions(+), 49 deletions(-) diff --git a/src/Token.cc b/src/Token.cc index ce3da9b..de9f93d 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -59,57 +59,14 @@ std::string clang::Token::get_type() { return spelling; } -//TODO: use clang_Cursor_getBriefCommentText std::string clang::Token::get_brief_comments() { std::string comment_string; - auto referenced=clang_getCursorReferenced(cx_cursor); - auto comment=clang_Cursor_getParsedComment(referenced); - if(clang_Comment_getKind(comment)==CXComment_FullComment) { - size_t para_c=0; - for(unsigned c=0;c=2) - break; - for(unsigned c=0;c0) - comment_string.pop_back(); - if(clang_InlineCommandComment_getNumArgs(grandchild_comment)==0) - comment_string+=clang_getCString(cxstr); - clang_disposeString(cxstr); - for(unsigned arg_c=0;arg_c0) - comment_string+=" "; - comment_string+=clang_getCString(cxstr); - clang_disposeString(cxstr); - } - } - } - } - } - /*cout << " " << clang_Comment_getKind(child_comment) << ", children: " << clang_Comment_getNumChildren(child_comment) << endl; - auto cxstr=clang_FullComment_getAsHTML(child_comment); - cout << " " << clang_getCString(cxstr) << endl; - clang_disposeString(cxstr);*/ - } - while(comment_string.size()>0 && (comment_string.back()=='\n' || comment_string.back()==' ')) - comment_string.pop_back(); + auto referenced=get_cursor().get_referenced(); + if(referenced) { + auto cxstr=clang_Cursor_getBriefCommentText(referenced.cx_cursor); + if(cxstr.data!=NULL) + comment_string=clang_getCString(cxstr); + clang_disposeString(cxstr); } - return comment_string; } From 7eaafa5dde03d941fe3668614b7ca3d842c62b54 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 23 Jul 2015 11:53:30 +0200 Subject: [PATCH 19/21] Added clang::to_string in Utility.* to and used this to clean up all the CXString code. --- src/CMakeLists.txt | 2 ++ src/CompileCommand.cc | 5 +++-- src/CompletionString.cc | 19 ++++++------------- src/CompletionString.h | 4 ++-- src/Cursor.cc | 6 ++---- src/Diagnostic.cc | 5 ++--- src/SourceLocation.cc | 5 ++--- src/Token.cc | 21 ++++++--------------- src/Tokens.cc | 14 ++++---------- src/TranslationUnit.cc | 9 +++------ src/Utility.cc | 10 ++++++++++ src/Utility.h | 10 ++++++++++ src/clangmm.h | 1 + 13 files changed, 53 insertions(+), 58 deletions(-) create mode 100644 src/Utility.cc create mode 100644 src/Utility.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 478c921..22cab39 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,6 +26,7 @@ set(header_files Tokens.h TranslationUnit.h Diagnostic.h + Utility.h ) set(cc_files CodeCompleteResults.cc @@ -41,6 +42,7 @@ set(cc_files Tokens.cc TranslationUnit.cc Diagnostic.cc + Utility.cc ) add_library(${project_name} SHARED ${header_files} ${cc_files}) diff --git a/src/CompileCommand.cc b/src/CompileCommand.cc index 0cb8971..5c4ec93 100644 --- a/src/CompileCommand.cc +++ b/src/CompileCommand.cc @@ -1,12 +1,13 @@ #include "CompileCommand.h" #include "CompileCommands.h" +#include "Utility.h" std::string clang::CompileCommand:: get_command() { std::string res; unsigned N = clang_CompileCommand_getNumArgs(cx_command); for (unsigned i = 0; i < N; i++) { - res += clang_getCString(clang_CompileCommand_getArg(cx_command, i)); + res += clang::to_string(clang_CompileCommand_getArg(cx_command, i)); } return res; } @@ -16,7 +17,7 @@ get_command_as_args() { unsigned N = clang_CompileCommand_getNumArgs(cx_command); std::vector res(N); for (unsigned i = 0; i < N; i++) { - res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i)); + res[i] = clang::to_string(clang_CompileCommand_getArg(cx_command, i)); } return res; } diff --git a/src/CompletionString.cc b/src/CompletionString.cc index 2d291c1..9833466 100644 --- a/src/CompletionString.cc +++ b/src/CompletionString.cc @@ -1,34 +1,27 @@ #include "CompletionString.h" +#include "Utility.h" clang::CompletionString:: -CompletionString(const CXCompletionString &cx_str) : cx_str(cx_str) {} +CompletionString(const CXCompletionString &cx_completion_sting) : cx_completion_sting(cx_completion_sting) {} bool clang::CompletionString::available() { - return clang_getCompletionAvailability(cx_str) == CXAvailability_Available; + return clang_getCompletionAvailability(cx_completion_sting) == CXAvailability_Available; } unsigned clang::CompletionString::get_num_chunks() { - return clang_getNumCompletionChunks(cx_str); + return clang_getNumCompletionChunks(cx_completion_sting); } std::vector clang::CompletionString::get_chunks() { std::vector res; for (unsigned i = 0; i < get_num_chunks(); i++) { - auto cxstr=clang_getCompletionChunkText(cx_str, i); - res.emplace_back(clang_getCString(cxstr), static_cast (clang_getCompletionChunkKind(cx_str, i))); - clang_disposeString(cxstr); + res.emplace_back(clang::to_string(clang_getCompletionChunkText(cx_completion_sting, i)), static_cast (clang_getCompletionChunkKind(cx_completion_sting, i))); } return res; } std::string clang::CompletionString::get_brief_comments() { - std::string brief_comments; - auto cxstr=clang_getCompletionBriefComment(cx_str); - if(cxstr.data!=NULL) { - brief_comments=clang_getCString(cxstr); - clang_disposeString(cxstr); - } - return brief_comments; + return clang::to_string(clang_getCompletionBriefComment(cx_completion_sting)); } clang::CompletionChunk:: diff --git a/src/CompletionString.h b/src/CompletionString.h index a9d9830..8c3b16c 100644 --- a/src/CompletionString.h +++ b/src/CompletionString.h @@ -28,13 +28,13 @@ namespace clang { class CompletionString { public: - explicit CompletionString(const CXCompletionString &cx_str); + explicit CompletionString(const CXCompletionString &cx_completion_sting); bool available(); std::vector get_chunks(); std::string get_brief_comments(); unsigned get_num_chunks(); - CXCompletionString cx_str; + CXCompletionString cx_completion_sting; }; } // namespace clang #endif // COMPLETIONSTRING_H_ diff --git a/src/Cursor.cc b/src/Cursor.cc index d5be2a2..6cd3271 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -1,4 +1,5 @@ #include "Cursor.h" +#include "Utility.h" const clang::CursorKind clang::Cursor::get_kind() { return (CursorKind) clang_getCursorKind(this->cx_cursor); @@ -13,10 +14,7 @@ clang::SourceRange clang::Cursor::get_source_range() const { } std::string clang::Cursor::get_usr() const { - auto cxstr=clang_getCursorUSR(cx_cursor); - std::string USR=clang_getCString(cxstr); - clang_disposeString(cxstr); - return USR; + return clang::to_string(clang_getCursorUSR(cx_cursor)); } clang::Cursor clang::Cursor::get_referenced() const { diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 1ff3cfe..0fdc199 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -1,13 +1,12 @@ #include "Diagnostic.h" #include "SourceLocation.h" #include "Tokens.h" +#include "Utility.h" clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) { severity=clang_getDiagnosticSeverity(cx_diagnostic); severity_spelling=get_severity_spelling(severity); - auto cxstr=clang_getDiagnosticSpelling(cx_diagnostic); - spelling=clang_getCString(cxstr); - clang_disposeString(cxstr); + spelling=clang::to_string(clang_getDiagnosticSpelling(cx_diagnostic)); clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); path=start_location.get_path(); diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 62a2d6c..8227938 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -1,4 +1,5 @@ #include "SourceLocation.h" +#include "Utility.h" // // // // // // // // // SourceLocation // @@ -26,9 +27,7 @@ void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned CXFile file; clang_getExpansionLocation(cx_location, &file, line, column, offset); if (file!=NULL) { - auto cxstr=clang_getFileName(file); - *path=clang_getCString(cxstr); - clang_disposeString(cxstr); + *path=clang::to_string(clang_getFileName(file)); } } } diff --git a/src/Token.cc b/src/Token.cc index de9f93d..e3a9b9c 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -1,4 +1,5 @@ #include "Token.h" +#include "Utility.h" // // // // // // Token // @@ -16,8 +17,7 @@ clang::SourceRange clang::Token::get_source_range() { } // returns a string description of this tokens kind std::string clang::Token::get_spelling() { - CXString s = clang_getTokenSpelling(cx_tu, cx_token); - return std::string(clang_getCString(s)); + return clang::to_string(clang_getTokenSpelling(cx_tu, cx_token)); } const clang::TokenKind clang::Token::get_kind() { @@ -30,9 +30,7 @@ bool clang::Token::has_type() { if(clang_Cursor_isNull(referenced)) return false; auto type=clang_getCursorType(referenced); - auto cxstr=clang_getTypeSpelling(type); - std::string spelling=clang_getCString(cxstr); - clang_disposeString(cxstr); + auto spelling=clang::to_string(clang_getTypeSpelling(type)); return spelling!=""; } @@ -41,17 +39,13 @@ std::string clang::Token::get_type() { auto referenced=clang_getCursorReferenced(cx_cursor); if(!clang_Cursor_isNull(referenced)) { auto type=clang_getCursorType(referenced); - auto cxstr=clang_getTypeSpelling(type); - spelling=clang_getCString(cxstr); - clang_disposeString(cxstr); + spelling=clang::to_string(clang_getTypeSpelling(type)); std::string auto_end=""; //TODO fix const auto if((spelling.size()>=4 && spelling.substr(0, 4)=="auto")) { auto_end=spelling.substr(4); auto type=clang_getCanonicalType(clang_getCursorType(cx_cursor)); - auto cxstr=clang_getTypeSpelling(type); - spelling=clang_getCString(cxstr); - clang_disposeString(cxstr); + spelling=clang::to_string(clang_getTypeSpelling(type)); if(spelling.find(" ")==std::string::npos) spelling+=auto_end; } @@ -63,10 +57,7 @@ std::string clang::Token::get_brief_comments() { std::string comment_string; auto referenced=get_cursor().get_referenced(); if(referenced) { - auto cxstr=clang_Cursor_getBriefCommentText(referenced.cx_cursor); - if(cxstr.data!=NULL) - comment_string=clang_getCString(cxstr); - clang_disposeString(cxstr); + comment_string=clang::to_string(clang_Cursor_getBriefCommentText(referenced.cx_cursor)); } return comment_string; } diff --git a/src/Tokens.cc b/src/Tokens.cc index b62b982..5b79198 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -1,4 +1,5 @@ #include "Tokens.h" +#include "Utility.h" #include using namespace std; @@ -45,12 +46,9 @@ std::vector > clang::Tokens::get_cxx_methods() auto offset=cursor.get_source_location().get_offset(); if(offset!=last_offset) { std::string method; - CXString cxstr; if(kind==clang::CursorKind::CXXMethod) { auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor)); - auto cxstr=clang_getTypeSpelling(type); - method+=clang_getCString(cxstr); - clang_disposeString(cxstr); + method+=clang::to_string(clang_getTypeSpelling(type)); auto pos=method.find(" "); if(pos!=std::string::npos) method.erase(pos, 1); @@ -58,15 +56,11 @@ std::vector > clang::Tokens::get_cxx_methods() } clang::Cursor parent(clang_getCursorSemanticParent(cursor.cx_cursor)); - cxstr=clang_getCursorDisplayName(parent.cx_cursor); - method+=clang_getCString(cxstr); - clang_disposeString(cxstr); + method+=clang::to_string(clang_getCursorDisplayName(parent.cx_cursor)); method+="::"; - cxstr=clang_getCursorDisplayName(cursor.cx_cursor); - method+=clang_getCString(cxstr); - clang_disposeString(cxstr); + method+=clang::to_string(clang_getCursorDisplayName(cursor.cx_cursor)); methods.emplace_back(method, offset); } last_offset=offset; diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index edd2bde..1118f37 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -1,6 +1,7 @@ #include "TranslationUnit.h" #include "SourceLocation.h" #include "Tokens.h" +#include "Utility.h" #include #include @@ -91,9 +92,7 @@ unsigned clang::TranslationUnit::DefaultFlags() { } clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, unsigned line_number, unsigned column) { - auto cxstr=clang_getTranslationUnitSpelling(cx_tu); - std::string path=clang_getCString(cxstr); - clang_disposeString(cxstr); + auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column); return results; @@ -110,9 +109,7 @@ std::vector clang::TranslationUnit::get_diagnostics() { } std::unique_ptr clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) { - auto cxstr=clang_getTranslationUnitSpelling(cx_tu); - std::string path=clang_getCString(cxstr); - clang_disposeString(cxstr); + auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); clang::SourceLocation start_location(cx_tu, path, start_offset); clang::SourceLocation end_location(cx_tu, path, end_offset); clang::SourceRange range(start_location, end_location); diff --git a/src/Utility.cc b/src/Utility.cc new file mode 100644 index 0000000..7f1c266 --- /dev/null +++ b/src/Utility.cc @@ -0,0 +1,10 @@ +#include "Utility.h" + +std::string clang::to_string(CXString cx_string) { + std::string string; + if(cx_string.data!=NULL) { + string=clang_getCString(cx_string); + clang_disposeString(cx_string); + } + return string; +} \ No newline at end of file diff --git a/src/Utility.h b/src/Utility.h new file mode 100644 index 0000000..6f7e319 --- /dev/null +++ b/src/Utility.h @@ -0,0 +1,10 @@ +#ifndef UTILITY_H_ +#define UTILITY_H_ +#include +#include + +namespace clang { + std::string to_string(CXString cx_string); +} + +#endif // UTILITY_H_ \ No newline at end of file diff --git a/src/clangmm.h b/src/clangmm.h index 3ffe3ae..b1abb81 100644 --- a/src/clangmm.h +++ b/src/clangmm.h @@ -13,4 +13,5 @@ #include "Index.h" #include "Cursor.h" #include "Diagnostic.h" +#include "Utility.h" #endif // CLANGMM_H_ From a17de316ed5caef3a0317cda2f1ff0b00cd3a4c6 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 26 Jul 2015 13:21:01 +0200 Subject: [PATCH 20/21] Minor fix. --- src/Diagnostic.cc | 2 +- src/Tokens.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 0fdc199..177dabb 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -13,7 +13,7 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos unsigned start_offset=start_location.get_offset(); clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location)); if(tokens.size()==1) { - offsets=std::pair(start_offset, tokens[0].offsets.second); + offsets=std::pair(start_offset, tokens.begin()->offsets.second); } } diff --git a/src/Tokens.cc b/src/Tokens.cc index 5b79198..7600cc8 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -6,7 +6,7 @@ using namespace std; clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); cx_cursors.clear(); - cx_cursors.reserve(num_tokens); + cx_cursors.resize(num_tokens); clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); for (unsigned i = 0; i < num_tokens; i++) { emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i])); From 573a759b33b3636fdb86a0006789d8ec4c1843b9 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 27 Jul 2015 10:46:19 +0200 Subject: [PATCH 21/21] Slight change to Tokens::get_similar_token_offsets. --- src/Tokens.cc | 14 ++++++-------- src/Tokens.h | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Tokens.cc b/src/Tokens.cc index 7600cc8..f2fd316 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -20,15 +20,13 @@ clang::Tokens::~Tokens() { //This works across TranslationUnits! However, to get rename refactoring to work, //one have to open all the files that might include a similar token //Similar tokens defined as tokens with equal referenced cursors. -std::vector > clang::Tokens::get_similar_token_offsets(clang::Token& token) { +std::vector > clang::Tokens::get_similar_token_offsets(const std::string &usr) { std::vector > offsets; - auto referenced=token.get_cursor().get_referenced(); - if(referenced) { - auto referenced_usr=referenced.get_usr(); - for(auto &a_token: *this) { - auto a_referenced=a_token.get_cursor().get_referenced(); - if(a_referenced && referenced_usr==a_referenced.get_usr() && token.get_spelling()==a_token.get_spelling()) { - offsets.emplace_back(a_token.offsets); + for(auto &token: *this) { + if(token.get_kind()==clang::Token_Identifier) { + auto referenced=token.get_cursor().get_referenced(); + if(referenced && usr==referenced.get_usr()) { + offsets.emplace_back(token.offsets); } } } diff --git a/src/Tokens.h b/src/Tokens.h index 96366cd..7f1ea1b 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -13,7 +13,7 @@ namespace clang { Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); public: ~Tokens(); - std::vector > get_similar_token_offsets(clang::Token& token); + std::vector > get_similar_token_offsets(const std::string &usr); std::vector > get_cxx_methods(); private: CXToken *cx_tokens;