Browse Source

Slight optimization of Tokens::get_similar_token_offsets

merge-requests/37/head
eidheim 8 years ago
parent
commit
c42e39685e
  1. 21
      src/Cursor.cc
  2. 1
      src/Cursor.h
  3. 4
      src/Tokens.cc
  4. 2
      src/Tokens.h

21
src/Cursor.cc

@ -18,6 +18,27 @@ clangmm::Cursor::Kind clangmm::Cursor::get_kind() const {
return static_cast<Kind>(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));
}

1
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;

4
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<std::pair<clangmm::Offset, clangmm::Offset> > clangmm::Tokens::get_similar_token_offsets(const std::string &spelling,
std::vector<std::pair<clangmm::Offset, clangmm::Offset> > clangmm::Tokens::get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling,
const std::unordered_set<std::string> &usrs) {
std::vector<std::pair<Offset, Offset> > 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)

2
src/Tokens.h

@ -14,7 +14,7 @@ namespace clangmm {
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range);
public:
~Tokens();
std::vector<std::pair<clangmm::Offset, clangmm::Offset> > get_similar_token_offsets(const std::string &spelling,
std::vector<std::pair<clangmm::Offset, clangmm::Offset> > get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling,
const std::unordered_set<std::string> &usrs);
private:
CXToken *cx_tokens;

Loading…
Cancel
Save