Browse Source

Got rid of warning messages while compiling, and some minor cleanups.

merge-requests/37/head
eidheim 11 years ago
parent
commit
ec06c9716f
  1. 4
      src/CompileCommand.cc
  2. 4
      src/CompletionString.cc
  3. 2
      src/CompletionString.h
  4. 13
      src/Cursor.cc
  5. 3
      src/Cursor.h
  6. 4
      src/Token.cc
  7. 6
      src/Token.h
  8. 11
      src/Tokens.cc
  9. 4
      tests/Token_H_Test.cc

4
src/CompileCommand.cc

@ -5,7 +5,7 @@ std::string clang::CompileCommand::
get_command() { get_command() {
std::string res; std::string res;
unsigned N = clang_CompileCommand_getNumArgs(cx_command); 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)); res += clang_getCString(clang_CompileCommand_getArg(cx_command, i));
} }
return res; return res;
@ -15,7 +15,7 @@ std::vector<std::string> clang::CompileCommand::
get_command_as_args() { get_command_as_args() {
unsigned N = clang_CompileCommand_getNumArgs(cx_command); unsigned N = clang_CompileCommand_getNumArgs(cx_command);
std::vector<std::string> res(N); std::vector<std::string> 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)); res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i));
} }
return res; return res;

4
src/CompletionString.cc

@ -7,13 +7,13 @@ bool clang::CompletionString::available() {
return clang_getCompletionAvailability(cx_str) == CXAvailability_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); return clang_getNumCompletionChunks(cx_str);
} }
std::vector<clang::CompletionChunk> clang::CompletionString::get_chunks() { std::vector<clang::CompletionChunk> clang::CompletionString::get_chunks() {
std::vector<clang::CompletionChunk> res; std::vector<clang::CompletionChunk> 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); auto cxstr=clang_getCompletionChunkText(cx_str, i);
res.emplace_back(clang_getCString(cxstr), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(cx_str, i))); res.emplace_back(clang_getCString(cxstr), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(cx_str, i)));
clang_disposeString(cxstr); clang_disposeString(cxstr);

2
src/CompletionString.h

@ -32,7 +32,7 @@ namespace clang {
bool available(); bool available();
std::vector<CompletionChunk> get_chunks(); std::vector<CompletionChunk> get_chunks();
std::string get_brief_comments(); std::string get_brief_comments();
int get_num_chunks(); unsigned get_num_chunks();
CXCompletionString cx_str; CXCompletionString cx_str;
}; };

13
src/Cursor.cc

@ -19,13 +19,12 @@ std::string clang::Cursor::get_usr() const {
return USR; return USR;
} }
std::string clang::Cursor::get_referenced_usr() const { clang::Cursor clang::Cursor::get_referenced() const {
auto referenced=clang_getCursorReferenced(cx_cursor); return Cursor(clang_getCursorReferenced(cx_cursor));
if(!clang_Cursor_isNull(referenced)) { }
return Cursor(referenced).get_usr();
} clang::Cursor::operator bool() const {
else return !clang_Cursor_isNull(cx_cursor);
return "";
} }
bool clang::Cursor::operator==(const Cursor& rhs) const { bool clang::Cursor::operator==(const Cursor& rhs) const {

3
src/Cursor.h

@ -181,7 +181,8 @@ namespace clang {
SourceLocation get_source_location() const; SourceLocation get_source_location() const;
SourceRange get_source_range() const; SourceRange get_source_range() const;
std::string get_usr() 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; bool operator==(const Cursor& rhs) const;
CXCursor cx_cursor; CXCursor cx_cursor;
}; };

4
src/Token.cc

@ -15,12 +15,12 @@ clang::SourceRange clang::Token::get_source_range() {
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_token_spelling() { std::string clang::Token::get_spelling() {
CXString s = clang_getTokenSpelling(cx_tu, cx_token); CXString s = clang_getTokenSpelling(cx_tu, cx_token);
return std::string(clang_getCString(s)); 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); return (TokenKind) clang_getTokenKind(cx_token);
} }

6
src/Token.h

@ -19,19 +19,19 @@ namespace clang {
public: public:
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()) {};
const TokenKind kind(); const TokenKind get_kind();
std::string get_token_spelling(); std::string get_spelling();
SourceLocation get_source_location(); SourceLocation get_source_location();
clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);}
bool has_type(); bool has_type();
std::string get_type(); std::string get_type();
SourceRange get_source_range();
std::string get_brief_comments(); std::string get_brief_comments();
CXTranslationUnit &cx_tu; CXTranslationUnit &cx_tu;
CXToken& cx_token; CXToken& cx_token;
CXCursor& cx_cursor; CXCursor& cx_cursor;
std::pair<unsigned, unsigned> offsets; std::pair<unsigned, unsigned> offsets;
private: private:
SourceRange get_source_range();
}; };
} // namespace clang } // namespace clang
#endif // TOKEN_H_ #endif // TOKEN_H_

11
src/Tokens.cc

@ -3,12 +3,11 @@
using namespace std; using namespace std;
clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) {
clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens);
cx_cursors.clear(); cx_cursors.clear();
cx_cursors.reserve(num_tokens); cx_cursors.reserve(num_tokens);
clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); 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]); 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. //Similar tokens defined as tokens with equal referenced cursors.
std::vector<std::pair<unsigned, unsigned> > clang::Tokens::get_similar_token_offsets(clang::Token& token) { std::vector<std::pair<unsigned, unsigned> > clang::Tokens::get_similar_token_offsets(clang::Token& token) {
std::vector<std::pair<unsigned, unsigned> > offsets; std::vector<std::pair<unsigned, unsigned> > offsets;
auto referenced_usr=token.get_cursor().get_referenced_usr(); auto referenced=token.get_cursor().get_referenced();
if(referenced_usr!="") { if(referenced) {
auto referenced_usr=referenced.get_usr();
for(auto &a_token: *this) { 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); offsets.emplace_back(a_token.offsets);
} }
} }

4
tests/Token_H_Test.cc

@ -12,8 +12,8 @@ BOOST_AUTO_TEST_CASE(token) {
auto tokens=tu.get_tokens(0, 113); auto tokens=tu.get_tokens(0, 113);
BOOST_CHECK(tokens->size() == 32); 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"); BOOST_CHECK(str == "return");
} }

Loading…
Cancel
Save