summaryrefslogtreecommitdiff
path: root/srcpos.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2009-12-08 14:24:42 +1100
committerJon Loeliger <jdl@jdl.com>2010-01-14 07:52:56 -0600
commite1fee329e2a74141fc6872a5c4307d078c4ba553 (patch)
treee5572be2a873ccab39834abe22b93830b1034777 /srcpos.c
parent728c5e87c655c17c2fbc1d5386b12ff328e0fc76 (diff)
downloaddtc-e1fee329e2a74141fc6872a5c4307d078c4ba553.tar.gz
dtc: Cleanup srcpos_string()
There are several small problems with the current srcpos_string(). - The code unnecessarily uses a temp buffer and two rounds of *printf(); a single asprintf() will suffice. - With previous changes, pos->file->name can never be NULL, and the name field for a srcfile bound to stdin is already set to something sensible. - On allocation failure in asprintf() it returns a bogus result, instead of causing a fatal error like every other failed allocation. - The format for representing file/line/column is gratuitously different from the file/line format we used to use, and the format used by gcc and bison. This patch addresses all of these. There remains the problem that asprintf() is not portable, but that can wait until another patch. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'srcpos.c')
-rw-r--r--srcpos.c45
1 files changed, 16 insertions, 29 deletions
diff --git a/srcpos.c b/srcpos.c
index ca322bb..59ecbce 100644
--- a/srcpos.c
+++ b/srcpos.c
@@ -182,41 +182,28 @@ srcpos_dump(srcpos *pos)
char *
srcpos_string(srcpos *pos)
{
- const char *fname;
- char col_buf[100];
+ const char *fname = "<no-file>";
char *pos_str;
+ int rc;
- if (!pos) {
- fname = "<no-file>";
- } else if (pos->file->name) {
+ if (pos)
fname = pos->file->name;
- if (strcmp(fname, "-") == 0)
- fname = "stdin";
- } else {
- fname = "<no-file>";
- }
- if (pos->first_line == pos->last_line) {
- if (pos->first_column == pos->last_column) {
- snprintf(col_buf, sizeof(col_buf),
- "%d:%d",
- pos->first_line, pos->first_column);
- } else {
- snprintf(col_buf, sizeof(col_buf),
- "%d:%d-%d",
- pos->first_line,
- pos->first_column, pos->last_column);
- }
- } else {
- snprintf(col_buf, sizeof(col_buf),
- "%d:%d - %d:%d",
- pos->first_line, pos->first_column,
- pos->last_line, pos->last_column);
- }
+ if (pos->first_line != pos->last_line)
+ rc = asprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
+ pos->first_line, pos->first_column,
+ pos->last_line, pos->last_column);
+ else if (pos->first_column != pos->last_column)
+ rc = asprintf(&pos_str, "%s:%d.%d-%d", fname,
+ pos->first_line, pos->first_column,
+ pos->last_column);
+ else
+ rc = asprintf(&pos_str, "%s:%d.%d", fname,
+ pos->first_line, pos->first_column);
- if (asprintf(&pos_str, "%s %s", fname, col_buf) == -1)
- return "<unknown source position?";
+ if (rc == -1)
+ die("Couldn't allocate in srcpos string");
return pos_str;
}