Browse Source

Cleanup of TranslationUnit and CodeCompleteResults.

merge-requests/37/head
eidheim 10 years ago
parent
commit
decd187c09
  1. 23
      src/CodeCompleteResults.cc
  2. 4
      src/CodeCompleteResults.h
  3. 61
      src/TranslationUnit.cc
  4. 13
      src/TranslationUnit.h
  5. 24
      tests/CodeCompleteResults_H_Test.cc
  6. 24
      tests/CompletionString_H_Test.cc
  7. 2
      tests/Cursor_H_Test.cc
  8. 10
      tests/Diagnostics_Test.cc
  9. 2
      tests/SourceLocation_H_Test.cc
  10. 2
      tests/Token_H_Test.cc
  11. 17
      tests/TranslationUnit_Test.cc

23
src/CodeCompleteResults.cc

@ -23,29 +23,6 @@ clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
}
clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
const std::string &file_name,
const std::map<std::string, std::string> &buffers,
unsigned line_num, unsigned column) {
std::vector<CXUnsavedFile> files;
for (auto &buffer : buffers) {
CXUnsavedFile file;
file.Filename = buffer.first.c_str();
file.Contents = buffer.second.c_str();
file.Length = buffer.second.size();
files.push_back(file);
}
cx_results = clang_codeCompleteAt(cx_tu,
file_name.c_str(),
line_num,
column,
files.data(),
files.size(),
clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments);
if(cx_results!=NULL)
clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
}
clang::CodeCompleteResults::~CodeCompleteResults() {
clang_disposeCodeCompleteResults(cx_results);
}

4
src/CodeCompleteResults.h

@ -11,10 +11,6 @@ namespace clang {
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer,
unsigned line_num, unsigned column);
//TODO: remove
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_path,
const std::map<std::string, std::string> &buffers,
unsigned line_num, unsigned column);
public:
~CodeCompleteResults();
CompletionString get(unsigned index) const;

61
src/TranslationUnit.cc

@ -8,31 +8,6 @@
#include <iostream> //TODO: remove
using namespace std; //TODO: remove
clang::TranslationUnit::
~TranslationUnit() {
clang_disposeTranslationUnit(cx_tu);
}
clang::TranslationUnit::
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(file_path, std::ifstream::in);
std::stringstream ss;
ss << ifs.rdbuf();
buffers[file_path]=ss.str();
parse(index, file_path, command_line_args, buffers);
}
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(file_path, std::ifstream::in);
std::stringstream ss;
ss << ifs.rdbuf();
buffers[file_path]=ss.str();
parse(index, file_path, command_line_args, buffers);
}
clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::string &buffer, unsigned flags) {
@ -50,10 +25,20 @@ clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_pa
args.size(), files, 1, flags);
}
clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path,
clang::TranslationUnit::TranslationUnit(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, file_path, command_line_args, buffers, flags);
unsigned flags) {
std::vector<const char*> args;
for(auto &a: command_line_args) {
args.push_back(a.c_str());
}
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
args.size(), NULL, 0, flags);
}
clang::TranslationUnit::~TranslationUnit() {
clang_disposeTranslationUnit(cx_tu);
}
void clang::TranslationUnit::parse(Index &index, const std::string &file_path,
@ -87,18 +72,6 @@ int clang::TranslationUnit::ReparseTranslationUnit(const std::string &buffer, un
return clang_reparseTranslationUnit(cx_tu, 1, files, 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;
file.Filename = buffer.first.c_str();
file.Contents = buffer.second.c_str();
file.Length = buffer.second.size();
files.push_back(file);
}
return clang_reparseTranslationUnit(cx_tu, files.size(), files.data(), flags);
}
unsigned clang::TranslationUnit::DefaultFlags() {
return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
}
@ -109,14 +82,6 @@ clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const st
return results;
}
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++) {

13
src/TranslationUnit.h

@ -14,28 +14,18 @@
namespace clang {
class TranslationUnit {
public:
//TODO: remove
TranslationUnit(Index &index,
const std::string &file_path,
const std::vector<std::string> &command_line_args);
TranslationUnit(Index &index,
const std::string &file_path,
const std::vector<std::string> &command_line_args,
const std::string &buffer,
unsigned flags=DefaultFlags());
//TODO: remove
TranslationUnit(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=DefaultFlags());
TranslationUnit(Index &index, const std::string &file_path);
~TranslationUnit();
int ReparseTranslationUnit(const std::string &buffer, unsigned flags=DefaultFlags());
//TODO: remove
int ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
unsigned flags=DefaultFlags());
static unsigned DefaultFlags();
@ -47,9 +37,6 @@ namespace clang {
clang::CodeCompleteResults get_code_completions(const std::string &buffer,
unsigned line_number, unsigned column);
//TODO: remove
clang::CodeCompleteResults get_code_completions(const std::map<std::string, std::string> &buffers,
unsigned line_number, unsigned column);
std::vector<clang::Diagnostic> get_diagnostics();

24
tests/CodeCompleteResults_H_Test.cc

@ -9,26 +9,18 @@ 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
std::map<std::string, std::string> buffers;
// create buffer
std::string file;
file.append("#include <string>\n");
file.append("int main(int argc, char *argv[]) {\n");
file.append("std::string str;\n");
file.append("str.\n");
file.append("return 0\n");
file.append("}");
buffers[path] = file;
std::string buffer="#include <string>\n"
"int main(int argc, char *argv[]) {\n"
"std::string str;\n"
"str.\n"
"return 0\n"
"}";
// ]
auto results=tu.get_code_completions(buffers, 4, 5);
auto results=tu.get_code_completions(buffer, 4, 5);
bool substr_found=false;
for(unsigned c=0;c<results.size();c++) {

24
tests/CompletionString_H_Test.cc

@ -16,24 +16,16 @@ 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
std::map<std::string, std::string> buffers;
std::string buffer="#include <string>\n"
"int main(int argc, char *argv[]) {\n"
"std::string str;\n"
"str.\n"
"return 0\n"
"}";
// create buffer
std::string file;
file.append("#include <string>\n");
file.append("int main(int argc, char *argv[]) {\n");
file.append("std::string str;\n");
file.append("str.\n");
file.append("return 0\n");
file.append("}");
buffers[path] = file;
auto results=tu.get_code_completions(buffers, 4, 5);
auto results=tu.get_code_completions(buffer, 4, 5);
// ]
clang::CompletionString str = results.get(0);

2
tests/Cursor_H_Test.cc

@ -8,7 +8,7 @@ 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, {});
// ]

10
tests/Diagnostics_Test.cc

@ -10,15 +10,7 @@ BOOST_AUTO_TEST_CASE(diagnostics_test) {
clang::Index index(0, 0);
std::map<std::string, std::string> map_buffers;
ifstream ifs(path, ifstream::in);
stringstream ss;
ss << ifs.rdbuf();
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, {});
auto diagnostics=tu.get_diagnostics();
BOOST_CHECK(diagnostics.size()==1);

2
tests/SourceLocation_H_Test.cc

@ -7,7 +7,7 @@ BOOST_AUTO_TEST_CASE(source_location) {
clang::Index index(0, 0);
clang::TranslationUnit tu(index, path);
clang::TranslationUnit tu(index, path, {});
auto tokens=tu.get_tokens(0, 113);
auto offsets=(*tokens)[28].offsets;

2
tests/Token_H_Test.cc

@ -7,7 +7,7 @@ BOOST_AUTO_TEST_CASE(token) {
clang::Index index(0, 0);
clang::TranslationUnit tu(index, path);
clang::TranslationUnit tu(index, path, {});
auto tokens=tu.get_tokens(0, 113);

17
tests/TranslationUnit_Test.cc

@ -7,18 +7,13 @@ BOOST_AUTO_TEST_CASE(translation_unit) {
std::string path("./case/main.cpp");
clang::Index index(0, 0);
clang::TranslationUnit tu(index, path);
// ReparseTranslationUnit takes a map with filepath as key
// and buffer as value
std::map<std::string, std::string> buffers;
clang::TranslationUnit tu(index, path, {});
// create buffer
std::string file = "int main(int argc, char *argv[]) {\n";
file.append("std::cout << \"Hello World!\" << std::endl;\n");
file.append("return 0\n");
file.append("}");
std::string buffer = "int main(int argc, char *argv[]) {\n"
"std::cout << \"Hello World!\" << std::endl;\n"
"return 0\n"
"}\n";
buffers[path] = file;
BOOST_CHECK(tu.ReparseTranslationUnit(buffers) == 0);
BOOST_CHECK(tu.ReparseTranslationUnit(buffer) == 0);
}

Loading…
Cancel
Save