From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Jon Loeliger <jdl-CYoMK+44s/E@public.gmane.org>
Cc: devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org
Subject: [4/5] dtc: Cleanup srcpos_string()
Date: Sat, 4 Oct 2008 22:27:06 +1000 (EST) [thread overview]
Message-ID: <20081004122706.65D55DDE17@ozlabs.org> (raw)
In-Reply-To: <20081004122157.GT30184-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
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-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
---
srcpos.c | 49 ++++++++++++++++++-------------------------------
1 file changed, 18 insertions(+), 31 deletions(-)
Index: dtc/srcpos.c
===================================================================
--- dtc.orig/srcpos.c 2008-10-04 22:15:30.000000000 +1000
+++ dtc/srcpos.c 2008-10-04 22:22:14.000000000 +1000
@@ -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 (asprintf(&pos_str, "%s %s", fname, col_buf) == -1)
- return "<unknown source position?";
+
+ 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 (rc == -1)
+ die("Couldn't allocate in srcpos string");
return pos_str;
}
next prev parent reply other threads:[~2008-10-04 12:27 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-04 12:21 [0/5] Input file and srcpos rework (v2) David Gibson
[not found] ` <20081004122157.GT30184-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-04 12:27 ` [1/5] dtc: Move some functions to util.[ch] David Gibson
2008-10-04 12:27 ` [2/5] dtc: Simpler interface to source file management David Gibson
2008-10-04 12:27 ` [5/5] dtc: Cleanup YYLTYPE and YYLLOC_DEFAULT declarations David Gibson
2008-10-04 12:27 ` David Gibson [this message]
2008-10-04 12:27 ` [3/5] dtc: Cleanup line number tracking, add column number tracking David Gibson
2008-10-08 19:52 ` [0/5] Input file and srcpos rework (v2) Jon Loeliger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20081004122706.65D55DDE17@ozlabs.org \
--to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
--cc=devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org \
--cc=jdl-CYoMK+44s/E@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.