Browse Source

Merge branch 'master' of http://github.com/eidheim/libclangmm

merge-requests/37/head
Jørgen Lien Sellæg 11 years ago
parent
commit
24de478b57
  1. 2
      src/CMakeLists.txt
  2. 5
      src/CompileCommand.cc
  3. 19
      src/CompletionString.cc
  4. 4
      src/CompletionString.h
  5. 6
      src/Cursor.cc
  6. 7
      src/Diagnostic.cc
  7. 5
      src/SourceLocation.cc
  8. 68
      src/Token.cc
  9. 30
      src/Tokens.cc
  10. 2
      src/Tokens.h
  11. 9
      src/TranslationUnit.cc
  12. 10
      src/Utility.cc
  13. 10
      src/Utility.h
  14. 1
      src/clangmm.h

2
src/CMakeLists.txt

@ -26,6 +26,7 @@ set(header_files
Tokens.h
TranslationUnit.h
Diagnostic.h
Utility.h
)
set(cc_files
CodeCompleteResults.cc
@ -41,6 +42,7 @@ set(cc_files
Tokens.cc
TranslationUnit.cc
Diagnostic.cc
Utility.cc
)
add_library(${project_name} SHARED ${header_files} ${cc_files})

5
src/CompileCommand.cc

@ -1,12 +1,13 @@
#include "CompileCommand.h"
#include "CompileCommands.h"
#include "Utility.h"
std::string clang::CompileCommand::
get_command() {
std::string res;
unsigned N = clang_CompileCommand_getNumArgs(cx_command);
for (unsigned i = 0; i < N; i++) {
res += clang_getCString(clang_CompileCommand_getArg(cx_command, i));
res += clang::to_string(clang_CompileCommand_getArg(cx_command, i));
}
return res;
}
@ -16,7 +17,7 @@ get_command_as_args() {
unsigned N = clang_CompileCommand_getNumArgs(cx_command);
std::vector<std::string> res(N);
for (unsigned i = 0; i < N; i++) {
res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i));
res[i] = clang::to_string(clang_CompileCommand_getArg(cx_command, i));
}
return res;
}

19
src/CompletionString.cc

@ -1,34 +1,27 @@
#include "CompletionString.h"
#include "Utility.h"
clang::CompletionString::
CompletionString(const CXCompletionString &cx_str) : cx_str(cx_str) {}
CompletionString(const CXCompletionString &cx_completion_sting) : cx_completion_sting(cx_completion_sting) {}
bool clang::CompletionString::available() {
return clang_getCompletionAvailability(cx_str) == CXAvailability_Available;
return clang_getCompletionAvailability(cx_completion_sting) == CXAvailability_Available;
}
unsigned clang::CompletionString::get_num_chunks() {
return clang_getNumCompletionChunks(cx_str);
return clang_getNumCompletionChunks(cx_completion_sting);
}
std::vector<clang::CompletionChunk> clang::CompletionString::get_chunks() {
std::vector<clang::CompletionChunk> res;
for (unsigned i = 0; i < get_num_chunks(); i++) {
auto cxstr=clang_getCompletionChunkText(cx_str, i);
res.emplace_back(clang_getCString(cxstr), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(cx_str, i)));
clang_disposeString(cxstr);
res.emplace_back(clang::to_string(clang_getCompletionChunkText(cx_completion_sting, i)), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(cx_completion_sting, i)));
}
return res;
}
std::string clang::CompletionString::get_brief_comments() {
std::string brief_comments;
auto cxstr=clang_getCompletionBriefComment(cx_str);
if(cxstr.data!=NULL) {
brief_comments=clang_getCString(cxstr);
clang_disposeString(cxstr);
}
return brief_comments;
return clang::to_string(clang_getCompletionBriefComment(cx_completion_sting));
}
clang::CompletionChunk::

4
src/CompletionString.h

@ -28,13 +28,13 @@ namespace clang {
class CompletionString {
public:
explicit CompletionString(const CXCompletionString &cx_str);
explicit CompletionString(const CXCompletionString &cx_completion_sting);
bool available();
std::vector<CompletionChunk> get_chunks();
std::string get_brief_comments();
unsigned get_num_chunks();
CXCompletionString cx_str;
CXCompletionString cx_completion_sting;
};
} // namespace clang
#endif // COMPLETIONSTRING_H_

6
src/Cursor.cc

@ -1,4 +1,5 @@
#include "Cursor.h"
#include "Utility.h"
const clang::CursorKind clang::Cursor::get_kind() {
return (CursorKind) clang_getCursorKind(this->cx_cursor);
@ -13,10 +14,7 @@ clang::SourceRange clang::Cursor::get_source_range() const {
}
std::string clang::Cursor::get_usr() const {
auto cxstr=clang_getCursorUSR(cx_cursor);
std::string USR=clang_getCString(cxstr);
clang_disposeString(cxstr);
return USR;
return clang::to_string(clang_getCursorUSR(cx_cursor));
}
clang::Cursor clang::Cursor::get_referenced() const {

7
src/Diagnostic.cc

@ -1,20 +1,19 @@
#include "Diagnostic.h"
#include "SourceLocation.h"
#include "Tokens.h"
#include "Utility.h"
clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) {
severity=clang_getDiagnosticSeverity(cx_diagnostic);
severity_spelling=get_severity_spelling(severity);
auto cxstr=clang_getDiagnosticSpelling(cx_diagnostic);
spelling=clang_getCString(cxstr);
clang_disposeString(cxstr);
spelling=clang::to_string(clang_getDiagnosticSpelling(cx_diagnostic));
clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic));
path=start_location.get_path();
unsigned start_offset=start_location.get_offset();
clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location));
if(tokens.size()==1) {
offsets=std::pair<unsigned, unsigned>(start_offset, tokens[0].offsets.second);
offsets=std::pair<unsigned, unsigned>(start_offset, tokens.begin()->offsets.second);
}
}

5
src/SourceLocation.cc

@ -1,4 +1,5 @@
#include "SourceLocation.h"
#include "Utility.h"
// // // // // // // //
// SourceLocation //
@ -26,9 +27,7 @@ void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned
CXFile file;
clang_getExpansionLocation(cx_location, &file, line, column, offset);
if (file!=NULL) {
auto cxstr=clang_getFileName(file);
*path=clang_getCString(cxstr);
clang_disposeString(cxstr);
*path=clang::to_string(clang_getFileName(file));
}
}
}

68
src/Token.cc

@ -1,4 +1,5 @@
#include "Token.h"
#include "Utility.h"
// // // // //
// Token //
@ -16,8 +17,7 @@ clang::SourceRange clang::Token::get_source_range() {
}
// returns a string description of this tokens kind
std::string clang::Token::get_spelling() {
CXString s = clang_getTokenSpelling(cx_tu, cx_token);
return std::string(clang_getCString(s));
return clang::to_string(clang_getTokenSpelling(cx_tu, cx_token));
}
const clang::TokenKind clang::Token::get_kind() {
@ -30,9 +30,7 @@ bool clang::Token::has_type() {
if(clang_Cursor_isNull(referenced))
return false;
auto type=clang_getCursorType(referenced);
auto cxstr=clang_getTypeSpelling(type);
std::string spelling=clang_getCString(cxstr);
clang_disposeString(cxstr);
auto spelling=clang::to_string(clang_getTypeSpelling(type));
return spelling!="";
}
@ -41,17 +39,13 @@ std::string clang::Token::get_type() {
auto referenced=clang_getCursorReferenced(cx_cursor);
if(!clang_Cursor_isNull(referenced)) {
auto type=clang_getCursorType(referenced);
auto cxstr=clang_getTypeSpelling(type);
spelling=clang_getCString(cxstr);
clang_disposeString(cxstr);
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));
auto cxstr=clang_getTypeSpelling(type);
spelling=clang_getCString(cxstr);
clang_disposeString(cxstr);
spelling=clang::to_string(clang_getTypeSpelling(type));
if(spelling.find(" ")==std::string::npos)
spelling+=auto_end;
}
@ -59,57 +53,11 @@ std::string clang::Token::get_type() {
return spelling;
}
//TODO: use clang_Cursor_getBriefCommentText
std::string clang::Token::get_brief_comments() {
std::string comment_string;
auto referenced=clang_getCursorReferenced(cx_cursor);
auto comment=clang_Cursor_getParsedComment(referenced);
if(clang_Comment_getKind(comment)==CXComment_FullComment) {
size_t para_c=0;
for(unsigned c=0;c<clang_Comment_getNumChildren(comment);c++) {
auto child_comment=clang_Comment_getChild(comment, c);
if(clang_Comment_getKind(child_comment)==CXComment_Paragraph) {
para_c++;
if(para_c>=2)
break;
for(unsigned c=0;c<clang_Comment_getNumChildren(child_comment);c++) {
auto grandchild_comment=clang_Comment_getChild(child_comment, c);
if(clang_Comment_getKind(grandchild_comment)==CXComment_Text) {
auto cxstr=clang_TextComment_getText(grandchild_comment);
comment_string+=clang_getCString(cxstr);
comment_string+="\n";
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);
}
}
}
}
}
/*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();
auto referenced=get_cursor().get_referenced();
if(referenced) {
comment_string=clang::to_string(clang_Cursor_getBriefCommentText(referenced.cx_cursor));
}
return comment_string;
}

30
src/Tokens.cc

@ -1,11 +1,12 @@
#include "Tokens.h"
#include "Utility.h"
#include <iostream>
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);
cx_cursors.resize(num_tokens);
clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data());
for (unsigned i = 0; i < num_tokens; i++) {
emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i]));
@ -19,15 +20,13 @@ clang::Tokens::~Tokens() {
//This works across TranslationUnits! However, to get rename refactoring to work,
//one have to open all the files that might include a similar token
//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(const std::string &usr) {
std::vector<std::pair<unsigned, unsigned> > offsets;
auto referenced=token.get_cursor().get_referenced();
if(referenced) {
auto referenced_usr=referenced.get_usr();
for(auto &a_token: *this) {
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);
for(auto &token: *this) {
if(token.get_kind()==clang::Token_Identifier) {
auto referenced=token.get_cursor().get_referenced();
if(referenced && usr==referenced.get_usr()) {
offsets.emplace_back(token.offsets);
}
}
}
@ -45,12 +44,9 @@ std::vector<std::pair<std::string, unsigned> > clang::Tokens::get_cxx_methods()
auto offset=cursor.get_source_location().get_offset();
if(offset!=last_offset) {
std::string method;
CXString cxstr;
if(kind==clang::CursorKind::CXXMethod) {
auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor));
auto cxstr=clang_getTypeSpelling(type);
method+=clang_getCString(cxstr);
clang_disposeString(cxstr);
method+=clang::to_string(clang_getTypeSpelling(type));
auto pos=method.find(" ");
if(pos!=std::string::npos)
method.erase(pos, 1);
@ -58,15 +54,11 @@ std::vector<std::pair<std::string, unsigned> > clang::Tokens::get_cxx_methods()
}
clang::Cursor parent(clang_getCursorSemanticParent(cursor.cx_cursor));
cxstr=clang_getCursorDisplayName(parent.cx_cursor);
method+=clang_getCString(cxstr);
clang_disposeString(cxstr);
method+=clang::to_string(clang_getCursorDisplayName(parent.cx_cursor));
method+="::";
cxstr=clang_getCursorDisplayName(cursor.cx_cursor);
method+=clang_getCString(cxstr);
clang_disposeString(cxstr);
method+=clang::to_string(clang_getCursorDisplayName(cursor.cx_cursor));
methods.emplace_back(method, offset);
}
last_offset=offset;

2
src/Tokens.h

@ -13,7 +13,7 @@ namespace clang {
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range);
public:
~Tokens();
std::vector<std::pair<unsigned, unsigned> > get_similar_token_offsets(clang::Token& token);
std::vector<std::pair<unsigned, unsigned> > get_similar_token_offsets(const std::string &usr);
std::vector<std::pair<std::string, unsigned> > get_cxx_methods();
private:
CXToken *cx_tokens;

9
src/TranslationUnit.cc

@ -1,6 +1,7 @@
#include "TranslationUnit.h"
#include "SourceLocation.h"
#include "Tokens.h"
#include "Utility.h"
#include <fstream>
#include <sstream>
@ -91,9 +92,7 @@ unsigned clang::TranslationUnit::DefaultFlags() {
}
clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map<std::string, std::string> &buffers, unsigned line_number, unsigned column) {
auto cxstr=clang_getTranslationUnitSpelling(cx_tu);
std::string path=clang_getCString(cxstr);
clang_disposeString(cxstr);
auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu));
clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column);
return results;
@ -110,9 +109,7 @@ std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() {
}
std::unique_ptr<clang::Tokens> clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) {
auto cxstr=clang_getTranslationUnitSpelling(cx_tu);
std::string path=clang_getCString(cxstr);
clang_disposeString(cxstr);
auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu));
clang::SourceLocation start_location(cx_tu, path, start_offset);
clang::SourceLocation end_location(cx_tu, path, end_offset);
clang::SourceRange range(start_location, end_location);

10
src/Utility.cc

@ -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;
}

10
src/Utility.h

@ -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_

1
src/clangmm.h

@ -13,4 +13,5 @@
#include "Index.h"
#include "Cursor.h"
#include "Diagnostic.h"
#include "Utility.h"
#endif // CLANGMM_H_

Loading…
Cancel
Save