Jack Palevich bdb087c930 Add includes to enable host C++ tools to compile with GCC 4.4
Otherwise printf is undeclared. These files worked with earlier versions
of gcc because either cstdio or stdio.h was being included by some other
header file. But this was not guaranteed behavior, so with GCC 4.4 there
are errors. The fix is backwards compatible with earlier versions of GCC.

This change includes either <cstdio> or <stdio.h> whichever looks more
appropriate given the other headers included by the given source file.

Note, I'm using "GCC" to mean Gnu Compile Collection, as this problem is
specific to C++ source files. (Presumably a C++-specific header file
changed to no longer include cstdio.)
2009-06-24 19:01:27 -07:00

168 lines
3.1 KiB
C++

#include "SourcePos.h"
#include <stdarg.h>
#include <cstdio>
#include <set>
using namespace std;
const SourcePos GENERATED_POS("<generated>", -1);
// ErrorPos
// =============================================================================
struct ErrorPos
{
string file;
int line;
string error;
ErrorPos();
ErrorPos(const ErrorPos& that);
ErrorPos(const string& file, int line, const string& error);
~ErrorPos();
bool operator<(const ErrorPos& rhs) const;
bool operator==(const ErrorPos& rhs) const;
ErrorPos& operator=(const ErrorPos& rhs);
void Print(FILE* to) const;
};
static set<ErrorPos> g_errors;
ErrorPos::ErrorPos()
{
}
ErrorPos::ErrorPos(const ErrorPos& that)
:file(that.file),
line(that.line),
error(that.error)
{
}
ErrorPos::ErrorPos(const string& f, int l, const string& e)
:file(f),
line(l),
error(e)
{
}
ErrorPos::~ErrorPos()
{
}
bool
ErrorPos::operator<(const ErrorPos& rhs) const
{
if (this->file < rhs.file) return true;
if (this->file == rhs.file) {
if (this->line < rhs.line) return true;
if (this->line == rhs.line) {
if (this->error < rhs.error) return true;
}
}
return false;
}
bool
ErrorPos::operator==(const ErrorPos& rhs) const
{
return this->file == rhs.file
&& this->line == rhs.line
&& this->error == rhs.error;
}
ErrorPos&
ErrorPos::operator=(const ErrorPos& rhs)
{
this->file = rhs.file;
this->line = rhs.line;
this->error = rhs.error;
return *this;
}
void
ErrorPos::Print(FILE* to) const
{
if (this->line >= 0) {
fprintf(to, "%s:%d: %s\n", this->file.c_str(), this->line, this->error.c_str());
} else {
fprintf(to, "%s: %s\n", this->file.c_str(), this->error.c_str());
}
}
// SourcePos
// =============================================================================
SourcePos::SourcePos(const string& f, int l)
: file(f), line(l)
{
}
SourcePos::SourcePos(const SourcePos& that)
: file(that.file), line(that.line)
{
}
SourcePos::SourcePos()
: file("???", 0)
{
}
SourcePos::~SourcePos()
{
}
string
SourcePos::ToString() const
{
char buf[1024];
if (this->line >= 0) {
snprintf(buf, sizeof(buf)-1, "%s:%d", this->file.c_str(), this->line);
} else {
snprintf(buf, sizeof(buf)-1, "%s:", this->file.c_str());
}
buf[sizeof(buf)-1] = '\0';
return string(buf);
}
int
SourcePos::Error(const char* fmt, ...) const
{
int retval=0;
char buf[1024];
va_list ap;
va_start(ap, fmt);
retval = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
char* p = buf + retval - 1;
while (p > buf && *p == '\n') {
*p = '\0';
p--;
}
ErrorPos err(this->file, this->line, string(buf));
if (g_errors.find(err) == g_errors.end()) {
err.Print(stderr);
g_errors.insert(err);
}
return retval;
}
bool
SourcePos::HasErrors()
{
return g_errors.size() > 0;
}
void
SourcePos::PrintErrors(FILE* to)
{
set<ErrorPos>::const_iterator it;
for (it=g_errors.begin(); it!=g_errors.end(); it++) {
it->Print(to);
}
}