tradcpp: upgrade to 0.5.3

This commit is contained in:
Jon Trulson
2019-12-03 18:12:22 -07:00
parent 15dfdf231a
commit ec8f4b7464
28 changed files with 843 additions and 186 deletions

View File

@@ -32,6 +32,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "utils.h"
#include "array.h"
@@ -52,8 +53,10 @@ static bool overall_failure;
static const char *myprogname;
static FILE *debuglogfile;
////////////////////////////////////////////////////////////
// seenfiles
// placefiles
static
struct placefile *
@@ -101,6 +104,42 @@ place_getparsedir(const struct place *place)
return place->file->dir;
}
static
struct placefile *
placefile_find(const struct place *incfrom, const char *name)
{
unsigned i, num;
struct placefile *pf;
num = placefilearray_num(&placefiles);
for (i=0; i<num; i++) {
pf = placefilearray_get(&placefiles, i);
if (place_eq(incfrom, &pf->includedfrom) &&
!strcmp(name, pf->name)) {
return pf;
}
}
return NULL;
}
void
place_changefile(struct place *p, const char *name)
{
struct placefile *pf;
assert(p->type == P_FILE);
if (!strcmp(name, p->file->name)) {
return;
}
pf = placefile_find(&p->file->includedfrom, name);
if (pf == NULL) {
pf = placefile_create(&p->file->includedfrom, name,
p->file->fromsystemdir);
placefilearray_add(&placefiles, pf, NULL);
}
p->file = pf;
}
const struct placefile *
place_addfile(const struct place *place, const char *file, bool issystem)
{
@@ -154,7 +193,34 @@ place_setfilestart(struct place *p, const struct placefile *pf)
p->column = 1;
}
static
void
place_addcolumns(struct place *p, unsigned cols)
{
unsigned newcol;
newcol = p->column + cols;
if (newcol < p->column) {
/* overflow (use the old place to complain) */
complain(p, "Column numbering overflow");
die();
}
p->column = newcol;
}
void
place_addlines(struct place *p, unsigned lines)
{
unsigned nextline;
nextline = p->line + lines;
if (nextline < p->line) {
/* overflow (use the old place to complain) */
complain(p, "Line numbering overflow");
die();
}
p->line = nextline;
}
const char *
place_getname(const struct place *p)
{
@@ -168,6 +234,30 @@ place_getname(const struct place *p)
return NULL;
}
bool
place_samefile(const struct place *a, const struct place *b)
{
if (a->type != b->type) {
return false;
}
if (a->file != b->file) {
return false;
}
return true;
}
bool
place_eq(const struct place *a, const struct place *b)
{
if (!place_samefile(a, b)) {
return false;
}
if (a->line != b->line || a->column != b->column) {
return false;
}
return true;
}
static
void
place_printfrom(const struct place *p)
@@ -224,6 +314,70 @@ complain_failed(void)
return overall_failure;
}
////////////////////////////////////////////////////////////
// debug logging
void
debuglog_open(const struct place *p, /*const*/ char *file)
{
assert(debuglogfile == NULL);
debuglogfile = fopen(file, "w");
if (debuglogfile == NULL) {
complain(p, "%s: %s", file, strerror(errno));
die();
}
}
void
debuglog_close(void)
{
if (debuglogfile != NULL) {
fclose(debuglogfile);
debuglogfile = NULL;
}
}
PF(2, 3) void
debuglog(const struct place *p, const char *fmt, ...)
{
va_list ap;
if (debuglogfile == NULL) {
return;
}
fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line);
va_start(ap, fmt);
vfprintf(debuglogfile, fmt, ap);
va_end(ap);
fprintf(debuglogfile, "\n");
fflush(debuglogfile);
}
PF(3, 4) void
debuglog2(const struct place *p, const struct place *p2, const char *fmt, ...)
{
va_list ap;
if (debuglogfile == NULL) {
return;
}
fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line);
if (place_samefile(p, p2)) {
fprintf(debuglogfile, "(block began at line %u) ",
p2->line);
} else {
fprintf(debuglogfile, "(block began at %s:%u)",
place_getname(p2), p2->line);
}
va_start(ap, fmt);
vfprintf(debuglogfile, fmt, ap);
va_end(ap);
fprintf(debuglogfile, "\n");
fflush(debuglogfile);
}
////////////////////////////////////////////////////////////
// module init and cleanup