Mercurial > ~dholland > hg > tradcpp > index.cgi
changeset 8:97243badae69
split place stuff to its own file
author | David A. Holland |
---|---|
date | Sun, 19 Dec 2010 19:15:55 -0500 |
parents | b8167949474a |
children | 1fbcbd58742e |
files | Makefile files.c files.h main.c place.c place.h utils.h |
diffstat | 7 files changed, 192 insertions(+), 160 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Sun Dec 19 19:08:24 2010 -0500 +++ b/Makefile Sun Dec 19 19:15:55 2010 -0500 @@ -1,7 +1,7 @@ # $NetBSD$ PROG= tradcpp -SRCS= main.c files.c array.c utils.c +SRCS= main.c files.c place.c array.c utils.c WARNS= 5 .include <bsd.prog.mk>
--- a/files.c Sun Dec 19 19:08:24 2010 -0500 +++ b/files.c Sun Dec 19 19:15:55 2010 -0500 @@ -1,4 +1,3 @@ -#include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -7,14 +6,9 @@ #include <err.h> #include "array.h" +#include "place.h" #include "files.h" -struct place { - struct seenfile *file; - unsigned line; - unsigned column; -}; - struct incdir { const char *name; bool issystem; @@ -33,7 +27,6 @@ static struct incdirarray quotepath, bracketpath; static struct seenfilearray seenfiles; -static bool overall_failure; //////////////////////////////////////////////////////////// // management @@ -138,152 +131,18 @@ } //////////////////////////////////////////////////////////// -// places and complaints - -#define NOWHERE_LINE 0 -#define BUILTIN_LINE 1 -#define COMMANDLINE_LINE 2 - -static struct place scratchplace; -static bool scratchplace_inuse; - -static -bool -place_isnowhere(const struct place *p) -{ - return p->file == NULL && p->line == NOWHERE_LINE; -} +// seenfile functions exposed for places.c -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) +const char * +seenfile_getname(const struct seenfile *file) { - 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); + return file->name; } -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) +const struct place * +seenfile_getincludeplace(const struct seenfile *file) { - 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) -{ - if (!place_isnowhere(&p->file->includedfrom)) { - place_printfrom(&p->file->includedfrom); - } - fprintf(stderr, "In file included from "); - fprintf(stderr, ":\n"); -} - -void -complain(const struct place *p, const char *fmt, ...) -{ - va_list ap; - - if (!place_isnowhere(&p->file->includedfrom)) { - place_printfrom(&p->file->includedfrom); - } - 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; + return &file->includedfrom; } ////////////////////////////////////////////////////////////
--- a/files.h Sun Dec 19 19:08:24 2010 -0500 +++ b/files.h Sun Dec 19 19:15:55 2010 -0500 @@ -1,14 +1,5 @@ struct place; -struct place *place_gettemporary(void); -void place_puttemporary(struct place *p); -struct place *place_create(void); -struct place *place_clone(const struct place *p); -void place_destroy(struct place *); -void place_setnowhere(struct place *p); -void place_setbuiltin(struct place *p, unsigned num); -void place_setcommandline(struct place *p, unsigned column); - void files_init(void); void files_cleanup(void);
--- a/main.c Sun Dec 19 19:08:24 2010 -0500 +++ b/main.c Sun Dec 19 19:15:55 2010 -0500 @@ -10,6 +10,7 @@ #include "utils.h" #include "array.h" #include "mode.h" +#include "place.h" #include "files.h" #include "macro.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/place.c Sun Dec 19 19:15:55 2010 -0500 @@ -0,0 +1,163 @@ +#include <assert.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include "utils.h" +#include "place.h" + +static bool overall_failure; + +#define NOWHERE_LINE 0 +#define BUILTIN_LINE 1 +#define COMMANDLINE_LINE 2 + +static struct place scratchplace; +static bool scratchplace_inuse; + +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", seenfile_getname(p->file), + p->line, p->column); + } +} + +static +void +place_printfrom(const struct place *p) +{ + const struct place *from; + + from = seenfile_getincludeplace(p->file); + if (!place_isnowhere(from)) { + place_printfrom(from); + } + fprintf(stderr, "In file included from "); + place_print(p); + fprintf(stderr, ":\n"); +} + +void +complain(const struct place *p, const char *fmt, ...) +{ + va_list ap; + const struct place *from; + + from = seenfile_getincludeplace(p->file); + 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; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/place.h Sun Dec 19 19:15:55 2010 -0500 @@ -0,0 +1,18 @@ +struct place { + struct seenfile *file; + unsigned line; + unsigned column; +}; + +struct place *place_gettemporary(void); +void place_puttemporary(struct place *p); +struct place *place_create(void); +struct place *place_clone(const struct place *p); +void place_destroy(struct place *); +void place_setnowhere(struct place *p); +void place_setbuiltin(struct place *p, unsigned num); +void place_setcommandline(struct place *p, unsigned column); + +/* in files.c */ +const char *seenfile_getname(const struct seenfile *file); +const struct place *seenfile_getincludeplace(const struct seenfile *file);
--- a/utils.h Sun Dec 19 19:08:24 2010 -0500 +++ b/utils.h Sun Dec 19 19:15:55 2010 -0500 @@ -13,7 +13,7 @@ char *dostrdup2(const char *s, const char *t); char *dostrdup3(const char *s, const char *t, const char *u); -/* in files.c */ +/* in place.c */ void complain(const struct place *, const char *fmt, ...); void complain_fail(void); bool complain_failed(void);