| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "SourcePos.h" | 
|  | 2 |  | 
|  | 3 | #include <stdarg.h> | 
|  | 4 | #include <vector> | 
|  | 5 |  | 
|  | 6 | using namespace std; | 
|  | 7 |  | 
|  | 8 |  | 
|  | 9 | // ErrorPos | 
|  | 10 | // ============================================================================= | 
|  | 11 | struct ErrorPos | 
|  | 12 | { | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 13 | enum Level { | 
|  | 14 | NOTE, | 
|  | 15 | WARNING, | 
|  | 16 | ERROR | 
|  | 17 | }; | 
|  | 18 |  | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | String8 file; | 
|  | 20 | int line; | 
|  | 21 | String8 error; | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 22 | Level level; | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 23 |  | 
|  | 24 | ErrorPos(); | 
|  | 25 | ErrorPos(const ErrorPos& that); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 26 | ErrorPos(const String8& file, int line, const String8& error, Level level); | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 27 | ErrorPos& operator=(const ErrorPos& rhs); | 
|  | 28 |  | 
|  | 29 | void print(FILE* to) const; | 
|  | 30 | }; | 
|  | 31 |  | 
|  | 32 | static vector<ErrorPos> g_errors; | 
|  | 33 |  | 
|  | 34 | ErrorPos::ErrorPos() | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 35 | :line(-1), level(NOTE) | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 36 | { | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | ErrorPos::ErrorPos(const ErrorPos& that) | 
|  | 40 | :file(that.file), | 
|  | 41 | line(that.line), | 
|  | 42 | error(that.error), | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 43 | level(that.level) | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 44 | { | 
|  | 45 | } | 
|  | 46 |  | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 47 | ErrorPos::ErrorPos(const String8& f, int l, const String8& e, Level lev) | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 48 | :file(f), | 
|  | 49 | line(l), | 
|  | 50 | error(e), | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 51 | level(lev) | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 52 | { | 
|  | 53 | } | 
|  | 54 |  | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 55 | ErrorPos& | 
|  | 56 | ErrorPos::operator=(const ErrorPos& rhs) | 
|  | 57 | { | 
|  | 58 | this->file = rhs.file; | 
|  | 59 | this->line = rhs.line; | 
|  | 60 | this->error = rhs.error; | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 61 | this->level = rhs.level; | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 62 | return *this; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | void | 
|  | 66 | ErrorPos::print(FILE* to) const | 
|  | 67 | { | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 68 | const char* type = ""; | 
|  | 69 | switch (level) { | 
|  | 70 | case NOTE: | 
|  | 71 | type = "note: "; | 
|  | 72 | break; | 
|  | 73 | case WARNING: | 
|  | 74 | type = "warning: "; | 
|  | 75 | break; | 
|  | 76 | case ERROR: | 
|  | 77 | type = "error: "; | 
|  | 78 | break; | 
|  | 79 | } | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 80 |  | 
| Tomasz Wasilczyk | 7e22cab | 2023-08-24 19:02:33 +0000 | [diff] [blame] | 81 | if (!this->file.empty()) { | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 82 | if (this->line >= 0) { | 
| Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 83 | fprintf(to, "%s:%d: %s%s\n", this->file.c_str(), this->line, type, this->error.c_str()); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 84 | } else { | 
| Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 85 | fprintf(to, "%s: %s%s\n", this->file.c_str(), type, this->error.c_str()); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 86 | } | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 87 | } else { | 
| Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 88 | fprintf(to, "%s%s\n", type, this->error.c_str()); | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 89 | } | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | // SourcePos | 
|  | 93 | // ============================================================================= | 
|  | 94 | SourcePos::SourcePos(const String8& f, int l) | 
|  | 95 | : file(f), line(l) | 
|  | 96 | { | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | SourcePos::SourcePos(const SourcePos& that) | 
|  | 100 | : file(that.file), line(that.line) | 
|  | 101 | { | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | SourcePos::SourcePos() | 
|  | 105 | : file("???", 0), line(-1) | 
|  | 106 | { | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | SourcePos::~SourcePos() | 
|  | 110 | { | 
|  | 111 | } | 
|  | 112 |  | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 113 | void | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 114 | SourcePos::error(const char* fmt, ...) const | 
|  | 115 | { | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 116 | va_list ap; | 
|  | 117 | va_start(ap, fmt); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 118 | String8 msg = String8::formatV(fmt, ap); | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 119 | va_end(ap); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 120 | g_errors.push_back(ErrorPos(this->file, this->line, msg, ErrorPos::ERROR)); | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 123 | void | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 124 | SourcePos::warning(const char* fmt, ...) const | 
|  | 125 | { | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 126 | va_list ap; | 
|  | 127 | va_start(ap, fmt); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 128 | String8 msg = String8::formatV(fmt, ap); | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 129 | va_end(ap); | 
| Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 130 | ErrorPos(this->file, this->line, msg, ErrorPos::WARNING).print(stderr); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | void | 
|  | 134 | SourcePos::printf(const char* fmt, ...) const | 
|  | 135 | { | 
|  | 136 | va_list ap; | 
|  | 137 | va_start(ap, fmt); | 
|  | 138 | String8 msg = String8::formatV(fmt, ap); | 
|  | 139 | va_end(ap); | 
|  | 140 | ErrorPos(this->file, this->line, msg, ErrorPos::NOTE).print(stderr); | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 141 | } | 
|  | 142 |  | 
|  | 143 | bool | 
| Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 144 | SourcePos::operator<(const SourcePos& rhs) const | 
|  | 145 | { | 
|  | 146 | return (file < rhs.file) || (line < rhs.line); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | bool | 
| Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 150 | SourcePos::hasErrors() | 
|  | 151 | { | 
|  | 152 | return g_errors.size() > 0; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | void | 
|  | 156 | SourcePos::printErrors(FILE* to) | 
|  | 157 | { | 
|  | 158 | vector<ErrorPos>::const_iterator it; | 
|  | 159 | for (it=g_errors.begin(); it!=g_errors.end(); it++) { | 
|  | 160 | it->print(to); | 
|  | 161 | } | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 |  | 
|  | 165 |  |