[PATCH 1/6] add helper for new parsing errors: unexpected()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Most parsing errors are reported during parsing itself but
sometimes some extra conditions must be checked.
One problem with this is that these extra conditions are not
met because they are the consequence of a primary error which
has already been reported. In this case, it this not desirable
to emit a new error message.

To help with this, add a small function which will emit an
error message followed by the current token but only if the
current token does not originated from a previous parsing error.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 lib.c | 17 ++++++++++++++++-
 lib.h |  2 ++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/lib.c b/lib.c
index 0be4548a6..a05d04744 100644
--- a/lib.c
+++ b/lib.c
@@ -70,10 +70,10 @@ struct token *skip_to(struct token *token, int op)
 	return token;
 }
 
+static struct token bad_token;
 struct token *expect(struct token *token, int op, const char *where)
 {
 	if (!match_op(token, op)) {
-		static struct token bad_token;
 		if (token != &bad_token) {
 			bad_token.next = token;
 			sparse_error(token->pos, "Expected %s %s", show_special(op), where);
@@ -86,6 +86,21 @@ struct token *expect(struct token *token, int op, const char *where)
 	return token->next;
 }
 
+///
+// issue an error message on new parsing errors
+// @token: the current token
+// @errmsg: the error message
+// If the current token is from a previous error, an error message
+// has already been issued, so nothing more is done.
+// Otherwise, @errmsg is displayed followed by the current token.
+void unexpected(struct token *token, const char *errmsg)
+{
+	if (token == &bad_token)
+		return;
+	sparse_error(token->pos, "%s", errmsg);
+	sparse_error(token->pos, "got %s", show_token(token));
+}
+
 unsigned int hexval(unsigned int c)
 {
 	int retval = 256;
diff --git a/lib.h b/lib.h
index aea46741d..d7bd3651f 100644
--- a/lib.h
+++ b/lib.h
@@ -83,6 +83,8 @@ typedef struct pseudo *pseudo_t;
 
 struct token *skip_to(struct token *, int);
 struct token *expect(struct token *, int, const char *);
+void unexpected(struct token *, const char *errmsg);
+
 #ifdef __GNUC__
 #define FORMAT_ATTR(pos) __attribute__ ((__format__ (__printf__, pos, pos+1)))
 #define NORETURN_ATTR __attribute__ ((__noreturn__))
-- 
2.17.0

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux