[gobject-introspection] scanner: Add --accept-unprefixed



commit 5d114c3832a8ac6b3897c02be420d299fb90692c
Author: Colin Walters <walters verbum org>
Date:   Wed Sep 1 12:19:27 2010 -0400

    scanner: Add --accept-unprefixed
    
    This is necessary for Mutter right now.

 docs/g-ir-scanner.1                |    4 ++++
 giscanner/scannermain.py           |    8 ++++++--
 giscanner/transformer.py           |    8 +++++++-
 tests/scanner/Bar-1.0-expected.gir |   28 ++++++++++++++++++++++++++++
 tests/scanner/Makefile.am          |    1 +
 tests/scanner/barapp.c             |   18 ++++++++++++++++++
 tests/scanner/barapp.h             |   26 ++++++++++++++++++++++++++
 7 files changed, 90 insertions(+), 3 deletions(-)
---
diff --git a/docs/g-ir-scanner.1 b/docs/g-ir-scanner.1
index 7e08d1d..cb5a50a 100644
--- a/docs/g-ir-scanner.1
+++ b/docs/g-ir-scanner.1
@@ -103,6 +103,10 @@ will export the symbol
 as
 .B Foo.Bar.do_something.
 .TP
+.B \--accept-unprefixed
+If specified, the scanner will accept identifiers and symbols which
+do not match the namespace prefix.  Try to avoid using this if possible.
+.TP
 .B \--output=FILENAME
 Name of the file to output. Normally namespace + format extension.
 Eg, GLib-2.0.gir.
diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
index 89fe42c..384727e 100644
--- a/giscanner/scannermain.py
+++ b/giscanner/scannermain.py
@@ -100,6 +100,9 @@ the latter is not specified.""")
     parser.add_option("", "--symbol-prefix",
                       action="append", dest="symbol_prefixes", default=[],
                       help="Remove this prefix from C symbols (function names)")
+    parser.add_option("", "--accept-unprefixed",
+                      action="store_true", dest="accept_unprefixed", default=False,
+                      help="If specified, accept symbols and identifiers that do not match the namespace prefix.")
     parser.add_option("", "--add-init-section",
                       action="append", dest="init_sections", default=[],
             help="add extra initialization code in the introspection program")
@@ -276,8 +279,9 @@ def scanner_main(args):
     transformer = Transformer(cachestore,
                               options.namespace_name,
                               options.namespace_version,
-                              identifier_prefixes,
-                              symbol_prefixes)
+                              identifier_prefixes=identifier_prefixes,
+                              symbol_prefixes=symbol_prefixes,
+                              accept_unprefixed=options.accept_unprefixed)
     if options.warn_all:
         transformer.enable_warnings(True)
     transformer.set_include_paths(options.include_paths)
diff --git a/giscanner/transformer.py b/giscanner/transformer.py
index 647dde7..aa1f4eb 100644
--- a/giscanner/transformer.py
+++ b/giscanner/transformer.py
@@ -46,10 +46,12 @@ class Transformer(object):
     UCASE_CONSTANT_RE = re.compile(r'[_A-Z0-9]+')
 
     def __init__(self, cachestore, namespace_name, namespace_version,
-                 identifier_prefixes=None, symbol_prefixes=None):
+                 identifier_prefixes=None, symbol_prefixes=None,
+                 accept_unprefixed=False):
         self._cwd = os.getcwd() + os.sep
         self._cachestore = cachestore
         self.generator = None
+        self._accept_unprefixed = accept_unprefixed
         self._namespace = ast.Namespace(namespace_name, namespace_version,
                                     identifier_prefixes=identifier_prefixes,
                                     symbol_prefixes=symbol_prefixes)
@@ -291,6 +293,8 @@ it is always biggest (i.e. last)."""
         if matches:
             matches.sort(self._sort_matches)
             return map(lambda x: (x[0], x[1]), matches)
+        elif self._accept_unprefixed:
+            return [(self._namespace, ident)]
         raise ValueError("Unknown namespace for identifier %r" % (ident, ))
 
     def split_csymbol(self, symbol):
@@ -307,6 +311,8 @@ it is always biggest (i.e. last)."""
         if matches:
             matches.sort(self._sort_matches)
             return (matches[-1][0], matches[-1][1])
+        elif self._accept_unprefixed:
+            return (self._namespace, symbol)
         raise ValueError("Unknown namespace for symbol %r" % (symbol, ))
 
     def strip_identifier_or_warn(self, ident, fatal=False):
diff --git a/tests/scanner/Bar-1.0-expected.gir b/tests/scanner/Bar-1.0-expected.gir
index 4d919fd..392a303 100644
--- a/tests/scanner/Bar-1.0-expected.gir
+++ b/tests/scanner/Bar-1.0-expected.gir
@@ -32,6 +32,34 @@ and/or use gtk-doc annotations.  -->
         <type name="GObject.ObjectClass" c:type="GObjectClass"/>
       </field>
     </record>
+    <class name="MutterWindow"
+           c:symbol-prefix="mutter_window"
+           c:type="MutterWindow"
+           parent="GObject.Object"
+           glib:type-name="MutterWindow"
+           glib:get-type="mutter_window_get_type"
+           glib:type-struct="MutterWindowClass">
+      <method name="func" c:identifier="mutter_window_func">
+        <return-value transfer-ownership="none">
+          <type name="none" c:type="void"/>
+        </return-value>
+        <parameters>
+          <parameter name="v" transfer-ownership="none">
+            <type name="guint" c:type="guint"/>
+          </parameter>
+        </parameters>
+      </method>
+      <field name="parent_instance">
+        <type name="GObject.Object" c:type="GObject"/>
+      </field>
+    </class>
+    <record name="MutterWindowClass"
+            c:type="MutterWindowClass"
+            glib:is-gtype-struct-for="MutterWindow">
+      <field name="parent_class">
+        <type name="GObject.ObjectClass" c:type="GObjectClass"/>
+      </field>
+    </record>
     <function name="app_func" c:identifier="bar_app_func">
       <return-value transfer-ownership="none">
         <type name="none" c:type="void"/>
diff --git a/tests/scanner/Makefile.am b/tests/scanner/Makefile.am
index a30336a..5b20131 100644
--- a/tests/scanner/Makefile.am
+++ b/tests/scanner/Makefile.am
@@ -105,6 +105,7 @@ Bar_1_0_gir_PACKAGES = gobject-2.0
 Bar_1_0_gir_PROGRAM = ./barapp
 Bar_1_0_gir_INCLUDES = GObject-2.0
 Bar_1_0_gir_FILES = $(barapp_SOURCES)
+Bar_1_0_gir_SCANNERFLAGS = --accept-unprefixed
 GIRS += Bar-1.0.gir
 
 pre-check:
diff --git a/tests/scanner/barapp.c b/tests/scanner/barapp.c
index d336975..de296ef 100644
--- a/tests/scanner/barapp.c
+++ b/tests/scanner/barapp.c
@@ -25,6 +25,24 @@ bar_app_func2 (int x, double y)
 {
 }
 
+G_DEFINE_TYPE(MutterWindow, mutter_window, G_TYPE_OBJECT);
+
+static void
+mutter_window_class_init (MutterWindowClass *klass)
+{
+}
+
+static void
+mutter_window_init (MutterWindow *object)
+{
+}
+
+void
+mutter_window_func (MutterWindow *window,
+		    guint         v)
+{
+}
+
 int
 main(int argc, char **argv)
 {
diff --git a/tests/scanner/barapp.h b/tests/scanner/barapp.h
index ff8037d..12c39a5 100644
--- a/tests/scanner/barapp.h
+++ b/tests/scanner/barapp.h
@@ -22,3 +22,29 @@ GType bar_baz_get_type          (void) G_GNUC_CONST;
 
 void bar_app_func (void);
 void bar_app_func2 (int x, double y);
+
+
+/* The intent of this case is to match mutter, which has unprefixed names like "MutterWindow".
+ * In this case we don't want to add "Mutter" as a namespace prefix, since that gives us a
+ * clash on a class name.  (MetaWindow and MutterWindow)
+ */
+#define MUTTER_TYPE_WINDOW              (mutter_window_get_type ())
+#define MUTTER_WINDOW(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), MUTTER_TYPE_WINDOW, MutterWindow))
+#define MUTTER_IS_WINDOW(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), MUTTER_TYPE_WINDOW))
+
+typedef struct MutterWindow          MutterWindow;
+typedef struct MutterWindowClass     MutterWindowClass;
+
+struct MutterWindow
+{
+  GObject parent_instance;
+};
+
+struct MutterWindowClass
+{
+  GObjectClass parent_class;
+};
+
+GType mutter_window_get_type          (void) G_GNUC_CONST;
+
+void mutter_window_func (MutterWindow *window, guint v);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]