changeset 12:6c15ca895585

improve places more
author David A. Holland
date Sun, 19 Dec 2010 19:39:26 -0500
parents b9d50e786322
children 120629a5d6bf
files place.c place.h
diffstat 2 files changed, 27 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/place.c	Sun Dec 19 19:30:24 2010 -0500
+++ b/place.c	Sun Dec 19 19:39:26 2010 -0500
@@ -61,41 +61,22 @@
 ////////////////////////////////////////////////////////////
 // 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;
-}
-
 void
 place_setnowhere(struct place *p)
 {
+	p->type = P_NOWHERE;
 	p->file = NULL;
-	p->line = NOWHERE_LINE;
+	p->line = 0;
 	p->column = 0;
 }
 
 void
 place_setbuiltin(struct place *p, unsigned num)
 {
+	p->type = P_BUILTIN;
 	p->file = NULL;
-	p->line = BUILTIN_LINE;
-	p->column = num;
+	p->line = num;
+	p->column = 1;
 }
 
 void
@@ -107,17 +88,17 @@
 }
 
 static
-void
-place_print(const struct place *p)
+const char *
+place_getname(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);
+	switch (p->type) {
+	    case P_NOWHERE: return "<nowhere>";
+	    case P_BUILTIN: return "<built-in>";
+	    case P_COMMANDLINE: return "<command-line>";
+	    case P_FILE: return p->file->name;
 	}
+	assert(0);
+	return NULL;
 }
 
 static
@@ -127,12 +108,11 @@
 	const struct place *from;
 
 	from = &p->file->includedfrom;
-	if (!place_isnowhere(from)) {
+	if (from->type != P_NOWHERE) {
 		place_printfrom(from);
+		fprintf(stderr, "In file included from %s:%u:%u:\n",
+			place_getname(from), from->line, from->column);
 	}
-	fprintf(stderr, "In file included from ");
-	place_print(p);
-	fprintf(stderr, ":\n");
 }
 
 ////////////////////////////////////////////////////////////
@@ -142,14 +122,9 @@
 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, ": ");
+	place_printfrom(p);
+	fprintf(stderr, "%s:%u:%u: ", place_getname(p), p->line, p->column);
 	va_start(ap, fmt);
 	vfprintf(stderr, fmt, ap);
 	va_end(ap);
--- a/place.h	Sun Dec 19 19:30:24 2010 -0500
+++ b/place.h	Sun Dec 19 19:39:26 2010 -0500
@@ -1,4 +1,12 @@
+
+enum places {
+	P_NOWHERE,
+	P_BUILTIN,
+	P_COMMANDLINE,
+	P_FILE,
+};
 struct place {
+	enum places type;
 	struct seenfile *file;
 	unsigned line;
 	unsigned column;