diff --git a/src/Cursor.cc b/src/Cursor.cc index d5cbd9c..5c0caef 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -74,15 +74,15 @@ bool clang::Cursor::operator==(const Cursor& rhs) const { return clang_equalCursors(cx_cursor, rhs.cx_cursor); } -bool clang::Cursor::has_type_description() { +bool clang::Cursor::is_valid_kind() const { auto referenced=clang_getCursorReferenced(cx_cursor); if(clang_Cursor_isNull(referenced)) return false; - auto type=clang_getCursorType(referenced); - return type.kind!=0; + auto kind=get_kind(); + return kind>Kind::UnexposedDecl && (kindKind::LastInvalid); } -std::string clang::Cursor::get_type_description() { +std::string clang::Cursor::get_type_description() const { std::string spelling; auto referenced=clang_getCursorReferenced(cx_cursor); if(!clang_Cursor_isNull(referenced)) { @@ -112,10 +112,13 @@ std::string clang::Cursor::get_type_description() { #endif } + if(spelling.empty()) + return get_spelling(); + return spelling; } -std::string clang::Cursor::get_brief_comments() { +std::string clang::Cursor::get_brief_comments() const { std::string comment_string; auto referenced=get_referenced(); if(referenced) { diff --git a/src/Cursor.h b/src/Cursor.h index 6d14003..b04ee6d 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -203,9 +203,9 @@ namespace clang { operator bool() const; bool operator==(const Cursor& rhs) const; - bool has_type_description(); - std::string get_type_description(); - std::string get_brief_comments(); + bool is_valid_kind() const; + std::string get_type_description() const; + std::string get_brief_comments() const; CXCursor cx_cursor; }; diff --git a/src/Token.cc b/src/Token.cc index 7dc793e..94e39ee 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -7,19 +7,40 @@ // returns gets an source location for this token objekt // based on the translationunit given -clang::SourceLocation clang::Token::get_source_location() { +clang::SourceLocation clang::Token::get_source_location() const { return SourceLocation(clang_getTokenLocation(cx_tu, cx_token)); } // returns a sourcerange that covers this token -clang::SourceRange clang::Token::get_source_range() { +clang::SourceRange clang::Token::get_source_range() const { return SourceRange(clang_getTokenExtent(cx_tu, cx_token)); } // returns a string description of this tokens kind -std::string clang::Token::get_spelling() { +std::string clang::Token::get_spelling() const { return to_string(clang_getTokenSpelling(cx_tu, cx_token)); } -clang::Token::Kind clang::Token::get_kind() { +clang::Token::Kind clang::Token::get_kind() const { return static_cast(clang_getTokenKind(cx_token)); } + +bool clang::Token::is_identifier() const { + auto token_kind=get_kind(); + auto cursor=get_cursor(); + if(token_kind==clang::Token::Kind::Identifier && cursor.is_valid_kind()) + return true; + else if(token_kind==clang::Token::Kind::Keyword && cursor.is_valid_kind()) { + auto spelling=get_spelling(); + if(spelling=="operator" || (spelling=="bool" && get_cursor().get_spelling()=="operator bool")) + return true; + } + else if(token_kind==clang::Token::Kind::Punctuation && cursor.is_valid_kind()) { + auto referenced=get_cursor().get_referenced(); + if(referenced) { + auto referenced_kind=referenced.get_kind(); + if(referenced_kind==Cursor::Kind::FunctionDecl || referenced_kind==Cursor::Kind::CXXMethod || referenced_kind==Cursor::Kind::Constructor) + return true; + } + } + return false; +} diff --git a/src/Token.h b/src/Token.h index f22c964..bc24fe1 100644 --- a/src/Token.h +++ b/src/Token.h @@ -21,11 +21,13 @@ namespace clang { Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {}; public: - Kind get_kind(); - std::string get_spelling(); - SourceLocation get_source_location(); - SourceRange get_source_range(); - clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} + Kind get_kind() const; + std::string get_spelling() const; + SourceLocation get_source_location() const; + SourceRange get_source_range() const; + clang::Cursor get_cursor() const {return clang::Cursor(cx_cursor);} + + bool is_identifier() const; CXTranslationUnit &cx_tu; CXToken& cx_token; diff --git a/src/Tokens.cc b/src/Tokens.cc index ba8cc9f..d954773 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -26,7 +26,7 @@ std::vector > clang::Tokens::get_similar const std::string &usr) { std::vector > offsets; for(auto &token: *this) { - if(token.get_kind()==Token::Kind::Identifier) { + 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()) offsets.emplace_back(token.offsets);