Browse Source

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

merge-requests/37/head
Jørgen Lien Sellæg 11 years ago
parent
commit
908e471450
  1. 14
      src/CodeCompleteResults.cc
  2. 10
      src/CodeCompleteResults.h
  3. 4
      src/CompilationDatabase.cc
  4. 6
      src/CompilationDatabase.h
  5. 13
      src/CompileCommand.cc
  6. 9
      src/CompileCommand.h
  7. 16
      src/CompileCommands.cc
  8. 7
      src/CompileCommands.h
  9. 14
      src/CompletionString.cc
  10. 10
      src/CompletionString.h
  11. 22
      src/Cursor.cc
  12. 18
      src/Cursor.h
  13. 27
      src/Diagnostic.cc
  14. 12
      src/Diagnostic.h
  15. 2
      src/Index.cc
  16. 6
      src/Index.h
  17. 32
      src/SourceLocation.cc
  18. 28
      src/SourceLocation.h
  19. 26
      src/SourceRange.cc
  20. 29
      src/SourceRange.h
  21. 109
      src/Token.cc
  22. 26
      src/Token.h
  23. 201
      src/Tokens.cc
  24. 18
      src/Tokens.h
  25. 55
      src/TranslationUnit.cc
  26. 39
      src/TranslationUnit.h
  27. 4
      tests/CodeCompleteResults_H_Test.cc
  28. 5
      tests/CompletionString_H_Test.cc
  29. 8
      tests/Cursor_H_Test.cc
  30. 16
      tests/Diagnostics_Test.cc
  31. 28
      tests/SourceLocation_H_Test.cc
  32. 14
      tests/Token_H_Test.cc
  33. 4
      tests/TranslationUnit_Test.cc

14
src/CodeCompleteResults.cc

@ -3,7 +3,7 @@
#include <exception> #include <exception>
clang::CodeCompleteResults:: clang::CodeCompleteResults::
CodeCompleteResults(clang::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, int line_num,
@ -16,24 +16,24 @@ 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_, cx_results = clang_codeCompleteAt(cx_tu,
file_name.c_str(), file_name.c_str(),
line_num, line_num,
column, column,
files.data(), files.data(),
files.size(), files.size(),
clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments); clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments);
clang_sortCodeCompletionResults(results_->Results, results_->NumResults); clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
} }
clang::CodeCompleteResults::~CodeCompleteResults() { clang::CodeCompleteResults::~CodeCompleteResults() {
delete[] results_->Results; delete[] cx_results->Results;
delete results_; delete cx_results;
} }
int clang::CodeCompleteResults:: int clang::CodeCompleteResults::
size() { size() {
return results_->NumResults; return cx_results->NumResults;
} }
clang::CompletionString clang::CodeCompleteResults:: clang::CompletionString clang::CodeCompleteResults::
@ -41,5 +41,5 @@ get(int i) {
if (i >= size()) { if (i >= size()) {
throw std::invalid_argument("clang::CodeCompleteResults::get(int i): i>=size()"); throw std::invalid_argument("clang::CodeCompleteResults::get(int i): i>=size()");
} }
return CompletionString(results_->Results[i].CompletionString); return CompletionString(cx_results->Results[i].CompletionString);
} }

10
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 &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, int line_num,
@ -17,8 +16,7 @@ namespace clang {
CompletionString get(int index); CompletionString get(int index);
int size(); int size();
private: CXCodeCompleteResults *cx_results;
CXCodeCompleteResults *results_;
}; };
} // namespace clang } // namespace clang
#endif // CODECOMPLETERESULTS_H_ #endif // CODECOMPLETERESULTS_H_

4
src/CompilationDatabase.cc

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

6
src/CompilationDatabase.h

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

13
src/CompileCommand.cc

@ -1,27 +1,22 @@
#include "CompileCommand.h" #include "CompileCommand.h"
#include "CompileCommands.h" #include "CompileCommands.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 (int i = 0; i < N; i++) {
res += clang_getCString(clang_CompileCommand_getArg(command_, i)); res += clang_getCString(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 (int i = 0; i < N; i++) {
res[i] = clang_getCString(clang_CompileCommand_getArg(command_, i)); res[i] = clang_getCString(clang_CompileCommand_getArg(cx_command, i));
} }
return res; return res;
} }

9
src/CompileCommand.h

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

16
src/CompileCommands.cc

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

7
src/CompileCommands.h

@ -9,12 +9,11 @@
namespace clang { namespace clang {
class CompileCommands { class CompileCommands {
public: public:
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 cx_commands;
friend class CompileCommand;
}; };
} }
#endif // COMPILECOMMANDS_H_ #endif // COMPILECOMMANDS_H_

14
src/CompletionString.cc

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

10
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 &cx_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 cx_str;
CXCompletionString str_;
friend CodeCompleteResults;
}; };
} // namespace clang } // namespace clang
#endif // COMPLETIONSTRING_H_ #endif // COMPLETIONSTRING_H_

22
src/Cursor.cc

@ -1,10 +1,24 @@
#include "Cursor.h" #include "Cursor.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::Cursor::
Cursor(clang::TranslationUnit *tu, clang::SourceLocation *source_location) { Cursor(CXTranslationUnit &cx_tu, clang::SourceLocation &source_location) {
cursor_ = clang_getCursor(tu->tu_, source_location->location_); cx_cursor = clang_getCursor(cx_tu, source_location.cx_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));
}
bool clang::Cursor::operator==(const Cursor& rhs) const {
auto lhs_rd=get_source_range().get_range_data();
auto rhs_rd=rhs.get_source_range().get_range_data();
return lhs_rd.path==rhs_rd.path && lhs_rd.start_offset==rhs_rd.start_offset && lhs_rd.end_offset==rhs_rd.end_offset;
} }

18
src/Cursor.h

@ -1,7 +1,8 @@
#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"
#include "SourceRange.h"
namespace clang { namespace clang {
enum class CursorKind { enum class CursorKind {
@ -174,14 +175,13 @@ namespace clang {
class Cursor { class Cursor {
public: public:
Cursor() {} Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {}
Cursor(TranslationUnit *tu, SourceLocation *source_location); Cursor(CXTranslationUnit &cx_tu, SourceLocation &source_location);
const CursorKind kind(); const CursorKind get_kind();
private: SourceLocation get_source_location() const;
CXCursor cursor_; SourceRange get_source_range() const;
friend SourceRange; bool operator==(const Cursor& rhs) const;
friend SourceLocation; CXCursor cx_cursor;
friend Tokens;
}; };
} // namespace clang } // namespace clang
#endif // CURSOR_H_ #endif // CURSOR_H_

27
src/Diagnostic.cc

@ -2,29 +2,18 @@
#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& cx_tu, CXDiagnostic& cx_diagnostic) {
severity=clang_getDiagnosticSeverity(clang_diagnostic); severity=clang_getDiagnosticSeverity(cx_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(cx_diagnostic));
clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic)); clang::SourceLocation location(clang_getDiagnosticLocation(cx_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(cx_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); auto locations=token.source_range.get_source_locations();
clang::SourceLocation location(&range, false); this->range=SourceRange::get_range_data(location, locations.second);
location.get_location_info(NULL, &line, &column, &offset);
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& cx_tu, CXDiagnostic& cx_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;
}; };
} }

2
src/Index.cc

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

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

32
src/SourceLocation.cc

@ -4,43 +4,25 @@
// 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_, cx_location = clang_getLocation(tu,
file, file,
line_number, line_number,
line_offset); 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(TranslationUnit *tu,
Token *token) {
location_ = clang_getTokenLocation(tu->tu_,
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_, cx_location = clang_getLocationForOffset(tu,
file, file,
offset); offset);
} }
@ -51,7 +33,7 @@ get_location_info(std::string* path,
unsigned *column, unsigned *column,
unsigned *offset) { unsigned *offset) {
CXFile file; CXFile file;
clang_getExpansionLocation(location_, &file, line, column, offset); clang_getExpansionLocation(cx_location, &file, line, column, offset);
if (path != NULL && file!=NULL) { if (path != NULL && file!=NULL) {
path->operator=(((clang_getCString((clang_getFileName(file)))))); path->operator=(((clang_getCString((clang_getFileName(file))))));
} }

28
src/SourceLocation.h

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

26
src/SourceRange.cc

@ -1,15 +1,27 @@
#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<clang::SourceLocation, clang::SourceLocation> clang::SourceRange::get_source_locations() {
SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) { return std::pair<SourceLocation, SourceLocation>(clang_getRangeStart(cx_range), clang_getRangeEnd(cx_range));
range_ = clang_getRange(start->location_, end->location_); }
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::SourceRange::SourceRange(Cursor *cursor) { clang::RangeData clang::SourceRange::get_range_data() {
range_ = clang_getCursorExtent(cursor->cursor_); auto locations=get_source_locations();
return get_range_data(locations.first, locations.second);
} }

29
src/SourceRange.h

@ -1,23 +1,24 @@
#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>
namespace clang { namespace clang {
class SourceRange { class RangeData {
public: public:
SourceRange() {} std::string path;
SourceRange(TranslationUnit *tu, Token *token); unsigned start_offset, end_offset;
SourceRange(SourceLocation *start, };
SourceLocation *end);
explicit SourceRange(Cursor *cursor);
private: class SourceRange {
CXSourceRange range_; public:
friend Tokens; SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {}
friend SourceLocation; SourceRange(SourceLocation &start, SourceLocation &end);
friend Diagnostic; std::pair<SourceLocation, SourceLocation> get_source_locations();
static RangeData get_range_data(SourceLocation &start, SourceLocation &end);
RangeData get_range_data();
CXSourceRange cx_range;
}; };
} // namespace clang } // namespace clang
#endif // SOURCERANGE_H_ #endif // SOURCERANGE_H_

109
src/Token.cc

@ -4,29 +4,110 @@
// 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_token_spelling() {
CXString s = clang_getTokenSpelling(tu->tu_, token_); CXString s = clang_getTokenSpelling(cx_tu, cx_token);
return std::string(clang_getCString(s)); return std::string(clang_getCString(s));
} }
const clang::TokenKind clang::Token::kind() { const clang::TokenKind clang::Token::kind() {
return (TokenKind) clang_getTokenKind(token_); return (TokenKind) clang_getTokenKind(cx_token);
}
bool clang::Token::has_type() {
auto referenced=clang_getCursorReferenced(cx_cursor);
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);
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);
auto cxstr=clang_getTypeSpelling(type);
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(cx_cursor));
auto cxstr=clang_getTypeSpelling(type);
spelling=clang_getCString(cxstr);
clang_disposeString(cxstr);
if(spelling.find(" ")==std::string::npos)
spelling+=auto_end;
}
}
return spelling;
}
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();
}
return comment_string;
} }

26
src/Token.h

@ -1,8 +1,10 @@
#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 "Cursor.h"
#include <string>
namespace clang { namespace clang {
enum TokenKind { enum TokenKind {
@ -15,17 +17,21 @@ namespace clang {
class Token { class Token {
public: public:
Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor):
cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), source_range(get_source_range()) {};
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); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);}
std::string type; bool has_type();
std::string get_type();
std::string get_brief_comments();
CXTranslationUnit &cx_tu;
CXToken& cx_token;
CXCursor& cx_cursor;
SourceRange source_range;
private: private:
explicit Token(const CXToken &token); SourceRange get_source_range();
friend SourceRange;
friend SourceLocation;
friend Tokens;
const CXToken& token_;
}; };
} // namespace clang } // namespace clang
#endif // TOKEN_H_ #endif // TOKEN_H_

201
src/Tokens.cc

@ -2,135 +2,102 @@
#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, SourceRange &range): cx_tu(cx_tu) {
clang_tokenize(tu->tu_,
range->range_, clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens);
&tokens_, cx_cursors.clear();
&num_tokens_); cx_cursors.reserve(num_tokens);
for (int i = 0; i < num_tokens_; i++) { clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data());
push_back(clang::Token(tokens_[i])); for (int i = 0; i < num_tokens; i++) {
emplace_back(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) { /*CXCursor clang::Tokens::find_referenced() {
clang_cursors.clear(); cursors.clear();
clang_cursors.reserve(size()); cursors.reserve(size());
clang_annotateTokens(tu->tu_, tokens_, size(), clang_cursors.data()); clang_annotateTokens(tu, tokens.data(), size(), cursors.data());
auto kind=clang_getCursorKind(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(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;
}*/
void clang::Tokens::rename(CXCursor &referenced) {
for(size_t c=0;c<size();c++) { for(size_t c=0;c<size();c++) {
auto referenced=clang_getCursorReferenced(clang_cursors[c]); auto a_referenced=clang_getCursorReferenced(cx_cursors[c]);
if(!clang_Cursor_isNull(referenced)) { if(Cursor(a_referenced)==Cursor(referenced)) {
auto type=clang_getCursorType(referenced); std::string path;
auto cxstr=clang_getTypeSpelling(type); unsigned line, column, offset;
std::string spelling=clang_getCString(cxstr);
clang_disposeString(cxstr); clang::Cursor cursor(cx_cursors[c]);
std::string auto_end=""; auto range=cursor.get_source_range();
//TODO fix const auto auto locations=range.get_source_locations();
if((spelling.size()>=4 && spelling.substr(0, 4)=="auto")) { locations.first.get_location_info(&path, &line, &column, &offset);
auto_end=spelling.substr(4); cout << " start: " << path << ", " << line << ", " << column << endl;
auto type=clang_getCanonicalType(clang_getCursorType(clang_cursors[c])); locations.second.get_location_info(&path, &line, &column, &offset);
auto cxstr=clang_getTypeSpelling(type); cout << " end: " << path << ", " << line << ", " << column << endl;
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);
clang::Cursor referenced_cursor(a_referenced);
range=referenced_cursor.get_source_range();
locations=range.get_source_locations();
locations.first.get_location_info(&path, &line, &column, &offset);
cout << " start: " << path << ", " << line << ", " << column << endl;
locations.second.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;
} }
//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;
}
}*/
} }
//clang_visitChildren(clang_getTranslationUnitCursor(tu.tu_), clang::Tokens::clang_visitor, &referenced);
} }
std::string clang::Tokens::get_brief_comments(size_t cursor_id) { /*CXChildVisitResult clang::Tokens::clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData) {
std::string comment_string; CXCursor* referenced=(CXCursor*)clientData;
auto referenced=clang_getCursorReferenced(clang_cursors[cursor_id]); auto a_referenced=clang_getCursorReferenced(cursor);
auto comment=clang_Cursor_getParsedComment(referenced); if(clang_equalCursors(a_referenced, *referenced)) {
if(clang_Comment_getKind(comment)==CXComment_FullComment) { cout << "found" << endl;
size_t para_c=0; clang::Cursor a_cursor;
for(unsigned c=0;c<clang_Comment_getNumChildren(comment);c++) { a_cursor.cursor_=cursor;
auto child_comment=clang_Comment_getChild(comment, c); auto range=clang::SourceRange(&a_cursor);
if(clang_Comment_getKind(child_comment)==CXComment_Paragraph) {
para_c++; auto location=clang::SourceLocation(range, true);
if(para_c>=2) std::string path;
break; unsigned line, column, offset;
for(unsigned c=0;c<clang_Comment_getNumChildren(child_comment);c++) { location.get_location_info(&path, &line, &column, &offset);
auto grandchild_comment=clang_Comment_getChild(child_comment, c); cout << " start: " << path << ", " << line << ", " << column << endl;
if(clang_Comment_getKind(grandchild_comment)==CXComment_Text) {
auto cxstr=clang_TextComment_getText(grandchild_comment); location=clang::SourceLocation(range, false);
comment_string+=clang_getCString(cxstr); location.get_location_info(&path, &line, &column, &offset);
comment_string+="\n"; cout << " end: " << path << ", " << line << ", " << column << endl;
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();
} }
return comment_string; return CXChildVisit_Recurse;
} }*/

18
src/Tokens.h

@ -1,21 +1,23 @@
#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 &cx_tu, SourceRange &range);
~Tokens(); ~Tokens();
void update_types(clang::TranslationUnit *tu); void rename(CXCursor &referenced);
std::string get_brief_comments(size_t cursor_id); //std::unordered_map<std::string, std::unordered_multimap<unsigned, unsigned> >
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_

55
src/TranslationUnit.cc

@ -6,17 +6,11 @@
clang::TranslationUnit:: clang::TranslationUnit::
~TranslationUnit() { ~TranslationUnit() {
clang_disposeTranslationUnit(tu_); clang_disposeTranslationUnit(cx_tu);
}
clang::TranslationUnit& clang::TranslationUnit::
operator=(const clang::TranslationUnit &tu) {
tu_ = tu.tu_;
return *this;
} }
clang::TranslationUnit:: clang::TranslationUnit::
TranslationUnit(Index *index, TranslationUnit(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) {
std::map<std::string, std::string> buffers; std::map<std::string, std::string> buffers;
@ -28,7 +22,7 @@ TranslationUnit(Index *index,
} }
clang::TranslationUnit:: clang::TranslationUnit::
TranslationUnit(Index *index, TranslationUnit(Index &index,
const std::string &filepath) { const std::string &filepath) {
std::vector<std::string> command_line_args; std::vector<std::string> command_line_args;
std::map<std::string, std::string> buffers; std::map<std::string, std::string> buffers;
@ -40,7 +34,7 @@ TranslationUnit(Index *index,
} }
clang::TranslationUnit:: clang::TranslationUnit::
TranslationUnit(clang::Index *index, TranslationUnit(clang::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,
@ -48,7 +42,7 @@ TranslationUnit(clang::Index *index,
parse(index, filepath, command_line_args, buffers, flags); 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::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,
@ -65,8 +59,8 @@ void clang::TranslationUnit::parse(Index *index,
for(auto &a: command_line_args) { for(auto &a: command_line_args) {
args.push_back(a.c_str()); args.push_back(a.c_str());
} }
tu_ = cx_tu =
clang_parseTranslationUnit(index->index_, clang_parseTranslationUnit(index.cx_index,
filepath.c_str(), filepath.c_str(),
args.data(), args.data(),
args.size(), args.size(),
@ -76,8 +70,7 @@ void clang::TranslationUnit::parse(Index *index,
} }
int clang::TranslationUnit:: int clang::TranslationUnit::
ReparseTranslationUnit(const std::string &file_path, ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
const std::map<std::string, std::string> &buffers,
unsigned flags) { unsigned flags) {
std::vector<CXUnsavedFile> files; std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) { for (auto &buffer : buffers) {
@ -87,7 +80,7 @@ ReparseTranslationUnit(const std::string &file_path,
file.Length = buffer.second.size(); file.Length = buffer.second.size();
files.push_back(file); files.push_back(file);
} }
return clang_reparseTranslationUnit(tu_, return clang_reparseTranslationUnit(cx_tu,
files.size(), files.size(),
files.data(), files.data(),
flags); flags);
@ -97,11 +90,31 @@ unsigned clang::TranslationUnit::DefaultFlags() {
return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
} }
void clang::TranslationUnit::update_diagnostics() { clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map<std::string, std::string> &buffers, int line_number, int column) {
diagnostics.clear(); auto cxstr=clang_getTranslationUnitSpelling(cx_tu);
for(unsigned c=0;c<clang_getNumDiagnostics(tu_);c++) { std::string path=clang_getCString(cxstr);
CXDiagnostic clang_diagnostic=clang_getDiagnostic(tu_, c); clang_disposeString(cxstr);
diagnostics.emplace_back(clang::Diagnostic(*this, clang_diagnostic));
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); clang_disposeDiagnostic(clang_diagnostic);
} }
return 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);
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));
} }

39
src/TranslationUnit.h

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

4
tests/CodeCompleteResults_H_Test.cc

@ -9,7 +9,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) {
std::string path("./case/main.cpp"); std::string path("./case/main.cpp");
clang::Index index(0, 0); clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path); clang::TranslationUnit tu(index, path);
// ReparseTranslationUnit takes a map with filepath as key // ReparseTranslationUnit takes a map with filepath as key
// and buffer as value // and buffer as value
@ -28,7 +28,7 @@ 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; bool substr_found=false;
for(int c=0;c<results.size();c++) { for(int c=0;c<results.size();c++) {

5
tests/CompletionString_H_Test.cc

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

8
tests/Cursor_H_Test.cc

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

16
tests/Diagnostics_Test.cc

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

28
tests/SourceLocation_H_Test.cc

@ -7,30 +7,26 @@ 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); auto tokens=tu.get_tokens(0, 113);
clang::SourceLocation end(&tu, path, 7, 1);
clang::SourceRange range(&start, &end);
clang::Tokens tokens(&tu, &range);
clang::SourceRange token_range = tokens[28].get_source_range(&tu); clang::SourceRange token_range = (*tokens)[28].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;
std::string token_start_path, token_end_path; std::string token_start_path, token_end_path;
clang::SourceLocation token_start(&token_range, true); auto locations=token_range.get_source_locations();
clang::SourceLocation token_end(&token_range, false);
token_start.get_location_info(&token_start_path, locations.first.get_location_info(&token_start_path,
&token_start_line, &token_start_line,
&token_start_column, &token_start_column,
&token_start_offset); &token_start_offset);
token_end.get_location_info(&token_end_path, locations.second.get_location_info(&token_end_path,
&token_end_line, &token_end_line,
&token_end_column, &token_end_column,
&token_end_offset); &token_end_offset);
BOOST_CHECK(token_start_path == path); BOOST_CHECK(token_start_path == path);
BOOST_CHECK(token_start_line == 6); BOOST_CHECK(token_start_line == 6);

14
tests/Token_H_Test.cc

@ -7,17 +7,13 @@ 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 end(&tu, path, 7, 1);
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].kind() == clang::TokenKind::Token_Identifier);
BOOST_CHECK(tokens.size() == 32); std::string str = (*tokens)[28].get_token_spelling();
BOOST_CHECK(tokens[1].kind() == clang::TokenKind::Token_Identifier);
std::string str = tokens[28].get_token_spelling(&tu);
BOOST_CHECK(str == "return"); 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"); std::string path("./case/main.cpp");
clang::Index index(0, 0); clang::Index index(0, 0);
clang::TranslationUnit tu(&index, path); clang::TranslationUnit tu(index, path);
// ReparseTranslationUnit takes a map with filepath as key // ReparseTranslationUnit takes a map with filepath as key
// and buffer as value // and buffer as value
@ -20,5 +20,5 @@ BOOST_AUTO_TEST_CASE(translation_unit) {
file.append("}"); file.append("}");
buffers[path] = file; buffers[path] = file;
BOOST_CHECK(tu.ReparseTranslationUnit(path, buffers) == 0); BOOST_CHECK(tu.ReparseTranslationUnit(buffers) == 0);
} }

Loading…
Cancel
Save