Browse Source

Cleanup.

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

11
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<unsigned, unsigned>(start_offset, tokens[0].offsets.second);
}
}

3
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<unsigned, unsigned> offsets;
};
}

35
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) {
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 (path != NULL && file!=NULL) {
path->operator=(((clang_getCString((clang_getFileName(file))))));
if (file!=NULL) {
auto cxstr=clang_getFileName(file);
*path=clang_getCString(cxstr);
clang_disposeString(cxstr);
}
}
}

9
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;
};

25
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::SourceLocation, clang::SourceLocation> clang::SourceRange::get_source_locations() {
return std::pair<SourceLocation, SourceLocation>(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<unsigned, unsigned> clang::SourceRange::get_offsets() {
SourceLocation start(clang_getRangeStart(cx_range)), end(clang_getRangeEnd(cx_range));
std::pair<unsigned, unsigned> offsets;
start.get_data(NULL, NULL, NULL, &offsets.first);
end.get_data(NULL, NULL, NULL, &offsets.second);
return offsets;
}

11
src/SourceRange.h

@ -3,21 +3,14 @@
#include <clang-c/Index.h>
#include "SourceLocation.h"
#include <string>
#include <utility>
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<SourceLocation, SourceLocation> get_source_locations();
static RangeData get_range_data(SourceLocation &start, SourceLocation &end);
RangeData get_range_data();
std::pair<unsigned, unsigned> get_offsets();
CXSourceRange cx_range;
};
} // namespace clang

6
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<unsigned, unsigned> offsets;
private:
SourceRange get_source_range();
};
} // namespace clang
#endif // TOKEN_H_

5
src/Tokens.cc

@ -2,7 +2,7 @@
#include <iostream>
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<std::pair<unsigned, unsigned> > 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);
}
}
}

2
src/Tokens.h

@ -9,7 +9,7 @@
namespace clang {
class Tokens : public std::vector<clang::Token> {
public:
Tokens(CXTranslationUnit &cx_tu, SourceRange &range);
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range);
~Tokens();
std::vector<std::pair<unsigned, unsigned> > get_similar_token_offsets(clang::Token& token);
private:

1
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);
}

28
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);
}

Loading…
Cancel
Save