From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Jon Loeliger <jdl-KZfg59tc24xl57MIdRCFDg@public.gmane.org>
Cc: devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org
Subject: [4/5] dtc: Cleanup yylloc type and handling
Date: Fri, 3 Oct 2008 00:07:53 +1000 [thread overview]
Message-ID: <20081002140753.GH11662@yookeroo.seuss> (raw)
In-Reply-To: <20081002140652.GG11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
This patch makes several cleanups to the handling of location
variables as they're carried through the parser. Specifically:
- (for now) remove column numbers from YYLTYPE, since we were
never correctly filling them in from the lexer in any case.
- split the 'file' field in YYLTYPE into first and last file,
since for big parser groupings the first and last token could in
theory be in different files.
- use plain strings to store the files in YYLTYPE, instead of
dtc_file pointer. There's no need for the other info from dtc_file,
and the strings will have nicer lifetime properties in future patches.
- reorganize YYLTYPE into first and last nested structure
fields, each containing a file and line number, which will be more
convenient for us later on.
- no longer use the undocumented magic #defines
YYLTYPE_IS_DECLARED and YYLTYPE_IS_TRIVIAL. Instead we define our own
structure for the locations and make YYLTYPE a macro expanding to it,
as described in the flex info pages.
- reformat the YYLLOC_DEFAULT macro into the indentation style
we use everywhere else (as well as updating it to match the other
changes)
Signed-off-by: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
---
dtc-lexer.l | 4 +--
srcpos.c | 11 +++++++--
srcpos.h | 70 ++++++++++++++++++++++--------------------------------------
3 files changed, 37 insertions(+), 48 deletions(-)
Index: dtc/srcpos.h
===================================================================
--- dtc.orig/srcpos.h 2008-10-03 00:03:57.000000000 +1000
+++ dtc/srcpos.h 2008-10-03 00:04:07.000000000 +1000
@@ -18,60 +18,42 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
-
-/*
- * Augment the standard YYLTYPE with a filenum index into an
- * array of all opened filenames.
- */
-
#include <stdio.h>
char *xstrdup(const char *s);
+struct srcpos {
+ int line;
+ const char *file;
+};
+
+struct srcloc {
+ struct srcpos first, last;
+};
+
+#define YYLTYPE struct srcloc
+
+#define YYLLOC_DEFAULT(Cur, Rhs, N) \
+ do { \
+ if (N) { \
+ (Cur).first.file = YYRHSLOC(Rhs, 1).first.file; \
+ (Cur).first.line = YYRHSLOC(Rhs, 1).first.line; \
+ (Cur).last.file = YYRHSLOC(Rhs, N).last.file; \
+ (Cur).last.line = YYRHSLOC(Rhs, N).last.line; \
+ } else { \
+ (Cur).first.line = (Cur).last.line \
+ = YYRHSLOC(Rhs, 0).last.line; \
+ (Cur).first.file = (Cur).last.file \
+ = YYRHSLOC(Rhs, 0).last.file; \
+ } \
+ } while (0)
+
struct dtc_file {
char *dir;
const char *name;
FILE *file;
};
-#if ! defined(YYLTYPE) && ! defined(YYLTYPE_IS_DECLARED)
-typedef struct YYLTYPE {
- int first_line;
- int first_column;
- int last_line;
- int last_column;
- struct dtc_file *file;
-} YYLTYPE;
-
-#define YYLTYPE_IS_DECLARED 1
-#define YYLTYPE_IS_TRIVIAL 1
-#endif
-
-/* Cater to old parser templates. */
-#ifndef YYID
-#define YYID(n) (n)
-#endif
-
-#define YYLLOC_DEFAULT(Current, Rhs, N) \
- do \
- if (YYID (N)) \
- { \
- (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
- (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
- (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
- (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
- (Current).file = YYRHSLOC (Rhs, N).file; \
- } \
- else \
- { \
- (Current).first_line = (Current).last_line = \
- YYRHSLOC (Rhs, 0).last_line; \
- (Current).first_column = (Current).last_column = \
- YYRHSLOC (Rhs, 0).last_column; \
- (Current).file = YYRHSLOC (Rhs, 0).file; \
- } \
- while (YYID (0))
-
void srcpos_error(YYLTYPE *loc, char const *fmt, ...)
__attribute__((format(printf, 2, 3)));
Index: dtc/dtc-lexer.l
===================================================================
--- dtc.orig/dtc-lexer.l 2008-10-03 00:03:48.000000000 +1000
+++ dtc/dtc-lexer.l 2008-10-03 00:04:07.000000000 +1000
@@ -39,8 +39,8 @@ LINECOMMENT "//".*\n
#define YY_USER_ACTION \
{ \
- yylloc.file = srcpos_file; \
- yylloc.first_line = yylineno; \
+ yylloc.first.file = yylloc.last.file = srcpos_file->name; \
+ yylloc.first.line = yylloc.last.line = yylineno; \
}
/*#define LEXDEBUG 1*/
Index: dtc/srcpos.c
===================================================================
--- dtc.orig/srcpos.c 2008-10-03 00:03:57.000000000 +1000
+++ dtc/srcpos.c 2008-10-03 00:04:07.000000000 +1000
@@ -29,12 +29,19 @@ char *xstrdup(const char *s)
return dup;
}
-void srcpos_error(YYLTYPE *loc, char const *fmt, ...)
+void srcpos_error(struct srcloc *loc, char const *fmt, ...)
{
va_list va;
va_start(va, fmt);
- fprintf(stderr, "%s:%d ", loc->file->name, loc->first_line);
+ if (!streq(loc->first.file, loc->last.file))
+ fprintf(stderr, "%s:%d-%s:%d ", loc->first.file, loc->first.line,
+ loc->last.file, loc->last.line);
+ else if (loc->first.line != loc->last.line)
+ fprintf(stderr, "%s:%d-%d ", loc->first.file, loc->first.line,
+ loc->last.line);
+ else
+ fprintf(stderr, "%s:%d ", loc->first.file, loc->first.line);
vfprintf(stderr, fmt, va);
fprintf(stderr, "\n");
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
next prev parent reply other threads:[~2008-10-02 14:07 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-02 14:04 [0/5] dtc: srcpos, input handling cleanups David Gibson
[not found] ` <20081002140427.GD11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:05 ` [1/5] dtc: Implement and use an xstrdup() function David Gibson
[not found] ` <20081002140512.GE11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:05 ` [2/5] dtc: Use flex's YY_USER_ACTION feature to avoid code duplication David Gibson
[not found] ` <20081002140556.GF11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:06 ` [3/5] dtc: Cleanup yyerrorf() function David Gibson
[not found] ` <20081002140652.GG11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:07 ` David Gibson [this message]
[not found] ` <20081002140753.GH11662-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-02 14:09 ` [5/5] dtc: Clean up source file management David Gibson
2008-10-03 19:24 ` [4/5] dtc: Cleanup yylloc type and handling Jon Loeliger
[not found] ` <E1KlqGI-0006JF-6p-CYoMK+44s/E@public.gmane.org>
2008-10-04 2:25 ` David Gibson
2008-10-03 19:22 ` [3/5] dtc: Cleanup yyerrorf() function Jon Loeliger
[not found] ` <E1KlqEb-0006Io-Sc-CYoMK+44s/E@public.gmane.org>
2008-10-04 2:56 ` David Gibson
2008-10-02 16:25 ` [2/5] dtc: Use flex's YY_USER_ACTION feature to avoid code duplication Jon Loeliger
2008-10-03 1:05 ` David Gibson
[not found] ` <20081003010531.GE3002-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-03 14:17 ` Jon Loeliger
[not found] ` <48E62991.6010102-KZfg59tc24xl57MIdRCFDg@public.gmane.org>
2008-10-04 4:13 ` David Gibson
2008-10-03 17:16 ` Jon Loeliger
2008-10-03 17:17 ` [1/5] dtc: Implement and use an xstrdup() function Jon Loeliger
[not found] ` <E1KloHP-0005pb-R8-CYoMK+44s/E@public.gmane.org>
2008-10-04 2:49 ` David Gibson
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=20081002140753.GH11662@yookeroo.seuss \
--to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
--cc=devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org \
--cc=jdl-KZfg59tc24xl57MIdRCFDg@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox