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