Browse Source

Types are now received, even for auto. Some commented out testing code included, sorry.

merge-requests/37/head
eidheim 11 years ago
parent
commit
30e9f3755a
  1. 2
      src/Cursor.h
  2. 2
      src/Token.h
  3. 61
      src/Tokens.cc
  4. 1
      src/Tokens.h

2
src/Cursor.h

@ -174,12 +174,14 @@ namespace clang {
class Cursor { class Cursor {
public: public:
Cursor() {}
Cursor(TranslationUnit *tu, SourceLocation *source_location); Cursor(TranslationUnit *tu, SourceLocation *source_location);
const CursorKind kind(); const CursorKind kind();
private: private:
CXCursor cursor_; CXCursor cursor_;
friend SourceRange; friend SourceRange;
friend SourceLocation; friend SourceLocation;
friend Tokens;
}; };
} // namespace clang } // namespace clang
#endif // CURSOR_H_ #endif // CURSOR_H_

2
src/Token.h

@ -19,7 +19,7 @@ namespace clang {
std::string get_token_spelling(TranslationUnit *tu); std::string get_token_spelling(TranslationUnit *tu);
SourceLocation get_source_location(TranslationUnit *tu); SourceLocation get_source_location(TranslationUnit *tu);
SourceRange get_source_range(TranslationUnit *tu); SourceRange get_source_range(TranslationUnit *tu);
std::string type;
private: private:
explicit Token(const CXToken &token); explicit Token(const CXToken &token);
friend SourceRange; friend SourceRange;

61
src/Tokens.cc

@ -1,4 +1,7 @@
#include "Tokens.h" #include "Tokens.h"
#include <iostream>
using namespace std;
clang::Tokens::Tokens(clang::TranslationUnit *tu, clang::SourceRange *range) { clang::Tokens::Tokens(clang::TranslationUnit *tu, clang::SourceRange *range) {
clang_tokenize(tu->tu_, clang_tokenize(tu->tu_,
range->range_, range->range_,
@ -19,3 +22,61 @@ clang::Tokens::~Tokens() {
std::vector<clang::Token>& clang::Tokens::tokens() { std::vector<clang::Token>& clang::Tokens::tokens() {
return tks; return tks;
} }
std::vector<clang::Cursor> clang::Tokens::get_token_cursors(clang::TranslationUnit *tu) {
std::vector<CXToken> clang_tokens;
for(auto& token: tks)
clang_tokens.emplace_back(token.token_);
std::vector<CXCursor> clang_cursors(clang_tokens.size());
clang_annotateTokens(tu->tu_, clang_tokens.data(), clang_tokens.size(), &clang_cursors.data()[0]);
std::vector<clang::Cursor> cursors;
for(auto clang_cursor: clang_cursors) {
cursors.emplace_back();
cursors.back().cursor_=clang_cursor;
}
for(int c=0;c<tks.size();c++) {
auto referenced=clang_getCursorReferenced(cursors[c].cursor_);
if(!clang_Cursor_isNull(referenced)) {
//auto type=clang_getCursorType(referenced);
auto type=clang_getCanonicalType(clang_getCursorType(cursors[c].cursor_));
//auto cursor=clang_getTypeDeclaration(type);
//tks[c].type=clang_getCString(clang_getCursorSpelling(cursor));
//auto type=clang_getCursorType(referenced);
tks[c].type=clang_getCString(clang_getTypeSpelling(type));
}
//Testing:
/*if(tks[c].get_token_spelling(tu)=="text_view") {
cout << tks[c].get_token_spelling(tu) << endl;
auto kind=clang_getCursorKind(cursors[c].cursor_);
cout << " " << kind << endl;
cout << " Decl: " << clang_isDeclaration(kind) << endl;
cout << " Attr: " << clang_isAttribute(kind) << endl;
cout << " Ref: " << clang_isReference(kind) << endl;
cout << " Expr: " << clang_isExpression(kind) << endl;
auto referenced=clang_getCursorReferenced(cursors[c].cursor_);
if(!clang_Cursor_isNull(referenced)) {
cout << " " << clang_getCursorKind(referenced) << endl;
clang::Cursor referenced_cursor;
referenced_cursor.cursor_=referenced;
auto range=clang::SourceRange(&referenced_cursor);
auto location=clang::SourceLocation(&range, true);
std::string path;
unsigned line, column, offset;
location.get_location_info(&path, &line, &column, &offset);
cout << " start: " << path << ", " << line << ", " << column << endl;
location=clang::SourceLocation(&range, false);
location.get_location_info(&path, &line, &column, &offset);
cout << " start: " << path << ", " << line << ", " << column << endl;
auto type=clang_getCursorType(referenced);
cout << " " << clang_getCString(clang_getTypeSpelling(type)) << endl;
}
}*/
}
return cursors;
}

1
src/Tokens.h

@ -10,6 +10,7 @@ namespace clang {
Tokens(TranslationUnit *tu, SourceRange *range); Tokens(TranslationUnit *tu, SourceRange *range);
~Tokens(); ~Tokens();
std::vector<Token>& tokens(); std::vector<Token>& tokens();
std::vector<clang::Cursor> get_token_cursors(clang::TranslationUnit *tu);
private: private:
std::vector<clang::Token> tks; std::vector<clang::Token> tks;
CXToken *tokens_; CXToken *tokens_;

Loading…
Cancel
Save