blob: 38643201c22d9f1c607a472ccbac2704bf36a9ec [file] [log] [blame]
Adam Lesinski282e1812014-01-23 18:17:42 -08001#include "SourcePos.h"
2
3#include <stdarg.h>
4#include <vector>
5
6using namespace std;
7
8
9// ErrorPos
10// =============================================================================
11struct ErrorPos
12{
Adam Lesinskia01a9372014-03-20 18:04:57 -070013 enum Level {
14 NOTE,
15 WARNING,
16 ERROR
17 };
18
Adam Lesinski282e1812014-01-23 18:17:42 -080019 String8 file;
20 int line;
21 String8 error;
Adam Lesinskia01a9372014-03-20 18:04:57 -070022 Level level;
Adam Lesinski282e1812014-01-23 18:17:42 -080023
24 ErrorPos();
25 ErrorPos(const ErrorPos& that);
Adam Lesinskia01a9372014-03-20 18:04:57 -070026 ErrorPos(const String8& file, int line, const String8& error, Level level);
Adam Lesinski282e1812014-01-23 18:17:42 -080027 ErrorPos& operator=(const ErrorPos& rhs);
28
29 void print(FILE* to) const;
30};
31
32static vector<ErrorPos> g_errors;
33
34ErrorPos::ErrorPos()
Adam Lesinskia01a9372014-03-20 18:04:57 -070035 :line(-1), level(NOTE)
Adam Lesinski282e1812014-01-23 18:17:42 -080036{
37}
38
39ErrorPos::ErrorPos(const ErrorPos& that)
40 :file(that.file),
41 line(that.line),
42 error(that.error),
Adam Lesinskia01a9372014-03-20 18:04:57 -070043 level(that.level)
Adam Lesinski282e1812014-01-23 18:17:42 -080044{
45}
46
Adam Lesinskia01a9372014-03-20 18:04:57 -070047ErrorPos::ErrorPos(const String8& f, int l, const String8& e, Level lev)
Adam Lesinski282e1812014-01-23 18:17:42 -080048 :file(f),
49 line(l),
50 error(e),
Adam Lesinskia01a9372014-03-20 18:04:57 -070051 level(lev)
Adam Lesinski282e1812014-01-23 18:17:42 -080052{
53}
54
Adam Lesinski282e1812014-01-23 18:17:42 -080055ErrorPos&
56ErrorPos::operator=(const ErrorPos& rhs)
57{
58 this->file = rhs.file;
59 this->line = rhs.line;
60 this->error = rhs.error;
Adam Lesinskia01a9372014-03-20 18:04:57 -070061 this->level = rhs.level;
Adam Lesinski282e1812014-01-23 18:17:42 -080062 return *this;
63}
64
65void
66ErrorPos::print(FILE* to) const
67{
Adam Lesinskia01a9372014-03-20 18:04:57 -070068 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 Lesinski282e1812014-01-23 18:17:42 -080080
Adam Lesinskia01a9372014-03-20 18:04:57 -070081 if (!this->file.isEmpty()) {
82 if (this->line >= 0) {
83 fprintf(to, "%s:%d: %s%s\n", this->file.string(), this->line, type, this->error.string());
84 } else {
85 fprintf(to, "%s: %s%s\n", this->file.string(), type, this->error.string());
86 }
Adam Lesinski282e1812014-01-23 18:17:42 -080087 } else {
Adam Lesinskia01a9372014-03-20 18:04:57 -070088 fprintf(to, "%s%s\n", type, this->error.string());
Adam Lesinski282e1812014-01-23 18:17:42 -080089 }
90}
91
92// SourcePos
93// =============================================================================
94SourcePos::SourcePos(const String8& f, int l)
95 : file(f), line(l)
96{
97}
98
99SourcePos::SourcePos(const SourcePos& that)
100 : file(that.file), line(that.line)
101{
102}
103
104SourcePos::SourcePos()
105 : file("???", 0), line(-1)
106{
107}
108
109SourcePos::~SourcePos()
110{
111}
112
Adam Lesinskia01a9372014-03-20 18:04:57 -0700113void
Adam Lesinski282e1812014-01-23 18:17:42 -0800114SourcePos::error(const char* fmt, ...) const
115{
Adam Lesinski282e1812014-01-23 18:17:42 -0800116 va_list ap;
117 va_start(ap, fmt);
Adam Lesinskia01a9372014-03-20 18:04:57 -0700118 String8 msg = String8::formatV(fmt, ap);
Adam Lesinski282e1812014-01-23 18:17:42 -0800119 va_end(ap);
Adam Lesinskia01a9372014-03-20 18:04:57 -0700120 g_errors.push_back(ErrorPos(this->file, this->line, msg, ErrorPos::ERROR));
Adam Lesinski282e1812014-01-23 18:17:42 -0800121}
122
Adam Lesinskia01a9372014-03-20 18:04:57 -0700123void
Adam Lesinski282e1812014-01-23 18:17:42 -0800124SourcePos::warning(const char* fmt, ...) const
125{
Adam Lesinski282e1812014-01-23 18:17:42 -0800126 va_list ap;
127 va_start(ap, fmt);
Adam Lesinskia01a9372014-03-20 18:04:57 -0700128 String8 msg = String8::formatV(fmt, ap);
Adam Lesinski282e1812014-01-23 18:17:42 -0800129 va_end(ap);
Adam Lesinskia01a9372014-03-20 18:04:57 -0700130 ErrorPos(this->file, this->line, msg, ErrorPos::WARNING).print(stderr);
131}
132
133void
134SourcePos::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 Lesinski282e1812014-01-23 18:17:42 -0800141}
142
143bool
Adam Lesinskide7de472014-11-03 12:03:08 -0800144SourcePos::operator<(const SourcePos& rhs) const
145{
146 return (file < rhs.file) || (line < rhs.line);
147}
148
149bool
Adam Lesinski282e1812014-01-23 18:17:42 -0800150SourcePos::hasErrors()
151{
152 return g_errors.size() > 0;
153}
154
155void
156SourcePos::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