Browse Source

Corrected diagnostic offsets for macro warnings/errors

merge-requests/37/head
eidheim 8 years ago
parent
commit
6084560975
  1. 7
      src/Cursor.h
  2. 11
      src/Diagnostic.cc
  3. 6
      src/SourceLocation.cc
  4. 14
      src/SourceLocation.h
  5. 6
      src/SourceRange.h
  6. 7
      src/Token.h

7
src/Cursor.h

@ -6,7 +6,6 @@
#include <string>
#include <vector>
#include <unordered_set>
#include <ostream>
namespace clangmm {
class Cursor {
@ -215,11 +214,7 @@ namespace clangmm {
std::string get_brief_comments() const;
friend std::ostream &operator<<(std::ostream &os, const Cursor &cursor) {
auto offsets=cursor.get_source_range().get_offsets();
os << cursor.get_source_location().get_path() << ":"
<< offsets.first.line << ":" << offsets.first.index << "-"
<< offsets.second.line << ":" << offsets.second.index << " "
<< cursor.get_spelling();
os << cursor.get_source_range() << ' ' << cursor.get_spelling();
return os;
}

11
src/Diagnostic.cc

@ -8,12 +8,13 @@ clangmm::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagn
severity_spelling=get_severity_spelling(severity);
spelling=to_string(clang_getDiagnosticSpelling(cx_diagnostic));
SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic));
path=start_location.get_path();
auto start_offset=start_location.get_offset();
Tokens tokens(cx_tu, SourceRange(start_location, start_location), false);
SourceLocation location(clang_getDiagnosticLocation(cx_diagnostic));
path=location.get_path();
auto offset=location.get_offset();
auto corrected_location=SourceLocation(cx_tu, path.c_str(), offset.line, offset.index); // to avoid getting macro tokens
Tokens tokens(cx_tu, SourceRange(corrected_location, corrected_location), false);
if(tokens.size()==1)
offsets={start_offset, tokens.begin()->get_source_range().get_offsets().second};
offsets={offset, tokens.begin()->get_source_range().get_offsets().second};
unsigned num_fix_its=clang_getDiagnosticNumFixIts(cx_diagnostic);
for(unsigned c=0;c<num_fix_its;c++) {

6
src/SourceLocation.cc

@ -14,18 +14,18 @@ clangmm::SourceLocation::SourceLocation(CXTranslationUnit &tu, const std::string
cx_location = clang_getLocation(tu, file, line, column);
}
std::string clangmm::SourceLocation::get_path() {
std::string clangmm::SourceLocation::get_path() const {
std::string path;
get_data(&path, nullptr, nullptr, nullptr);
return path;
}
clangmm::Offset clangmm::SourceLocation::get_offset() {
clangmm::Offset clangmm::SourceLocation::get_offset() const {
unsigned line, index;
get_data(nullptr, &line, &index, nullptr);
return {line, index};
}
void clangmm::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) {
void clangmm::SourceLocation::get_data(std::string* path, unsigned *line, unsigned *column, unsigned *offset) const {
if(path==nullptr)
clang_getExpansionLocation(cx_location, NULL, line, column, offset);
else {

14
src/SourceLocation.h

@ -2,6 +2,7 @@
#define SOURCELOCATION_H_
#include <clang-c/Index.h>
#include <string>
#include <ostream>
namespace clangmm {
class Offset {
@ -15,19 +16,26 @@ namespace clangmm {
class SourceLocation {
friend class TranslationUnit;
friend class Diagnostic;
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset);
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned line, unsigned column);
public:
SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {}
public:
std::string get_path();
clangmm::Offset get_offset();
std::string get_path() const;
clangmm::Offset get_offset() const;
friend std::ostream &operator<<(std::ostream &os, const SourceLocation &location) {
auto offset=location.get_offset();
os << location.get_path() << ':' << offset.line << ':' << offset.index;
return os;
}
CXSourceLocation cx_location;
private:
void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset);
void get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset) const;
};
} // namespace clangmm

6
src/SourceRange.h

@ -13,6 +13,12 @@ namespace clangmm {
SourceLocation get_start() const;
SourceLocation get_end() const;
std::pair<clangmm::Offset, clangmm::Offset> get_offsets() const;
friend std::ostream &operator<<(std::ostream &os, const SourceRange &range) {
os << range.get_start() << '-' << range.get_end();
return os;
}
CXSourceRange cx_range;
};
} // namespace clangmm

7
src/Token.h

@ -5,7 +5,6 @@
#include "SourceRange.h"
#include "Cursor.h"
#include <string>
#include <ostream>
namespace clangmm {
class Token {
@ -31,11 +30,7 @@ namespace clangmm {
bool is_identifier() const;
friend std::ostream &operator<<(std::ostream &os, const Token &token) {
auto offsets=token.get_source_range().get_offsets();
os << token.get_source_location().get_path() << ":"
<< offsets.first.line << ":" << offsets.first.index << "-"
<< offsets.second.line << ":" << offsets.second.index << " "
<< token.get_spelling();
os << token.get_source_range() << ' ' << token.get_spelling();
return os;
}

Loading…
Cancel
Save