Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit c1f4eef

Browse files
committed
Merge pull request #23 from eidheim/master
Cleanup of TranslationUnit and CodeCompleteResults.
2 parents 9fb4553 + decd187 commit c1f4eef

11 files changed

+41
-145
lines changed

src/CodeCompleteResults.cc

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,6 @@ clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
2323
clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
2424
}
2525

26-
clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
27-
const std::string &file_name,
28-
const std::map<std::string, std::string> &buffers,
29-
unsigned line_num, unsigned column) {
30-
std::vector<CXUnsavedFile> files;
31-
for (auto &buffer : buffers) {
32-
CXUnsavedFile file;
33-
file.Filename = buffer.first.c_str();
34-
file.Contents = buffer.second.c_str();
35-
file.Length = buffer.second.size();
36-
files.push_back(file);
37-
}
38-
cx_results = clang_codeCompleteAt(cx_tu,
39-
file_name.c_str(),
40-
line_num,
41-
column,
42-
files.data(),
43-
files.size(),
44-
clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments);
45-
if(cx_results!=NULL)
46-
clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
47-
}
48-
4926
clang::CodeCompleteResults::~CodeCompleteResults() {
5027
clang_disposeCodeCompleteResults(cx_results);
5128
}

src/CodeCompleteResults.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ namespace clang {
1111

1212
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer,
1313
unsigned line_num, unsigned column);
14-
//TODO: remove
15-
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_path,
16-
const std::map<std::string, std::string> &buffers,
17-
unsigned line_num, unsigned column);
1814
public:
1915
~CodeCompleteResults();
2016
CompletionString get(unsigned index) const;

src/TranslationUnit.cc

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,6 @@
88
#include <iostream> //TODO: remove
99
using namespace std; //TODO: remove
1010

11-
clang::TranslationUnit::
12-
~TranslationUnit() {
13-
clang_disposeTranslationUnit(cx_tu);
14-
}
15-
16-
clang::TranslationUnit::
17-
TranslationUnit(Index &index, const std::string &file_path, const std::vector<std::string> &command_line_args) {
18-
std::map<std::string, std::string> buffers;
19-
std::ifstream ifs(file_path, std::ifstream::in);
20-
std::stringstream ss;
21-
ss << ifs.rdbuf();
22-
buffers[file_path]=ss.str();
23-
parse(index, file_path, command_line_args, buffers);
24-
}
25-
26-
clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path) {
27-
std::vector<std::string> command_line_args;
28-
std::map<std::string, std::string> buffers;
29-
std::ifstream ifs(file_path, std::ifstream::in);
30-
std::stringstream ss;
31-
ss << ifs.rdbuf();
32-
buffers[file_path]=ss.str();
33-
parse(index, file_path, command_line_args, buffers);
34-
}
35-
3611
clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
3712
const std::vector<std::string> &command_line_args,
3813
const std::string &buffer, unsigned flags) {
@@ -50,10 +25,20 @@ clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_pa
5025
args.size(), files, 1, flags);
5126
}
5227

53-
clang::TranslationUnit::TranslationUnit(clang::Index &index, const std::string &file_path,
54-
const std::vector<std::string> &command_line_args,
55-
const std::map<std::string, std::string> &buffers, unsigned flags) {
56-
parse(index, file_path, command_line_args, buffers, flags);
28+
clang::TranslationUnit::TranslationUnit(Index &index, const std::string &file_path,
29+
const std::vector<std::string> &command_line_args,
30+
unsigned flags) {
31+
std::vector<const char*> args;
32+
for(auto &a: command_line_args) {
33+
args.push_back(a.c_str());
34+
}
35+
36+
cx_tu = clang_parseTranslationUnit(index.cx_index, file_path.c_str(), args.data(),
37+
args.size(), NULL, 0, flags);
38+
}
39+
40+
clang::TranslationUnit::~TranslationUnit() {
41+
clang_disposeTranslationUnit(cx_tu);
5742
}
5843

5944
void clang::TranslationUnit::parse(Index &index, const std::string &file_path,
@@ -87,18 +72,6 @@ int clang::TranslationUnit::ReparseTranslationUnit(const std::string &buffer, un
8772
return clang_reparseTranslationUnit(cx_tu, 1, files, flags);
8873
}
8974

90-
int clang::TranslationUnit::ReparseTranslationUnit(const std::map<std::string, std::string> &buffers, unsigned flags) {
91-
std::vector<CXUnsavedFile> files;
92-
for (auto &buffer : buffers) {
93-
CXUnsavedFile file;
94-
file.Filename = buffer.first.c_str();
95-
file.Contents = buffer.second.c_str();
96-
file.Length = buffer.second.size();
97-
files.push_back(file);
98-
}
99-
return clang_reparseTranslationUnit(cx_tu, files.size(), files.data(), flags);
100-
}
101-
10275
unsigned clang::TranslationUnit::DefaultFlags() {
10376
return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
10477
}
@@ -109,14 +82,6 @@ clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const st
10982
return results;
11083
}
11184

112-
clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map<std::string, std::string> &buffers,
113-
unsigned line_number, unsigned column) {
114-
auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu));
115-
116-
clang::CodeCompleteResults results(cx_tu, path, buffers, line_number, column);
117-
return results;
118-
}
119-
12085
std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() {
12186
std::vector<clang::Diagnostic> diagnostics;
12287
for(unsigned c=0;c<clang_getNumDiagnostics(cx_tu);c++) {

src/TranslationUnit.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,18 @@
1414
namespace clang {
1515
class TranslationUnit {
1616
public:
17-
//TODO: remove
18-
TranslationUnit(Index &index,
19-
const std::string &file_path,
20-
const std::vector<std::string> &command_line_args);
2117
TranslationUnit(Index &index,
2218
const std::string &file_path,
2319
const std::vector<std::string> &command_line_args,
2420
const std::string &buffer,
2521
unsigned flags=DefaultFlags());
26-
//TODO: remove
2722
TranslationUnit(Index &index,
2823
const std::string &file_path,
2924
const std::vector<std::string> &command_line_args,
30-
const std::map<std::string, std::string> &buffers,
3125
unsigned flags=DefaultFlags());
32-
TranslationUnit(Index &index, const std::string &file_path);
3326
~TranslationUnit();
3427

3528
int ReparseTranslationUnit(const std::string &buffer, unsigned flags=DefaultFlags());
36-
//TODO: remove
37-
int ReparseTranslationUnit(const std::map<std::string, std::string> &buffers,
38-
unsigned flags=DefaultFlags());
3929

4030
static unsigned DefaultFlags();
4131

@@ -47,9 +37,6 @@ namespace clang {
4737

4838
clang::CodeCompleteResults get_code_completions(const std::string &buffer,
4939
unsigned line_number, unsigned column);
50-
//TODO: remove
51-
clang::CodeCompleteResults get_code_completions(const std::map<std::string, std::string> &buffers,
52-
unsigned line_number, unsigned column);
5340

5441
std::vector<clang::Diagnostic> get_diagnostics();
5542

tests/CodeCompleteResults_H_Test.cc

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,18 @@ BOOST_AUTO_TEST_CASE(code_complete_results) {
99
std::string path("./case/main.cpp");
1010

1111
clang::Index index(0, 0);
12-
clang::TranslationUnit tu(index, path);
12+
clang::TranslationUnit tu(index, path, {});
1313

14-
// ReparseTranslationUnit takes a map with filepath as key
15-
// and buffer as value
16-
std::map<std::string, std::string> buffers;
17-
18-
// create buffer
19-
std::string file;
20-
file.append("#include <string>\n");
21-
file.append("int main(int argc, char *argv[]) {\n");
22-
file.append("std::string str;\n");
23-
file.append("str.\n");
24-
file.append("return 0\n");
25-
file.append("}");
26-
27-
buffers[path] = file;
14+
std::string buffer="#include <string>\n"
15+
"int main(int argc, char *argv[]) {\n"
16+
"std::string str;\n"
17+
"str.\n"
18+
"return 0\n"
19+
"}";
2820

2921
// ]
3022

31-
auto results=tu.get_code_completions(buffers, 4, 5);
23+
auto results=tu.get_code_completions(buffer, 4, 5);
3224

3325
bool substr_found=false;
3426
for(unsigned c=0;c<results.size();c++) {

tests/CompletionString_H_Test.cc

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@ BOOST_AUTO_TEST_CASE(completion_string) {
1616
std::string path("./case/main.cpp");
1717

1818
clang::Index index(0, 0);
19-
clang::TranslationUnit tu(index, path);
19+
clang::TranslationUnit tu(index, path, {});
2020

21-
// ReparseTranslationUnit takes a map with filepath as key
22-
// and buffer as value
23-
std::map<std::string, std::string> buffers;
21+
std::string buffer="#include <string>\n"
22+
"int main(int argc, char *argv[]) {\n"
23+
"std::string str;\n"
24+
"str.\n"
25+
"return 0\n"
26+
"}";
2427

25-
// create buffer
26-
std::string file;
27-
file.append("#include <string>\n");
28-
file.append("int main(int argc, char *argv[]) {\n");
29-
file.append("std::string str;\n");
30-
file.append("str.\n");
31-
file.append("return 0\n");
32-
file.append("}");
33-
34-
buffers[path] = file;
35-
36-
auto results=tu.get_code_completions(buffers, 4, 5);
28+
auto results=tu.get_code_completions(buffer, 4, 5);
3729
// ]
3830

3931
clang::CompletionString str = results.get(0);

tests/Cursor_H_Test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BOOST_AUTO_TEST_CASE(cursor) {
88
std::string path("./case/main.cpp");
99

1010
clang::Index index(0, 0);
11-
clang::TranslationUnit tu(index, path);
11+
clang::TranslationUnit tu(index, path, {});
1212

1313
// ]
1414

tests/Diagnostics_Test.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ BOOST_AUTO_TEST_CASE(diagnostics_test) {
1010

1111
clang::Index index(0, 0);
1212

13-
std::map<std::string, std::string> map_buffers;
14-
ifstream ifs(path, ifstream::in);
15-
stringstream ss;
16-
ss << ifs.rdbuf();
17-
18-
map_buffers["./case/main_error.cpp"]=ss.str();
19-
20-
std::vector<std::string> args;
21-
clang::TranslationUnit tu(index, path, args, map_buffers);
13+
clang::TranslationUnit tu(index, path, {});
2214

2315
auto diagnostics=tu.get_diagnostics();
2416
BOOST_CHECK(diagnostics.size()==1);

tests/SourceLocation_H_Test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ BOOST_AUTO_TEST_CASE(source_location) {
77

88
clang::Index index(0, 0);
99

10-
clang::TranslationUnit tu(index, path);
10+
clang::TranslationUnit tu(index, path, {});
1111
auto tokens=tu.get_tokens(0, 113);
1212

1313
auto offsets=(*tokens)[28].offsets;

tests/Token_H_Test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ BOOST_AUTO_TEST_CASE(token) {
77

88
clang::Index index(0, 0);
99

10-
clang::TranslationUnit tu(index, path);
10+
clang::TranslationUnit tu(index, path, {});
1111

1212
auto tokens=tu.get_tokens(0, 113);
1313

0 commit comments

Comments
 (0)