diff mbox

Hexadecimal numbers in option arguments

Message ID 51E26A3C.7050606@codesourcery.com
State New
Headers show

Commit Message

Chung-Lin Tang July 14, 2013, 9:07 a.m. UTC
Original patch posted as part of Nios II patches:
http://gcc.gnu.org/ml/gcc-patches/2013-04/msg01087.html

This patch is to allow hexadecimal numbers to be used in option
arguments, e.g. -falign-loops=0x10 can now be used as equivalent to
-falign-loops=16.

Joseph, the patch has been modified to use IXDIGIT to check the argument
string first, as you suggested in the last submission. Is this okay for
trunk?

Thanks,
Chung-Lin

2013-07-14  Chung-Lin Tang  <cltang@codesourcery.com>

        * opts-common.c (integral_argument): Add support for hexadecimal
        command option integer arguments. Update comments.

Comments

Joseph Myers July 14, 2013, 1:27 p.m. UTC | #1
On Sun, 14 Jul 2013, Chung-Lin Tang wrote:

> Original patch posted as part of Nios II patches:
> http://gcc.gnu.org/ml/gcc-patches/2013-04/msg01087.html
> 
> This patch is to allow hexadecimal numbers to be used in option
> arguments, e.g. -falign-loops=0x10 can now be used as equivalent to
> -falign-loops=16.
> 
> Joseph, the patch has been modified to use IXDIGIT to check the argument
> string first, as you suggested in the last submission. Is this okay for
> trunk?

This version looks like it will allow plain "0x" or "0X" as an argument, 
treating it as 0, rather than treating it as an error (i.e., you need to 
check there is at least one hex digit after the "0x" or "0X" before 
passing the string to strtol).
diff mbox

Patch

Index: opts-common.c
===================================================================
--- opts-common.c	(revision 200946)
+++ opts-common.c	(working copy)
@@ -147,7 +147,7 @@  find_opt (const char *input, unsigned int lang_mas
   return match_wrong_lang;
 }
 
-/* If ARG is a non-negative integer made up solely of digits, return its
+/* If ARG is a non-negative decimal or hexadecimal integer, return its
    value, otherwise return -1.  */
 
 int
@@ -161,6 +161,17 @@  integral_argument (const char *arg)
   if (*p == '\0')
     return atoi (arg);
 
+  /* It wasn't a decimal number - try hexadecimal.  */
+  if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
+    {
+      p = arg + 2;
+      while (*p && ISXDIGIT (*p))
+	p++;
+
+      if (*p == '\0')
+	return strtol (arg, NULL, 16);
+    }
+
   return -1;
 }