aboutsummaryrefslogtreecommitdiff
path: root/src/compiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler.c')
-rw-r--r--src/compiler.c57
1 files changed, 32 insertions, 25 deletions
diff --git a/src/compiler.c b/src/compiler.c
index f367f9a..f72ab1c 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -16,7 +16,9 @@
#include <errno.h>
#include <fwd/compiler.h>
+#include <fwd/coverage.h>
#include <fwd/analyze.h>
+#include <fwd/tracker.h>
#include <fwd/parser.h>
#include <fwd/debug.h>
#include <fwd/lower.h>
@@ -46,13 +48,16 @@ static char *read_file(const char *file, FILE *f)
fseek(f, 0, SEEK_SET);
- char *buf = malloc((size_t)(s + 1));
- if (!buf)
+ char *buf = mallocc((size_t)(s + 1));
+ if (!buf) {
+ internal_error("failed allocating fread buffer");
return NULL;
+ }
fread(buf, (size_t)(s + 1), 1, f);
/* remember terminating null */
buf[s] = 0;
+ track_ptr(buf);
return buf;
}
@@ -81,10 +86,8 @@ static int process(struct scope *scope, const char *file)
return -1;
struct parser *p = create_parser();
- if (!p) {
- free((void *)buf);
+ if (!p)
return -1;
- }
parse(p, file, buf);
@@ -92,15 +95,21 @@ static int process(struct scope *scope, const char *file)
bool failed = p->failed;
destroy_parser(p);
- if (failed) {
- free((void *)buf);
+ if (failed)
return -1;
- }
ast_dump_list(0, tree);
+ char *fname = strdupc(file);
+ if (!fname) {
+ internal_error("failed allocating fname");
+ return -1;
+ }
+
+ track_ptr(fname);
+
scope->fctx.fbuf = buf;
- scope->fctx.fname = strdup(file);
+ scope->fctx.fname = fname;
if (analyze_root(scope, tree))
return -1;
@@ -124,7 +133,6 @@ static void destroy_scopes()
{
foreach(scopes, n, &scopes) {
destroy_scope(n->data);
- free((void *)n->key);
}
scopes_destroy(&scopes);
@@ -136,57 +144,56 @@ struct scope *compile_file(const char *file)
const char *base = NULL, *dir = NULL, *cwd = NULL, *real = NULL;
if (!(base = fwd_basename(file))) {
error("couldn't get basename of %s", file);
- goto out;
+ return NULL;
}
if (!(dir = fwd_dirname(file))) {
error("couldn't get dirname of %s", file);
- goto out;
+ return NULL;
}
if (!(cwd = fwd_cwdname())) {
error("couldn't get current working dir");
- goto out;
+ return NULL;
}
if (*dir != 0 && chdir(dir)) {
error("couldn't change to directory %s: %s", dir,
strerror(errno)
);
- goto out;
+ return NULL;
}
if (!(real = realpath(base, NULL))) {
error("no such file: %s", file);
- goto out;
+ return NULL;
}
+ /* const cast, ech */
+ track_ptr((void *)real);
+
struct scope **exists = scopes_find(&scopes, real);
if (exists) {
scope = *exists;
- goto out;
+ return NULL;
}
scope = create_scope();
- scopes_insert(&scopes, strdup(real), scope);
+ scopes_insert(&scopes, real, scope);
if (process(scope, base)) {
scope = NULL;
- goto out;
+ return NULL;
}
if (chdir(cwd)) {
error("couldn't change back to directory %s: %s", cwd,
strerror(errno)
);
- goto out;
+
+ return NULL;
}
-out:
- free((void *)base);
- free((void *)dir);
- free((void *)cwd);
- free((void *)real);
return scope;
}
@@ -207,6 +214,6 @@ int compile(const char *input) {
out:
destroy_scopes();
- destroy_allocs();
+ free_tracked_ptrs();
return ret;
}