Handle casing better for constants
authorOwen W. Taylor <otaylor@fishsoup.net>
Tue, 7 Sep 2010 23:36:58 +0000 (19:36 -0400)
committerOwen W. Taylor <otaylor@fishsoup.net>
Wed, 8 Sep 2010 00:03:59 +0000 (20:03 -0400)
Instead of handling constants by lower-casing them, stripping the
lower-case prefix and upper-casing them again, leave them in the
original case and check against upper-cased versions of
namespace.symbol_prefixes.

Wwe detect what version to test against by looking at the first character
of the identifier, so we assume that --symbol-prefix options are always
in lowercase. If that needs to be relaxed, then we'll have to check all
symbols against both sets of prefixes.

Add tests for constants to Regress.h and fix tests/warn/unresolved-type.h
for a warning message that improved with this change.

https://bugzilla.gnome.org/show_bug.cgi?id=629007

giscanner/ast.py
giscanner/scannermain.py
giscanner/transformer.py
tests/scanner/Regress-1.0-expected.gir
tests/scanner/regress.h
tests/warn/unresolved-type.h

index b03d43f9c399be406f4954c286b71d2419813490..ada6412dbebfc33148d48279f49f01660ced4281 100644 (file)
@@ -311,6 +311,8 @@ class Namespace(object):
         else:
             ps = self.identifier_prefixes
             self.symbol_prefixes = [to_underscores(p).lower() for p in ps]
+        # cache upper-cased versions
+        self._ucase_symbol_prefixes = [p.upper() for p in self.symbol_prefixes]
         self._names = odict() # Maps from GIName -> node
         self._aliases = {} # Maps from GIName -> GIName
         self._type_names = {} # Maps from GTName -> node
index 630acb6fdb947070a7adf002148fd244dcd078dc..0333dee456d093a3335460d9acf3016791b2bcb4 100644 (file)
@@ -263,6 +263,11 @@ def scanner_main(args):
     else:
         identifier_prefixes = None
     if options.symbol_prefixes:
+        for prefix in options.symbol_prefixes:
+            # See Transformer._split_c_string_for_namespace_matches() for
+            # why this check is needed
+            if prefix.lower() != prefix:
+                _error("Values for --symbol-prefix must be entirely lowercase")
         symbol_prefixes = options.symbol_prefixes
     else:
         symbol_prefixes = None
index 8a80e23f805a89ab47bc13edef08163039ea4ed8..6526c53c0f34501c93c7b8a1fb074eec8a93ca66 100644 (file)
@@ -20,7 +20,6 @@
 
 import os
 import sys
-import re
 
 from . import ast
 from . import glibast
@@ -47,8 +46,6 @@ _xdg_data_dirs = [x for x in os.environ.get('XDG_DATA_DIRS', '').split(':') \
 class Transformer(object):
     namespace = property(lambda self: self._namespace)
 
-    UCASE_CONSTANT_RE = re.compile(r'[_A-Z0-9]+')
-
     def __init__(self, namespace, accept_unprefixed=False):
         self._cachestore = CacheStore()
         self._accept_unprefixed = accept_unprefixed
@@ -204,6 +201,8 @@ currently-scanned namespace is first."""
         for ns in self._iter_namespaces():
             if is_identifier:
                 prefixes = ns.identifier_prefixes
+            elif name[0].isupper():
+                prefixes = ns._ucase_symbol_prefixes
             else:
                 prefixes = ns.symbol_prefixes
             if prefixes:
@@ -268,11 +267,8 @@ raise ValueError."""
             ident, ns.name, ))
         return None
 
-    def _strip_symbol(self, symbol, is_constant=False):
+    def _strip_symbol(self, symbol):
         ident = symbol.ident
-        if is_constant:
-            # Temporarily lowercase
-            ident = ident.lower()
         hidden = ident.startswith('_')
         if hidden:
             ident = ident[1:]
@@ -283,8 +279,6 @@ raise ValueError."""
         if ns != self._namespace:
             raise TransformerException(
                 "Skipping foreign symbol from namespace %s" % (ns.name, ))
-        if is_constant:
-            name = name.upper()
         if hidden:
             return '_' + name
         return name
@@ -351,7 +345,7 @@ raise ValueError."""
                 # among them, so let's just remove the global namespace
                 # prefix.
                 try:
-                    name = self._strip_symbol(child, is_constant=True)
+                    name = self._strip_symbol(child)
                 except TransformerException, e:
                     message.warn_symbol(symbol, e)
                     return None
@@ -582,11 +576,8 @@ raise ValueError."""
         if (symbol.source_filename is None or
             not symbol.source_filename.endswith('.h')):
             return None
-        # ignore non-uppercase defines
-        if not self.UCASE_CONSTANT_RE.match(symbol.ident):
-            return None
         try:
-            name = self._strip_symbol(symbol, is_constant=True)
+            name = self._strip_symbol(symbol)
         except TransformerException, e:
             message.warn_symbol(symbol, e)
             return None
index 3fce12b5e3f054462f98d1ef837d86c4b1953f84..6ae7172bcb88c539e018bc9ac88cbb92cfba6152 100644 (file)
@@ -15,6 +15,18 @@ and/or use gtk-doc annotations.  -->
              shared-library="libregress.so"
              c:identifier-prefixes="Regress"
              c:symbol-prefixes="regress">
+    <constant name="DOUBLE_CONSTANT" value="44.220000">
+      <type name="gdouble" c:type="gdouble"/>
+    </constant>
+    <constant name="INT_CONSTANT" value="4422">
+      <type name="gint" c:type="gint"/>
+    </constant>
+    <constant name="Mixed_Case_Constant" value="4423">
+      <type name="gint" c:type="gint"/>
+    </constant>
+    <constant name="STRING_CONSTANT" value="Some String">
+      <type name="utf8" c:type="gchar*"/>
+    </constant>
     <record name="SkippedStructure"
             c:type="RegressSkippedStructure"
             introspectable="0">
index 6fed4ca78e1e604270d64f51eaec8e7c6fd603ac..c0591f8422175b14fdb2a449652b5d49b203f435 100644 (file)
@@ -166,6 +166,13 @@ GType regress_test_flags_get_type (void) G_GNUC_CONST;
 
 const gchar * regress_test_enum_param(RegressTestEnum e);
 
+/* constants */
+
+#define REGRESS_INT_CONSTANT 4422
+#define REGRESS_DOUBLE_CONSTANT 44.22
+#define REGRESS_STRING_CONSTANT "Some String"
+#define REGRESS_Mixed_Case_Constant 4423
+
 /* structures */
 typedef struct _RegressTestStructA RegressTestStructA;
 typedef struct _RegressTestStructB RegressTestStructB;
index 9f1a05cbdfa3f9cc79cf677f58222d089b67ff03..a31db5eab84ca6a251d5d7b782b7f08ed293bc97 100644 (file)
@@ -16,4 +16,4 @@ typedef enum {
   MY_ENUM_A = 0
 } TestMyEnum2;
 
-// EXPECT:17: Warning: Test: symbol='TestMyEnum2': Unknown namespace for symbol 'my_enum_a'
+// EXPECT:17: Warning: Test: symbol='TestMyEnum2': Unknown namespace for symbol 'MY_ENUM_A'