diff --git a/src/Cursor.cc b/src/Cursor.cc index 62a222d..4066d78 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -18,6 +18,27 @@ clangmm::Cursor::Kind clangmm::Cursor::get_kind() const { return static_cast(clang_getCursorKind(cx_cursor)); } +bool clangmm::Cursor::is_similar_kind(Kind kind, Kind other_kind) { + auto is_function_or_method=[](Kind kind) { + if(kind==Kind::FunctionDecl || kind==Kind::CXXMethod || kind==Kind::FunctionTemplate) + return true; + return false; + }; + auto is_class_or_struct=[](Kind kind) { + if(kind==Kind::ClassDecl || kind==Kind::StructDecl || kind==Kind::ClassTemplate || + kind==Cursor::Kind::Constructor || kind==Cursor::Kind::Destructor || kind==Cursor::Kind::FunctionTemplate) + return true; + return false; + }; + if(kind==Kind::FunctionTemplate) + return is_function_or_method(other_kind) || is_class_or_struct(other_kind); + if(is_function_or_method(kind)) + return is_function_or_method(other_kind); + if(is_class_or_struct(kind)) + return is_class_or_struct(other_kind); + return kind==other_kind; +} + clangmm::Cursor::Type clangmm::Cursor::get_type() const { return Type(clang_getCursorType(cx_cursor)); } diff --git a/src/Cursor.h b/src/Cursor.h index 82ab0c0..53ebb5a 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -191,6 +191,7 @@ namespace clangmm { Cursor() { cx_cursor=clang_getNullCursor(); } Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {} Kind get_kind() const; + static bool is_similar_kind(Kind kind, Kind other_kind); Type get_type() const; SourceLocation get_source_location() const; SourceRange get_source_range() const; diff --git a/src/Tokens.cc b/src/Tokens.cc index fcd5224..1431e3f 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -55,13 +55,13 @@ clangmm::Tokens::~Tokens() { } //This works across TranslationUnits. Similar tokens defined as tokens with equal canonical cursors. -std::vector > clangmm::Tokens::get_similar_token_offsets(const std::string &spelling, +std::vector > clangmm::Tokens::get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling, const std::unordered_set &usrs) { std::vector > offsets; for(auto &token: *this) { if(token.is_identifier()) { auto referenced=token.get_cursor().get_referenced(); - if(referenced) { + if(referenced && Cursor::is_similar_kind(referenced.get_kind(), kind)) { bool equal_spelling=false; auto cx_string=clang_getTokenSpelling(cx_tu, token.cx_token); if(cx_string.data) diff --git a/src/Tokens.h b/src/Tokens.h index 6f905cf..9afc650 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -14,7 +14,7 @@ namespace clangmm { Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); public: ~Tokens(); - std::vector > get_similar_token_offsets(const std::string &spelling, + std::vector > get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling, const std::unordered_set &usrs); private: CXToken *cx_tokens;