Skip to contentSkip to conversation

recognize binary constants

PATCH Dan Carpenter 3 participants 4 messages Sep 8, 2011 – Nov 17, 2011 2 messages not in the archive Sparse checker

lore
Share & tools
lore
#3 lore raw
In reply to Kamal Mostafa
Quoted 3 lines · Kamal Mostafa

A small change to your patch, Dan: I added a call to tolower(), since "0B11" is also a valid syntax.

Doh... Good point. Thanks.

regards,
dan carpenter

#2 lore raw
In reply to a message not in the archive

A small change to your patch, Dan: I added a call to tolower(), since "0B11" is also a valid syntax.

-Kamal

---------------------------------------------------------------------------

From a9273391fe5f410321a0668e18beaebce086e23d Mon Sep 17 00:00:00 2001
From: Kamal Mostafa <[email protected]>
Date: Thu, 8 Sep 2011 10:34:27 -0700
Subject: [PATCH] recognize binary constants

Sparse doesn't parse binary constants properly so the following code generates an error:

	x = 0b11;

test.c:5:17: error: constant 0b11 is not a valid number

---
 expression.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
expression.c +8 -1
index 7e06e60..07d6846 100644
@@ -268,6 +268,13 @@ static struct token *string_expression(struct token *token, struct expression *e
 #define ULLONG_MAX (~0ULL)
 #endif
 
+static unsigned long long parse_num(const char *nptr, char **end)
+{
+	if (nptr[0] == '0' && tolower(nptr[1]) == 'b')
+		return strtoull(&nptr[2], end, 2);
+	return strtoull(nptr, end, 0);
+}
+
 static void get_number_value(struct expression *expr, struct token *token)
 {
 	const char *str = token->number;
@@ -279,7 +286,7 @@ static void get_number_value(struct expression *expr, struct token *token)
 	int bits;
 
 	errno = 0;
-	value = strtoull(str, &end, 0);
+	value = parse_num(str, &end);
 	if (end == str)
 		goto Float;
 	if (value == ULLONG_MAX && errno == ERANGE)
Signature · 1 line
1.7.4.1
maintainer
#5 lore raw
Quoted 6 lines · Dan Carpenter
Quoted 5 lines · Dan Carpenter

Sparse doesn't parse binary constants properly so the following code generates an error:

      x = 0b11;

Quoted 7 lines
Quoted 4 lines

test.c:5:17: error: constant 0b11 is not a valid number

I sent this patch before but the git tree was offline so everything was dead.

Thanks for the reminder. The patch looks fine.

I will apply it.

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