From 8137e7a9e6038b4acf7fb192fd97b06f05fdd930 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 16 Jul 2015 20:31:21 +0200 Subject: [PATCH] More cleanup. --- src/CodeCompleteResults.cc | 12 +++++------- src/CodeCompleteResults.h | 13 ++++++------- src/Diagnostic.cc | 4 ++-- src/Diagnostic.h | 4 ++-- src/SourceLocation.cc | 23 ++++++++++++----------- src/SourceLocation.h | 16 ++++++++-------- src/SourceRange.cc | 4 ++-- src/Token.h | 3 +-- src/Tokens.h | 4 +++- src/TranslationUnit.cc | 2 +- src/TranslationUnit.h | 2 +- 11 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index da916f8..e6aea17 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -3,11 +3,9 @@ #include clang::CodeCompleteResults:: -CodeCompleteResults(CXTranslationUnit &cx_tu, - const std::string &file_name, +CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, - int line_num, - int column) { + unsigned line_num, unsigned column) { std::vector files; for (auto &buffer : buffers) { CXUnsavedFile file; @@ -31,15 +29,15 @@ clang::CodeCompleteResults::~CodeCompleteResults() { delete cx_results; } -int clang::CodeCompleteResults:: +unsigned clang::CodeCompleteResults:: size() { return cx_results->NumResults; } clang::CompletionString clang::CodeCompleteResults:: -get(int i) { +get(unsigned i) { if (i >= size()) { - throw std::invalid_argument("clang::CodeCompleteResults::get(int i): i>=size()"); + throw std::invalid_argument("clang::CodeCompleteResults::get(unsigned i): i>=size()"); } return CompletionString(cx_results->Results[i].CompletionString); } diff --git a/src/CodeCompleteResults.h b/src/CodeCompleteResults.h index dda4d89..e6c8a17 100644 --- a/src/CodeCompleteResults.h +++ b/src/CodeCompleteResults.h @@ -6,15 +6,14 @@ namespace clang { class CodeCompleteResults { - public: - CodeCompleteResults(CXTranslationUnit &cx_tu, - const std::string &file_name, + friend class TranslationUnit; + CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &file_name, const std::map &buffers, - int line_num, - int column); + unsigned line_num, unsigned column); + public: ~CodeCompleteResults(); - CompletionString get(int index); - int size(); + CompletionString get(unsigned index); + unsigned size(); CXCodeCompleteResults *cx_results; }; diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 6b73f9b..1ff3cfe 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -10,8 +10,8 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos clang_disposeString(cxstr); clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); - unsigned start_offset; - start_location.get_data(&path, NULL, NULL, &start_offset); + path=start_location.get_path(); + unsigned start_offset=start_location.get_offset(); clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location)); if(tokens.size()==1) { offsets=std::pair(start_offset, tokens[0].offsets.second); diff --git a/src/Diagnostic.h b/src/Diagnostic.h index 9874186..42d3785 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -7,9 +7,9 @@ namespace clang { class Diagnostic { - public: + friend class TranslationUnit; Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic); - + public: static const std::string get_severity_spelling(unsigned severity); unsigned severity; diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 29047f0..62a2d6c 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -3,21 +3,22 @@ // // // // // // // // // SourceLocation // // // // // // // // // -clang::SourceLocation:: -SourceLocation(CXTranslationUnit &tu, - const std::string &filename, - int line_number, - int line_offset) { - CXFile file = clang_getFile(tu, filename.c_str()); - cx_location = clang_getLocation(tu, file, line_number, line_offset); -} - -clang::SourceLocation:: -SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset) { +clang::SourceLocation::SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset) { CXFile file = clang_getFile(tu, filepath.c_str()); cx_location = clang_getLocationForOffset(tu, file, offset); } +std::string clang::SourceLocation::get_path() { + std::string path; + get_data(&path, NULL, NULL, NULL); + return path; +} +unsigned clang::SourceLocation::get_offset() { + unsigned offset; + get_data(NULL, NULL, NULL, &offset); + return offset; +} + void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) { if(path==nullptr) clang_getExpansionLocation(cx_location, NULL, line, column, offset); diff --git a/src/SourceLocation.h b/src/SourceLocation.h index f041f82..9f29b99 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -6,19 +6,19 @@ namespace clang { class SourceLocation { + friend class TranslationUnit; + SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset); public: SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {} - - SourceLocation(CXTranslationUnit &cx_tu, - const std::string &filename, - int line_number, - int column); - - SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); - void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset); + public: + std::string get_path(); + unsigned get_offset(); CXSourceLocation cx_location; + + private: + void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset); }; } // namespace clang diff --git a/src/SourceRange.cc b/src/SourceRange.cc index 7e6e7ea..5c97323 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -8,7 +8,7 @@ SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { std::pair clang::SourceRange::get_offsets() { SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range)); std::pair offsets; - start.get_data(NULL, NULL, NULL, &offsets.first); - end.get_data(NULL, NULL, NULL, &offsets.second); + offsets.first=start.get_offset(); + offsets.second=end.get_offset(); return offsets; } \ No newline at end of file diff --git a/src/Token.h b/src/Token.h index 6bb4b64..77b4e89 100644 --- a/src/Token.h +++ b/src/Token.h @@ -22,6 +22,7 @@ namespace clang { const TokenKind get_kind(); std::string get_spelling(); SourceLocation get_source_location(); + SourceRange get_source_range(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); @@ -30,8 +31,6 @@ namespace clang { CXToken& cx_token; CXCursor& cx_cursor; std::pair offsets; - private: - SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.h b/src/Tokens.h index 054f510..ae35873 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -8,8 +8,10 @@ namespace clang { class Tokens : public std::vector { - public: + friend class TranslationUnit; + friend class Diagnostic; Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); + public: ~Tokens(); std::vector > get_similar_token_offsets(clang::Token& token); private: diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index 37f8104..edd2bde 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -90,7 +90,7 @@ unsigned clang::TranslationUnit::DefaultFlags() { return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete | CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; } -clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, int line_number, int column) { +clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::map &buffers, unsigned line_number, unsigned column) { auto cxstr=clang_getTranslationUnitSpelling(cx_tu); std::string path=clang_getCString(cxstr); clang_disposeString(cxstr); diff --git a/src/TranslationUnit.h b/src/TranslationUnit.h index 8bf2563..e10d465 100644 --- a/src/TranslationUnit.h +++ b/src/TranslationUnit.h @@ -35,7 +35,7 @@ namespace clang { const std::map &buffers, unsigned flags=DefaultFlags()); - clang::CodeCompleteResults get_code_completions(const std::map &buffers, int line_number, int column); + clang::CodeCompleteResults get_code_completions(const std::map &buffers, unsigned line_number, unsigned column); std::vector get_diagnostics(); std::unique_ptr get_tokens(unsigned start_offset, unsigned end_offset); clang::Cursor get_cursor(std::string path, unsigned offset);