Debian Codegraph @

codegen/genTestApi.py

raw
1 #!/usr/bin/env python3
2 #
3 # generate a test program for the API
4 #
5
6 import xml.etree.ElementTree as etree
7 import os
8 import re
9 import sys
10
11 import xmlmod
12
13 # Globals
14
15 dtors = {
16 'htmlDoc *': 'xmlFreeDoc',
17 'htmlParserCtxt *': 'htmlFreeParserCtxt',
18 'xmlAutomata *': 'xmlFreeAutomata',
19 'xmlBuffer *': 'xmlBufferFree',
20 'xmlCatalog *': 'xmlFreeCatalog',
21 'xmlChar *': 'xmlFree',
22 'xmlDOMWrapCtxt *': 'xmlDOMWrapFreeCtxt',
23 'xmlDict *': 'xmlDictFree',
24 'xmlDoc *': 'xmlFreeDoc',
25 'xmlDtd *': 'xmlFreeDtd',
26 'xmlEntitiesTable *': 'xmlFreeEntitiesTable',
27 'xmlElementContent *': 'xmlFreeElementContent',
28 'xmlEnumeration *': 'xmlFreeEnumeration',
29 'xmlList *': 'xmlListDelete',
30 'xmlModule *': 'xmlModuleFree',
31 'xmlMutex *': 'xmlFreeMutex',
32 'xmlNode *': 'xmlFreeNode',
33 'xmlNodeSet *': 'xmlXPathFreeNodeSet',
34 'xmlNs *': 'xmlFreeNs',
35 'xmlOutputBuffer *': 'xmlOutputBufferClose',
36 'xmlParserCtxt *': 'xmlFreeParserCtxt',
37 'xmlParserInputBuffer *': 'xmlFreeParserInputBuffer',
38 'xmlParserInput *': 'xmlFreeInputStream',
39 'xmlRMutex *': 'xmlFreeRMutex',
40 'xmlRelaxNGValidCtxt *': 'xmlRelaxNGFreeValidCtxt',
41 'xmlSaveCtxt *': 'xmlSaveClose',
42 'xmlSchemaFacet *': 'xmlSchemaFreeFacet',
43 'xmlSchemaVal *': 'xmlSchemaFreeValue',
44 'xmlSchemaValidCtxt *': 'xmlSchemaFreeValidCtxt',
45 'xmlTextWriter *': 'xmlFreeTextWriter',
46 'xmlURI *': 'xmlFreeURI',
47 'xmlValidCtxt *': 'xmlFreeValidCtxt',
48 'xmlXPathContext *': 'xmlXPathFreeContext',
49 'xmlXPathParserContext *': 'xmlXPathFreeParserContext',
50 'xmlXPathObject *': 'xmlXPathFreeObject',
51 }
52
53 blockList = {
54 # init/cleanup
55 'xmlCleanupParser': True,
56 'xmlInitParser': True,
57
58 # arg must be non-NULL
59 'xmlMemStrdupLoc': True,
60 'xmlMemoryStrdup': True,
61
62 # Returns void pointer which must be freed
63 'xmlMallocAtomicLoc': True,
64 'xmlMallocLoc': True,
65 'xmlMemMalloc': True,
66 'xmlMemRealloc': True,
67 'xmlReallocLoc': True,
68
69 # Would reset the error handler
70 'xmlSetStructuredErrorFunc': True,
71
72 # Prints errors
73 'xmlCatalogGetPublic': True,
74 'xmlCatalogGetSystem': True,
75 'xmlDebugDumpDTD': True,
76 'xmlDebugDumpDocument': True,
77 'xmlDebugDumpNode': True,
78 'xmlDebugDumpString': True,
79 'xmlParserError': True,
80 'xmlParserWarning': True,
81 'xmlParserValidityError': True,
82 'xmlParserValidityWarning': True,
83
84 # Internal parser unctions, ctxt must be non-NULL
85 'xmlParseAttribute': True,
86 'xmlParseAttributeListDecl': True,
87 'xmlParseAttributeType': True,
88 'xmlParseCDSect': True,
89 'xmlParseCharData': True,
90 'xmlParseCharRef': True,
91 'xmlParseComment': True,
92 'xmlParseDefaultDecl': True,
93 'xmlParseDocTypeDecl': True,
94 'xmlParseEndTag': True,
95 'xmlParseElement': True,
96 'xmlParseElementChildrenContentDecl': True,
97 'xmlParseElementContentDecl': True,
98 'xmlParseElementDecl': True,
99 'xmlParseElementMixedContentDecl': True,
100 'xmlParseEncName': True,
101 'xmlParseEncodingDecl': True,
102 'xmlParseEntityDecl': True,
103 'xmlParseEntityValue': True,
104 'xmlParseEnumeratedType': True,
105 'xmlParseEnumerationType': True,
106 'xmlParseExternalID': True,
107 'xmlParseExternalSubset': True,
108 'xmlParseMarkupDecl': True,
109 'xmlParseMisc': True,
110 'xmlParseName': True,
111 'xmlParseNmtoken': True,
112 'xmlParseNotationDecl': True,
113 'xmlParseNotationType': True,
114 'xmlParsePEReference': True,
115 'xmlParsePI': True,
116 'xmlParsePITarget': True,
117 'xmlParsePubidLiteral': True,
118 'xmlParseReference': True,
119 'xmlParseSDDecl': True,
120 'xmlParseStartTag': True,
121 'xmlParseSystemLiteral': True,
122 'xmlParseTextDecl': True,
123 'xmlParseVersionInfo': True,
124 'xmlParseVersionNum': True,
125 'xmlParseXMLDecl': True,
126 'xmlParserHandlePEReference': True,
127 'xmlSkipBlankChars': True,
128
129 # reads from stdin
130 'htmlReadFd': True,
131 'xmlReadFd': True,
132 'xmlReaderForFd': True,
133 }
134
135 # Parse document
136
137 if len(sys.argv) > 1:
138 buildDir = sys.argv[1]
139 else:
140 buildDir = '.'
141
142 xmlDocDir = buildDir + '/doc/xml'
143
144 filenames = {}
145 functions = {}
146
147 for file in os.listdir(xmlDocDir):
148 if not file.endswith('_8h.xml'):
149 continue
150
151 doc = etree.parse(xmlDocDir + '/' + file)
152
153 compound = doc.find('compounddef')
154 module = compound.find('compoundname').text
155 if not module.endswith('.h'):
156 continue
157 module = module[:-2]
158
159 for section in compound.findall('sectiondef'):
160 if section.get('kind') != 'func':
161 continue
162
163 for func in section.findall('memberdef'):
164 name = func.find('name').text
165 if name in blockList:
166 continue
167
168 module1, module2 = xmlmod.findModules(module, name)
169
170 cargs = []
171 skip = False
172 for arg in func.findall('param'):
173 atype = etree.tostring(arg.find('type'),
174 method='text', encoding='unicode').rstrip()
175 if atype == 'void':
176 continue
177 if atype == 'va_list':
178 skip = True
179 break
180 if re.search(r'(Ptr|\*)$', atype):
181 cargs.append('NULL')
182 else:
183 cargs.append('0')
184
185 if skip:
186 continue
187
188 mfunc = functions.get(module1)
189 if mfunc is None:
190 mfunc = {}
191 functions[module1] = mfunc
192
193 mmfunc = mfunc.get(module2)
194 if mmfunc is None:
195 mmfunc = {}
196 mfunc[module2] = mmfunc
197
198 code = f'{name}({', '.join(cargs)})'
199
200 rtype = etree.tostring(func.find('type'),
201 method='text', encoding='unicode').rstrip()
202 dtor = dtors.get(rtype)
203 if dtor is not None:
204 code = f'{dtor}({code})'
205 elif rtype == 'xmlHashTable *':
206 code = f'xmlHashFree({code}, NULL)'
207
208 mmfunc[name] = f' {code};\n'
209
210 # Write output
211
212 test = open('testapi.c', 'w')
213
214 test.write("""/*
215 * testapi.c: libxml2 API tester program.
216 *
217 * Automatically generated by gentest.py
218 *
219 * See Copyright for the status of this software.
220 */
221
222 /* Disable deprecation warnings */
223 #define XML_DEPRECATED
224
225 #include "libxml.h"
226 #include <libxml/HTMLparser.h>
227 #include <libxml/HTMLtree.h>
228 #include <libxml/c14n.h>
229 #include <libxml/catalog.h>
230 #include <libxml/debugXML.h>
231 #include <libxml/parserInternals.h>
232 #include <libxml/pattern.h>
233 #include <libxml/relaxng.h>
234 #include <libxml/schematron.h>
235 #include <libxml/uri.h>
236 #include <libxml/xinclude.h>
237 #include <libxml/xlink.h>
238 #include <libxml/xmlmodule.h>
239 #include <libxml/xmlreader.h>
240 #include <libxml/xmlsave.h>
241 #include <libxml/xmlschemas.h>
242 #include <libxml/xmlschemastypes.h>
243 #include <libxml/xmlwriter.h>
244 #include <libxml/xpathInternals.h>
245 #include <libxml/xpointer.h>
246
247 static void
248 ignoreError(void *userData ATTRIBUTE_UNUSED,
249 const xmlError *error ATTRIBUTE_UNUSED) {
250 }
251
252 int
253 main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
254 xmlInitParser();
255 xmlSetStructuredErrorFunc(NULL, ignoreError);
256
257 """)
258
259 for module1 in sorted(functions.keys()):
260 mfunc = functions[module1]
261
262 if module1 != '':
263 test.write(f'#ifdef LIBXML_{module1}_ENABLED\n')
264
265 for module2 in sorted(mfunc.keys()):
266 mmfunc = mfunc[module2]
267
268 if module2 != '':
269 test.write(f'#ifdef LIBXML_{module2}_ENABLED\n')
270
271 for name in sorted(mmfunc.keys()):
272 test.write(mmfunc[name])
273
274 if module2 != '':
275 test.write(f'#endif /* LIBXML_{module2}_ENABLED */\n')
276
277 if module1 != '':
278 test.write(f'#endif /* LIBXML_{module1}_ENABLED */\n')
279
280 test.write('\n')
281
282 test.write(""" xmlCleanupParser();
283 return 0;
284 }
285 """)