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;
operator bool() const;
bool operator==(const Cursor& rhs) const;
CXCursor cx_cursor;
};
} // namespace clang

1
src/Token.h

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

8
src/Tokens.cc

@ -1,7 +1,7 @@
#include "Tokens.h"
#include "Utility.h"
#include <iostream>
using namespace std;
#include <iostream> //TODO: remove
using namespace std; //TODO: remove
clang::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range): cx_tu(cx_tu) {
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);
clang_annotateTokens(cx_tu, cx_tokens, num_tokens, cx_cursors.data());
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]));
}
}

60
src/TranslationUnit.cc

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

9
src/TranslationUnit.h

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

Loading…
Cancel
Save