diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 177dabb..03e7c28 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -7,14 +7,13 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos severity=clang_getDiagnosticSeverity(cx_diagnostic); severity_spelling=get_severity_spelling(severity); spelling=clang::to_string(clang_getDiagnosticSpelling(cx_diagnostic)); - clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); + clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); path=start_location.get_path(); - unsigned start_offset=start_location.get_offset(); + auto 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.begin()->offsets.second); - } + if(tokens.size()==1) + offsets={start_offset, tokens.begin()->offsets.second}; } const std::string clang::Diagnostic::get_severity_spelling(unsigned severity) { diff --git a/src/Diagnostic.h b/src/Diagnostic.h index 42d3785..d4e54b4 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -16,7 +16,7 @@ namespace clang { std::string severity_spelling; std::string spelling; std::string path; - std::pair offsets; + std::pair offsets; }; } diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index 8227938..7638807 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -14,10 +14,10 @@ std::string clang::SourceLocation::get_path() { get_data(&path, NULL, NULL, NULL); return path; } -unsigned clang::SourceLocation::get_offset() { - unsigned offset; - get_data(NULL, NULL, NULL, &offset); - return offset; +clang::Offset clang::SourceLocation::get_offset() { + unsigned line, index; + get_data(NULL, &line, &index, NULL); + return {line, index}; } void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) { diff --git a/src/SourceLocation.h b/src/SourceLocation.h index 9f29b99..501c9e6 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -4,6 +4,15 @@ #include namespace clang { + class Offset { + public: + Offset() {} + Offset(unsigned line, unsigned index): line(line), index(index) {} + bool operator==(const clang::Offset &o) {return (line==o.line && index==o.index);} + bool operator!=(const clang::Offset &o) {return !(*this==o);} + unsigned line; + unsigned index; //byte index in line (not char number) + }; class SourceLocation { friend class TranslationUnit; @@ -13,7 +22,7 @@ namespace clang { public: std::string get_path(); - unsigned get_offset(); + clang::Offset get_offset(); CXSourceLocation cx_location; diff --git a/src/SourceRange.cc b/src/SourceRange.cc index 5c97323..91f7b3f 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -5,10 +5,7 @@ SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) { cx_range = clang_getRange(start.cx_location, end.cx_location); } -std::pair clang::SourceRange::get_offsets() { +std::pair clang::SourceRange::get_offsets() { SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range)); - std::pair offsets; - offsets.first=start.get_offset(); - offsets.second=end.get_offset(); - return offsets; + return {start.get_offset(), end.get_offset()}; } \ No newline at end of file diff --git a/src/SourceRange.h b/src/SourceRange.h index c39eb18..e8edd56 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -10,7 +10,7 @@ namespace clang { public: SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {} SourceRange(SourceLocation &start, SourceLocation &end); - std::pair get_offsets(); + std::pair get_offsets(); CXSourceRange cx_range; }; } // namespace clang diff --git a/src/Token.h b/src/Token.h index 15ded6c..ed31d8a 100644 --- a/src/Token.h +++ b/src/Token.h @@ -14,7 +14,7 @@ namespace clang { Token_Literal, Token_Comment }; - + class Token { friend class Tokens; Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): @@ -31,7 +31,7 @@ namespace clang { CXTranslationUnit &cx_tu; CXToken& cx_token; CXCursor& cx_cursor; - std::pair offsets; + std::pair offsets; }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index f2fd316..f49b747 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -20,8 +20,8 @@ clang::Tokens::~Tokens() { //This works across TranslationUnits! However, to get rename refactoring to work, //one have to open all the files that might include a similar token //Similar tokens defined as tokens with equal referenced cursors. -std::vector > clang::Tokens::get_similar_token_offsets(const std::string &usr) { - std::vector > offsets; +std::vector > clang::Tokens::get_similar_token_offsets(const std::string &usr) { + std::vector > offsets; for(auto &token: *this) { if(token.get_kind()==clang::Token_Identifier) { auto referenced=token.get_cursor().get_referenced(); @@ -33,9 +33,9 @@ std::vector > clang::Tokens::get_similar_token_off return offsets; } -std::vector > clang::Tokens::get_cxx_methods() { - std::vector > methods; - long last_offset=-1; +std::vector > clang::Tokens::get_cxx_methods() { + std::vector > methods; + clang::Offset last_offset={(unsigned)-1,(unsigned) -1}; for(auto &token: *this) { if(token.get_kind()==clang::Token_Identifier) { auto cursor=token.get_cursor(); diff --git a/src/Tokens.h b/src/Tokens.h index 7f1ea1b..26c007f 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -13,8 +13,8 @@ namespace clang { Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); public: ~Tokens(); - std::vector > get_similar_token_offsets(const std::string &usr); - std::vector > get_cxx_methods(); + std::vector > get_similar_token_offsets(const std::string &usr); + std::vector > get_cxx_methods(); private: CXToken *cx_tokens; unsigned num_tokens; diff --git a/tests/SourceLocation_H_Test.cc b/tests/SourceLocation_H_Test.cc index 6722b4a..f6982e8 100644 --- a/tests/SourceLocation_H_Test.cc +++ b/tests/SourceLocation_H_Test.cc @@ -11,7 +11,7 @@ BOOST_AUTO_TEST_CASE(source_location) { auto tokens=tu.get_tokens(0, 113); auto offsets=(*tokens)[28].offsets; - - BOOST_CHECK(offsets.first == 103); - BOOST_CHECK(offsets.second == 109); + + BOOST_CHECK(offsets.first.line == 6 && offsets.first.index == 3); + BOOST_CHECK(offsets.second.line == 6 && offsets.second.index == 9); }