Browse Source

Merge pull request #19 from eidheim/master

Fixes a bug where not all of the similar types gets returned.
merge-requests/37/head
Jørgen Lien Sellæg 10 years ago
parent
commit
df393cb412
  1. 1
      src/Cursor.h
  2. 1
      src/Token.h
  3. 8
      src/Tokens.cc
  4. 60
      src/TranslationUnit.cc
  5. 9
      src/TranslationUnit.h

1
src/Cursor.h

@ -184,6 +184,7 @@ namespace clang {
Cursor get_referenced() const; Cursor get_referenced() const;
operator bool() const; operator bool() const;
bool operator==(const Cursor& rhs) const; bool operator==(const Cursor& rhs) const;
CXCursor cx_cursor; CXCursor cx_cursor;
}; };
} // namespace clang } // namespace clang

1
src/Token.h

@ -28,6 +28,7 @@ namespace clang {
bool has_type(); bool has_type();
std::string get_type(); std::string get_type();
std::string get_brief_comments(); std::string get_brief_comments();
CXTranslationUnit &cx_tu; CXTranslationUnit &cx_tu;
CXToken& cx_token; CXToken& cx_token;
CXCursor& cx_cursor; CXCursor& cx_cursor;

8
src/Tokens.cc

@ -1,7 +1,7 @@
#include "Tokens.h" #include "Tokens.h"
#include "Utility.h" #include "Utility.h"
#include <iostream> #include <iostream> //TODO: remove
using namespace std; using namespace std; //TODO: remove
clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) { clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) {
clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens); clang_tokenize(cx_tu, range.cx_range, &cx_tokens, &num_tokens);
@ -9,6 +9,10 @@ clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu
cx_cursors.resize(num_tokens); cx_cursors.resize(num_tokens);
clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data()); clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data());
for (unsigned i = 0; i < num_tokens; i++) { for (unsigned i = 0; i < num_tokens; i++) {
if(cx_cursors[i].kind==CXCursor_DeclRefExpr) { //Temporary fix to a libclang bug
auto real_cursor=clang_getCursor(cx_tu, clang_getTokenLocation(cx_tu, cx_tokens[i]));
cx_cursors[i]=real_cursor;
}
emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i])); emplace_back(Token(cx_tu, cx_tokens[i], cx_cursors[i]));
} }
} }

60
src/TranslationUnit.cc

@ -5,49 +5,43 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> //TODO: remove
using namespace std; //TODO: remove
clang::TranslationUnit:: clang::TranslationUnit::
~TranslationUnit() { ~TranslationUnit() {
clang_disposeTranslationUnit(cx_tu); clang_disposeTranslationUnit(cx_tu);
} }
clang::TranslationUnit:: clang::TranslationUnit::
TranslationUnit(Index &index, TranslationUnit(Index &index, const std::string &file_path, const std::vector<std::string> &command_line_args) {
const std::string &filepath,
const std::vector<std::string> &command_line_args) {
std::map<std::string, std::string> buffers; std::map<std::string, std::string> buffers;
std::ifstream ifs(filepath, std::ifstream::in); std::ifstream ifs(file_path, std::ifstream::in);
std::stringstream ss; std::stringstream ss;
ss << ifs.rdbuf(); ss << ifs.rdbuf();
buffers[filepath]=ss.str(); buffers[file_path]=ss.str();
parse(index, filepath, command_line_args, buffers); parse(index, file_path, command_line_args, buffers);
} }
clang::TranslationUnit:: clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path) {
TranslationUnit(Index &index,
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;
std::ifstream ifs(filepath, std::ifstream::in); std::ifstream ifs(file_path, std::ifstream::in);
std::stringstream ss; std::stringstream ss;
ss << ifs.rdbuf(); ss << ifs.rdbuf();
buffers[filepath]=ss.str(); buffers[file_path]=ss.str();
parse(index, filepath, command_line_args, buffers); parse(index, file_path, command_line_args, buffers);
} }
clang::TranslationUnit:: clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path,
TranslationUnit(clang::Index &index,
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) {
unsigned flags) { parse(index, file_path, 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 &file_path,
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) {
unsigned flags) {
std::vector<CXUnsavedFile> files; std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) { for (auto &buffer : buffers) {
CXUnsavedFile file; CXUnsavedFile file;
@ -60,19 +54,11 @@ 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());
} }
cx_tu = cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
clang_parseTranslationUnit(index.cx_index, args.size(), files.data(), files.size(), flags);
filepath.c_str(),
args.data(),
args.size(),
files.data(),
files.size(),
flags);
} }
int clang::TranslationUnit:: int clang::TranslationUnit::ReparseTranslationUnit(const std::map<std::string, std::string> &buffers, unsigned flags) {
ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
unsigned flags) {
std::vector<CXUnsavedFile> files; std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) { for (auto &buffer : buffers) {
CXUnsavedFile file; CXUnsavedFile file;
@ -81,17 +67,15 @@ ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
file.Length = buffer.second.size(); file.Length = buffer.second.size();
files.push_back(file); files.push_back(file);
} }
return clang_reparseTranslationUnit(cx_tu, return clang_reparseTranslationUnit(cx_tu, files.size(), files.data(), flags);
files.size(),
files.data(),
flags);
} }
unsigned clang::TranslationUnit::DefaultFlags() { unsigned clang::TranslationUnit::DefaultFlags() {
return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
} }
clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map<std::string, std::string> &buffers, unsigned line_number, unsigned column) { 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)); auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu));
clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column); clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column);

9
src/TranslationUnit.h

@ -15,22 +15,21 @@ namespace clang {
class TranslationUnit { class TranslationUnit {
public: public:
TranslationUnit(Index &index, TranslationUnit(Index &index,
const std::string &filepath, const std::string &file_path,
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 &file_path,
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 &file_path);
const std::string &filepath);
~TranslationUnit(); ~TranslationUnit();
int ReparseTranslationUnit(const std::map<std::string, std::string> &buffers, int ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
unsigned flags=DefaultFlags()); unsigned flags=DefaultFlags());
static unsigned DefaultFlags(); static unsigned DefaultFlags();
void parse(Index &index, void parse(Index &index,
const std::string &filepath, const std::string &file_path,
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());

Loading…
Cancel
Save