Browse Source

Returned offsets are now line and line index to be able to find correct offsets in UTF-8 files.

merge-requests/37/head
eidheim 10 years ago
parent
commit
c27329af7d
  1. 9
      src/Diagnostic.cc
  2. 2
      src/Diagnostic.h
  3. 8
      src/SourceLocation.cc
  4. 11
      src/SourceLocation.h
  5. 7
      src/SourceRange.cc
  6. 2
      src/SourceRange.h
  7. 2
      src/Token.h
  8. 10
      src/Tokens.cc
  9. 4
      src/Tokens.h
  10. 4
      tests/SourceLocation_H_Test.cc

9
src/Diagnostic.cc

@ -7,14 +7,13 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnos
severity=clang_getDiagnosticSeverity(cx_diagnostic); severity=clang_getDiagnosticSeverity(cx_diagnostic);
severity_spelling=get_severity_spelling(severity); severity_spelling=get_severity_spelling(severity);
spelling=clang::to_string(clang_getDiagnosticSpelling(cx_diagnostic)); 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(); 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)); clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location));
if(tokens.size()==1) { if(tokens.size()==1)
offsets=std::pair<unsigned, unsigned>(start_offset, tokens.begin()->offsets.second); offsets={start_offset, tokens.begin()->offsets.second};
}
} }
const std::string clang::Diagnostic::get_severity_spelling(unsigned severity) { const std::string clang::Diagnostic::get_severity_spelling(unsigned severity) {

2
src/Diagnostic.h

@ -16,7 +16,7 @@ namespace clang {
std::string severity_spelling; std::string severity_spelling;
std::string spelling; std::string spelling;
std::string path; std::string path;
std::pair<unsigned, unsigned> offsets; std::pair<clang::Offset, clang::Offset> offsets;
}; };
} }

8
src/SourceLocation.cc

@ -14,10 +14,10 @@ std::string clang::SourceLocation::get_path() {
get_data(&path, NULL, NULL, NULL); get_data(&path, NULL, NULL, NULL);
return path; return path;
} }
unsigned clang::SourceLocation::get_offset() { clang::Offset clang::SourceLocation::get_offset() {
unsigned offset; unsigned line, index;
get_data(NULL, NULL, NULL, &offset); get_data(NULL, &line, &index, NULL);
return offset; return {line, index};
} }
void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) { void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) {

11
src/SourceLocation.h

@ -4,6 +4,15 @@
#include <string> #include <string>
namespace clang { 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 { class SourceLocation {
friend class TranslationUnit; friend class TranslationUnit;
@ -13,7 +22,7 @@ namespace clang {
public: public:
std::string get_path(); std::string get_path();
unsigned get_offset(); clang::Offset get_offset();
CXSourceLocation cx_location; CXSourceLocation cx_location;

7
src/SourceRange.cc

@ -5,10 +5,7 @@ SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) {
cx_range = clang_getRange(start.cx_location, end.cx_location); cx_range = clang_getRange(start.cx_location, end.cx_location);
} }
std::pair<unsigned, unsigned> clang::SourceRange::get_offsets() { std::pair<clang::Offset, clang::Offset> clang::SourceRange::get_offsets() {
SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range)); SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range));
std::pair<unsigned, unsigned> offsets; return {start.get_offset(), end.get_offset()};
offsets.first=start.get_offset();
offsets.second=end.get_offset();
return offsets;
} }

2
src/SourceRange.h

@ -10,7 +10,7 @@ namespace clang {
public: public:
SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {} SourceRange(const CXSourceRange& cx_range) : cx_range(cx_range) {}
SourceRange(SourceLocation &start, SourceLocation &end); SourceRange(SourceLocation &start, SourceLocation &end);
std::pair<unsigned, unsigned> get_offsets(); std::pair<clang::Offset, clang::Offset> get_offsets();
CXSourceRange cx_range; CXSourceRange cx_range;
}; };
} // namespace clang } // namespace clang

2
src/Token.h

@ -31,7 +31,7 @@ namespace clang {
CXTranslationUnit &cx_tu; CXTranslationUnit &cx_tu;
CXToken& cx_token; CXToken& cx_token;
CXCursor& cx_cursor; CXCursor& cx_cursor;
std::pair<unsigned, unsigned> offsets; std::pair<clang::Offset, clang::Offset> offsets;
}; };
} // namespace clang } // namespace clang
#endif // TOKEN_H_ #endif // TOKEN_H_

10
src/Tokens.cc

@ -20,8 +20,8 @@ clang::Tokens::~Tokens() {
//This works across TranslationUnits! However, to get rename refactoring to work, //This works across TranslationUnits! However, to get rename refactoring to work,
//one have to open all the files that might include a similar token //one have to open all the files that might include a similar token
//Similar tokens defined as tokens with equal referenced cursors. //Similar tokens defined as tokens with equal referenced cursors.
std::vector<std::pair<unsigned, unsigned> > clang::Tokens::get_similar_token_offsets(const std::string &usr) { std::vector<std::pair<clang::Offset, clang::Offset> > clang::Tokens::get_similar_token_offsets(const std::string &usr) {
std::vector<std::pair<unsigned, unsigned> > offsets; std::vector<std::pair<clang::Offset, clang::Offset> > offsets;
for(auto &token: *this) { for(auto &token: *this) {
if(token.get_kind()==clang::Token_Identifier) { if(token.get_kind()==clang::Token_Identifier) {
auto referenced=token.get_cursor().get_referenced(); auto referenced=token.get_cursor().get_referenced();
@ -33,9 +33,9 @@ std::vector<std::pair<unsigned, unsigned> > clang::Tokens::get_similar_token_off
return offsets; return offsets;
} }
std::vector<std::pair<std::string, unsigned> > clang::Tokens::get_cxx_methods() { std::vector<std::pair<std::string, clang::Offset> > clang::Tokens::get_cxx_methods() {
std::vector<std::pair<std::string, unsigned> > methods; std::vector<std::pair<std::string, clang::Offset> > methods;
long last_offset=-1; clang::Offset last_offset={(unsigned)-1,(unsigned) -1};
for(auto &token: *this) { for(auto &token: *this) {
if(token.get_kind()==clang::Token_Identifier) { if(token.get_kind()==clang::Token_Identifier) {
auto cursor=token.get_cursor(); auto cursor=token.get_cursor();

4
src/Tokens.h

@ -13,8 +13,8 @@ namespace clang {
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); Tokens(CXTranslationUnit &cx_tu, const SourceRange &range);
public: public:
~Tokens(); ~Tokens();
std::vector<std::pair<unsigned, unsigned> > get_similar_token_offsets(const std::string &usr); std::vector<std::pair<clang::Offset, clang::Offset> > get_similar_token_offsets(const std::string &usr);
std::vector<std::pair<std::string, unsigned> > get_cxx_methods(); std::vector<std::pair<std::string, clang::Offset> > get_cxx_methods();
private: private:
CXToken *cx_tokens; CXToken *cx_tokens;
unsigned num_tokens; unsigned num_tokens;

4
tests/SourceLocation_H_Test.cc

@ -12,6 +12,6 @@ BOOST_AUTO_TEST_CASE(source_location) {
auto offsets=(*tokens)[28].offsets; auto offsets=(*tokens)[28].offsets;
BOOST_CHECK(offsets.first == 103); BOOST_CHECK(offsets.first.line == 6 && offsets.first.index == 3);
BOOST_CHECK(offsets.second == 109); BOOST_CHECK(offsets.second.line == 6 && offsets.second.index == 9);
} }

Loading…
Cancel
Save