Patchwork Hexadecimal numbers in option arguments

login
register
mail settings
Submitter Chung-Lin Tang
Date July 14, 2013, 9:07 a.m.
Message ID <51E26A3C.7050606@codesourcery.com>
Download mbox | patch
Permalink /patch/258890/
State New
Headers show

Comments

Chung-Lin Tang - July 14, 2013, 9:07 a.m.
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.
Joseph S. Myers - July 14, 2013, 1:27 p.m.
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).

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;
 }