view place.c @ 10:800f3a560a3b

move seenfiles to place.c too
author David A. Holland
date Sun, 19 Dec 2010 19:27:14 -0500
parents 97243badae69
children b9d50e786322
line wrap: on
line source

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#include "utils.h"
#include "array.h"
#include "place.h"

#define NOWHERE_LINE      0
#define BUILTIN_LINE      1
#define COMMANDLINE_LINE  2

struct seenfile {
	struct place includedfrom;
	char *name;
	bool fromsystemdir;
};
DECLARRAY(seenfile);
DEFARRAY(seenfile, );

static bool overall_failure;

static struct place scratchplace;
static bool scratchplace_inuse;

static struct seenfilearray seenfiles;

////////////////////////////////////////////////////////////
// seenfiles

static
struct seenfile *
seenfile_create(const struct place *from, char *name, bool fromsystemdir)
{
	struct seenfile *sf;

	sf = domalloc(sizeof(*sf));
	sf->includedfrom = *from;
	sf->name = name;
	sf->fromsystemdir = fromsystemdir;
	return sf;
}

static
void
seenfile_destroy(struct seenfile *sf)
{
	free(sf->name);
	free(sf);
}

DESTROYALL_ARRAY(seenfile, );

struct seenfile *
place_seen_file(const struct place *place, char *file, bool issystem)
{
	struct seenfile *sf;

	sf = seenfile_create(place, file, issystem);
	seenfilearray_add(&seenfiles, sf, NULL);
	return sf;
}

////////////////////////////////////////////////////////////
// places

static
bool
place_isnowhere(const struct place *p)
{
	return p->file == NULL && p->line == NOWHERE_LINE;
}

static
bool
place_isbuiltin(const struct place *p)
{
	return p->file == NULL && p->line == BUILTIN_LINE;
}

static
bool
place_iscommandline(const struct place *p)
{
	return p->file == NULL && p->line == COMMANDLINE_LINE;
}

struct place *
place_gettemporary(void)
{
	assert(!scratchplace_inuse);
	scratchplace_inuse = true;
	return &scratchplace;
}

void
place_puttemporary(struct place *p)
{
	assert(scratchplace_inuse);
	assert(p == &scratchplace);
	scratchplace_inuse = false;
}

struct place *
place_create(void)
{
	struct place *p;

	p = domalloc(sizeof(*p));
	place_setnowhere(p);
	return p;
}

struct place *
place_clone(const struct place *op)
{
	struct place *p;

	p = domalloc(sizeof(*p));
	*p = *op;
	return p;
}

void
place_destroy(struct place *p)
{
	free(p);
}

void
place_setnowhere(struct place *p)
{
	p->file = NULL;
	p->line = NOWHERE_LINE;
	p->column = 0;
}

void
place_setbuiltin(struct place *p, unsigned num)
{
	p->file = NULL;
	p->line = BUILTIN_LINE;
	p->column = num;
}

void
place_setcommandline(struct place *p, unsigned column)
{
	p->file = NULL;
	p->line = COMMANDLINE_LINE;
	p->column = column;
}

static
void
place_print(const struct place *p)
{
	if (place_iscommandline(p)) {
		fprintf(stderr, "<command-line>:1:%u", p->column);
	} else if (place_isbuiltin(p)) {
		fprintf(stderr, "<built-in>:%u:1", p->column);
	} else {
		fprintf(stderr, "%s:%u:%u", p->file->name,
			p->line, p->column);
	}
}

static
void
place_printfrom(const struct place *p)
{
	const struct place *from;

	from = &p->file->includedfrom;
	if (!place_isnowhere(from)) {
		place_printfrom(from);
	}
	fprintf(stderr, "In file included from ");
	place_print(p);
	fprintf(stderr, ":\n");
}

////////////////////////////////////////////////////////////
// complaints

void
complain(const struct place *p, const char *fmt, ...)
{
	va_list ap;
	const struct place *from;

	from = &p->file->includedfrom;
	if (!place_isnowhere(from)) {
		place_printfrom(from);
	}
	place_print(p);
	fprintf(stderr, ": ");
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fprintf(stderr, "\n");
}

void
complain_fail(void)
{
	overall_failure = true;
}

bool
complain_failed(void)
{
	return overall_failure;
}

////////////////////////////////////////////////////////////
// module init and cleanup

void
place_init(void)
{
	seenfilearray_init(&seenfiles);
}

void
place_cleanup(void)
{
	seenfilearray_destroyall(&seenfiles);
	seenfilearray_cleanup(&seenfiles);
}