Browse Source

Made some functions const, renamed Cursor::has_type_description to Cursor::is_valid_kind, and added Token::is_identifier

merge-requests/37/head
eidheim 9 years ago
parent
commit
1ef04246b8
  1. 13
      src/Cursor.cc
  2. 6
      src/Cursor.h
  3. 29
      src/Token.cc
  4. 12
      src/Token.h
  5. 2
      src/Tokens.cc

13
src/Cursor.cc

@ -74,15 +74,15 @@ bool clang::Cursor::operator==(const Cursor& rhs) const {
return clang_equalCursors(cx_cursor, rhs.cx_cursor); 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); auto referenced=clang_getCursorReferenced(cx_cursor);
if(clang_Cursor_isNull(referenced)) if(clang_Cursor_isNull(referenced))
return false; return false;
auto type=clang_getCursorType(referenced); auto kind=get_kind();
return type.kind!=0; return kind>Kind::UnexposedDecl && (kind<Kind::FirstInvalid || kind>Kind::LastInvalid);
} }
std::string clang::Cursor::get_type_description() { std::string clang::Cursor::get_type_description() const {
std::string spelling; std::string spelling;
auto referenced=clang_getCursorReferenced(cx_cursor); auto referenced=clang_getCursorReferenced(cx_cursor);
if(!clang_Cursor_isNull(referenced)) { if(!clang_Cursor_isNull(referenced)) {
@ -112,10 +112,13 @@ std::string clang::Cursor::get_type_description() {
#endif #endif
} }
if(spelling.empty())
return get_spelling();
return spelling; return spelling;
} }
std::string clang::Cursor::get_brief_comments() { std::string clang::Cursor::get_brief_comments() const {
std::string comment_string; std::string comment_string;
auto referenced=get_referenced(); auto referenced=get_referenced();
if(referenced) { if(referenced) {

6
src/Cursor.h

@ -203,9 +203,9 @@ namespace clang {
operator bool() const; operator bool() const;
bool operator==(const Cursor& rhs) const; bool operator==(const Cursor& rhs) const;
bool has_type_description(); bool is_valid_kind() const;
std::string get_type_description(); std::string get_type_description() const;
std::string get_brief_comments(); std::string get_brief_comments() const;
CXCursor cx_cursor; CXCursor cx_cursor;
}; };

29
src/Token.cc

@ -7,19 +7,40 @@
// returns gets an source location for this token objekt // returns gets an source location for this token objekt
// based on the translationunit given // 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)); return SourceLocation(clang_getTokenLocation(cx_tu, cx_token));
} }
// returns a sourcerange that covers this 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)); return SourceRange(clang_getTokenExtent(cx_tu, cx_token));
} }
// returns a string description of this tokens kind // 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)); 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<Kind>(clang_getTokenKind(cx_token)); return static_cast<Kind>(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;
}

12
src/Token.h

@ -21,11 +21,13 @@ namespace clang {
Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): 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()) {}; cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {};
public: public:
Kind get_kind(); Kind get_kind() const;
std::string get_spelling(); std::string get_spelling() const;
SourceLocation get_source_location(); SourceLocation get_source_location() const;
SourceRange get_source_range(); SourceRange get_source_range() const;
clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} clang::Cursor get_cursor() const {return clang::Cursor(cx_cursor);}
bool is_identifier() const;
CXTranslationUnit &cx_tu; CXTranslationUnit &cx_tu;
CXToken& cx_token; CXToken& cx_token;

2
src/Tokens.cc

@ -26,7 +26,7 @@ std::vector<std::pair<clang::Offset, clang::Offset> > clang::Tokens::get_similar
const std::string &usr) { const std::string &usr) {
std::vector<std::pair<Offset, Offset> > offsets; std::vector<std::pair<Offset, Offset> > offsets;
for(auto &token: *this) { for(auto &token: *this) {
if(token.get_kind()==Token::Kind::Identifier) { if(token.is_identifier()) {
auto referenced=token.get_cursor().get_referenced(); auto referenced=token.get_cursor().get_referenced();
if(referenced && kind==referenced.get_kind() && spelling==token.get_spelling() && usr==referenced.get_usr()) if(referenced && kind==referenced.get_kind() && spelling==token.get_spelling() && usr==referenced.get_usr())
offsets.emplace_back(token.offsets); offsets.emplace_back(token.offsets);

Loading…
Cancel
Save