Bug 569373 - make Preprocessor.py handle -DVAR=NUMBER such that #if VAR == NUMBER works, r=l10n@mozilla.com
authorTed Mielczarek <ted.mielczarek@gmail.com>
Wed, 02 Jun 2010 09:48:31 +1200
changeset 42999 8385a247ab0c0d6c6e0641bd68e4a062991fd5fa
parent 42998 7d5c3c3647c32618f8600e259948d292b1f93e6c
child 43000 9837814d8d86c92c48d76dd3d833ad686cd3f4d5
push idunknown
push userunknown
push dateunknown
reviewersl10n
bugs569373
milestone1.9.3a5pre
Bug 569373 - make Preprocessor.py handle -DVAR=NUMBER such that #if VAR == NUMBER works, r=l10n@mozilla.com
config/Expression.py
config/Preprocessor.py
config/tests/unit-Preprocessor.py
--- a/config/Expression.py
+++ b/config/Expression.py
@@ -104,17 +104,21 @@ class Expression:
     Production: ( [0-9]+ | \w+)
     Note that the order is important, and the expression is kind-of
     ambiguous as \w includes 0-9. One could make it unambiguous by
     removing 0-9 from the first char of a string literal.
     """
     rv = None
     word_len = re.match('[0-9]*', self.content).end()
     if word_len:
-      rv = Expression.__ASTLeaf('int', int(self.content[:word_len]))
+      if self.content[0] == '0':
+        value = int(self.content[:word_len], 8)
+      else:
+        value = int(self.content[:word_len])
+      rv = Expression.__ASTLeaf('int', value)
     else:
       word_len = re.match('\w*', self.content).end()
       if word_len:
         rv = Expression.__ASTLeaf('string', self.content[:word_len])
       else:
         raise Expression.ParseError, self
     self.__strip(word_len)
     self.__ignore_whitespace()
--- a/config/Preprocessor.py
+++ b/config/Preprocessor.py
@@ -165,26 +165,32 @@ class Preprocessor:
       args = [sys.stdin]
     includes.extend(args)
     for f in includes:
       self.do_include(f)
     pass
 
   def getCommandLineParser(self, unescapeDefines = False):
     escapedValue = re.compile('".*"$')
+    numberValue = re.compile('\d+$')
     def handleE(option, opt, value, parser):
       for k,v in os.environ.iteritems():
         self.context[k] = v
     def handleD(option, opt, value, parser):
       vals = value.split('=', 1)
       if len(vals) == 1:
         vals.append(1)
       elif unescapeDefines and escapedValue.match(vals[1]):
         # strip escaped string values
         vals[1] = vals[1][1:-1]
+      elif numberValue.match(vals[1]):
+        if vals[1][0] == '0':
+          vals[1] = int(vals[1], 8)
+        else:
+          vals[1] = int(vals[1])
       self.context[vals[0]] = vals[1]
     def handleU(option, opt, value, parser):
       del self.context[value]
     def handleF(option, opt, value, parser):
       self.do_filter(value)
     def handleLE(option, opt, value, parser):
       self.setLineEndings(value)
     def handleMarker(option, opt, value, parser):
--- a/config/tests/unit-Preprocessor.py
+++ b/config/tests/unit-Preprocessor.py
@@ -399,10 +399,54 @@ FAIL
   def test_lineEndings(self):
     f = NamedIO('lineEndings.in', '''first
 #literal second
 ''')
     self.pp.setLineEndings('cr')
     self.pp.do_include(f)
     self.assertEqual(self.pp.out.getvalue(), "first\rsecond\r")
 
+  def test_number_value_equals(self):
+    f = NamedIO("number_value_equals.in", """#define FOO 1000
+#if FOO == 1000
+number value is equal
+#else
+number value is not equal
+#endif
+""")
+    self.pp.do_include(f)
+    self.assertEqual(self.pp.out.getvalue(), "number value is equal\n")
+
+  def test_number_value_equals_defines(self):
+    f = NamedIO("number_value_equals_defines.in", """#if FOO == 1000
+number value is equal
+#else
+number value is not equal
+#endif
+""")
+    self.pp.handleCommandLine(["-DFOO=1000"])
+    self.pp.do_include(f)
+    self.assertEqual(self.pp.out.getvalue(), "number value is equal\n")
+
+  def test_octal_value_equals(self):
+    f = NamedIO("octal_value_equals.in", """#define FOO 0100
+#if FOO == 0100
+octal value is equal
+#else
+octal value is not equal
+#endif
+""")
+    self.pp.do_include(f)
+    self.assertEqual(self.pp.out.getvalue(), "octal value is equal\n")
+
+  def test_octal_value_equals_defines(self):
+    f = NamedIO("octal_value_equals_defines.in", """#if FOO == 0100
+octal value is equal
+#else
+octal value is not equal
+#endif
+""")
+    self.pp.handleCommandLine(["-DFOO=0100"])
+    self.pp.do_include(f)
+    self.assertEqual(self.pp.out.getvalue(), "octal value is equal\n")
+
 if __name__ == '__main__':
   unittest.main()