Browse Source

Some cleanup.

merge-requests/37/head
eidheim 11 years ago
parent
commit
0c0615f9ee
  1. 38
      src/Diagnostic.cc
  2. 5
      src/Diagnostic.h
  3. 2
      src/SourceRange.cc
  4. 3
      src/SourceRange.h
  5. 2
      src/Tokens.h
  6. 27
      src/TranslationUnit.cc
  7. 3
      src/TranslationUnit.h

38
src/Diagnostic.cc

@ -1,16 +1,44 @@
#include "Diagnostic.h" #include "Diagnostic.h"
#include "SourceLocation.h"
#include "Tokens.h"
clang::Diagnostic::Diagnostic(clang::TranslationUnit& tu, CXDiagnostic clang_diagnostic) {
severity=clang_getDiagnosticSeverity(clang_diagnostic);
severity_spelling=get_severity_spelling(severity);
spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic));
clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic));
std::string tmp_path;
unsigned line, column, offset;
location.get_location_info(&tmp_path, &line, &column, &offset);
path=tmp_path;
start_location.line=line;
start_location.column=column;
start_location.offset=offset;
clang::SourceRange range(&location, &location);
clang::Tokens tokens(&tu, &range);
if(tokens.tokens().size()==1) {
auto& token=tokens.tokens()[0];
clang::SourceRange range=token.get_source_range(&tu);
clang::SourceLocation location(&range, false);
location.get_location_info(NULL, &line, &column, &offset);
end_location.line=line;
end_location.column=column;
end_location.offset=offset;
}
}
const std::string clang::Diagnostic::get_severity_spelling(unsigned severity) { const std::string clang::Diagnostic::get_severity_spelling(unsigned severity) {
switch(severity) { switch(severity) {
case 0: case CXDiagnostic_Ignored:
return "Ignored"; return "Ignored";
case 1: case CXDiagnostic_Note:
return "Note"; return "Note";
case 2: case CXDiagnostic_Warning:
return "Warning"; return "Warning";
case 3: case CXDiagnostic_Error:
return "Error"; return "Error";
case 4: case CXDiagnostic_Fatal:
return "Fatal"; return "Fatal";
default: default:
return ""; return "";

5
src/Diagnostic.h

@ -1,6 +1,9 @@
#ifndef DIAGNOSTIC_H_ #ifndef DIAGNOSTIC_H_
#define DIAGNOSTIC_H_ #define DIAGNOSTIC_H_
#include <string> #include <string>
#include <vector>
#include <clang-c/Index.h>
#include "TranslationUnit.h"
namespace clang { namespace clang {
class Diagnostic { class Diagnostic {
@ -10,6 +13,8 @@ namespace clang {
unsigned line, column, offset; unsigned line, column, offset;
}; };
Diagnostic(clang::TranslationUnit& tu, CXDiagnostic clang_diagnostic);
static const std::string get_severity_spelling(unsigned severity); static const std::string get_severity_spelling(unsigned severity);
unsigned severity; unsigned severity;

2
src/SourceRange.cc

@ -10,8 +10,6 @@ SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) {
range_ = clang_getRange(start->location_, end->location_); range_ = clang_getRange(start->location_, end->location_);
} }
clang::SourceRange::~SourceRange() { }
clang::SourceRange::SourceRange(Cursor *cursor) { clang::SourceRange::SourceRange(Cursor *cursor) {
range_ = clang_getCursorExtent(cursor->cursor_); range_ = clang_getCursorExtent(cursor->cursor_);
} }

3
src/SourceRange.h

@ -7,16 +7,17 @@
namespace clang { namespace clang {
class SourceRange { class SourceRange {
public: public:
SourceRange() {}
SourceRange(TranslationUnit *tu, Token *token); SourceRange(TranslationUnit *tu, Token *token);
SourceRange(SourceLocation *start, SourceRange(SourceLocation *start,
SourceLocation *end); SourceLocation *end);
explicit SourceRange(Cursor *cursor); explicit SourceRange(Cursor *cursor);
~SourceRange();
private: private:
CXSourceRange range_; CXSourceRange range_;
friend Tokens; friend Tokens;
friend SourceLocation; friend SourceLocation;
friend Diagnostic;
}; };
} // namespace clang } // namespace clang
#endif // SOURCERANGE_H_ #endif // SOURCERANGE_H_

2
src/Tokens.h

@ -10,7 +10,7 @@ namespace clang {
Tokens(TranslationUnit *tu, SourceRange *range); Tokens(TranslationUnit *tu, SourceRange *range);
~Tokens(); ~Tokens();
std::vector<Token>& tokens(); std::vector<Token>& tokens();
protected: private:
std::vector<clang::Token> tks; std::vector<clang::Token> tks;
CXToken *tokens_; CXToken *tokens_;
unsigned num_tokens_; unsigned num_tokens_;

27
src/TranslationUnit.cc

@ -94,32 +94,7 @@ std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() {
std::vector<clang::Diagnostic> diagnostics; std::vector<clang::Diagnostic> diagnostics;
for(unsigned c=0;c<clang_getNumDiagnostics(tu_);c++) { for(unsigned c=0;c<clang_getNumDiagnostics(tu_);c++) {
CXDiagnostic clang_diagnostic=clang_getDiagnostic(tu_, c); CXDiagnostic clang_diagnostic=clang_getDiagnostic(tu_, c);
diagnostics.emplace_back(); diagnostics.emplace_back(clang::Diagnostic(*this, clang_diagnostic));
auto& diagnostic=diagnostics.back();
diagnostic.severity=clang_getDiagnosticSeverity(clang_diagnostic);
diagnostic.severity_spelling=clang::Diagnostic::get_severity_spelling(diagnostic.severity);
diagnostic.spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic));
SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic));
std::string path;
unsigned line, column, offset;
location.get_location_info(&path, &line, &column, &offset);
diagnostic.path=path;
diagnostic.start_location.line=line;
diagnostic.start_location.column=column;
diagnostic.start_location.offset=offset;
clang::SourceRange range(&location, &location);
clang::Tokens tokens(this, &range);
if(tokens.tokens().size()==1) {
auto& token=tokens.tokens()[0];
clang::SourceRange range=token.get_source_range(this);
clang::SourceLocation end_location(&range, false);
end_location.get_location_info(NULL, &line, &column, &offset);
diagnostic.end_location.line=line;
diagnostic.end_location.column=column;
diagnostic.end_location.offset=offset;
}
} }
return diagnostics; return diagnostics;
} }

3
src/TranslationUnit.h

@ -15,6 +15,7 @@ namespace clang {
class SourceRange; class SourceRange;
class Cursor; class Cursor;
class CodeCompleteResults; class CodeCompleteResults;
class Diagnostic;
class TranslationUnit { class TranslationUnit {
public: public:
@ -35,7 +36,7 @@ namespace clang {
&buffers, &buffers,
unsigned flags=DefaultFlags()); unsigned flags=DefaultFlags());
static unsigned DefaultFlags(); static unsigned DefaultFlags();
std::vector<Diagnostic> get_diagnostics(); std::vector<clang::Diagnostic> get_diagnostics();
private: private:
friend Token; friend Token;

Loading…
Cancel
Save