Browse Source

Improved Tokens::get_similar_token_offsets and added Cursor::get_usr_extended

merge-requests/37/head
eidheim 9 years ago
parent
commit
98741cff18
  1. 28
      src/Cursor.cc
  2. 2
      src/Cursor.h
  3. 10
      src/Tokens.cc
  4. 4
      src/Tokens.h

28
src/Cursor.cc

@ -42,6 +42,34 @@ std::string clangmm::Cursor::get_usr() const {
return to_string(clang_getCursorUSR(cx_cursor));
}
std::string clangmm::Cursor::get_usr_extended() const {
if(!is_valid_kind())
return std::string();
const auto token_spelling=[](const std::string &spelling) -> std::string {
for(size_t i=0;i<spelling.size();++i) {
if(spelling[i]=='<' || spelling[i]=='(')
return spelling.substr(0, i);
}
return spelling;
};
std::string usr=token_spelling(get_spelling());
auto cursor=get_semantic_parent();
Kind kind;
while((kind=cursor.get_kind())!=Kind::TranslationUnit && cursor.is_valid_kind()) {
if(kind==Kind::CXXMethod || kind==Kind::FunctionDecl || kind==Kind::FunctionTemplate ||
kind==Kind::Constructor || kind==Kind::Destructor) {
auto canonical=get_canonical();
auto location=canonical.get_source_location();
auto offset=location.get_offset();
return std::to_string(offset.line)+':'+std::to_string(offset.index)+':'+location.get_path();
}
usr+=':'+token_spelling(cursor.get_spelling());
cursor=cursor.get_semantic_parent();
}
return usr;
}
clangmm::Cursor clangmm::Cursor::get_referenced() const {
return Cursor(clang_getCursorReferenced(cx_cursor));
}

2
src/Cursor.h

@ -195,6 +195,8 @@ namespace clangmm {
std::string get_spelling() const;
std::string get_display_name() const;
std::string get_usr() const;
/// Improved usr that is also template and argument invariant
std::string get_usr_extended() const;
Cursor get_referenced() const;
Cursor get_canonical() const;
Cursor get_definition() const;

10
src/Tokens.cc

@ -18,17 +18,13 @@ clangmm::Tokens::~Tokens() {
clang_disposeTokens(cx_tu, cx_tokens, size());
}
//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<std::pair<clangmm::Offset, clangmm::Offset> > clangmm::Tokens::get_similar_token_offsets(Cursor::Kind kind,
const std::string &spelling,
const std::string &usr) {
//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, const std::string &usr) {
std::vector<std::pair<Offset, Offset> > offsets;
for(auto &token: *this) {
if(token.is_identifier()) {
auto referenced=token.get_cursor().get_referenced();
if(referenced && kind==referenced.get_kind() && spelling==token.get_spelling() && usr==referenced.get_usr())
if(referenced && spelling==token.get_spelling() && usr==referenced.get_usr_extended())
offsets.emplace_back(token.offsets);
}
}

4
src/Tokens.h

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

Loading…
Cancel
Save