From ec06c9716f181e1d27e5a00b537062ca8a927759 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 19:44:40 +0200 Subject: [PATCH] Got rid of warning messages while compiling, and some minor cleanups. --- src/CompileCommand.cc | 4 ++-- src/CompletionString.cc | 4 ++-- src/CompletionString.h | 2 +- src/Cursor.cc | 13 ++++++------- src/Cursor.h | 3 ++- src/Token.cc | 4 ++-- src/Token.h | 6 +++--- src/Tokens.cc | 11 ++++++----- tests/Token_H_Test.cc | 4 ++-- 9 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/CompileCommand.cc b/src/CompileCommand.cc index 3d98579..0cb8971 100644 --- a/src/CompileCommand.cc +++ b/src/CompileCommand.cc @@ -5,7 +5,7 @@ std::string clang::CompileCommand:: get_command() { std::string res; unsigned N = clang_CompileCommand_getNumArgs(cx_command); - for (int i = 0; i < N; i++) { + for (unsigned i = 0; i < N; i++) { res += clang_getCString(clang_CompileCommand_getArg(cx_command, i)); } return res; @@ -15,7 +15,7 @@ std::vector clang::CompileCommand:: get_command_as_args() { unsigned N = clang_CompileCommand_getNumArgs(cx_command); std::vector res(N); - for (int i = 0; i < N; i++) { + for (unsigned i = 0; i < N; i++) { res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i)); } return res; diff --git a/src/CompletionString.cc b/src/CompletionString.cc index 1c0cb88..2d291c1 100644 --- a/src/CompletionString.cc +++ b/src/CompletionString.cc @@ -7,13 +7,13 @@ bool clang::CompletionString::available() { return clang_getCompletionAvailability(cx_str) == CXAvailability_Available; } -int clang::CompletionString::get_num_chunks() { +unsigned clang::CompletionString::get_num_chunks() { return clang_getNumCompletionChunks(cx_str); } std::vector clang::CompletionString::get_chunks() { std::vector res; - for (size_t i = 0; i < get_num_chunks(); i++) { + for (unsigned i = 0; i < get_num_chunks(); i++) { auto cxstr=clang_getCompletionChunkText(cx_str, i); res.emplace_back(clang_getCString(cxstr), static_cast (clang_getCompletionChunkKind(cx_str, i))); clang_disposeString(cxstr); diff --git a/src/CompletionString.h b/src/CompletionString.h index 43d953b..a9d9830 100644 --- a/src/CompletionString.h +++ b/src/CompletionString.h @@ -32,7 +32,7 @@ namespace clang { bool available(); std::vector get_chunks(); std::string get_brief_comments(); - int get_num_chunks(); + unsigned get_num_chunks(); CXCompletionString cx_str; }; diff --git a/src/Cursor.cc b/src/Cursor.cc index 4a7c992..d5be2a2 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -19,13 +19,12 @@ std::string clang::Cursor::get_usr() const { return USR; } -std::string clang::Cursor::get_referenced_usr() const { - auto referenced=clang_getCursorReferenced(cx_cursor); - if(!clang_Cursor_isNull(referenced)) { - return Cursor(referenced).get_usr(); - } - else - return ""; +clang::Cursor clang::Cursor::get_referenced() const { + return Cursor(clang_getCursorReferenced(cx_cursor)); +} + +clang::Cursor::operator bool() const { + return !clang_Cursor_isNull(cx_cursor); } bool clang::Cursor::operator==(const Cursor& rhs) const { diff --git a/src/Cursor.h b/src/Cursor.h index ae85534..7c01ca7 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -181,7 +181,8 @@ namespace clang { SourceLocation get_source_location() const; SourceRange get_source_range() const; std::string get_usr() const; - std::string get_referenced_usr() const; + Cursor get_referenced() const; + operator bool() const; bool operator==(const Cursor& rhs) const; CXCursor cx_cursor; }; diff --git a/src/Token.cc b/src/Token.cc index 30f2c54..310a130 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -15,12 +15,12 @@ clang::SourceRange clang::Token::get_source_range() { return SourceRange(clang_getTokenExtent(cx_tu, cx_token)); } // returns a string description of this tokens kind -std::string clang::Token::get_token_spelling() { +std::string clang::Token::get_spelling() { CXString s = clang_getTokenSpelling(cx_tu, cx_token); return std::string(clang_getCString(s)); } -const clang::TokenKind clang::Token::kind() { +const clang::TokenKind clang::Token::get_kind() { return (TokenKind) clang_getTokenKind(cx_token); } diff --git a/src/Token.h b/src/Token.h index 9fcb7e3..6bb4b64 100644 --- a/src/Token.h +++ b/src/Token.h @@ -19,19 +19,19 @@ namespace clang { public: 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()) {}; - const TokenKind kind(); - std::string get_token_spelling(); + const TokenKind get_kind(); + std::string get_spelling(); SourceLocation get_source_location(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); - SourceRange get_source_range(); std::string get_brief_comments(); CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; std::pair offsets; private: + SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 2d3f0e1..a597557 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -3,12 +3,11 @@ using namespace std; clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { - clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); cx_cursors.clear(); cx_cursors.reserve(num_tokens); clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); - for (int i = 0; i < num_tokens; i++) { + for (unsigned i = 0; i < num_tokens; i++) { emplace_back(cx_tu, cx_tokens[i], cx_cursors[i]); } } @@ -22,10 +21,12 @@ clang::Tokens::~Tokens() { //Similar tokens defined as tokens with equal referenced cursors. std::vector > clang::Tokens::get_similar_token_offsets(clang::Token& token) { std::vector > offsets; - auto referenced_usr=token.get_cursor().get_referenced_usr(); - if(referenced_usr!="") { + auto referenced=token.get_cursor().get_referenced(); + if(referenced) { + auto referenced_usr=referenced.get_usr(); for(auto &a_token: *this) { - if(referenced_usr==a_token.get_cursor().get_referenced_usr() && token.get_token_spelling()==a_token.get_token_spelling()) { + auto a_referenced=a_token.get_cursor().get_referenced(); + if(a_referenced && referenced_usr==a_referenced.get_usr() && token.get_spelling()==a_token.get_spelling()) { offsets.emplace_back(a_token.offsets); } } diff --git a/tests/Token_H_Test.cc b/tests/Token_H_Test.cc index 38198c2..6325062 100644 --- a/tests/Token_H_Test.cc +++ b/tests/Token_H_Test.cc @@ -12,8 +12,8 @@ BOOST_AUTO_TEST_CASE(token) { auto tokens=tu.get_tokens(0, 113); BOOST_CHECK(tokens->size() == 32); - BOOST_CHECK((*tokens)[1].kind() == clang::TokenKind::Token_Identifier); + BOOST_CHECK((*tokens)[1].get_kind() == clang::TokenKind::Token_Identifier); - std::string str = (*tokens)[28].get_token_spelling(); + std::string str = (*tokens)[28].get_spelling(); BOOST_CHECK(str == "return"); }