summaryrefslogtreecommitdiff
path: root/tests/offsets/gen-gitestoffsets
blob: 94a2062d3d2c923e66ce308a9a9510e6cb83fd0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/env python
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2008  Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

from __future__ import print_function

# The idea here is that we want to compare offset information two ways:
#
#  1) As generated by the compiler
#  2) As found in the typelib
#
# So we find all the structures in the input file (offsets.h), parse out
# fields within the structure and generate code that outputs the field
# offsets using G_STRUCT_OFFSET() to one file and the field offsets using
# the typelib to the another file. We can then diff the two files to see
# if they are the same

import re
import sys

if len(sys.argv) != 2:
    print("Usage: gen-gitestoffsets INPUT > OUTPUT", file=sys.stderr)
    sys.exit(1)

# Helper function that we use to generate source. It does substitions
# from a dictionary, removes white space at the ends and removes a
# leading '|' from each line
STRIP_AROUND_RE = re.compile("^[ \t]*\n?(.*?)[ \t]*$", re.DOTALL)
STRIP_LEADER_RE = re.compile("^\|", re.MULTILINE)
def output(args, format):
    format = STRIP_AROUND_RE.sub(r"\1", format)
    format = STRIP_LEADER_RE.sub(r"", format)
    sys.stdout.write(format % args)

#
# Some regular expressions we use when parsing
#
TOKEN = "(?:[A-Za-z_][A-Za-z_0-9]+)"

def compile_re(expr):
    expr = expr.replace("TOKEN", TOKEN)
    return re.compile(expr, re.VERBOSE)

COMMENT_RE = compile_re("/\*([^*]+|\*[^/])*\*/")
STRUCT_DEF_RE = compile_re("struct\s+_(TOKEN)\s*{([^}]*)}")

# This certainly can't handle all type declarations, but it only
# needs to handle the ones we use in the test cases
FIELD_RE = compile_re(r"^(?:const\s+)?TOKEN(?:[\s*]+)(TOKEN)\s*(?:\[([0-9]*)\])?\s*;$")


input_f = open(sys.argv[1])
input = input_f.read()
input_f.close()

# Strip comments
input = COMMENT_RE.sub("", input)

symbols = []
symbol_fields = {}

for m in STRUCT_DEF_RE.finditer(input):
    symbol = m.group(1)

    fields = []

    body = m.group(2)
    for line in re.split("\n|\r\n", body):
        line = line.strip()
        if line == "":
            continue
        n = FIELD_RE.match(line)
        if not n:
            print("Can't parse structure line '%s'" % line, file=sys.stderr)
            sys.exit(1)
        fields.append(n.group(1))

    symbols.append(symbol)
    symbol_fields[symbol] = fields

# Sort for convenience
symbols.sort()

output({}, r'''
| /* GENERATED FILE. DO NOT EDIT. See gen-gitestoffsets */
|
|#include <errno.h>
|#include <stdio.h>
|#include <string.h>
|#include <glib.h>
|#include <girepository.h>
|#include "offsets.h"
|
|static GIRepository *repository;
|static const char *namespace = "Offsets";
|static const char *version = "1.0";
|
|static void
|print_field_offset(FILE         *outfile,
|                   GIStructInfo *struct_info,
|                   const gchar  *name)
|{
|   gint i;
|   gint n_fields = g_struct_info_get_n_fields (struct_info);
|   for (i = 0; i < n_fields; i++)
|     {
|       GIFieldInfo *field_info = g_struct_info_get_field (struct_info, i);
|       const char *field_name = g_base_info_get_name ((GIBaseInfo *)field_info);
|       if (strcmp (field_name, name) == 0)
|         {
|           fprintf (outfile, "%%s %%d\n", name, g_field_info_get_offset (field_info));
|           g_base_info_unref ((GIBaseInfo *)field_info);
|           return;
|         }
|
|       g_base_info_unref ((GIBaseInfo *)field_info);
|     }
|
|   g_error("Can't find field '%%s.%%s' in introspection information",
|           g_base_info_get_name ((GIBaseInfo *)struct_info), name);
|}
|
''')

for symbol in symbols:
    fields = symbol_fields[symbol]

    output({'symbol' : symbol}, r'''
|typedef struct {
|   char dummy;
|   %(symbol)s struct_;
|} Align%(symbol)s;
|
|static void
|compiled_%(symbol)s (FILE *outfile)
|{
|  fprintf (outfile, "%(symbol)s: size=%%" G_GSIZE_FORMAT ", alignment=%%ld\n",
|           sizeof(%(symbol)s),
|           G_STRUCT_OFFSET(Align%(symbol)s, struct_));
|
           ''')

    for field in fields:
        output({ 'field' : field, 'symbol' : symbol }, r'''
|  fprintf (outfile, "%%s %%ld\n", "%(field)s", G_STRUCT_OFFSET(%(symbol)s, %(field)s));
               ''')

    output({}, r'''
|
|  fprintf (outfile, "\n");
|}
|
           ''')

    if not symbol.startswith("Offsets"):
        print >> sys.stderr, "Symbol '%s' doesn't start with Offsets" % symbol
    bare_symbol = symbol[len("Offsets"):]


    output({'symbol' : symbol, 'bare_symbol' : bare_symbol}, r'''
|static void
|introspected_%(symbol)s (FILE *outfile)
|{
|  GIStructInfo *struct_info = (GIStructInfo *)g_irepository_find_by_name(repository, namespace,
|                                                                         "%(bare_symbol)s");
|  if (!struct_info)
|     g_error ("Can't find GIStructInfo for '%(symbol)s'");
|
|  fprintf (outfile, "%(symbol)s: size=%%" G_GSIZE_FORMAT ", alignment=%%" G_GSIZE_FORMAT "\n",
|           g_struct_info_get_size (struct_info),
|           g_struct_info_get_alignment (struct_info));
|
           ''')
    for field in fields:
        output({'field' : field}, '''
|  print_field_offset(outfile, struct_info, "%(field)s");
               ''')

    output({}, r'''
|
|  fprintf (outfile, "\n");
|
|  g_base_info_unref ((GIBaseInfo *)struct_info);
|}
           ''')

output({}, r'''
|
|int main(int argc, char **argv)
|{
|  GError *error = NULL;
|  FILE *outfile;
|
|  if (argc != 3)
|    g_error ("Usage: gitestoffsets COMPILED_OFFSETS_FILE INTROSPECTED_OFFSETS_FILE");
|
|  repository = g_irepository_get_default ();
|  if (!g_irepository_require (repository, namespace, version, 0, &error))
|     g_error ("Failed to load %%s-%%s.typelib: %%s", namespace, version, error->message);
|
|  outfile = fopen (argv[1], "w");
|  if (!outfile)
|    g_error ("Cannot open '%%s': %%s'", argv[1], g_strerror(errno));
|
''')

for symbol in symbols:
    output({'symbol' : symbol}, '''
|  compiled_%(symbol)s (outfile);
   ''')

output({}, '''
|
|  fclose (outfile);
|
|  outfile = fopen (argv[2], "w");
|  if (!outfile)
|    g_error ("Cannot open '%%s': %%s'", argv[1], g_strerror(errno));
|
''')


for symbol in symbols:
    output({'symbol' : symbol}, '''
|  introspected_%(symbol)s (outfile);
''')

output({}, r'''
|
|  fclose (outfile);
|
|  return 0;
}
''')