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 <string>
#include <vector> #include <vector>
#include <unordered_set> #include <unordered_set>
#include <ostream>
namespace clangmm { namespace clangmm {
class Cursor { class Cursor {
@ -215,11 +214,7 @@ namespace clangmm {
std::string get_brief_comments() const; std::string get_brief_comments() const;
friend std::ostream &operator<<(std::ostream &os, const Cursor &cursor) { friend std::ostream &operator<<(std::ostream &os, const Cursor &cursor) {
auto offsets=cursor.get_source_range().get_offsets(); os << cursor.get_source_range() << ' ' << cursor.get_spelling();
os << cursor.get_source_location().get_path() << ":"
<< offsets.first.line << ":" << offsets.first.index << "-"
<< offsets.second.line << ":" << offsets.second.index << " "
<< cursor.get_spelling();
return os; 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); severity_spelling=get_severity_spelling(severity);
spelling=to_string(clang_getDiagnosticSpelling(cx_diagnostic)); spelling=to_string(clang_getDiagnosticSpelling(cx_diagnostic));
SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); SourceLocation location(clang_getDiagnosticLocation(cx_diagnostic));
path=start_location.get_path(); path=location.get_path();
auto start_offset=start_location.get_offset(); auto offset=location.get_offset();
Tokens tokens(cx_tu, SourceRange(start_location, start_location), false); 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) 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); unsigned num_fix_its=clang_getDiagnosticNumFixIts(cx_diagnostic);
for(unsigned c=0;c<num_fix_its;c++) { 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); cx_location = clang_getLocation(tu, file, line, column);
} }
std::string clangmm::SourceLocation::get_path() { std::string clangmm::SourceLocation::get_path() const {
std::string path; std::string path;
get_data(&path, nullptr, nullptr, nullptr); get_data(&path, nullptr, nullptr, nullptr);
return path; return path;
} }
clangmm::Offset clangmm::SourceLocation::get_offset() { clangmm::Offset clangmm::SourceLocation::get_offset() const {
unsigned line, index; unsigned line, index;
get_data(nullptr, &line, &index, nullptr); get_data(nullptr, &line, &index, nullptr);
return {line, index}; 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) if(path==nullptr)
clang_getExpansionLocation(cx_location, NULL, line, column, offset); clang_getExpansionLocation(cx_location, NULL, line, column, offset);
else { else {

14
src/SourceLocation.h

@ -2,6 +2,7 @@
#define SOURCELOCATION_H_ #define SOURCELOCATION_H_
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include <string> #include <string>
#include <ostream>
namespace clangmm { namespace clangmm {
class Offset { class Offset {
@ -15,19 +16,26 @@ namespace clangmm {
class SourceLocation { class SourceLocation {
friend class TranslationUnit; friend class TranslationUnit;
friend class Diagnostic;
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset); SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset);
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned line, unsigned column); SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned line, unsigned column);
public: public:
SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {} SourceLocation(const CXSourceLocation& cx_location) : cx_location(cx_location) {}
public: public:
std::string get_path(); std::string get_path() const;
clangmm::Offset get_offset(); 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; CXSourceLocation cx_location;
private: 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 } // namespace clangmm

6
src/SourceRange.h

@ -13,6 +13,12 @@ namespace clangmm {
SourceLocation get_start() const; SourceLocation get_start() const;
SourceLocation get_end() const; SourceLocation get_end() const;
std::pair<clangmm::Offset, clangmm::Offset> get_offsets() 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; CXSourceRange cx_range;
}; };
} // namespace clangmm } // namespace clangmm

7
src/Token.h

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

Loading…
Cancel
Save