Skip to content

Commit

Permalink
Merge pull request #2868 from yebblies/rooterror
Browse files Browse the repository at this point in the history
[DDMD] Do all error emission in the frontend
  • Loading branch information
Iain Buclaw committed Dec 18, 2013
2 parents b8d76b8 + a422fb6 commit 9370338
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 147 deletions.
10 changes: 5 additions & 5 deletions src/doc.c
Expand Up @@ -242,7 +242,7 @@ void Module::gendocfile()
{
FileName f((*global.params.ddocfiles)[i]);
File file(&f);
file.readv();
readFile(loc, &file);
// BUG: convert file contents to UTF-8 before use

//printf("file: '%.*s'\n", file.len, file.buffer);
Expand Down Expand Up @@ -347,8 +347,8 @@ void Module::gendocfile()
assert(docfile);
docfile->setbuffer(buf.data, buf.offset);
docfile->ref = 1;
FileName::ensurePathToNameExists(docfile->toChars());
docfile->writev();
ensurePathToNameExists(Loc(), docfile->toChars());
writeFile(loc, docfile);
#else
/* Remove all the escape sequences from buf2
*/
Expand All @@ -370,8 +370,8 @@ void Module::gendocfile()
// Transfer image to file
docfile->setbuffer(buf2.data, buf2.offset);
docfile->ref = 1;
FileName::ensurePathToNameExists(docfile->toChars());
docfile->writev();
ensurePathToNameExists(Loc(), docfile->toChars());
writeFile(loc, docfile);
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions src/glue.c
Expand Up @@ -259,10 +259,10 @@ void obj_end(Library *library, File *objfile)
objfile->setbuffer(objbuf.buf, objbuf.p - objbuf.buf);
objbuf.buf = NULL;

FileName::ensurePathToNameExists(objfilename);
ensurePathToNameExists(Loc(), objfilename);

//printf("write obj %s\n", objfilename);
objfile->writev();
writeFile(Loc(), objfile);
}
objbuf.pend = NULL;
objbuf.p = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/hdrgen.c
Expand Up @@ -63,8 +63,8 @@ void Module::genhdrfile()
hdrfile->setbuffer(hdrbufr.data, hdrbufr.offset);
hdrbufr.data = NULL;

FileName::ensurePathToNameExists(hdrfile->toChars());
hdrfile->writev();
ensurePathToNameExists(Loc(), hdrfile->toChars());
writeFile(loc, hdrfile);
}


Expand Down
6 changes: 3 additions & 3 deletions src/libelf.c
Expand Up @@ -123,9 +123,9 @@ void LibElf::write()
libbuf.extractData();


FileName::ensurePathToNameExists(libfile->name->toChars());
ensurePathToNameExists(Loc(), libfile->name->toChars());

libfile->writev();
writeFile(Loc(), libfile);
}

/*****************************************************************************/
Expand Down Expand Up @@ -310,7 +310,7 @@ void LibElf::addObject(const char *module_name, void *buf, size_t buflen)
{ assert(module_name[0]);
FileName f((char *)module_name);
File file(&f);
file.readv();
readFile(Loc(), &file);
buf = file.buffer;
buflen = file.len;
file.ref = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/libmach.c
Expand Up @@ -133,9 +133,9 @@ void LibMach::write()
libbuf.extractData();


FileName::ensurePathToNameExists(libfile->name->toChars());
ensurePathToNameExists(Loc(), libfile->name->toChars());

libfile->writev();
writeFile(Loc(), libfile);
}

/*****************************************************************************/
Expand Down Expand Up @@ -323,7 +323,7 @@ void LibMach::addObject(const char *module_name, void *buf, size_t buflen)
{ assert(module_name[0]);
FileName f((char *)module_name);
File file(&f);
file.readv();
readFile(Loc(), &file);
buf = file.buffer;
buflen = file.len;
file.ref = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/libmscoff.c
Expand Up @@ -149,9 +149,9 @@ void LibMSCoff::write()
libbuf.extractData();


FileName::ensurePathToNameExists(libfile->name->toChars());
ensurePathToNameExists(Loc(), libfile->name->toChars());

libfile->writev();
writeFile(Loc(), libfile);
}

/*****************************************************************************/
Expand Down Expand Up @@ -343,7 +343,7 @@ void LibMSCoff::addObject(const char *module_name, void *buf, size_t buflen)
{ assert(module_name[0]);
FileName f((char *)module_name);
File file(&f);
file.readv();
readFile(Loc(), &file);
buf = file.buffer;
buflen = file.len;
file.ref = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/libomf.c
Expand Up @@ -129,9 +129,9 @@ void LibOMF::write()
libbuf.extractData();


FileName::ensurePathToNameExists(libfile->name->toChars());
ensurePathToNameExists(Loc(), libfile->name->toChars());

libfile->writev();
writeFile(Loc(), libfile);
}

/*****************************************************************************/
Expand Down Expand Up @@ -236,7 +236,7 @@ void LibOMF::addObject(const char *module_name, void *buf, size_t buflen)
{ assert(module_name);
FileName f((char *)module_name);
File file(&f);
file.readv();
readFile(Loc(), &file);
buf = file.buffer;
buflen = file.len;
file.ref = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/link.c
Expand Up @@ -186,7 +186,7 @@ int runLINK()
}

// Make sure path to exe file exists
FileName::ensurePathToNameExists(global.params.exefile);
ensurePathToNameExists(Loc(), global.params.exefile);

cmdbuf.writeByte(' ');
if (global.params.mapfile)
Expand Down Expand Up @@ -330,7 +330,7 @@ int runLINK()
}

// Make sure path to exe file exists
FileName::ensurePathToNameExists(global.params.exefile);
ensurePathToNameExists(Loc(), global.params.exefile);

cmdbuf.writeByte(',');
if (global.params.mapfile)
Expand Down Expand Up @@ -514,7 +514,7 @@ int runLINK()
}

// Make sure path to exe file exists
FileName::ensurePathToNameExists(global.params.exefile);
ensurePathToNameExists(Loc(), global.params.exefile);

if (global.params.symdebug)
argv.push("-g");
Expand Down
40 changes: 37 additions & 3 deletions src/mars.c
Expand Up @@ -319,6 +319,40 @@ void vdeprecation(Loc loc, const char *format, va_list ap,
verrorPrint(loc, header, format, ap, p1, p2);
}

void readFile(Loc loc, File *f)
{
if (f->read())
{
error(loc, "Error reading file '%s'", f->name->toChars());
fatal();
}
}

void writeFile(Loc loc, File *f)
{
if (f->write())
{
error(loc, "Error writing file '%s'", f->name->toChars());
fatal();
}
}

void ensurePathToNameExists(Loc loc, const char *name)
{
const char *pt = FileName::path(name);
int r = 0;
if (*pt)
{
if (FileName::ensurePathExists(pt))
{
error(loc, "cannot create directory %s", pt);
fatal();
}
}
FileName::free(pt);
}


/***************************************
* Call this after printing out fatal error messages to clean up and exit
* the compiler.
Expand Down Expand Up @@ -1562,7 +1596,7 @@ Language changes listed by -transition=id:\n\
{
File deps(global.params.moduleDepsFile);
deps.setbuffer((void*)ob->data, ob->offset);
deps.writev();
writeFile(Loc(), &deps);
}
else
printf("%.*s", (int)ob->offset, ob->data);
Expand Down Expand Up @@ -1638,13 +1672,13 @@ Language changes listed by -transition=id:\n\
jsonfilename = FileName::forceExt(n, global.json_ext);
}

FileName::ensurePathToNameExists(jsonfilename);
ensurePathToNameExists(Loc(), jsonfilename);

File *jsonfile = new File(jsonfilename);

jsonfile->setbuffer(buf.data, buf.offset);
jsonfile->ref = 1;
jsonfile->writev();
writeFile(Loc(), jsonfile);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/mars.h
Expand Up @@ -440,6 +440,10 @@ void obj_end(Library *library, File *objfile);
void obj_append(Dsymbol *s);
void obj_write_deferred(Library *library);

void readFile(Loc loc, File *f);
void writeFile(Loc loc, File *f);
void ensurePathToNameExists(Loc loc, const char *name);

const char *importHint(const char *s);
/// Little helper function for writting out deps.
void escapePath(OutBuffer *buf, const char *fname);
Expand Down
2 changes: 1 addition & 1 deletion src/module.c
Expand Up @@ -852,7 +852,7 @@ void Module::gensymfile()
symfile->setbuffer(buf.data, buf.offset);
buf.data = NULL;

symfile->writev();
writeFile(loc, symfile);
}

/**********************************
Expand Down
38 changes: 0 additions & 38 deletions src/root/file.c
Expand Up @@ -37,8 +37,6 @@
#include "array.h"
#include "port.h"

void error(const char *format, ...);

/****************************** File ********************************/

File::File(const FileName *n)
Expand Down Expand Up @@ -385,36 +383,6 @@ int File::append()
#endif
}

/**************************************
*/

void File::readv()
{
if (read())
error("Error reading file '%s'",name->toChars());
}

/**************************************
*/

void File::mmreadv()
{
if (mmread())
readv();
}

void File::writev()
{
if (write())
error("Error writing file '%s'",name->toChars());
}

void File::appendv()
{
if (write())
error("Error appending to file '%s'",name->toChars());
}

/*******************************************
* Return !=0 if file exists.
* 0: file doesn't exist
Expand Down Expand Up @@ -539,12 +507,6 @@ void File::stat()
#endif
}

void File::checkoffset(size_t offset, size_t nbytes)
{
if (offset > len || offset + nbytes > len)
error("Corrupt file '%s': offset x%llx off end of file",toChars(),(ulonglong)offset);
}

char *File::toChars()
{
return name->toChars();
Expand Down
26 changes: 0 additions & 26 deletions src/root/file.h
Expand Up @@ -43,34 +43,16 @@ struct File

int read();

/* Write file, either succeed or fail
* with error message & exit.
*/

void readv();

/* Read file, return !=0 if error
*/

int mmread();

/* Write file, either succeed or fail
* with error message & exit.
*/

void mmreadv();

/* Write file, return !=0 if error
*/

int write();

/* Write file, either succeed or fail
* with error message & exit.
*/

void writev();

/* Return !=0 if file exists.
* 0: file doesn't exist
* 1: normal file
Expand All @@ -82,12 +64,6 @@ struct File

int append();

/* Append to file, either succeed or fail
* with error message & exit.
*/

void appendv();

/* Return !=0 if file exists.
* 0: file doesn't exist
* 1: normal file
Expand Down Expand Up @@ -121,8 +97,6 @@ struct File
this->len = len;
}

void checkoffset(size_t offset, size_t nbytes);

void remove(); // delete file
};

Expand Down

0 comments on commit 9370338

Please sign in to comment.