Browse Source

Big cleanup, got rid of several circular includes and class forward declarations. Still some work left, but I'll wait with this. Main reason behind the cleanup was compilation troubles.

merge-requests/37/head
eidheim 11 years ago
parent
commit
e2c8256b26
  1. 1
      src/CMakeLists.txt
  2. 4
      src/CodeCompleteResults.cc
  3. 8
      src/CodeCompleteResults.h
  4. 3
      src/CompilationDatabase.h
  5. 2
      src/CompileCommand.h
  6. 3
      src/CompileCommands.h
  7. 8
      src/CompletionString.h
  8. 4
      src/Cursor.cc
  9. 10
      src/Cursor.h
  10. 20
      src/Diagnostic.cc
  11. 12
      src/Diagnostic.h
  12. 4
      src/Index.h
  13. 16
      src/SourceLocation.cc
  14. 16
      src/SourceLocation.h
  15. 22
      src/SourceRange.cc
  16. 23
      src/SourceRange.h
  17. 16
      src/Token.cc
  18. 19
      src/Token.h
  19. 125
      src/Tokens.cc
  20. 16
      src/Tokens.h
  21. 2
      src/TranslationUnit.cc
  22. 16
      src/TranslationUnit.h
  23. 2
      tests/CodeCompleteResults_H_Test.cc
  24. 2
      tests/CompletionString_H_Test.cc
  25. 4
      tests/Cursor_H_Test.cc
  26. 6
      tests/Diagnostics_Test.cc
  27. 8
      tests/SourceLocation_H_Test.cc
  28. 8
      tests/Token_H_Test.cc

1
src/CMakeLists.txt

@ -12,7 +12,6 @@ message("Searcing for libclang")
find_package(LibClang REQUIRED) find_package(LibClang REQUIRED)
set(header_files set(header_files
clangmm.h
CodeCompleteResults.h CodeCompleteResults.h
CompilationDatabase.h CompilationDatabase.h
CompileCommand.h CompileCommand.h

4
src/CodeCompleteResults.cc

@ -3,7 +3,7 @@
#include <exception> #include <exception>
clang::CodeCompleteResults:: clang::CodeCompleteResults::
CodeCompleteResults(clang::TranslationUnit *tu, CodeCompleteResults(CXTranslationUnit &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, int line_num,
@ -16,7 +16,7 @@ CodeCompleteResults(clang::TranslationUnit *tu,
file.Length = buffer.second.size(); file.Length = buffer.second.size();
files.push_back(file); files.push_back(file);
} }
results_ = clang_codeCompleteAt(tu->tu_, results_ = clang_codeCompleteAt(tu,
file_name.c_str(), file_name.c_str(),
line_num, line_num,
column, column,

8
src/CodeCompleteResults.h

@ -1,14 +1,13 @@
#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: public:
CodeCompleteResults(TranslationUnit *tu, CodeCompleteResults(CXTranslationUnit &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, int line_num,
@ -17,7 +16,6 @@ namespace clang {
CompletionString get(int index); CompletionString get(int index);
int size(); int size();
private:
CXCodeCompleteResults *results_; CXCodeCompleteResults *results_;
}; };
} // namespace clang } // namespace clang

3
src/CompilationDatabase.h

@ -11,9 +11,8 @@ namespace clang {
explicit CompilationDatabase(const std::string &project_path); explicit CompilationDatabase(const std::string &project_path);
CompilationDatabase(); CompilationDatabase();
~CompilationDatabase(); ~CompilationDatabase();
private:
CXCompilationDatabase db_; CXCompilationDatabase db_;
friend CompileCommands;
}; };
} }

2
src/CompileCommand.h

@ -9,7 +9,7 @@ namespace clang {
CompileCommand(int nth, CompileCommands *commands); CompileCommand(int nth, CompileCommands *commands);
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 command_;
}; };
} }

3
src/CompileCommands.h

@ -12,9 +12,8 @@ namespace clang {
CompileCommands(const std::string &filename, CompilationDatabase *db); CompileCommands(const std::string &filename, CompilationDatabase *db);
std::vector<CompileCommand> get_commands(); std::vector<CompileCommand> get_commands();
~CompileCommands(); ~CompileCommands();
private:
CXCompileCommands commands_; CXCompileCommands commands_;
friend class CompileCommand;
}; };
} }
#endif // COMPILECOMMANDS_H_ #endif // COMPILECOMMANDS_H_

8
src/CompletionString.h

@ -1,7 +1,8 @@
#ifndef COMPLETIONSTRING_H_ #ifndef COMPLETIONSTRING_H_
#define COMPLETIONSTRING_H_ #define COMPLETIONSTRING_H_
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include "CodeCompleteResults.h" #include <string>
#include <vector>
namespace clang { namespace clang {
enum CompletionChunkKind { enum CompletionChunkKind {
@ -27,14 +28,13 @@ namespace clang {
class CompletionString { class CompletionString {
public: public:
explicit CompletionString(const CXCompletionString &str);
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(); int get_num_chunks();
private:
explicit CompletionString(const CXCompletionString &str);
CXCompletionString str_; CXCompletionString str_;
friend CodeCompleteResults;
}; };
} // namespace clang } // namespace clang
#endif // COMPLETIONSTRING_H_ #endif // COMPLETIONSTRING_H_

4
src/Cursor.cc

@ -5,6 +5,6 @@ const clang::CursorKind clang::Cursor::kind() {
} }
clang::Cursor:: clang::Cursor::
Cursor(clang::TranslationUnit *tu, clang::SourceLocation *source_location) { Cursor(CXTranslationUnit &tu, clang::SourceLocation *source_location) {
cursor_ = clang_getCursor(tu->tu_, source_location->location_); cursor_ = clang_getCursor(tu, source_location->location_);
} }

10
src/Cursor.h

@ -1,9 +1,11 @@
#ifndef CURSOR_H_ #ifndef CURSOR_H_
#define CURSOR_H_ #define CURSOR_H_
#include "TranslationUnit.h" #include <clang-c/Index.h>
#include "SourceLocation.h" #include "SourceLocation.h"
namespace clang { namespace clang {
class SourceLocation;
enum class CursorKind { enum class CursorKind {
UnexposedDecl = 1, UnexposedDecl = 1,
StructDecl = 2, StructDecl = 2,
@ -175,13 +177,9 @@ namespace clang {
class Cursor { class Cursor {
public: public:
Cursor() {} Cursor() {}
Cursor(TranslationUnit *tu, SourceLocation *source_location); Cursor(CXTranslationUnit &tu, SourceLocation *source_location);
const CursorKind kind(); const CursorKind kind();
private:
CXCursor cursor_; CXCursor cursor_;
friend SourceRange;
friend SourceLocation;
friend Tokens;
}; };
} // namespace clang } // namespace clang
#endif // CURSOR_H_ #endif // CURSOR_H_

20
src/Diagnostic.cc

@ -2,29 +2,19 @@
#include "SourceLocation.h" #include "SourceLocation.h"
#include "Tokens.h" #include "Tokens.h"
clang::Diagnostic::Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic) { clang::Diagnostic::Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic) {
severity=clang_getDiagnosticSeverity(clang_diagnostic); severity=clang_getDiagnosticSeverity(clang_diagnostic);
severity_spelling=get_severity_spelling(severity); severity_spelling=get_severity_spelling(severity);
spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic)); spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic));
clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic)); clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic));
std::string tmp_path;
unsigned line, column, offset;
location.get_location_info(&tmp_path, &line, &column, &offset);
path=tmp_path;
start_location.line=line;
start_location.column=column;
start_location.offset=offset;
clang::SourceRange range(&location, &location); clang::SourceRange range(&location, &location);
clang::Tokens tokens(&tu, &range); clang::Tokens tokens(tu, &range);
if(tokens.size()==1) { if(tokens.size()==1) {
auto& token=tokens[0]; auto& token=tokens[0];
clang::SourceRange range=token.get_source_range(&tu); clang::SourceRange range=token.get_source_range();
clang::SourceLocation location(&range, false); auto end_location=clang::SourceLocation(&range, false);
location.get_location_info(NULL, &line, &column, &offset); this->range=range.get_range_data(location, end_location);
end_location.line=line;
end_location.column=column;
end_location.offset=offset;
} }
} }

12
src/Diagnostic.h

@ -3,25 +3,19 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include "TranslationUnit.h" #include "SourceRange.h"
namespace clang { namespace clang {
class Diagnostic { class Diagnostic {
public: public:
class LocationData { Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic);
public:
unsigned line, column, offset;
};
Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic);
static const std::string get_severity_spelling(unsigned severity); static const std::string get_severity_spelling(unsigned severity);
unsigned severity; unsigned severity;
std::string severity_spelling; std::string severity_spelling;
std::string spelling; std::string spelling;
std::string path; RangeData range;
LocationData start_location, end_location;
}; };
} }

4
src/Index.h

@ -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 index_; CXIndex index_;
friend TranslationUnit;
}; };
} // namespace clang } // namespace clang
#endif // INDEX_H_ #endif // INDEX_H_

16
src/SourceLocation.cc

@ -4,13 +4,13 @@
// SourceLocation // // SourceLocation //
// // // // // // // // // // // // // // // //
clang::SourceLocation:: clang::SourceLocation::
SourceLocation(clang::TranslationUnit *tu, SourceLocation(CXTranslationUnit &tu,
const std::string &filename, const std::string &filename,
int line_number, int line_number,
int line_offset) { int line_offset) {
CXFile file = clang_getFile(tu->tu_, CXFile file = clang_getFile(tu,
filename.c_str()); filename.c_str());
location_ = clang_getLocation(tu->tu_, location_ = clang_getLocation(tu,
file, file,
line_number, line_number,
line_offset); line_offset);
@ -28,19 +28,19 @@ SourceLocation(clang::SourceRange *range, bool start) {
} }
clang::SourceLocation:: clang::SourceLocation::
SourceLocation(TranslationUnit *tu, SourceLocation(CXTranslationUnit &tu,
Token *token) { Token *token) {
location_ = clang_getTokenLocation(tu->tu_, location_ = clang_getTokenLocation(tu,
token->token_); token->token_);
} }
clang::SourceLocation:: clang::SourceLocation::
SourceLocation(clang::TranslationUnit *tu, SourceLocation(CXTranslationUnit &tu,
const std::string &filepath, const std::string &filepath,
int offset) { int offset) {
CXFile file = clang_getFile(tu->tu_, CXFile file = clang_getFile(tu,
filepath.c_str()); filepath.c_str());
location_ = clang_getLocationForOffset(tu->tu_, location_ = clang_getLocationForOffset(tu,
file, file,
offset); offset);
} }

16
src/SourceLocation.h

@ -1,23 +1,28 @@
#ifndef SOURCELOCATION_H_ #ifndef SOURCELOCATION_H_
#define SOURCELOCATION_H_ #define SOURCELOCATION_H_
#include "TranslationUnit.h" #include <clang-c/Index.h>
#include "Token.h" #include "Token.h"
#include "Cursor.h" #include "Cursor.h"
#include <string>
namespace clang { namespace clang {
class Token;
class SourceRange;
class Cursor;
class SourceLocation { class SourceLocation {
public: public:
SourceLocation(TranslationUnit* tu, SourceLocation(CXTranslationUnit &tu,
const std::string &filename, const std::string &filename,
int line_number, int line_number,
int column); int column);
SourceLocation(TranslationUnit *tu, SourceLocation(CXTranslationUnit &tu,
Token *token); Token *token);
SourceLocation(SourceRange *range, bool start); SourceLocation(SourceRange *range, bool start);
SourceLocation(TranslationUnit *tu, SourceLocation(CXTranslationUnit &tu,
const std::string &filepath, const std::string &filepath,
int offset); int offset);
@ -30,10 +35,7 @@ namespace clang {
unsigned *column, unsigned *column,
unsigned *offset); unsigned *offset);
private:
CXSourceLocation location_; CXSourceLocation location_;
friend SourceRange;
friend Cursor;
}; };
} // namespace clang } // namespace clang

22
src/SourceRange.cc

@ -1,8 +1,8 @@
#include "SourceRange.h" #include "SourceRange.h"
clang::SourceRange:: clang::SourceRange::
SourceRange(clang::TranslationUnit *tu, clang::Token *token) { SourceRange(clang::Token *token) {
range_ = clang_getTokenExtent(tu->tu_, token->token_); range_ = clang_getTokenExtent(token->tu, token->token_);
} }
clang::SourceRange:: clang::SourceRange::
@ -13,3 +13,21 @@ SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) {
clang::SourceRange::SourceRange(Cursor *cursor) { clang::SourceRange::SourceRange(Cursor *cursor) {
range_ = clang_getCursorExtent(cursor->cursor_); range_ = clang_getCursorExtent(cursor->cursor_);
} }
clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start, clang::SourceLocation &end) {
std::string path;
unsigned start_offset, end_offset;
start.get_location_info(&path, NULL, NULL, &start_offset);
end.get_location_info(NULL, NULL, NULL, &end_offset);
RangeData range_data;
range_data.path=path;
range_data.start_offset=start_offset;
range_data.end_offset=end_offset;
return range_data;
}
clang::RangeData clang::SourceRange::get_range_data() {
clang::SourceLocation start(this, true);
clang::SourceLocation end(this, false);
return get_range_data(start, end);
}

23
src/SourceRange.h

@ -1,23 +1,32 @@
#ifndef SOURCERANGE_H_ #ifndef SOURCERANGE_H_
#define SOURCERANGE_H_ #define SOURCERANGE_H_
#include "TranslationUnit.h" #include <clang-c/Index.h>
#include "Token.h" #include "Token.h"
#include "Cursor.h" #include "Cursor.h"
#include "SourceLocation.h"
#include <string>
namespace clang { namespace clang {
class Token;
class SourceLocation;
class Cursor;
class RangeData {
public:
std::string path;
unsigned start_offset, end_offset;
};
class SourceRange { class SourceRange {
public: public:
SourceRange() {} SourceRange() {}
SourceRange(TranslationUnit *tu, Token *token); SourceRange(Token *token);
SourceRange(SourceLocation *start, SourceRange(SourceLocation *start,
SourceLocation *end); SourceLocation *end);
explicit SourceRange(Cursor *cursor); explicit SourceRange(Cursor *cursor);
static RangeData get_range_data(SourceLocation &start, SourceLocation &end);
private: RangeData get_range_data();
CXSourceRange range_; CXSourceRange range_;
friend Tokens;
friend SourceLocation;
friend Diagnostic;
}; };
} // namespace clang } // namespace clang
#endif // SOURCERANGE_H_ #endif // SOURCERANGE_H_

16
src/Token.cc

@ -5,25 +5,23 @@
// // // // // // // // // //
// clang::Token instansiates an token // clang::Token instansiates an token
clang::Token::Token(const CXToken &token) : clang::Token::Token(CXTranslationUnit &tu, const CXToken &token) :
token_(token) { tu(tu), 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(tu, this); 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(this);
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_token_spelling() {
CXString s = clang_getTokenSpelling(tu->tu_, token_); CXString s = clang_getTokenSpelling(tu, token_);
return std::string(clang_getCString(s)); return std::string(clang_getCString(s));
} }

19
src/Token.h

@ -1,10 +1,14 @@
#ifndef TOKEN_H_ #ifndef TOKEN_H_
#define TOKEN_H_ #define TOKEN_H_
#include <clang-c/Index.h>
#include "SourceLocation.h" #include "SourceLocation.h"
#include "SourceRange.h" #include "SourceRange.h"
#include "TranslationUnit.h" #include <string>
namespace clang { namespace clang {
class SourceLocation;
class SourceRange;
enum TokenKind { enum TokenKind {
Token_Punctuation, Token_Punctuation,
Token_Keyword, Token_Keyword,
@ -15,17 +19,14 @@ namespace clang {
class Token { class Token {
public: public:
explicit Token(CXTranslationUnit &tu, const CXToken &token);
const TokenKind kind(); const TokenKind kind();
std::string get_token_spelling(TranslationUnit *tu); std::string get_token_spelling();
SourceLocation get_source_location(TranslationUnit *tu); SourceLocation get_source_location();
SourceRange get_source_range(TranslationUnit *tu); SourceRange get_source_range();
std::string type; std::string type;
private:
explicit Token(const CXToken &token);
friend SourceRange;
friend SourceLocation;
friend Tokens;
const CXToken& token_; const CXToken& token_;
CXTranslationUnit &tu;
}; };
} // namespace clang } // namespace clang
#endif // TOKEN_H_ #endif // TOKEN_H_

125
src/Tokens.cc

@ -2,24 +2,24 @@
#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 &tu, clang::SourceRange *range): tu(tu) {
clang_tokenize(tu->tu_, clang_tokenize(tu,
range->range_, range->range_,
&tokens_, &tokens_,
&num_tokens_); &num_tokens_);
for (int i = 0; i < num_tokens_; i++) { for (int i = 0; i < num_tokens_; i++) {
push_back(clang::Token(tokens_[i])); emplace_back(tu, tokens_[i]);
} }
} }
clang::Tokens::~Tokens() { clang::Tokens::~Tokens() {
clang_disposeTokens(tu.tu_, tokens_, size()); clang_disposeTokens(tu, tokens_, size());
} }
void clang::Tokens::update_types(clang::TranslationUnit *tu) { void clang::Tokens::update_types() {
clang_cursors.clear(); clang_cursors.clear();
clang_cursors.reserve(size()); clang_cursors.reserve(size());
clang_annotateTokens(tu->tu_, tokens_, size(), clang_cursors.data()); clang_annotateTokens(tu, tokens_, size(), clang_cursors.data());
for(size_t c=0;c<size();c++) { for(size_t c=0;c<size();c++) {
auto referenced=clang_getCursorReferenced(clang_cursors[c]); auto referenced=clang_getCursorReferenced(clang_cursors[c]);
@ -81,6 +81,119 @@ void clang::Tokens::update_types(clang::TranslationUnit *tu) {
} }
} }
CXCursor clang::Tokens::find_referenced() {
clang_cursors.clear();
clang_cursors.reserve(size());
clang_annotateTokens(tu, tokens_, size(), clang_cursors.data());
auto kind=clang_getCursorKind(clang_cursors[0]);
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(clang_cursors[0]);
kind=clang_getCursorKind(referenced);
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;
//TODO: To find similar function declarations, these must be equal (if cursor is declaration):
//TODO: How do we know it is a function? clang_getCursorKind(cursor)==CXCursor_CXXMethod?
cout << " " << clang_getCString(clang_getTypeSpelling(clang_getCursorType(referenced))) << endl;
cout << " " << clang_getCString(clang_getTypeSpelling(clang_getCursorType(clang_getCursorSemanticParent(referenced)))) << endl;
cout << " " << clang_getCString(clang_getCursorSpelling(referenced)) << endl;
return referenced;
}
bool clang::Tokens::equalCursors(CXCursor a, CXCursor b) {
clang::Cursor cursor_a;
cursor_a.cursor_=a;
auto range=clang::SourceRange(&cursor_a);
auto location=clang::SourceLocation(&range, true);
std::string path_a;
unsigned offset_start_a, offset_end_a;
location.get_location_info(&path_a, NULL, NULL, &offset_start_a);
location=clang::SourceLocation(&range, false);
location.get_location_info(NULL, NULL, NULL, &offset_end_a);
clang::Cursor cursor_b;
cursor_b.cursor_=b;
range=clang::SourceRange(&cursor_b);
location=clang::SourceLocation(&range, true);
std::string path_b;
unsigned offset_start_b, offset_end_b;
location.get_location_info(&path_b, NULL, NULL, &offset_start_b);
location=clang::SourceLocation(&range, false);
location.get_location_info(NULL, NULL, NULL, &offset_end_b);
return path_a==path_b && offset_start_a==offset_start_b && offset_end_a==offset_end_b;
}
void clang::Tokens::rename(CXCursor& referenced, std::unordered_map<std::string, std::unordered_multimap<unsigned, unsigned> > &ranges) {
for(size_t c=0;c<size();c++) {
auto a_referenced=clang_getCursorReferenced(clang_cursors[c]);
if(equalCursors(a_referenced, referenced)) {
std::string path;
unsigned line, column, offset;
clang::Cursor cursor;
cursor.cursor_=clang_cursors[c];
auto range=clang::SourceRange(&cursor);
auto location=clang::SourceLocation(&range, true);
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 << " end: " << path << ", " << line << ", " << column << endl;
clang::Cursor referenced_cursor;
referenced_cursor.cursor_=a_referenced;
range=clang::SourceRange(&referenced_cursor);
location=clang::SourceLocation(&range, true);
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 << " end: " << path << ", " << line << ", " << column << endl;
auto type=clang_getCursorType(a_referenced);
cout << " " << clang_getCString(clang_getTypeSpelling(type)) << endl;
}
}
//clang_visitChildren(clang_getTranslationUnitCursor(tu.tu_), clang::Tokens::clang_visitor, &referenced);
}
/*CXChildVisitResult clang::Tokens::clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData) {
CXCursor* referenced=(CXCursor*)clientData;
auto a_referenced=clang_getCursorReferenced(cursor);
if(clang_equalCursors(a_referenced, *referenced)) {
cout << "found" << endl;
clang::Cursor a_cursor;
a_cursor.cursor_=cursor;
auto range=clang::SourceRange(&a_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 << " end: " << path << ", " << line << ", " << column << endl;
}
return CXChildVisit_Recurse;
}*/
std::string clang::Tokens::get_brief_comments(size_t cursor_id) { std::string clang::Tokens::get_brief_comments(size_t cursor_id) {
std::string comment_string; std::string comment_string;
auto referenced=clang_getCursorReferenced(clang_cursors[cursor_id]); auto referenced=clang_getCursorReferenced(clang_cursors[cursor_id]);

16
src/Tokens.h

@ -1,21 +1,29 @@
#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> {
public: public:
Tokens(TranslationUnit *tu, SourceRange *range); Tokens(CXTranslationUnit &tu, SourceRange *range);
~Tokens(); ~Tokens();
void update_types(clang::TranslationUnit *tu); void update_types();
std::string get_brief_comments(size_t cursor_id); std::string get_brief_comments(size_t cursor_id);
CXCursor find_referenced();
bool equalCursors(CXCursor a, CXCursor b);
void rename(CXCursor &referenced, std::unordered_map<std::string, std::unordered_multimap<unsigned, unsigned> > &ranges);
private: private:
CXToken *tokens_; CXToken *tokens_;
unsigned num_tokens_; unsigned num_tokens_;
std::vector<CXCursor> clang_cursors; std::vector<CXCursor> clang_cursors;
TranslationUnit& tu; CXTranslationUnit& tu;
static CXChildVisitResult clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData);
}; };
} // namespace clang } // namespace clang
#endif // TOKENS_H_ #endif // TOKENS_H_

2
src/TranslationUnit.cc

@ -101,7 +101,7 @@ void clang::TranslationUnit::update_diagnostics() {
diagnostics.clear(); diagnostics.clear();
for(unsigned c=0;c<clang_getNumDiagnostics(tu_);c++) { for(unsigned c=0;c<clang_getNumDiagnostics(tu_);c++) {
CXDiagnostic clang_diagnostic=clang_getDiagnostic(tu_, c); CXDiagnostic clang_diagnostic=clang_getDiagnostic(tu_, c);
diagnostics.emplace_back(clang::Diagnostic(*this, clang_diagnostic)); diagnostics.emplace_back(clang::Diagnostic(tu_, clang_diagnostic));
clang_disposeDiagnostic(clang_diagnostic); clang_disposeDiagnostic(clang_diagnostic);
} }
} }

16
src/TranslationUnit.h

@ -9,14 +9,6 @@
#include "Diagnostic.h" #include "Diagnostic.h"
namespace clang { namespace clang {
class Token;
class Tokens;
class SourceLocation;
class SourceRange;
class Cursor;
class CodeCompleteResults;
class Diagnostic;
class TranslationUnit { class TranslationUnit {
public: public:
TranslationUnit(Index *index, TranslationUnit(Index *index,
@ -39,18 +31,12 @@ namespace clang {
void update_diagnostics(); void update_diagnostics();
std::vector<clang::Diagnostic> diagnostics; std::vector<clang::Diagnostic> diagnostics;
private:
void parse(Index *index, void parse(Index *index,
const std::string &filepath, const std::string &filepath,
const std::vector<std::string> &command_line_args, const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers, const std::map<std::string, std::string> &buffers,
unsigned flags=DefaultFlags()); unsigned flags=DefaultFlags());
friend Token;
friend Tokens;
friend SourceLocation;
friend SourceRange;
friend Cursor;
friend CodeCompleteResults;
CXTranslationUnit tu_; CXTranslationUnit tu_;
}; };
} // namespace clang } // namespace clang

2
tests/CodeCompleteResults_H_Test.cc

@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) {
// ] // ]
clang::CodeCompleteResults results(&tu, path, buffers, 4, 5); clang::CodeCompleteResults results(tu.tu_, path, buffers, 4, 5);
bool substr_found=false; bool substr_found=false;
for(int c=0;c<results.size();c++) { for(int c=0;c<results.size();c++) {

2
tests/CompletionString_H_Test.cc

@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(completion_string) {
buffers[path] = file; buffers[path] = file;
clang::CodeCompleteResults results(&tu, path, buffers, 4, 5); clang::CodeCompleteResults results(tu.tu_, path, buffers, 4, 5);
// ] // ]

4
tests/Cursor_H_Test.cc

@ -12,8 +12,8 @@ BOOST_AUTO_TEST_CASE(cursor) {
// ] // ]
clang::SourceLocation location(&tu, path, 6, 4); clang::SourceLocation location(tu.tu_, path, 6, 4);
clang::Cursor cursor(&tu, &location); clang::Cursor cursor(tu.tu_, &location);
BOOST_CHECK(cursor.kind() == clang::CursorKind::ReturnStmt); BOOST_CHECK(cursor.kind() == clang::CursorKind::ReturnStmt);
} }

6
tests/Diagnostics_Test.cc

@ -23,10 +23,6 @@ BOOST_AUTO_TEST_CASE(diagnostics_test) {
tu.update_diagnostics(); tu.update_diagnostics();
BOOST_CHECK(tu.diagnostics.size()==1); BOOST_CHECK(tu.diagnostics.size()==1);
BOOST_CHECK(tu.diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'"); BOOST_CHECK(tu.diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'");
BOOST_CHECK(tu.diagnostics[0].path=="./case/main_error.cpp"); BOOST_CHECK(tu.diagnostics[0].range.path=="./case/main_error.cpp");
BOOST_CHECK(tu.diagnostics[0].severity==3); BOOST_CHECK(tu.diagnostics[0].severity==3);
BOOST_CHECK(tu.diagnostics[0].start_location.line==5);
BOOST_CHECK(tu.diagnostics[0].end_location.line==5);
BOOST_CHECK(tu.diagnostics[0].start_location.column==16);
BOOST_CHECK(tu.diagnostics[0].end_location.column==35);
} }

8
tests/SourceLocation_H_Test.cc

@ -8,12 +8,12 @@ BOOST_AUTO_TEST_CASE(source_location) {
clang::Index index(0, 0); clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path); clang::TranslationUnit tu(&index, path);
clang::SourceLocation start(&tu, path, 0); clang::SourceLocation start(tu.tu_, path, 0);
clang::SourceLocation end(&tu, path, 7, 1); clang::SourceLocation end(tu.tu_, path, 7, 1);
clang::SourceRange range(&start, &end); clang::SourceRange range(&start, &end);
clang::Tokens tokens(&tu, &range); clang::Tokens tokens(tu.tu_, &range);
clang::SourceRange token_range = tokens[28].get_source_range(&tu); clang::SourceRange token_range = tokens[28].get_source_range();
unsigned token_start_line, token_start_column, token_start_offset, unsigned token_start_line, token_start_column, token_start_offset,
token_end_line, token_end_column, token_end_offset; token_end_line, token_end_column, token_end_offset;

8
tests/Token_H_Test.cc

@ -8,16 +8,16 @@ BOOST_AUTO_TEST_CASE(token) {
clang::Index index(0, 0); clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path); clang::TranslationUnit tu(&index, path);
clang::SourceLocation start(&tu, path, 0); clang::SourceLocation start(tu.tu_, path, 0);
clang::SourceLocation end(&tu, path, 7, 1); clang::SourceLocation end(tu.tu_, path, 7, 1);
clang::SourceRange range(&start, &end); clang::SourceRange range(&start, &end);
clang::Tokens tokens(&tu, &range); clang::Tokens tokens(tu.tu_, &range);
BOOST_CHECK(tokens.size() == 32); BOOST_CHECK(tokens.size() == 32);
BOOST_CHECK(tokens[1].kind() == clang::TokenKind::Token_Identifier); BOOST_CHECK(tokens[1].kind() == clang::TokenKind::Token_Identifier);
std::string str = tokens[28].get_token_spelling(&tu); std::string str = tokens[28].get_token_spelling();
BOOST_CHECK(str == "return"); BOOST_CHECK(str == "return");
} }

Loading…
Cancel
Save