From 0b9dda0e725446882dca84b6a64688c8f0e5a4e3 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 25 Feb 2009 20:25:36 +0000 Subject: Bug 555964 - Parse floating-point #defines Previously we just supported int and string, add double to this. Technically we should probably differentiate between float and double, but it's not likely to be very useful in practice to do so. --- diff --git a/giscanner/giscannermodule.c b/giscanner/giscannermodule.c index 7d63784..80d7f6b 100644 --- a/giscanner/giscannermodule.c +++ b/giscanner/giscannermodule.c @@ -135,10 +135,27 @@ static PyObject * symbol_get_const_int (PyGISourceSymbol *self, void *context) { + if (!self->symbol->const_int_set) + { + Py_INCREF(Py_None); + return Py_None; + } return PyInt_FromLong (self->symbol->const_int); } static PyObject * +symbol_get_const_double (PyGISourceSymbol *self, + void *context) +{ + if (!self->symbol->const_double_set) + { + Py_INCREF(Py_None); + return Py_None; + } + return PyFloat_FromDouble (self->symbol->const_double); +} + +static PyObject * symbol_get_const_string (PyGISourceSymbol *self, void *context) { @@ -171,7 +188,9 @@ static const PyGetSetDef _PyGISourceSymbol_getsets[] = { { "ident", (getter)symbol_get_ident, NULL, NULL}, { "base_type", (getter)symbol_get_base_type, NULL, NULL}, /* gboolean const_int_set; */ - { "const_int", (getter)symbol_get_const_int, NULL, NULL}, + { "const_int", (getter)symbol_get_const_int, NULL, NULL}, + /* gboolean const_double_set; */ + { "const_double", (getter)symbol_get_const_double, NULL, NULL}, { "const_string", (getter)symbol_get_const_string, NULL, NULL}, { "source_filename", (getter)symbol_get_source_filename, NULL, NULL}, { 0 } diff --git a/giscanner/scannerparser.y b/giscanner/scannerparser.y index 85640fe..c92a538 100644 --- a/giscanner/scannerparser.y +++ b/giscanner/scannerparser.y @@ -167,7 +167,10 @@ primary_expression } | FLOATING { - $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID); + $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST); + $$->const_double_set = TRUE; + $$->const_double = 0.0; + sscanf (yytext, "%lf", &($$->const_double)); } | strings | '(' expression ')' @@ -1256,7 +1259,7 @@ function_macro_define object_macro_define : object_macro constant_expression { - if ($2->const_int_set || $2->const_string != NULL) { + if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) { $2->ident = $1; gi_source_scanner_add_symbol (scanner, $2); gi_source_symbol_unref ($2); diff --git a/giscanner/sourcescanner.h b/giscanner/sourcescanner.h index bbc4977..a49de5c 100644 --- a/giscanner/sourcescanner.h +++ b/giscanner/sourcescanner.h @@ -116,6 +116,8 @@ struct _GISourceSymbol gboolean const_int_set; int const_int; char *const_string; + gboolean const_double_set; + double const_double; char *source_filename; }; diff --git a/giscanner/sourcescanner.py b/giscanner/sourcescanner.py index 30e624b..1a8194d 100644 --- a/giscanner/sourcescanner.py +++ b/giscanner/sourcescanner.py @@ -146,7 +146,8 @@ class SourceType(object): class SourceSymbol(object): - __members__ = ['const_int', 'const_string', 'ident', 'type', 'base_type'] + __members__ = ['const_int', 'const_double', 'const_string', 'ident', + 'type', 'base_type'] def __init__(self, scanner, symbol): self._scanner = scanner @@ -163,6 +164,10 @@ class SourceSymbol(object): return self._symbol.const_int @property + def const_double(self): + return self._symbol.const_double + + @property def const_string(self): return self._symbol.const_string diff --git a/giscanner/transformer.py b/giscanner/transformer.py index 58c2fbc..eb76f83 100644 --- a/giscanner/transformer.py +++ b/giscanner/transformer.py @@ -465,12 +465,18 @@ class Transformer(object): if not symbol.source_filename.endswith('.h'): return None name = self.remove_prefix(symbol.ident) - if symbol.const_string is None: + if symbol.const_string is not None: + type_name = 'utf8' + value = symbol.const_string + elif symbol.const_int is not None: type_name = 'int' value = symbol.const_int + elif symbol.const_double is not None: + type_name = 'double' + value = symbol.const_double else: - type_name = 'utf8' - value = symbol.const_string + raise AssertionError() + const = Constant(name, type_name, value) return const diff --git a/tests/scanner/foo-1.0-expected.gir b/tests/scanner/foo-1.0-expected.gir index 2f82175..4bd7745 100644 --- a/tests/scanner/foo-1.0-expected.gir +++ b/tests/scanner/foo-1.0-expected.gir @@ -399,6 +399,9 @@ and/or use gtk-doc annotations. --> + + + diff --git a/tests/scanner/foo-1.0-expected.tgir b/tests/scanner/foo-1.0-expected.tgir index edf58ef..dcd9789 100644 --- a/tests/scanner/foo-1.0-expected.tgir +++ b/tests/scanner/foo-1.0-expected.tgir @@ -277,6 +277,9 @@ + + + diff --git a/tests/scanner/foo.h b/tests/scanner/foo.h index d3dd29f..da30df3 100644 --- a/tests/scanner/foo.h +++ b/tests/scanner/foo.h @@ -8,6 +8,8 @@ #define FOO_DEFINE_SHOULD_BE_EXPOSED "should be exposed" +#define FOO_PIE_IS_TASTY 3.14159 + #define FOO_TYPE_INTERFACE (foo_interface_get_type ()) #define FOO_INTERFACE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), FOO_TYPE_INTERFACE, FooInterface)) #define FOO_IS_INTERFACE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), FOO_TYPE_INTERFACE)) -- cgit v0.9.1