The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #include "SourcePos.h" |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <set> |
| 5 | |
| 6 | using namespace std; |
| 7 | |
| 8 | const SourcePos GENERATED_POS("<generated>", -1); |
| 9 | |
| 10 | // ErrorPos |
| 11 | // ============================================================================= |
| 12 | struct ErrorPos |
| 13 | { |
| 14 | string file; |
| 15 | int line; |
| 16 | string error; |
| 17 | |
| 18 | ErrorPos(); |
| 19 | ErrorPos(const ErrorPos& that); |
| 20 | ErrorPos(const string& file, int line, const string& error); |
| 21 | ~ErrorPos(); |
| 22 | bool operator<(const ErrorPos& rhs) const; |
| 23 | bool operator==(const ErrorPos& rhs) const; |
| 24 | ErrorPos& operator=(const ErrorPos& rhs); |
| 25 | |
| 26 | void Print(FILE* to) const; |
| 27 | }; |
| 28 | |
| 29 | static set<ErrorPos> g_errors; |
| 30 | |
| 31 | ErrorPos::ErrorPos() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | ErrorPos::ErrorPos(const ErrorPos& that) |
| 36 | :file(that.file), |
| 37 | line(that.line), |
| 38 | error(that.error) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | ErrorPos::ErrorPos(const string& f, int l, const string& e) |
| 43 | :file(f), |
| 44 | line(l), |
| 45 | error(e) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | ErrorPos::~ErrorPos() |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | bool |
| 54 | ErrorPos::operator<(const ErrorPos& rhs) const |
| 55 | { |
| 56 | if (this->file < rhs.file) return true; |
| 57 | if (this->file == rhs.file) { |
| 58 | if (this->line < rhs.line) return true; |
| 59 | if (this->line == rhs.line) { |
| 60 | if (this->error < rhs.error) return true; |
| 61 | } |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | bool |
| 67 | ErrorPos::operator==(const ErrorPos& rhs) const |
| 68 | { |
| 69 | return this->file == rhs.file |
| 70 | && this->line == rhs.line |
| 71 | && this->error == rhs.error; |
| 72 | } |
| 73 | |
| 74 | ErrorPos& |
| 75 | ErrorPos::operator=(const ErrorPos& rhs) |
| 76 | { |
| 77 | this->file = rhs.file; |
| 78 | this->line = rhs.line; |
| 79 | this->error = rhs.error; |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | ErrorPos::Print(FILE* to) const |
| 85 | { |
| 86 | if (this->line >= 0) { |
| 87 | fprintf(to, "%s:%d: %s\n", this->file.c_str(), this->line, this->error.c_str()); |
| 88 | } else { |
| 89 | fprintf(to, "%s: %s\n", this->file.c_str(), this->error.c_str()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // SourcePos |
| 94 | // ============================================================================= |
| 95 | SourcePos::SourcePos(const string& f, int l) |
| 96 | : file(f), line(l) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | SourcePos::SourcePos(const SourcePos& that) |
| 101 | : file(that.file), line(that.line) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | SourcePos::SourcePos() |
| 106 | : file("???", 0) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | SourcePos::~SourcePos() |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | string |
| 115 | SourcePos::ToString() const |
| 116 | { |
| 117 | char buf[1024]; |
| 118 | if (this->line >= 0) { |
| 119 | snprintf(buf, sizeof(buf)-1, "%s:%d", this->file.c_str(), this->line); |
| 120 | } else { |
| 121 | snprintf(buf, sizeof(buf)-1, "%s:", this->file.c_str()); |
| 122 | } |
| 123 | buf[sizeof(buf)-1] = '\0'; |
| 124 | return string(buf); |
| 125 | } |
| 126 | |
| 127 | int |
| 128 | SourcePos::Error(const char* fmt, ...) const |
| 129 | { |
| 130 | int retval=0; |
| 131 | char buf[1024]; |
| 132 | va_list ap; |
| 133 | va_start(ap, fmt); |
| 134 | retval = vsnprintf(buf, sizeof(buf), fmt, ap); |
| 135 | va_end(ap); |
| 136 | char* p = buf + retval - 1; |
| 137 | while (p > buf && *p == '\n') { |
| 138 | *p = '\0'; |
| 139 | p--; |
| 140 | } |
| 141 | ErrorPos err(this->file, this->line, string(buf)); |
| 142 | if (g_errors.find(err) == g_errors.end()) { |
| 143 | err.Print(stderr); |
| 144 | g_errors.insert(err); |
| 145 | } |
| 146 | return retval; |
| 147 | } |
| 148 | |
| 149 | bool |
| 150 | SourcePos::HasErrors() |
| 151 | { |
| 152 | return g_errors.size() > 0; |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | SourcePos::PrintErrors(FILE* to) |
| 157 | { |
| 158 | set<ErrorPos>::const_iterator it; |
| 159 | for (it=g_errors.begin(); it!=g_errors.end(); it++) { |
| 160 | it->Print(to); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | |