diff --git a/src/Cursor.h b/src/Cursor.h index 53ebb5a..55b547a 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -6,7 +6,6 @@ #include #include #include -#include 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; } diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index f527399..bc42dc2 100644 --- a/src/Diagnostic.cc +++ b/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 #include +#include 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 diff --git a/src/SourceRange.h b/src/SourceRange.h index 0533362..8eff771 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -13,6 +13,12 @@ namespace clangmm { SourceLocation get_start() const; SourceLocation get_end() const; std::pair 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 diff --git a/src/Token.h b/src/Token.h index f3a94a0..2a11a2e 100644 --- a/src/Token.h +++ b/src/Token.h @@ -5,7 +5,6 @@ #include "SourceRange.h" #include "Cursor.h" #include -#include 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; }