From d041071ebafcfbc1baafef0c09f24cf882ea0550 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 15 Jul 2015 19:06:02 +0200 Subject: [PATCH 1/2] 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 a92970fd690a3b7d548a93abb68b78d904b015d7 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 11:13:02 +0200 Subject: [PATCH 2/2] 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;