mirror of https://gitlab.com/cppit/libclangmm
38 changed files with 407 additions and 494 deletions
@ -1,24 +1,21 @@ |
|||||||
#ifndef CODECOMPLETERESULTS_H_ |
#ifndef CODECOMPLETERESULTS_H_ |
||||||
#define CODECOMPLETERESULTS_H_ |
#define CODECOMPLETERESULTS_H_ |
||||||
#include <clang-c/Index.h> |
#include <clang-c/Index.h> |
||||||
#include "TranslationUnit.h" |
#include <map> |
||||||
|
#include "CompletionString.h" |
||||||
|
|
||||||
namespace clang { |
namespace clang { |
||||||
class CompletionString; |
|
||||||
|
|
||||||
class CodeCompleteResults { |
class CodeCompleteResults { |
||||||
public: |
friend class TranslationUnit; |
||||||
CodeCompleteResults(TranslationUnit *tu, |
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, |
||||||
const std::string &file_name, |
|
||||||
const std::map<std::string, std::string> &buffers, |
const std::map<std::string, std::string> &buffers, |
||||||
int line_num, |
unsigned line_num, unsigned column); |
||||||
int column); |
public: |
||||||
~CodeCompleteResults(); |
~CodeCompleteResults(); |
||||||
CompletionString get(int index); |
CompletionString get(unsigned index); |
||||||
int size(); |
unsigned size(); |
||||||
|
|
||||||
private: |
CXCodeCompleteResults *cx_results; |
||||||
CXCodeCompleteResults *results_; |
|
||||||
}; |
}; |
||||||
} // namespace clang
|
} // namespace clang
|
||||||
#endif // CODECOMPLETERESULTS_H_
|
#endif // CODECOMPLETERESULTS_H_
|
||||||
|
|||||||
@ -1,27 +1,23 @@ |
|||||||
#include "CompileCommand.h" |
#include "CompileCommand.h" |
||||||
#include "CompileCommands.h" |
#include "CompileCommands.h" |
||||||
|
#include "Utility.h" |
||||||
clang::CompileCommand:: |
|
||||||
CompileCommand(int nth, clang::CompileCommands *commands) { |
|
||||||
command_ = clang_CompileCommands_getCommand(commands->commands_, nth); |
|
||||||
} |
|
||||||
|
|
||||||
std::string clang::CompileCommand:: |
std::string clang::CompileCommand:: |
||||||
get_command() { |
get_command() { |
||||||
std::string res; |
std::string res; |
||||||
unsigned N = clang_CompileCommand_getNumArgs(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(command_, i)); |
res += clang::to_string(clang_CompileCommand_getArg(cx_command, i)); |
||||||
} |
} |
||||||
return res; |
return res; |
||||||
} |
} |
||||||
|
|
||||||
std::vector<std::string> clang::CompileCommand:: |
std::vector<std::string> clang::CompileCommand:: |
||||||
get_command_as_args() { |
get_command_as_args() { |
||||||
unsigned N = clang_CompileCommand_getNumArgs(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(command_, i)); |
res[i] = clang::to_string(clang_CompileCommand_getArg(cx_command, i)); |
||||||
} |
} |
||||||
return res; |
return res; |
||||||
} |
} |
||||||
|
|||||||
@ -1,16 +1,17 @@ |
|||||||
#ifndef COMPILECOMMAND_H_ |
#ifndef COMPILECOMMAND_H_ |
||||||
#define COMPILECOMMAND_H_ |
#define COMPILECOMMAND_H_ |
||||||
#include "CompilationDatabase.h" |
#include <clang-c/CXCompilationDatabase.h> |
||||||
#include <vector> |
#include <vector> |
||||||
|
#include <string> |
||||||
|
|
||||||
namespace clang { |
namespace clang { |
||||||
class CompileCommand { |
class CompileCommand { |
||||||
public: |
public: |
||||||
CompileCommand(int nth, CompileCommands *commands); |
CompileCommand(const CXCompileCommand& cx_command) : cx_command(cx_command) {}; |
||||||
std::string get_command(); |
std::string get_command(); |
||||||
std::vector<std::string> get_command_as_args(); |
std::vector<std::string> get_command_as_args(); |
||||||
private: |
|
||||||
CXCompileCommand command_; |
CXCompileCommand cx_command; |
||||||
}; |
}; |
||||||
} |
} |
||||||
#endif // COMPILECOMMAND_H_
|
#endif // COMPILECOMMAND_H_
|
||||||
|
|||||||
@ -1,24 +1,24 @@ |
|||||||
#include "CompileCommands.h" |
#include "CompileCommands.h" |
||||||
|
|
||||||
clang::CompileCommands:: |
clang::CompileCommands:: |
||||||
CompileCommands(const std::string &filename, CompilationDatabase *db) { |
CompileCommands(const std::string &filename, CompilationDatabase &db) { |
||||||
commands_ = |
cx_commands = |
||||||
clang_CompilationDatabase_getCompileCommands(db->db_, filename.c_str()); |
clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str()); |
||||||
if(clang_CompileCommands_getSize(commands_)==0) |
if(clang_CompileCommands_getSize(cx_commands)==0) |
||||||
commands_ = clang_CompilationDatabase_getAllCompileCommands(db->db_); |
cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db); |
||||||
} |
} |
||||||
|
|
||||||
clang::CompileCommands:: |
clang::CompileCommands:: |
||||||
~CompileCommands() { |
~CompileCommands() { |
||||||
clang_CompileCommands_dispose(commands_); |
clang_CompileCommands_dispose(cx_commands); |
||||||
} |
} |
||||||
|
|
||||||
std::vector<clang::CompileCommand> clang::CompileCommands:: |
std::vector<clang::CompileCommand> clang::CompileCommands:: |
||||||
get_commands() { |
get_commands() { |
||||||
unsigned N = clang_CompileCommands_getSize(commands_); |
unsigned N = clang_CompileCommands_getSize(cx_commands); |
||||||
std::vector<clang::CompileCommand> res; |
std::vector<clang::CompileCommand> res; |
||||||
for (unsigned i = 0; i < N; i++) { |
for (unsigned i = 0; i < N; i++) { |
||||||
res.emplace_back(clang::CompileCommand(i, this)); |
res.emplace_back(clang_CompileCommands_getCommand(cx_commands, i)); |
||||||
} |
} |
||||||
return res; |
return res; |
||||||
} |
} |
||||||
|
|||||||
@ -1,10 +1,30 @@ |
|||||||
#include "Cursor.h" |
#include "Cursor.h" |
||||||
|
#include "Utility.h" |
||||||
|
|
||||||
const clang::CursorKind clang::Cursor::kind() { |
const clang::CursorKind clang::Cursor::get_kind() { |
||||||
return (CursorKind) clang_getCursorKind(this->cursor_); |
return (CursorKind) clang_getCursorKind(this->cx_cursor); |
||||||
} |
} |
||||||
|
|
||||||
clang::Cursor:: |
clang::SourceLocation clang::Cursor::get_source_location() const { |
||||||
Cursor(clang::TranslationUnit *tu, clang::SourceLocation *source_location) { |
return SourceLocation(clang_getCursorLocation(cx_cursor)); |
||||||
cursor_ = clang_getCursor(tu->tu_, source_location->location_); |
|
||||||
} |
} |
||||||
|
|
||||||
|
clang::SourceRange clang::Cursor::get_source_range() const { |
||||||
|
return SourceRange(clang_getCursorExtent(cx_cursor)); |
||||||
|
} |
||||||
|
|
||||||
|
std::string clang::Cursor::get_usr() const { |
||||||
|
return clang::to_string(clang_getCursorUSR(cx_cursor)); |
||||||
|
} |
||||||
|
|
||||||
|
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 { |
||||||
|
return get_usr()==rhs.get_usr(); |
||||||
|
} |
||||||
@ -1,16 +1,12 @@ |
|||||||
#ifndef INDEX_H_ |
#ifndef INDEX_H_ |
||||||
#define INDEX_H_ |
#define INDEX_H_ |
||||||
|
|
||||||
#include <clang-c/Index.h> |
#include <clang-c/Index.h> |
||||||
|
|
||||||
namespace clang { |
namespace clang { |
||||||
class TranslationUnit; |
|
||||||
class Index { |
class Index { |
||||||
public: |
public: |
||||||
Index(int excludeDeclarationsFromPCH, int displayDiagnostics); |
Index(int excludeDeclarationsFromPCH, int displayDiagnostics); |
||||||
private: |
CXIndex cx_index; |
||||||
CXIndex index_; |
|
||||||
friend TranslationUnit; |
|
||||||
}; |
}; |
||||||
} // namespace clang
|
} // namespace clang
|
||||||
#endif // INDEX_H_
|
#endif // INDEX_H_
|
||||||
|
|||||||
@ -1,59 +1,34 @@ |
|||||||
#include "SourceLocation.h" |
#include "SourceLocation.h" |
||||||
|
#include "Utility.h" |
||||||
|
|
||||||
// // // // // // // //
|
// // // // // // // //
|
||||||
// SourceLocation //
|
// SourceLocation //
|
||||||
// // // // // // // //
|
// // // // // // // //
|
||||||
clang::SourceLocation:: |
clang::SourceLocation::SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset) { |
||||||
SourceLocation(clang::TranslationUnit *tu, |
CXFile file = clang_getFile(tu, filepath.c_str()); |
||||||
const std::string &filename, |
cx_location = clang_getLocationForOffset(tu, file, offset); |
||||||
int line_number, |
|
||||||
int line_offset) { |
|
||||||
CXFile file = clang_getFile(tu->tu_, |
|
||||||
filename.c_str()); |
|
||||||
location_ = clang_getLocation(tu->tu_, |
|
||||||
file, |
|
||||||
line_number, |
|
||||||
line_offset); |
|
||||||
} |
|
||||||
|
|
||||||
clang::SourceLocation:: |
|
||||||
SourceLocation(Cursor *cursor) { |
|
||||||
location_ = clang_getCursorLocation(cursor->cursor_); |
|
||||||
} |
|
||||||
|
|
||||||
clang::SourceLocation:: |
|
||||||
SourceLocation(clang::SourceRange *range, bool start) { |
|
||||||
location_ = start ? clang_getRangeStart(range->range_) : |
|
||||||
clang_getRangeEnd(range->range_); |
|
||||||
} |
} |
||||||
|
|
||||||
clang::SourceLocation:: |
std::string clang::SourceLocation::get_path() { |
||||||
SourceLocation(TranslationUnit *tu, |
std::string path; |
||||||
Token *token) { |
get_data(&path, NULL, NULL, NULL); |
||||||
location_ = clang_getTokenLocation(tu->tu_, |
return path; |
||||||
token->token_); |
|
||||||
} |
} |
||||||
|
unsigned clang::SourceLocation::get_offset() { |
||||||
clang::SourceLocation:: |
unsigned offset; |
||||||
SourceLocation(clang::TranslationUnit *tu, |
get_data(NULL, NULL, NULL, &offset); |
||||||
const std::string &filepath, |
return offset; |
||||||
int offset) { |
|
||||||
CXFile file = clang_getFile(tu->tu_, |
|
||||||
filepath.c_str()); |
|
||||||
location_ = clang_getLocationForOffset(tu->tu_, |
|
||||||
file, |
|
||||||
offset); |
|
||||||
} |
} |
||||||
|
|
||||||
void clang::SourceLocation:: |
void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) { |
||||||
get_location_info(std::string* path, |
if(path==nullptr) |
||||||
unsigned *line, |
clang_getExpansionLocation(cx_location, NULL, line, column, offset); |
||||||
unsigned *column, |
else { |
||||||
unsigned *offset) { |
CXFile file; |
||||||
CXFile file; |
clang_getExpansionLocation(cx_location, &file, line, column, offset); |
||||||
clang_getExpansionLocation(location_, &file, line, column, offset); |
if (file!=NULL) { |
||||||
if (path != NULL && file!=NULL) { |
*path=clang::to_string(clang_getFileName(file)); |
||||||
path->operator=(((clang_getCString((clang_getFileName(file)))))); |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
|||||||
@ -1,15 +1,14 @@ |
|||||||
#include "SourceRange.h" |
#include "SourceRange.h" |
||||||
|
|
||||||
clang::SourceRange:: |
clang::SourceRange:: |
||||||
SourceRange(clang::TranslationUnit *tu, clang::Token *token) { |
SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { |
||||||
range_ = clang_getTokenExtent(tu->tu_, token->token_); |
cx_range = clang_getRange(start.cx_location, end.cx_location); |
||||||
} |
} |
||||||
|
|
||||||
clang::SourceRange:: |
std::pair<unsigned, unsigned> clang::SourceRange::get_offsets() { |
||||||
SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) { |
SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range)); |
||||||
range_ = clang_getRange(start->location_, end->location_); |
std::pair<unsigned, unsigned> offsets; |
||||||
} |
offsets.first=start.get_offset(); |
||||||
|
offsets.second=end.get_offset(); |
||||||
clang::SourceRange::SourceRange(Cursor *cursor) { |
return offsets; |
||||||
range_ = clang_getCursorExtent(cursor->cursor_); |
} |
||||||
} |
|
||||||
@ -1,23 +1,17 @@ |
|||||||
#ifndef SOURCERANGE_H_ |
#ifndef SOURCERANGE_H_ |
||||||
#define SOURCERANGE_H_ |
#define SOURCERANGE_H_ |
||||||
#include "TranslationUnit.h" |
#include <clang-c/Index.h> |
||||||
#include "Token.h" |
#include "SourceLocation.h" |
||||||
#include "Cursor.h" |
#include <string> |
||||||
|
#include <utility> |
||||||
|
|
||||||
namespace clang { |
namespace clang {
|
||||||
class SourceRange { |
class SourceRange { |
||||||
public: |
public: |
||||||
SourceRange() {} |
SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {} |
||||||
SourceRange(TranslationUnit *tu, Token *token); |
SourceRange(SourceLocation &start, SourceLocation &end); |
||||||
SourceRange(SourceLocation *start, |
std::pair<unsigned, unsigned> get_offsets(); |
||||||
SourceLocation *end); |
CXSourceRange cx_range; |
||||||
explicit SourceRange(Cursor *cursor); |
|
||||||
|
|
||||||
private: |
|
||||||
CXSourceRange range_; |
|
||||||
friend Tokens; |
|
||||||
friend SourceLocation; |
|
||||||
friend Diagnostic; |
|
||||||
}; |
}; |
||||||
} // namespace clang
|
} // namespace clang
|
||||||
#endif // SOURCERANGE_H_
|
#endif // SOURCERANGE_H_
|
||||||
|
|||||||
@ -1,32 +1,63 @@ |
|||||||
#include "Token.h" |
#include "Token.h" |
||||||
|
#include "Utility.h" |
||||||
|
|
||||||
// // // // //
|
// // // // //
|
||||||
// Token //
|
// Token //
|
||||||
// // // // //
|
// // // // //
|
||||||
|
|
||||||
// clang::Token instansiates an token
|
|
||||||
clang::Token::Token(const CXToken &token) : |
|
||||||
token_(token) { |
|
||||||
} |
|
||||||
|
|
||||||
// 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:: |
clang::SourceLocation clang::Token::get_source_location() { |
||||||
get_source_location(clang::TranslationUnit *tu) { |
return SourceLocation(clang_getTokenLocation(cx_tu, cx_token)); |
||||||
return SourceLocation(tu, this); |
|
||||||
} |
} |
||||||
|
|
||||||
// returns a sourcerange that covers this token
|
// returns a sourcerange that covers this token
|
||||||
clang::SourceRange clang::Token:: |
clang::SourceRange clang::Token::get_source_range() { |
||||||
get_source_range(clang::TranslationUnit *tu) { |
return SourceRange(clang_getTokenExtent(cx_tu, cx_token)); |
||||||
return SourceRange(tu, this); |
|
||||||
} |
} |
||||||
// returns a string description of this tokens kind
|
// returns a string description of this tokens kind
|
||||||
std::string clang::Token::get_token_spelling(clang::TranslationUnit *tu) { |
std::string clang::Token::get_spelling() { |
||||||
CXString s = clang_getTokenSpelling(tu->tu_, token_); |
return clang::to_string(clang_getTokenSpelling(cx_tu, cx_token)); |
||||||
return std::string(clang_getCString(s)); |
} |
||||||
|
|
||||||
|
const clang::TokenKind clang::Token::get_kind() { |
||||||
|
return (TokenKind) clang_getTokenKind(cx_token); |
||||||
|
} |
||||||
|
|
||||||
|
//TODO: Is there a way to optimise this?
|
||||||
|
bool clang::Token::has_type() { |
||||||
|
auto referenced=clang_getCursorReferenced(cx_cursor); |
||||||
|
if(clang_Cursor_isNull(referenced)) |
||||||
|
return false; |
||||||
|
auto type=clang_getCursorType(referenced); |
||||||
|
auto spelling=clang::to_string(clang_getTypeSpelling(type)); |
||||||
|
return spelling!=""; |
||||||
|
} |
||||||
|
|
||||||
|
std::string clang::Token::get_type() { |
||||||
|
std::string spelling; |
||||||
|
auto referenced=clang_getCursorReferenced(cx_cursor); |
||||||
|
if(!clang_Cursor_isNull(referenced)) { |
||||||
|
auto type=clang_getCursorType(referenced); |
||||||
|
spelling=clang::to_string(clang_getTypeSpelling(type)); |
||||||
|
std::string auto_end=""; |
||||||
|
//TODO fix const auto
|
||||||
|
if((spelling.size()>=4 && spelling.substr(0, 4)=="auto")) { |
||||||
|
auto_end=spelling.substr(4); |
||||||
|
auto type=clang_getCanonicalType(clang_getCursorType(cx_cursor)); |
||||||
|
spelling=clang::to_string(clang_getTypeSpelling(type)); |
||||||
|
if(spelling.find(" ")==std::string::npos) |
||||||
|
spelling+=auto_end; |
||||||
|
} |
||||||
|
} |
||||||
|
return spelling; |
||||||
} |
} |
||||||
|
|
||||||
const clang::TokenKind clang::Token::kind() { |
std::string clang::Token::get_brief_comments() { |
||||||
return (TokenKind) clang_getTokenKind(token_); |
std::string comment_string; |
||||||
|
auto referenced=get_cursor().get_referenced(); |
||||||
|
if(referenced) { |
||||||
|
comment_string=clang::to_string(clang_Cursor_getBriefCommentText(referenced.cx_cursor)); |
||||||
|
} |
||||||
|
return comment_string; |
||||||
} |
} |
||||||
|
|||||||
@ -1,136 +1,69 @@ |
|||||||
#include "Tokens.h" |
#include "Tokens.h" |
||||||
|
#include "Utility.h" |
||||||
#include <iostream> |
#include <iostream> |
||||||
using namespace std; |
using namespace std; |
||||||
|
|
||||||
clang::Tokens::Tokens(clang::TranslationUnit *tu, clang::SourceRange *range): tu(*tu) { |
clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { |
||||||
clang_tokenize(tu->tu_, |
clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); |
||||||
range->range_, |
cx_cursors.clear(); |
||||||
&tokens_, |
cx_cursors.resize(num_tokens); |
||||||
&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++) { |
||||||
push_back(clang::Token(tokens_[i])); |
emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i])); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
clang::Tokens::~Tokens() { |
clang::Tokens::~Tokens() { |
||||||
clang_disposeTokens(tu.tu_, tokens_, size()); |
clang_disposeTokens(cx_tu, cx_tokens, size()); |
||||||
} |
} |
||||||
|
|
||||||
void clang::Tokens::update_types(clang::TranslationUnit *tu) { |
//This works across TranslationUnits! However, to get rename refactoring to work,
|
||||||
clang_cursors.clear(); |
//one have to open all the files that might include a similar token
|
||||||
clang_cursors.reserve(size()); |
//Similar tokens defined as tokens with equal referenced cursors.
|
||||||
clang_annotateTokens(tu->tu_, tokens_, size(), clang_cursors.data()); |
std::vector<std::pair<unsigned, unsigned> > clang::Tokens::get_similar_token_offsets(const std::string &usr) { |
||||||
|
std::vector<std::pair<unsigned, unsigned> > offsets; |
||||||
for(size_t c=0;c<size();c++) { |
for(auto &token: *this) { |
||||||
auto referenced=clang_getCursorReferenced(clang_cursors[c]); |
if(token.get_kind()==clang::Token_Identifier) { |
||||||
if(!clang_Cursor_isNull(referenced)) { |
auto referenced=token.get_cursor().get_referenced(); |
||||||
auto type=clang_getCursorType(referenced); |
if(referenced && usr==referenced.get_usr()) { |
||||||
auto cxstr=clang_getTypeSpelling(type); |
offsets.emplace_back(token.offsets); |
||||||
std::string spelling=clang_getCString(cxstr); |
|
||||||
clang_disposeString(cxstr); |
|
||||||
std::string auto_end=""; |
|
||||||
//TODO fix const auto
|
|
||||||
if((spelling.size()>=4 && spelling.substr(0, 4)=="auto")) { |
|
||||||
auto_end=spelling.substr(4); |
|
||||||
auto type=clang_getCanonicalType(clang_getCursorType(clang_cursors[c])); |
|
||||||
auto cxstr=clang_getTypeSpelling(type); |
|
||||||
spelling=clang_getCString(cxstr); |
|
||||||
clang_disposeString(cxstr); |
|
||||||
if(spelling.find(" ")==std::string::npos) |
|
||||||
spelling+=auto_end; |
|
||||||
} |
} |
||||||
|
|
||||||
(*this)[c].type=spelling; |
|
||||||
//std::cout << clang_getCString(clang_getTypeSpelling(type)) << ": " << type.kind << endl;
|
|
||||||
////auto cursor=clang_getTypeDeclaration(type);
|
|
||||||
////tks[c].type=clang_getCString(clang_getCursorSpelling(cursor));
|
|
||||||
////auto type=clang_getCursorType(referenced);
|
|
||||||
|
|
||||||
} |
} |
||||||
//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 offsets; |
||||||
} |
} |
||||||
|
|
||||||
std::string clang::Tokens::get_brief_comments(size_t cursor_id) { |
std::vector<std::pair<std::string, unsigned> > clang::Tokens::get_cxx_methods() { |
||||||
std::string comment_string; |
std::vector<std::pair<std::string, unsigned> > methods; |
||||||
auto referenced=clang_getCursorReferenced(clang_cursors[cursor_id]); |
long last_offset=-1; |
||||||
auto comment=clang_Cursor_getParsedComment(referenced); |
for(auto &token: *this) { |
||||||
if(clang_Comment_getKind(comment)==CXComment_FullComment) { |
if(token.get_kind()==clang::Token_Identifier) { |
||||||
size_t para_c=0; |
auto cursor=token.get_cursor(); |
||||||
for(unsigned c=0;c<clang_Comment_getNumChildren(comment);c++) { |
auto kind=cursor.get_kind(); |
||||||
auto child_comment=clang_Comment_getChild(comment, c); |
if(kind==clang::CursorKind::CXXMethod || kind==clang::CursorKind::Constructor || kind==clang::CursorKind::Destructor) { |
||||||
if(clang_Comment_getKind(child_comment)==CXComment_Paragraph) { |
auto offset=cursor.get_source_location().get_offset(); |
||||||
para_c++; |
if(offset!=last_offset) { |
||||||
if(para_c>=2) |
std::string method; |
||||||
break; |
if(kind==clang::CursorKind::CXXMethod) { |
||||||
for(unsigned c=0;c<clang_Comment_getNumChildren(child_comment);c++) { |
auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor)); |
||||||
auto grandchild_comment=clang_Comment_getChild(child_comment, c); |
method+=clang::to_string(clang_getTypeSpelling(type)); |
||||||
if(clang_Comment_getKind(grandchild_comment)==CXComment_Text) { |
auto pos=method.find(" "); |
||||||
auto cxstr=clang_TextComment_getText(grandchild_comment); |
if(pos!=std::string::npos) |
||||||
comment_string+=clang_getCString(cxstr); |
method.erase(pos, 1); |
||||||
comment_string+="\n"; |
method+=" "; |
||||||
clang_disposeString(cxstr); |
|
||||||
size_t dot_position=comment_string.find("."); |
|
||||||
if(dot_position!=std::string::npos) |
|
||||||
return comment_string.substr(0, dot_position); |
|
||||||
} |
|
||||||
if(clang_Comment_getKind(grandchild_comment)==CXComment_InlineCommand) { |
|
||||||
auto cxstr=clang_InlineCommandComment_getCommandName(grandchild_comment); |
|
||||||
if(comment_string.size()>0) |
|
||||||
comment_string.pop_back(); |
|
||||||
if(clang_InlineCommandComment_getNumArgs(grandchild_comment)==0) |
|
||||||
comment_string+=clang_getCString(cxstr); |
|
||||||
clang_disposeString(cxstr); |
|
||||||
for(unsigned arg_c=0;arg_c<clang_InlineCommandComment_getNumArgs(grandchild_comment);arg_c++) { |
|
||||||
auto cxstr=clang_InlineCommandComment_getArgText(grandchild_comment, arg_c); |
|
||||||
if(cxstr.data!=NULL) { |
|
||||||
if(arg_c>0) |
|
||||||
comment_string+=" "; |
|
||||||
comment_string+=clang_getCString(cxstr); |
|
||||||
clang_disposeString(cxstr); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
|
clang::Cursor parent(clang_getCursorSemanticParent(cursor.cx_cursor)); |
||||||
|
method+=clang::to_string(clang_getCursorDisplayName(parent.cx_cursor)); |
||||||
|
|
||||||
|
method+="::"; |
||||||
|
|
||||||
|
method+=clang::to_string(clang_getCursorDisplayName(cursor.cx_cursor)); |
||||||
|
methods.emplace_back(method, offset); |
||||||
} |
} |
||||||
|
last_offset=offset; |
||||||
} |
} |
||||||
/*cout << " " << clang_Comment_getKind(child_comment) << ", children: " << clang_Comment_getNumChildren(child_comment) << endl;
|
|
||||||
auto cxstr=clang_FullComment_getAsHTML(child_comment); |
|
||||||
cout << " " << clang_getCString(cxstr) << endl; |
|
||||||
clang_disposeString(cxstr);*/ |
|
||||||
} |
} |
||||||
while(comment_string.size()>0 && (comment_string.back()=='\n' || comment_string.back()==' ')) |
|
||||||
comment_string.pop_back(); |
|
||||||
} |
} |
||||||
|
return methods; |
||||||
return comment_string; |
} |
||||||
} |
|
||||||
|
|||||||
@ -1,21 +1,25 @@ |
|||||||
#ifndef TOKENS_H_ |
#ifndef TOKENS_H_ |
||||||
#define TOKENS_H_ |
#define TOKENS_H_ |
||||||
#include "TranslationUnit.h" |
#include <clang-c/Index.h> |
||||||
#include "SourceRange.h" |
#include "SourceRange.h" |
||||||
#include "Token.h" |
#include "Token.h" |
||||||
|
#include <unordered_map> |
||||||
|
#include <vector> |
||||||
|
|
||||||
namespace clang { |
namespace clang { |
||||||
class Tokens : public std::vector<clang::Token> { |
class Tokens : public std::vector<clang::Token> { |
||||||
|
friend class TranslationUnit; |
||||||
|
friend class Diagnostic; |
||||||
|
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); |
||||||
public: |
public: |
||||||
Tokens(TranslationUnit *tu, SourceRange *range); |
|
||||||
~Tokens(); |
~Tokens(); |
||||||
void update_types(clang::TranslationUnit *tu); |
std::vector<std::pair<unsigned, unsigned> > get_similar_token_offsets(const std::string &usr); |
||||||
std::string get_brief_comments(size_t cursor_id); |
std::vector<std::pair<std::string, unsigned> > get_cxx_methods(); |
||||||
private: |
private: |
||||||
CXToken *tokens_; |
CXToken *cx_tokens; |
||||||
unsigned num_tokens_; |
unsigned num_tokens; |
||||||
std::vector<CXCursor> clang_cursors; |
std::vector<CXCursor> cx_cursors; |
||||||
TranslationUnit& tu; |
CXTranslationUnit& cx_tu; |
||||||
}; |
}; |
||||||
} // namespace clang
|
} // namespace clang
|
||||||
#endif // TOKENS_H_
|
#endif // TOKENS_H_
|
||||||
|
|||||||
@ -0,0 +1,10 @@ |
|||||||
|
#include "Utility.h" |
||||||
|
|
||||||
|
std::string clang::to_string(CXString cx_string) { |
||||||
|
std::string string; |
||||||
|
if(cx_string.data!=NULL) { |
||||||
|
string=clang_getCString(cx_string); |
||||||
|
clang_disposeString(cx_string); |
||||||
|
} |
||||||
|
return string; |
||||||
|
} |
||||||
@ -0,0 +1,10 @@ |
|||||||
|
#ifndef UTILITY_H_ |
||||||
|
#define UTILITY_H_ |
||||||
|
#include <clang-c/Index.h> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
namespace clang { |
||||||
|
std::string to_string(CXString cx_string); |
||||||
|
} |
||||||
|
|
||||||
|
#endif // UTILITY_H_
|
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# Windows Install Guide # |
||||||
|
## Requirements ## |
||||||
|
|
||||||
|
```bash |
||||||
|
* chocolatey [website](http://chocolatey.org) |
||||||
|
``` |
||||||
|
|
||||||
|
##Preperation ## |
||||||
|
First you install some dependencies |
||||||
|
|
||||||
|
```bash |
||||||
|
PS: choco install cmake make nmake |
||||||
|
``` |
||||||
Loading…
Reference in new issue