diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index f2f4f52..6b73f9b 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -8,14 +8,13 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos auto cxstr=clang_getDiagnosticSpelling(cx_diagnostic); spelling=clang_getCString(cxstr); clang_disposeString(cxstr); - clang::SourceLocation location(clang_getDiagnosticLocation(cx_diagnostic)); + clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); - clang::SourceRange range(location, location); - clang::Tokens tokens(cx_tu, range); + unsigned start_offset; + start_location.get_data(&path, NULL, NULL, &start_offset); + clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location)); if(tokens.size()==1) { - auto& token=tokens[0]; - auto locations=token.source_range.get_source_locations(); - this->range=SourceRange::get_range_data(location, locations.second); + offsets=std::pair(start_offset, tokens[0].offsets.second); } } diff --git a/src/Diagnostic.h b/src/Diagnostic.h index a01e776..9874186 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -15,7 +15,8 @@ namespace clang { unsigned severity; std::string severity_spelling; std::string spelling; - RangeData range; + std::string path; + std::pair offsets; }; } diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 976eb01..29047f0 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -8,34 +8,27 @@ 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); + 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) { - CXFile file = clang_getFile(tu, - filepath.c_str()); - cx_location = clang_getLocationForOffset(tu, - file, - offset); +SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset) { + CXFile file = clang_getFile(tu, filepath.c_str()); + cx_location = clang_getLocationForOffset(tu, file, offset); } -void clang::SourceLocation:: -get_location_info(std::string* path, - unsigned *line, - unsigned *column, - unsigned *offset) { - CXFile file; - clang_getExpansionLocation(cx_location, &file, line, column, offset); - if (path != NULL && file!=NULL) { - path->operator=(((clang_getCString((clang_getFileName(file)))))); - } +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); + else { + CXFile file; + clang_getExpansionLocation(cx_location, &file, line, column, offset); + if (file!=NULL) { + auto cxstr=clang_getFileName(file); + *path=clang_getCString(cxstr); + clang_disposeString(cxstr); + } + } } diff --git a/src/SourceLocation.h b/src/SourceLocation.h index 0837dc1..f041f82 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -14,14 +14,9 @@ namespace clang { int line_number, int column); - SourceLocation(CXTranslationUnit &tu, - const std::string &filepath, - int offset); + SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); - void get_location_info(std::string *path, - unsigned *line, - unsigned *column, - unsigned *offset); + void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset); CXSourceLocation cx_location; }; diff --git a/src/SourceRange.cc b/src/SourceRange.cc index ae3bd24..7e6e7ea 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -5,23 +5,10 @@ SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { cx_range = clang_getRange(start.cx_location, end.cx_location); } -std::pair clang::SourceRange::get_source_locations() { - return std::pair(clang_getRangeStart(cx_range), clang_getRangeEnd(cx_range)); -} - -clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start, clang::SourceLocation &end) { - std::string path; - unsigned start_offset, end_offset; - start.get_location_info(&path, NULL, NULL, &start_offset); - end.get_location_info(NULL, NULL, NULL, &end_offset); - RangeData range_data; - range_data.path=path; - range_data.start_offset=start_offset; - range_data.end_offset=end_offset; - return range_data; -} - -clang::RangeData clang::SourceRange::get_range_data() { - auto locations=get_source_locations(); - return get_range_data(locations.first, locations.second); +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); + return offsets; } \ No newline at end of file diff --git a/src/SourceRange.h b/src/SourceRange.h index 7a85504..c39eb18 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -3,21 +3,14 @@ #include #include "SourceLocation.h" #include +#include namespace clang { - class RangeData { - public: - std::string path; - unsigned start_offset, end_offset; - }; - class SourceRange { public: SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {} SourceRange(SourceLocation &start, SourceLocation &end); - std::pair get_source_locations(); - static RangeData get_range_data(SourceLocation &start, SourceLocation &end); - RangeData get_range_data(); + std::pair get_offsets(); CXSourceRange cx_range; }; } // namespace clang diff --git a/src/Token.h b/src/Token.h index d88817f..9fcb7e3 100644 --- a/src/Token.h +++ b/src/Token.h @@ -18,20 +18,20 @@ namespace clang { class Token { public: Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): - cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), source_range(get_source_range()) {}; + cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {}; const TokenKind kind(); std::string get_token_spelling(); SourceLocation get_source_location(); clang::Cursor get_cursor() {return clang::Cursor(cx_cursor);} bool has_type(); std::string get_type(); + SourceRange get_source_range(); std::string get_brief_comments(); CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; - SourceRange source_range; + std::pair offsets; private: - SourceRange get_source_range(); }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 390948c..2d3f0e1 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -2,7 +2,7 @@ #include using namespace std; -clang::Tokens::Tokens(CXTranslationUnit &cx_tu, 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); cx_cursors.clear(); @@ -26,8 +26,7 @@ std::vector > clang::Tokens::get_similar_token_off if(referenced_usr!="") { for(auto &a_token: *this) { if(referenced_usr==a_token.get_cursor().get_referenced_usr() && token.get_token_spelling()==a_token.get_token_spelling()) { - auto range_data=a_token.source_range.get_range_data(); - offsets.emplace_back(range_data.start_offset, range_data.end_offset); + offsets.emplace_back(a_token.offsets); } } } diff --git a/src/Tokens.h b/src/Tokens.h index 81693d1..054f510 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -9,7 +9,7 @@ namespace clang { class Tokens : public std::vector { public: - Tokens(CXTranslationUnit &cx_tu, SourceRange &range); + Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); ~Tokens(); std::vector > get_similar_token_offsets(clang::Token& token); private: diff --git a/tests/Diagnostics_Test.cc b/tests/Diagnostics_Test.cc index 70972eb..c56b6a6 100644 --- a/tests/Diagnostics_Test.cc +++ b/tests/Diagnostics_Test.cc @@ -23,6 +23,5 @@ BOOST_AUTO_TEST_CASE(diagnostics_test) { auto diagnostics=tu.get_diagnostics(); BOOST_CHECK(diagnostics.size()==1); BOOST_CHECK(diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'"); - BOOST_CHECK(diagnostics[0].range.path=="./case/main_error.cpp"); BOOST_CHECK(diagnostics[0].severity==3); } diff --git a/tests/SourceLocation_H_Test.cc b/tests/SourceLocation_H_Test.cc index ab6d1c0..6722b4a 100644 --- a/tests/SourceLocation_H_Test.cc +++ b/tests/SourceLocation_H_Test.cc @@ -10,30 +10,8 @@ BOOST_AUTO_TEST_CASE(source_location) { clang::TranslationUnit tu(index, path); auto tokens=tu.get_tokens(0, 113); - clang::SourceRange token_range = (*tokens)[28].source_range; + auto offsets=(*tokens)[28].offsets; - unsigned token_start_line, token_start_column, token_start_offset, - token_end_line, token_end_column, token_end_offset; - std::string token_start_path, token_end_path; - - auto locations=token_range.get_source_locations(); - - locations.first.get_location_info(&token_start_path, - &token_start_line, - &token_start_column, - &token_start_offset); - - locations.second.get_location_info(&token_end_path, - &token_end_line, - &token_end_column, - &token_end_offset); - - BOOST_CHECK(token_start_path == path); - BOOST_CHECK(token_start_line == 6); - BOOST_CHECK(token_start_column == 3); - BOOST_CHECK(token_start_offset == 103); - BOOST_CHECK(token_end_path == path); - BOOST_CHECK(token_end_line == 6); - BOOST_CHECK(token_end_column == 9); - BOOST_CHECK(token_end_offset == 109); + BOOST_CHECK(offsets.first == 103); + BOOST_CHECK(offsets.second == 109); }