From 7ca8f23776ff92d8ac80611aafb361a53ddc9bda Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Tue, 21 Sep 2010 13:51:11 +0200 Subject: [PATCH] Assembler: Fix CPP lineinfo interpreter Signed-off-by: Michael Buesch --- assembler/args.c | 2 +- assembler/parser.y | 2 +- assembler/scanner.l | 46 ++++++++++++++++++++++++++------------------- assembler/util.h | 5 +++++ 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/assembler/args.c b/assembler/args.c index 56707a1..3f45129 100644 --- a/assembler/args.c +++ b/assembler/args.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2007 Michael Buesch + * Copyright (C) 2006-2010 Michael Buesch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 diff --git a/assembler/parser.y b/assembler/parser.y index 3e83c19..b5ef6a8 100644 --- a/assembler/parser.y +++ b/assembler/parser.y @@ -1,7 +1,7 @@ %{ /* - * Copyright (C) 2006-2007 Michael Buesch + * Copyright (C) 2006-2010 Michael Buesch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 diff --git a/assembler/scanner.l b/assembler/scanner.l index 11f3890..dc6e608 100644 --- a/assembler/scanner.l +++ b/assembler/scanner.l @@ -1,7 +1,7 @@ %{ /* - * Copyright (C) 2006-2007 Michael Buesch + * Copyright (C) 2006-2010 Michael Buesch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 @@ -15,11 +15,10 @@ #include "parser.h" #include "main.h" +#include "util.h" #include - -#undef min -#define min(x,y) ((x) < (y) ? (x) : (y)) +#include static void interpret_cppinfo(const char *); @@ -30,7 +29,7 @@ static inline void log_current_line(void) size_t len; len = min(sizeof(cur_lineinfo.linecopy) - 1, strlen(yytext)); - strncpy(cur_lineinfo.linecopy, yytext, len); + memcpy(cur_lineinfo.linecopy, yytext, len); cur_lineinfo.linecopy[len] = '\0'; } @@ -43,7 +42,7 @@ NEWLINE ((\r)|(\n)|(\r\n)) %% ^.*$ { log_current_line(); REJECT; } -#\ [0-9]+\ \".*\"[ ]*[0-9]*{NEWLINE} { interpret_cppinfo(yytext); } +#{WS}+[0-9]+{WS}+\".*\"[0-9 \t]*{NEWLINE} { interpret_cppinfo(yytext); } {WS}+ { update_lineinfo(); /* whitespace */ } @@ -157,36 +156,44 @@ tram { update_lineinfo(); return IVAL_TRAM; } %% struct lineinfo cur_lineinfo; -//FIXME The linenumber sometimes is wrong. + +static inline const char * strip_leading_ws(const char *str) +{ + while (*str != '\0' && isspace(*str)) + str++; + return str; +} static void interpret_cppinfo(const char *str) { const char * const orig = str; char tmp[64]; - char *found; + const char *found; + char *tail; /* This will interpret lines added by CPP. * They look like: - * # 3 "file.asm" 1 + * # lineno "filename" flags... */ - if (*str == '\0') + str = strip_leading_ws(str); + if (*str != '#') goto error; str++; /* skip # character */ + str = strip_leading_ws(str); if (*str == '\0') goto error; - str++; /* skip whitespace */ /* Line number */ found = strchr(str, ' '); if (!found) goto error; memset(tmp, 0, sizeof(tmp)); - memcpy(tmp, str, min(sizeof(tmp) - 1, - (size_t)(found - str))); - cur_lineinfo.lineno = strtoul(tmp, NULL, 10) - 1; - str = found; - str++; + memcpy(tmp, str, min(sizeof(tmp) - 1, (size_t)(found - str))); + cur_lineinfo.lineno = strtoul(tmp, &tail, 0); + if (*tail != '\0') + goto error; + str = strip_leading_ws(found); /* File name */ if (*str != '\"') @@ -198,9 +205,10 @@ static void interpret_cppinfo(const char *str) if (!found) goto error; memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file)); - memcpy(cur_lineinfo.file, str, - min(sizeof(cur_lineinfo.file) - 1, - (size_t)(found - str))); + memcpy(cur_lineinfo.file, str, min(sizeof(cur_lineinfo.file) - 1, + (size_t)(found - str))); + + /* We ignore the flags. */ return; error: diff --git a/assembler/util.h b/assembler/util.h index a3d1a82..25bbbdb 100644 --- a/assembler/util.h +++ b/assembler/util.h @@ -7,6 +7,11 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) +#undef min +#undef max +#define min(x,y) ((x) < (y) ? (x) : (y)) +#define max(x,y) ((x) > (y) ? (x) : (y)) + void dump(const char *data, size_t size, -- 2.31.1