blob: 9d7c5c65447e0e67362036138e6d6f4383637f00 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001#include "SourcePos.h"
2
3#include <stdarg.h>
4#include <set>
5
6using namespace std;
7
8const SourcePos GENERATED_POS("<generated>", -1);
9
10// ErrorPos
11// =============================================================================
12struct 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
29static set<ErrorPos> g_errors;
30
31ErrorPos::ErrorPos()
32{
33}
34
35ErrorPos::ErrorPos(const ErrorPos& that)
36 :file(that.file),
37 line(that.line),
38 error(that.error)
39{
40}
41
42ErrorPos::ErrorPos(const string& f, int l, const string& e)
43 :file(f),
44 line(l),
45 error(e)
46{
47}
48
49ErrorPos::~ErrorPos()
50{
51}
52
53bool
54ErrorPos::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
66bool
67ErrorPos::operator==(const ErrorPos& rhs) const
68{
69 return this->file == rhs.file
70 && this->line == rhs.line
71 && this->error == rhs.error;
72}
73
74ErrorPos&
75ErrorPos::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
83void
84ErrorPos::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// =============================================================================
95SourcePos::SourcePos(const string& f, int l)
96 : file(f), line(l)
97{
98}
99
100SourcePos::SourcePos(const SourcePos& that)
101 : file(that.file), line(that.line)
102{
103}
104
105SourcePos::SourcePos()
106 : file("???", 0)
107{
108}
109
110SourcePos::~SourcePos()
111{
112}
113
114string
115SourcePos::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
127int
128SourcePos::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
149bool
150SourcePos::HasErrors()
151{
152 return g_errors.size() > 0;
153}
154
155void
156SourcePos::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