Browse Source

Merge branch 'master' into readme

merge-requests/37/head
Jørgen Lien Sellæg 11 years ago
parent
commit
79a6b08021
  1. 2
      src/CMakeLists.txt
  2. 24
      src/CodeCompleteResults.cc
  3. 21
      src/CodeCompleteResults.h
  4. 4
      src/CompilationDatabase.cc
  5. 6
      src/CompilationDatabase.h
  6. 18
      src/CompileCommand.cc
  7. 9
      src/CompileCommand.h
  8. 16
      src/CompileCommands.cc
  9. 7
      src/CompileCommands.h
  10. 25
      src/CompletionString.cc
  11. 12
      src/CompletionString.h
  12. 30
      src/Cursor.cc
  13. 21
      src/Cursor.h
  14. 29
      src/Diagnostic.cc
  15. 13
      src/Diagnostic.h
  16. 2
      src/Index.cc
  17. 6
      src/Index.h
  18. 65
      src/SourceLocation.cc
  19. 37
      src/SourceLocation.h
  20. 17
      src/SourceRange.cc
  21. 22
      src/SourceRange.h
  22. 63
      src/Token.cc
  23. 30
      src/Token.h
  24. 163
      src/Tokens.cc
  25. 20
      src/Tokens.h
  26. 57
      src/TranslationUnit.cc
  27. 41
      src/TranslationUnit.h
  28. 10
      src/Utility.cc
  29. 10
      src/Utility.h
  30. 1
      src/clangmm.h
  31. 13
      src/docs/windows-install.md
  32. 6
      tests/CodeCompleteResults_H_Test.cc
  33. 5
      tests/CompletionString_H_Test.cc
  34. 7
      tests/Cursor_H_Test.cc
  35. 15
      tests/Diagnostics_Test.cc
  36. 36
      tests/SourceLocation_H_Test.cc
  37. 14
      tests/Token_H_Test.cc
  38. 4
      tests/TranslationUnit_Test.cc

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})

24
src/CodeCompleteResults.cc

@ -3,11 +3,9 @@
#include <exception>
clang::CodeCompleteResults::
CodeCompleteResults(clang::TranslationUnit *tu,
const std::string &file_name,
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name,
const std::map<std::string, std::string> &buffers,
int line_num,
int column) {
unsigned line_num, unsigned column) {
std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) {
CXUnsavedFile file;
@ -16,30 +14,30 @@ CodeCompleteResults(clang::TranslationUnit *tu,
file.Length = buffer.second.size();
files.push_back(file);
}
results_ = clang_codeCompleteAt(tu->tu_,
cx_results = clang_codeCompleteAt(cx_tu,
file_name.c_str(),
line_num,
column,
files.data(),
files.size(),
clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments);
clang_sortCodeCompletionResults(results_->Results, results_->NumResults);
clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
}
clang::CodeCompleteResults::~CodeCompleteResults() {
delete[] results_->Results;
delete results_;
delete[] cx_results->Results;
delete cx_results;
}
int clang::CodeCompleteResults::
unsigned clang::CodeCompleteResults::
size() {
return results_->NumResults;
return cx_results->NumResults;
}
clang::CompletionString clang::CodeCompleteResults::
get(int i) {
get(unsigned i) {
if (i >= size()) {
throw std::invalid_argument("clang::CodeCompleteResults::get(int i): i>=size()");
throw std::invalid_argument("clang::CodeCompleteResults::get(unsigned i): i>=size()");
}
return CompletionString(results_->Results[i].CompletionString);
return CompletionString(cx_results->Results[i].CompletionString);
}

21
src/CodeCompleteResults.h

@ -1,24 +1,21 @@
#ifndef CODECOMPLETERESULTS_H_
#define CODECOMPLETERESULTS_H_
#include <clang-c/Index.h>
#include "TranslationUnit.h"
#include <map>
#include "CompletionString.h"
namespace clang {
class CompletionString;
class CodeCompleteResults {
public:
CodeCompleteResults(TranslationUnit *tu,
const std::string &file_name,
friend class TranslationUnit;
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name,
const std::map<std::string, std::string> &buffers,
int line_num,
int column);
unsigned line_num, unsigned column);
public:
~CodeCompleteResults();
CompletionString get(int index);
int size();
CompletionString get(unsigned index);
unsigned size();
private:
CXCodeCompleteResults *results_;
CXCodeCompleteResults *cx_results;
};
} // namespace clang
#endif // CODECOMPLETERESULTS_H_

4
src/CompilationDatabase.cc

@ -4,7 +4,7 @@
clang::CompilationDatabase::
CompilationDatabase(const std::string &project_path) {
CXCompilationDatabase_Error error;
db_ = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error);
cx_db = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error);
if(error) {
//TODO: compile_commands.json is missing, create it?
}
@ -12,5 +12,5 @@ CompilationDatabase(const std::string &project_path) {
clang::CompilationDatabase::
~CompilationDatabase() {
clang_CompilationDatabase_dispose(db_);
clang_CompilationDatabase_dispose(cx_db);
}

6
src/CompilationDatabase.h

@ -5,15 +5,13 @@
#include <string>
namespace clang {
class CompileCommands;
class CompilationDatabase {
public:
explicit CompilationDatabase(const std::string &project_path);
CompilationDatabase();
~CompilationDatabase();
private:
CXCompilationDatabase db_;
friend CompileCommands;
CXCompilationDatabase cx_db;
};
}

18
src/CompileCommand.cc

@ -1,27 +1,23 @@
#include "CompileCommand.h"
#include "CompileCommands.h"
clang::CompileCommand::
CompileCommand(int nth, clang::CompileCommands *commands) {
command_ = clang_CompileCommands_getCommand(commands->commands_, nth);
}
#include "Utility.h"
std::string clang::CompileCommand::
get_command() {
std::string res;
unsigned N = clang_CompileCommand_getNumArgs(command_);
for (int i = 0; i < N; i++) {
res += clang_getCString(clang_CompileCommand_getArg(command_, i));
unsigned N = clang_CompileCommand_getNumArgs(cx_command);
for (unsigned i = 0; i < N; i++) {
res += clang::to_string(clang_CompileCommand_getArg(cx_command, i));
}
return res;
}
std::vector<std::string> clang::CompileCommand::
get_command_as_args() {
unsigned N = clang_CompileCommand_getNumArgs(command_);
unsigned N = clang_CompileCommand_getNumArgs(cx_command);
std::vector<std::string> res(N);
for (int i = 0; i < N; i++) {
res[i] = clang_getCString(clang_CompileCommand_getArg(command_, i));
for (unsigned i = 0; i < N; i++) {
res[i] = clang::to_string(clang_CompileCommand_getArg(cx_command, i));
}
return res;
}

9
src/CompileCommand.h

@ -1,16 +1,17 @@
#ifndef COMPILECOMMAND_H_
#define COMPILECOMMAND_H_
#include "CompilationDatabase.h"
#include <clang-c/CXCompilationDatabase.h>
#include <vector>
#include <string>
namespace clang {
class CompileCommand {
public:
CompileCommand(int nth, CompileCommands *commands);
CompileCommand(const CXCompileCommand& cx_command) : cx_command(cx_command) {};
std::string get_command();
std::vector<std::string> get_command_as_args();
private:
CXCompileCommand command_;
CXCompileCommand cx_command;
};
}
#endif // COMPILECOMMAND_H_

16
src/CompileCommands.cc

@ -1,24 +1,24 @@
#include "CompileCommands.h"
clang::CompileCommands::
CompileCommands(const std::string &filename, CompilationDatabase *db) {
commands_ =
clang_CompilationDatabase_getCompileCommands(db->db_, filename.c_str());
if(clang_CompileCommands_getSize(commands_)==0)
commands_ = clang_CompilationDatabase_getAllCompileCommands(db->db_);
CompileCommands(const std::string &filename, CompilationDatabase &db) {
cx_commands =
clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str());
if(clang_CompileCommands_getSize(cx_commands)==0)
cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db);
}
clang::CompileCommands::
~CompileCommands() {
clang_CompileCommands_dispose(commands_);
clang_CompileCommands_dispose(cx_commands);
}
std::vector<clang::CompileCommand> clang::CompileCommands::
get_commands() {
unsigned N = clang_CompileCommands_getSize(commands_);
unsigned N = clang_CompileCommands_getSize(cx_commands);
std::vector<clang::CompileCommand> res;
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;
}

7
src/CompileCommands.h

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

25
src/CompletionString.cc

@ -1,36 +1,27 @@
#include "CompletionString.h"
#include "Utility.h"
clang::CompletionString::
CompletionString(const CXCompletionString &str) {
str_ = str;
}
CompletionString(const CXCompletionString &cx_completion_sting) : cx_completion_sting(cx_completion_sting) {}
bool clang::CompletionString::available() {
return clang_getCompletionAvailability(str_) == CXAvailability_Available;
return clang_getCompletionAvailability(cx_completion_sting) == CXAvailability_Available;
}
int clang::CompletionString::get_num_chunks() {
return clang_getNumCompletionChunks(str_);
unsigned clang::CompletionString::get_num_chunks() {
return clang_getNumCompletionChunks(cx_completion_sting);
}
std::vector<clang::CompletionChunk> clang::CompletionString::get_chunks() {
std::vector<clang::CompletionChunk> res;
for (size_t i = 0; i < get_num_chunks(); i++) {
auto cxstr=clang_getCompletionChunkText(str_, i);
res.emplace_back(clang_getCString(cxstr), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(str_, i)));
clang_disposeString(cxstr);
for (unsigned i = 0; i < get_num_chunks(); i++) {
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(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::

12
src/CompletionString.h

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

30
src/Cursor.cc

@ -1,10 +1,30 @@
#include "Cursor.h"
#include "Utility.h"
const clang::CursorKind clang::Cursor::kind() {
return (CursorKind) clang_getCursorKind(this->cursor_);
const clang::CursorKind clang::Cursor::get_kind() {
return (CursorKind) clang_getCursorKind(this->cx_cursor);
}
clang::Cursor::
Cursor(clang::TranslationUnit *tu, clang::SourceLocation *source_location) {
cursor_ = clang_getCursor(tu->tu_, source_location->location_);
clang::SourceLocation clang::Cursor::get_source_location() const {
return SourceLocation(clang_getCursorLocation(cx_cursor));
}
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();
}

21
src/Cursor.h

@ -1,7 +1,9 @@
#ifndef CURSOR_H_
#define CURSOR_H_
#include "TranslationUnit.h"
#include <clang-c/Index.h>
#include "SourceLocation.h"
#include "SourceRange.h"
#include <string>
namespace clang {
enum class CursorKind {
@ -174,14 +176,15 @@ namespace clang {
class Cursor {
public:
Cursor() {}
Cursor(TranslationUnit *tu, SourceLocation *source_location);
const CursorKind kind();
private:
CXCursor cursor_;
friend SourceRange;
friend SourceLocation;
friend Tokens;
Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {}
const CursorKind get_kind();
SourceLocation get_source_location() const;
SourceRange get_source_range() const;
std::string get_usr() const;
Cursor get_referenced() const;
operator bool() const;
bool operator==(const Cursor& rhs) const;
CXCursor cx_cursor;
};
} // namespace clang
#endif // CURSOR_H_

29
src/Diagnostic.cc

@ -1,30 +1,19 @@
#include "Diagnostic.h"
#include "SourceLocation.h"
#include "Tokens.h"
#include "Utility.h"
clang::Diagnostic::Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic) {
severity=clang_getDiagnosticSeverity(clang_diagnostic);
clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) {
severity=clang_getDiagnosticSeverity(cx_diagnostic);
severity_spelling=get_severity_spelling(severity);
spelling=clang_getCString(clang_getDiagnosticSpelling(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;
spelling=clang::to_string(clang_getDiagnosticSpelling(cx_diagnostic));
clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic));
clang::SourceRange range(&location, &location);
clang::Tokens tokens(&tu, &range);
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) {
auto& token=tokens[0];
clang::SourceRange range=token.get_source_range(&tu);
clang::SourceLocation location(&range, false);
location.get_location_info(NULL, &line, &column, &offset);
end_location.line=line;
end_location.column=column;
end_location.offset=offset;
offsets=std::pair<unsigned, unsigned>(start_offset, tokens.begin()->offsets.second);
}
}

13
src/Diagnostic.h

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

2
src/Index.cc

@ -2,6 +2,6 @@
clang::Index::
Index(int excludeDeclarationsFromPCH, int displayDiagnostics) {
index_ = clang_createIndex(excludeDeclarationsFromPCH,
cx_index = clang_createIndex(excludeDeclarationsFromPCH,
displayDiagnostics);
}

6
src/Index.h

@ -1,16 +1,12 @@
#ifndef INDEX_H_
#define INDEX_H_
#include <clang-c/Index.h>
namespace clang {
class TranslationUnit;
class Index {
public:
Index(int excludeDeclarationsFromPCH, int displayDiagnostics);
private:
CXIndex index_;
friend TranslationUnit;
CXIndex cx_index;
};
} // namespace clang
#endif // INDEX_H_

65
src/SourceLocation.cc

@ -1,59 +1,34 @@
#include "SourceLocation.h"
#include "Utility.h"
// // // // // // // //
// SourceLocation //
// // // // // // // //
clang::SourceLocation::
SourceLocation(clang::TranslationUnit *tu,
const std::string &filename,
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::SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset) {
CXFile file = clang_getFile(tu, filepath.c_str());
cx_location = clang_getLocationForOffset(tu, file, offset);
}
clang::SourceLocation::
SourceLocation(TranslationUnit *tu,
Token *token) {
location_ = clang_getTokenLocation(tu->tu_,
token->token_);
std::string clang::SourceLocation::get_path() {
std::string path;
get_data(&path, NULL, NULL, NULL);
return path;
}
clang::SourceLocation::
SourceLocation(clang::TranslationUnit *tu,
const std::string &filepath,
int offset) {
CXFile file = clang_getFile(tu->tu_,
filepath.c_str());
location_ = clang_getLocationForOffset(tu->tu_,
file,
offset);
unsigned clang::SourceLocation::get_offset() {
unsigned offset;
get_data(NULL, NULL, NULL, &offset);
return offset;
}
void clang::SourceLocation::
get_location_info(std::string* path,
unsigned *line,
unsigned *column,
unsigned *offset) {
void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) {
if(path==nullptr)
clang_getExpansionLocation(cx_location, NULL, line, column, offset);
else {
CXFile file;
clang_getExpansionLocation(location_, &file, line, column, offset);
if (path != NULL && file!=NULL) {
path->operator=(((clang_getCString((clang_getFileName(file))))));
clang_getExpansionLocation(cx_location, &file, line, column, offset);
if (file!=NULL) {
*path=clang::to_string(clang_getFileName(file));
}
}
}

37
src/SourceLocation.h

@ -1,39 +1,24 @@
#ifndef SOURCELOCATION_H_
#define SOURCELOCATION_H_
#include "TranslationUnit.h"
#include "Token.h"
#include "Cursor.h"
#include <clang-c/Index.h>
#include <string>
namespace clang {
class SourceLocation {
friend class TranslationUnit;
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset);
public:
SourceLocation(TranslationUnit* tu,
const std::string &filename,
int line_number,
int column);
SourceLocation(TranslationUnit *tu,
Token *token);
SourceLocation(SourceRange *range, bool start);
SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {}
SourceLocation(TranslationUnit *tu,
const std::string &filepath,
int offset);
SourceLocation(CXSourceLocation location) {location_=location;}
explicit SourceLocation(Cursor *cursor);
public:
std::string get_path();
unsigned get_offset();
void get_location_info(std::string* path,
unsigned *line,
unsigned *column,
unsigned *offset);
CXSourceLocation cx_location;
private:
CXSourceLocation location_;
friend SourceRange;
friend Cursor;
void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset);
};
} // namespace clang

17
src/SourceRange.cc

@ -1,15 +1,14 @@
#include "SourceRange.h"
clang::SourceRange::
SourceRange(clang::TranslationUnit *tu, clang::Token *token) {
range_ = clang_getTokenExtent(tu->tu_, token->token_);
SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) {
cx_range = clang_getRange(start.cx_location, end.cx_location);
}
clang::SourceRange::
SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) {
range_ = clang_getRange(start->location_, end->location_);
}
clang::SourceRange::SourceRange(Cursor *cursor) {
range_ = clang_getCursorExtent(cursor->cursor_);
std::pair<unsigned, unsigned> clang::SourceRange::get_offsets() {
SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range));
std::pair<unsigned, unsigned> offsets;
offsets.first=start.get_offset();
offsets.second=end.get_offset();
return offsets;
}

22
src/SourceRange.h

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

63
src/Token.cc

@ -1,32 +1,63 @@
#include "Token.h"
#include "Utility.h"
// // // // //
// Token //
// // // // //
// clang::Token instansiates an token
clang::Token::Token(const CXToken &token) :
token_(token) {
}
// returns gets an source location for this token objekt
// based on the translationunit given
clang::SourceLocation clang::Token::
get_source_location(clang::TranslationUnit *tu) {
return SourceLocation(tu, this);
clang::SourceLocation clang::Token::get_source_location() {
return SourceLocation(clang_getTokenLocation(cx_tu, cx_token));
}
// returns a sourcerange that covers this token
clang::SourceRange clang::Token::
get_source_range(clang::TranslationUnit *tu) {
return SourceRange(tu, this);
clang::SourceRange clang::Token::get_source_range() {
return SourceRange(clang_getTokenExtent(cx_tu, cx_token));
}
// returns a string description of this tokens kind
std::string clang::Token::get_token_spelling(clang::TranslationUnit *tu) {
CXString s = clang_getTokenSpelling(tu->tu_, token_);
return std::string(clang_getCString(s));
std::string clang::Token::get_spelling() {
return clang::to_string(clang_getTokenSpelling(cx_tu, cx_token));
}
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() {
return (TokenKind) clang_getTokenKind(token_);
std::string clang::Token::get_brief_comments() {
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;
}

30
src/Token.h

@ -1,8 +1,10 @@
#ifndef TOKEN_H_
#define TOKEN_H_
#include <clang-c/Index.h>
#include "SourceLocation.h"
#include "SourceRange.h"
#include "TranslationUnit.h"
#include "Cursor.h"
#include <string>
namespace clang {
enum TokenKind {
@ -14,18 +16,22 @@ namespace clang {
};
class Token {
friend class Tokens;
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()) {};
public:
const TokenKind kind();
std::string get_token_spelling(TranslationUnit *tu);
SourceLocation get_source_location(TranslationUnit *tu);
SourceRange get_source_range(TranslationUnit *tu);
std::string type;
private:
explicit Token(const CXToken &token);
friend SourceRange;
friend SourceLocation;
friend Tokens;
const CXToken& token_;
const TokenKind get_kind();
std::string get_spelling();
SourceLocation get_source_location();
SourceRange get_source_range();
clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);}
bool has_type();
std::string get_type();
std::string get_brief_comments();
CXTranslationUnit &cx_tu;
CXToken& cx_token;
CXCursor& cx_cursor;
std::pair<unsigned, unsigned> offsets;
};
} // namespace clang
#endif // TOKEN_H_

163
src/Tokens.cc

@ -1,136 +1,69 @@
#include "Tokens.h"
#include "Utility.h"
#include <iostream>
using namespace std;
clang::Tokens::Tokens(clang::TranslationUnit *tu, clang::SourceRange *range): tu(*tu) {
clang_tokenize(tu->tu_,
range->range_,
&tokens_,
&num_tokens_);
for (int i = 0; i < num_tokens_; i++) {
push_back(clang::Token(tokens_[i]));
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.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]));
}
}
clang::Tokens::~Tokens() {
clang_disposeTokens(tu.tu_, tokens_, size());
clang_disposeTokens(cx_tu, cx_tokens, size());
}
void clang::Tokens::update_types(clang::TranslationUnit *tu) {
clang_cursors.clear();
clang_cursors.reserve(size());
clang_annotateTokens(tu->tu_, tokens_, size(), clang_cursors.data());
for(size_t c=0;c<size();c++) {
auto referenced=clang_getCursorReferenced(clang_cursors[c]);
if(!clang_Cursor_isNull(referenced)) {
auto type=clang_getCursorType(referenced);
auto cxstr=clang_getTypeSpelling(type);
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);
//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(const std::string &usr) {
std::vector<std::pair<unsigned, unsigned> > 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);
}
//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::string comment_string;
auto referenced=clang_getCursorReferenced(clang_cursors[cursor_id]);
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);
}
}
std::vector<std::pair<std::string, unsigned> > clang::Tokens::get_cxx_methods() {
std::vector<std::pair<std::string, unsigned> > methods;
long last_offset=-1;
for(auto &token: *this) {
if(token.get_kind()==clang::Token_Identifier) {
auto cursor=token.get_cursor();
auto kind=cursor.get_kind();
if(kind==clang::CursorKind::CXXMethod || kind==clang::CursorKind::Constructor || kind==clang::CursorKind::Destructor) {
auto offset=cursor.get_source_location().get_offset();
if(offset!=last_offset) {
std::string method;
if(kind==clang::CursorKind::CXXMethod) {
auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor));
method+=clang::to_string(clang_getTypeSpelling(type));
auto pos=method.find(" ");
if(pos!=std::string::npos)
method.erase(pos, 1);
method+=" ";
}
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 comment_string;
return methods;
}

20
src/Tokens.h

@ -1,21 +1,25 @@
#ifndef TOKENS_H_
#define TOKENS_H_
#include "TranslationUnit.h"
#include <clang-c/Index.h>
#include "SourceRange.h"
#include "Token.h"
#include <unordered_map>
#include <vector>
namespace clang {
class Tokens : public std::vector<clang::Token> {
friend class TranslationUnit;
friend class Diagnostic;
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range);
public:
Tokens(TranslationUnit *tu, SourceRange *range);
~Tokens();
void update_types(clang::TranslationUnit *tu);
std::string get_brief_comments(size_t cursor_id);
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 *tokens_;
unsigned num_tokens_;
std::vector<CXCursor> clang_cursors;
TranslationUnit& tu;
CXToken *cx_tokens;
unsigned num_tokens;
std::vector<CXCursor> cx_cursors;
CXTranslationUnit& cx_tu;
};
} // namespace clang
#endif // TOKENS_H_

57
src/TranslationUnit.cc

@ -1,22 +1,17 @@
#include "TranslationUnit.h"
#include "SourceLocation.h"
#include "Tokens.h"
#include "Utility.h"
#include <fstream>
#include <sstream>
clang::TranslationUnit::
~TranslationUnit() {
clang_disposeTranslationUnit(tu_);
}
clang::TranslationUnit& clang::TranslationUnit::
operator=(const clang::TranslationUnit &tu) {
tu_ = tu.tu_;
return *this;
clang_disposeTranslationUnit(cx_tu);
}
clang::TranslationUnit::
TranslationUnit(Index *index,
TranslationUnit(Index &index,
const std::string &filepath,
const std::vector<std::string> &command_line_args) {
std::map<std::string, std::string> buffers;
@ -28,7 +23,7 @@ TranslationUnit(Index *index,
}
clang::TranslationUnit::
TranslationUnit(Index *index,
TranslationUnit(Index &index,
const std::string &filepath) {
std::vector<std::string> command_line_args;
std::map<std::string, std::string> buffers;
@ -40,7 +35,7 @@ TranslationUnit(Index *index,
}
clang::TranslationUnit::
TranslationUnit(clang::Index *index,
TranslationUnit(clang::Index &index,
const std::string &filepath,
const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers,
@ -48,7 +43,7 @@ TranslationUnit(clang::Index *index,
parse(index, filepath, command_line_args, buffers, flags);
}
void clang::TranslationUnit::parse(Index *index,
void clang::TranslationUnit::parse(Index &index,
const std::string &filepath,
const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers,
@ -65,8 +60,8 @@ void clang::TranslationUnit::parse(Index *index,
for(auto &a: command_line_args) {
args.push_back(a.c_str());
}
tu_ =
clang_parseTranslationUnit(index->index_,
cx_tu =
clang_parseTranslationUnit(index.cx_index,
filepath.c_str(),
args.data(),
args.size(),
@ -76,8 +71,7 @@ void clang::TranslationUnit::parse(Index *index,
}
int clang::TranslationUnit::
ReparseTranslationUnit(const std::string &file_path,
const std::map<std::string, std::string> &buffers,
ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
unsigned flags) {
std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) {
@ -87,7 +81,7 @@ ReparseTranslationUnit(const std::string &file_path,
file.Length = buffer.second.size();
files.push_back(file);
}
return clang_reparseTranslationUnit(tu_,
return clang_reparseTranslationUnit(cx_tu,
files.size(),
files.data(),
flags);
@ -97,11 +91,32 @@ unsigned clang::TranslationUnit::DefaultFlags() {
return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
}
void clang::TranslationUnit::update_diagnostics() {
diagnostics.clear();
for(unsigned c=0;c<clang_getNumDiagnostics(tu_);c++) {
CXDiagnostic clang_diagnostic=clang_getDiagnostic(tu_, c);
diagnostics.emplace_back(clang::Diagnostic(*this, clang_diagnostic));
clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map<std::string, std::string> &buffers, unsigned line_number, unsigned column) {
auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu));
clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column);
return results;
}
std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() {
std::vector<clang::Diagnostic> diagnostics;
for(unsigned c=0;c<clang_getNumDiagnostics(cx_tu);c++) {
CXDiagnostic clang_diagnostic=clang_getDiagnostic(cx_tu, c);
diagnostics.emplace_back(clang::Diagnostic(cx_tu, clang_diagnostic));
clang_disposeDiagnostic(clang_diagnostic);
}
return diagnostics;
}
std::unique_ptr<clang::Tokens> clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) {
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);
return std::unique_ptr<Tokens>(new Tokens(cx_tu, range));
}
clang::Cursor clang::TranslationUnit::get_cursor(std::string path, unsigned offset) {
clang::SourceLocation location(cx_tu, path, offset);
return Cursor(clang_getCursor(cx_tu, location.cx_location));
}

41
src/TranslationUnit.h

@ -7,51 +7,40 @@
#include <memory>
#include "Index.h"
#include "Diagnostic.h"
#include "Tokens.h"
#include "CodeCompleteResults.h"
#include "Cursor.h"
namespace clang {
class Token;
class Tokens;
class SourceLocation;
class SourceRange;
class Cursor;
class CodeCompleteResults;
class Diagnostic;
class TranslationUnit {
public:
TranslationUnit(Index *index,
TranslationUnit(Index &index,
const std::string &filepath,
const std::vector<std::string> &command_line_args);
TranslationUnit(Index *index,
TranslationUnit(Index &index,
const std::string &filepath,
const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers,
unsigned flags=DefaultFlags());
TranslationUnit(Index *index,
TranslationUnit(Index &index,
const std::string &filepath);
~TranslationUnit();
TranslationUnit& operator=(const TranslationUnit &tu);
int ReparseTranslationUnit(const std::string &file_path,
const std::map<std::string, std::string>
&buffers,
int ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
unsigned flags=DefaultFlags());
static unsigned DefaultFlags();
void update_diagnostics();
std::vector<clang::Diagnostic> diagnostics;
private:
void parse(Index *index,
void parse(Index &index,
const std::string &filepath,
const std::vector<std::string> &command_line_args,
const std::map<std::string, std::string> &buffers,
unsigned flags=DefaultFlags());
friend Token;
friend Tokens;
friend SourceLocation;
friend SourceRange;
friend Cursor;
friend CodeCompleteResults;
CXTranslationUnit tu_;
clang::CodeCompleteResults get_code_completions(const std::map<std::string, std::string> &buffers, unsigned line_number, unsigned column);
std::vector<clang::Diagnostic> get_diagnostics();
std::unique_ptr<Tokens> get_tokens(unsigned start_offset, unsigned end_offset);
clang::Cursor get_cursor(std::string path, unsigned offset);
CXTranslationUnit cx_tu;
};
} // namespace clang
#endif // TRANSLATIONUNIT_H_

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_

13
src/docs/windows-install.md

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

6
tests/CodeCompleteResults_H_Test.cc

@ -9,7 +9,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) {
std::string path("./case/main.cpp");
clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path);
clang::TranslationUnit tu(index, path);
// ReparseTranslationUnit takes a map with filepath as key
// and buffer as value
@ -28,10 +28,10 @@ BOOST_AUTO_TEST_CASE(code_complete_results) {
// ]
clang::CodeCompleteResults results(&tu, path, buffers, 4, 5);
auto results=tu.get_code_completions(buffers, 4, 5);
bool substr_found=false;
for(int c=0;c<results.size();c++) {
for(unsigned c=0;c<results.size();c++) {
if(results.get(c).get_chunks()[1].chunk=="substr") {
substr_found=true;
break;

5
tests/CompletionString_H_Test.cc

@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE(completion_string) {
std::string path("./case/main.cpp");
clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path);
clang::TranslationUnit tu(index, path);
// ReparseTranslationUnit takes a map with filepath as key
// and buffer as value
@ -33,8 +33,7 @@ BOOST_AUTO_TEST_CASE(completion_string) {
buffers[path] = file;
clang::CodeCompleteResults results(&tu, path, buffers, 4, 5);
auto results=tu.get_code_completions(buffers, 4, 5);
// ]
clang::CompletionString str = results.get(0);

7
tests/Cursor_H_Test.cc

@ -8,12 +8,11 @@ BOOST_AUTO_TEST_CASE(cursor) {
std::string path("./case/main.cpp");
clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path);
clang::TranslationUnit tu(index, path);
// ]
clang::SourceLocation location(&tu, path, 6, 4);
clang::Cursor cursor(&tu, &location);
auto cursor=tu.get_cursor(path, 103);
BOOST_CHECK(cursor.kind() == clang::CursorKind::ReturnStmt);
BOOST_CHECK(cursor.get_kind() == clang::CursorKind::ReturnStmt);
}

15
tests/Diagnostics_Test.cc

@ -18,15 +18,10 @@ BOOST_AUTO_TEST_CASE(diagnostics_test) {
map_buffers["./case/main_error.cpp"]=ss.str();
std::vector<std::string> args;
clang::TranslationUnit tu(&index, path, args, map_buffers);
clang::TranslationUnit tu(index, path, args, map_buffers);
tu.update_diagnostics();
BOOST_CHECK(tu.diagnostics.size()==1);
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].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);
auto diagnostics=tu.get_diagnostics();
BOOST_CHECK(diagnostics.size()==1);
BOOST_CHECK(diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'");
BOOST_CHECK(diagnostics[0].severity==3);
}

36
tests/SourceLocation_H_Test.cc

@ -7,37 +7,11 @@ BOOST_AUTO_TEST_CASE(source_location) {
clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path);
clang::SourceLocation start(&tu, path, 0);
clang::SourceLocation end(&tu, path, 7, 1);
clang::SourceRange range(&start, &end);
clang::Tokens tokens(&tu, &range);
clang::TranslationUnit tu(index, path);
auto tokens=tu.get_tokens(0, 113);
clang::SourceRange token_range = tokens[28].get_source_range(&tu);
auto offsets=(*tokens)[28].offsets;
unsigned token_start_line, token_start_column, token_start_offset,
token_end_line, token_end_column, token_end_offset;
std::string token_start_path, token_end_path;
clang::SourceLocation token_start(&token_range, true);
clang::SourceLocation token_end(&token_range, false);
token_start.get_location_info(&token_start_path,
&token_start_line,
&token_start_column,
&token_start_offset);
token_end.get_location_info(&token_end_path,
&token_end_line,
&token_end_column,
&token_end_offset);
BOOST_CHECK(token_start_path == path);
BOOST_CHECK(token_start_line == 6);
BOOST_CHECK(token_start_column == 3);
BOOST_CHECK(token_start_offset == 103);
BOOST_CHECK(token_end_path == path);
BOOST_CHECK(token_end_line == 6);
BOOST_CHECK(token_end_column == 9);
BOOST_CHECK(token_end_offset == 109);
BOOST_CHECK(offsets.first == 103);
BOOST_CHECK(offsets.second == 109);
}

14
tests/Token_H_Test.cc

@ -7,17 +7,13 @@ BOOST_AUTO_TEST_CASE(token) {
clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path);
clang::SourceLocation start(&tu, path, 0);
clang::SourceLocation end(&tu, path, 7, 1);
clang::TranslationUnit tu(index, path);
clang::SourceRange range(&start, &end);
auto tokens=tu.get_tokens(0, 113);
clang::Tokens tokens(&tu, &range);
BOOST_CHECK(tokens->size() == 32);
BOOST_CHECK((*tokens)[1].get_kind() == clang::TokenKind::Token_Identifier);
BOOST_CHECK(tokens.size() == 32);
BOOST_CHECK(tokens[1].kind() == clang::TokenKind::Token_Identifier);
std::string str = tokens[28].get_token_spelling(&tu);
std::string str = (*tokens)[28].get_spelling();
BOOST_CHECK(str == "return");
}

4
tests/TranslationUnit_Test.cc

@ -7,7 +7,7 @@ BOOST_AUTO_TEST_CASE(translation_unit) {
std::string path("./case/main.cpp");
clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path);
clang::TranslationUnit tu(index, path);
// ReparseTranslationUnit takes a map with filepath as key
// and buffer as value
@ -20,5 +20,5 @@ BOOST_AUTO_TEST_CASE(translation_unit) {
file.append("}");
buffers[path] = file;
BOOST_CHECK(tu.ReparseTranslationUnit(path, buffers) == 0);
BOOST_CHECK(tu.ReparseTranslationUnit(buffers) == 0);
}

Loading…
Cancel
Save