Browse Source

Tokens and Token cleanup

merge-requests/37/head
eidheim 8 years ago
parent
commit
f34b2c604d
  1. 2
      src/Diagnostic.cc
  2. 2
      src/Token.cc
  3. 3
      src/Token.h
  4. 12
      src/Tokens.cc
  5. 4
      src/Tokens.h

2
src/Diagnostic.cc

@ -13,7 +13,7 @@ clangmm::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagn
auto start_offset=start_location.get_offset();
Tokens tokens(cx_tu, SourceRange(start_location, start_location));
if(tokens.size()==1)
offsets={start_offset, tokens.begin()->offsets.second};
offsets={start_offset, tokens.begin()->get_source_range().get_offsets().second};
unsigned num_fix_its=clang_getDiagnosticNumFixIts(cx_diagnostic);
for(unsigned c=0;c<num_fix_its;c++) {

2
src/Token.cc

@ -35,7 +35,7 @@ bool clangmm::Token::is_identifier() const {
return true;
}
else if(token_kind==clangmm::Token::Kind::Punctuation && cursor.is_valid_kind()) {
auto referenced=get_cursor().get_referenced();
auto referenced=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)

3
src/Token.h

@ -20,7 +20,7 @@ namespace clangmm {
};
private:
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) {}
public:
Kind get_kind() const;
std::string get_spelling() const;
@ -42,7 +42,6 @@ namespace clangmm {
CXTranslationUnit &cx_tu;
CXToken& cx_token;
CXCursor& cx_cursor;
std::pair<clangmm::Offset, clangmm::Offset> offsets;
};
} // namespace clangmm
#endif // TOKEN_H_

12
src/Tokens.cc

@ -4,9 +4,10 @@
#include <cstring>
clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) {
unsigned num_tokens;
clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens);
cx_cursors.resize(num_tokens);
clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data());
cx_cursors=std::unique_ptr<CXCursor[]>(new CXCursor[num_tokens]); // To avoid allocation with initialization
clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.get());
for (unsigned i = 0; i < num_tokens; i++) {
if(cx_cursors[i].kind==CXCursor_DeclRefExpr) { // Temporary fix to a libclang bug
auto real_cursor=clang_getCursor(cx_tu, clang_getTokenLocation(cx_tu, cx_tokens[i]));
@ -16,7 +17,8 @@ clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_
else if(cx_cursors[i].kind==CXCursor_ClassDecl || cx_cursors[i].kind==CXCursor_StructDecl) {
Token token(cx_tu, cx_tokens[i], cx_cursors[i]);
auto cursor=token.get_cursor();
if(token.offsets.second!=cursor.get_source_range().get_offsets().second && token.offsets.first!=cursor.get_source_location().get_offset() && token.is_identifier()) {
auto token_offsets=token.get_source_range().get_offsets();
if(token_offsets.second!=cursor.get_source_range().get_offsets().second && token_offsets.first!=cursor.get_source_location().get_offset() && token.is_identifier()) {
class VisitorData {
public:
std::string path;
@ -24,7 +26,7 @@ clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_
std::pair<Offset, Offset> offsets;
CXCursor found_cursor;
};
VisitorData data{token.get_source_location().get_path(), false, token.offsets, clang_getNullCursor()};
VisitorData data{token.get_source_location().get_path(), false, token_offsets, clang_getNullCursor()};
auto translation_unit_cursor = clang_getTranslationUnitCursor(cx_tu);
clang_visitChildren(translation_unit_cursor, [](CXCursor cx_cursor, CXCursor cx_parent, CXClientData data_) {
auto data=static_cast<VisitorData*>(data_);
@ -69,7 +71,7 @@ std::vector<std::pair<clangmm::Offset, clangmm::Offset> > clangmm::Tokens::get_s
auto referenced_usrs=referenced.get_all_usr_extended();
for(auto &usr: referenced_usrs) {
if(usrs.count(usr)) {
offsets.emplace_back(token.offsets);
offsets.emplace_back(token.get_source_range().get_offsets());
break;
}
}

4
src/Tokens.h

@ -5,6 +5,7 @@
#include "Token.h"
#include <unordered_set>
#include <vector>
#include <memory>
namespace clangmm {
class Tokens : public std::vector<clangmm::Token> {
@ -17,8 +18,7 @@ namespace clangmm {
const std::unordered_set<std::string> &usrs);
private:
CXToken *cx_tokens;
unsigned num_tokens;
std::vector<CXCursor> cx_cursors;
std::unique_ptr<CXCursor[]> cx_cursors;
CXTranslationUnit& cx_tu;
};
} // namespace clangmm

Loading…
Cancel
Save