Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | # |
| 3 | # generate a tester program for the API |
| 4 | # |
| 5 | import sys |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 6 | import os |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 7 | import string |
| 8 | try: |
| 9 | import libxml2 |
| 10 | except: |
| 11 | print "libxml2 python bindings not available, skipping testapi.c generation" |
| 12 | sys.exit(0) |
| 13 | |
| 14 | # |
| 15 | # Modules we don't want skip in API test |
| 16 | # |
| 17 | skipped_modules = [ "SAX", "SAX2", "xlink", "threads", "globals", |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame] | 18 | "xpathInternals", "parserInternals", "xmlmemory", |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 19 | "xmlversion", "debugXML", "xmlexports", "DOCBparser", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 20 | |
| 21 | # temporary |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 22 | "xmlautomata", "xmlregexp", "c14n", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 23 | |
| 24 | ] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 25 | |
| 26 | # |
| 27 | # Some function really need to be skipped for the tests. |
| 28 | # |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 29 | skipped_functions = [ |
| 30 | # block on I/O |
| 31 | "xmlFdRead", "xmlReadFd", "xmlCtxtReadFd", |
| 32 | "htmlFdRead", "htmlReadFd", "htmlCtxtReadFd", |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 33 | "xmlReaderNewFd", "xmlReaderForFd", |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 34 | "xmlIORead", "xmlReadIO", "xmlCtxtReadIO", |
| 35 | "htmlIORead", "htmlReadIO", "htmlCtxtReadIO", |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 36 | "xmlReaderNewIO", "xmlBufferDump", "xmlNanoFTPConnect", |
| 37 | "xmlNanoFTPConnectTo", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 38 | # library state cleanup, generate false leak informations and other |
| 39 | # troubles, heavillyb tested otherwise. |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 40 | "xmlCleanupParser", "xmlRelaxNGCleanupTypes", "xmlSetListDoc", |
| 41 | "xmlSetTreeDoc", "xmlUnlinkNode", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 42 | # hard to avoid leaks in the tests |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 43 | "xmlStrcat", "xmlStrncat", "xmlCatalogAddLocal", "xmlNewTextWriterDoc", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 44 | # unimplemented |
| 45 | "xmlTextReaderReadInnerXml", "xmlTextReaderReadOuterXml", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 46 | "xmlTextReaderReadString", |
| 47 | # destructor |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 48 | "xmlListDelete", "xmlOutputBufferClose", "xmlNanoFTPClose", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 49 | # deprecated |
| 50 | "xmlCatalogGetPublic", "xmlCatalogGetSystem", "xmlEncodeEntities", |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 51 | "xmlNewGlobalNs", |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 52 | # allocators |
| 53 | "xmlMemFree", |
Daniel Veillard | c2c894f | 2004-11-07 12:17:35 +0000 | [diff] [blame^] | 54 | # verbosity |
| 55 | "xmlCatalogSetDebug", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 56 | ] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 57 | |
| 58 | # |
| 59 | # Those functions have side effect on the global state |
| 60 | # and hence generate errors on memory allocation tests |
| 61 | # |
| 62 | skipped_memcheck = [ "xmlLoadCatalog", "xmlAddEncodingAlias", |
| 63 | "xmlSchemaInitTypes", "xmlNanoFTPProxy", "xmlNanoFTPScanProxy", |
| 64 | "xmlNanoHTTPScanProxy", "xmlResetLastError", "xmlCatalogConvert", |
| 65 | "xmlCatalogRemove", "xmlLoadCatalogs", "xmlCleanupCharEncodingHandlers", |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 66 | "xmlInitCharEncodingHandlers", "xmlCatalogCleanup", |
| 67 | "htmlParseFile" # loads the catalogs |
| 68 | ] |
| 69 | |
| 70 | # |
| 71 | # Extra code needed for some test cases |
| 72 | # |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 73 | extra_pre_call = { |
| 74 | "xmlSAXUserParseFile": |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 75 | "if (sax == (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) user_data = NULL;", |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 76 | "xmlSAXUserParseMemory": |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 77 | "if (sax == (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) user_data = NULL;", |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 78 | "xmlParseBalancedChunkMemory": |
| 79 | "if (sax == (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) user_data = NULL;", |
| 80 | "xmlParseBalancedChunkMemoryRecover": |
| 81 | "if (sax == (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) user_data = NULL;", |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 82 | "xmlParserInputBufferCreateFd": |
| 83 | "if (fd >= 0) fd = -1;", |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 84 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 85 | extra_post_call = { |
| 86 | "xmlAddChild": |
| 87 | "if (ret_val == NULL) { xmlFreeNode(cur) ; cur = NULL ; }", |
| 88 | "xmlAddChildList": |
| 89 | "if (ret_val == NULL) { xmlFreeNodeList(cur) ; cur = NULL ; }", |
| 90 | "xmlAddSibling": |
| 91 | "if (ret_val == NULL) { xmlFreeNode(elem) ; elem = NULL ; }", |
| 92 | "xmlAddNextSibling": |
| 93 | "if (ret_val == NULL) { xmlFreeNode(elem) ; elem = NULL ; }", |
| 94 | "xmlAddPrevSibling": |
| 95 | "if (ret_val == NULL) { xmlFreeNode(elem) ; elem = NULL ; }", |
| 96 | "xmlDocSetRootElement": |
| 97 | "if (doc == NULL) { xmlFreeNode(root) ; root = NULL ; }", |
| 98 | "xmlReplaceNode": |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 99 | """if (cur != NULL) { |
| 100 | xmlUnlinkNode(cur); |
| 101 | xmlFreeNode(cur) ; cur = NULL ; } |
| 102 | if (old != NULL) { |
| 103 | xmlUnlinkNode(old); |
| 104 | xmlFreeNode(old) ; old = NULL ; } |
| 105 | ret_val = NULL;""", |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 106 | "xmlTextMerge": |
| 107 | """if ((first != NULL) && (first->type != XML_TEXT_NODE)) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 108 | xmlUnlinkNode(second); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 109 | xmlFreeNode(second) ; second = NULL ; }""", |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 110 | "xmlBuildQName": |
| 111 | """if ((ret_val != NULL) && (ret_val != ncname) && |
| 112 | (ret_val != prefix) && (ret_val != memory)) |
| 113 | xmlFree(ret_val); |
| 114 | ret_val = NULL;""", |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 115 | "xmlDictReference": "xmlDictFree(dict);", |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 116 | # Functions which deallocates one of their parameters |
| 117 | "xmlXPathConvertBoolean": """val = NULL;""", |
| 118 | "xmlXPathConvertNumber": """val = NULL;""", |
| 119 | "xmlXPathConvertString": """val = NULL;""", |
| 120 | "xmlSaveFileTo": """buf = NULL;""", |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 121 | "xmlSaveFormatFileTo": """buf = NULL;""", |
| 122 | "xmlIOParseDTD": "input = NULL;", |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 123 | "xmlRemoveProp": "cur = NULL;", |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 124 | "xmlNewNs": "if ((node == NULL) && (ret_val != NULL)) xmlFreeNs(ret_val);", |
| 125 | "xmlCopyNamespace": "if (ret_val != NULL) xmlFreeNs(ret_val);", |
| 126 | "xmlCopyNamespaceList": "if (ret_val != NULL) xmlFreeNsList(ret_val);", |
| 127 | "xmlNewTextWriter": "if (ret_val != NULL) out = NULL;", |
| 128 | "xmlNewTextWriterPushParser": "if (ret_val != NULL) ctxt = NULL;", |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 129 | } |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 130 | |
| 131 | modules = [] |
| 132 | |
| 133 | def is_skipped_module(name): |
| 134 | for mod in skipped_modules: |
| 135 | if mod == name: |
| 136 | return 1 |
| 137 | return 0 |
| 138 | |
| 139 | def is_skipped_function(name): |
| 140 | for fun in skipped_functions: |
| 141 | if fun == name: |
| 142 | return 1 |
| 143 | # Do not test destructors |
| 144 | if string.find(name, 'Free') != -1: |
| 145 | return 1 |
| 146 | return 0 |
| 147 | |
| 148 | def is_skipped_memcheck(name): |
| 149 | for fun in skipped_memcheck: |
| 150 | if fun == name: |
| 151 | return 1 |
| 152 | return 0 |
| 153 | |
| 154 | missing_types = {} |
| 155 | def add_missing_type(name, func): |
| 156 | try: |
| 157 | list = missing_types[name] |
| 158 | list.append(func) |
| 159 | except: |
| 160 | missing_types[name] = [func] |
| 161 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 162 | generated_param_types = [] |
| 163 | def add_generated_param_type(name): |
| 164 | generated_param_types.append(name) |
| 165 | |
| 166 | generated_return_types = [] |
| 167 | def add_generated_return_type(name): |
| 168 | generated_return_types.append(name) |
| 169 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 170 | missing_functions = {} |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame] | 171 | missing_functions_nr = 0 |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 172 | def add_missing_functions(name, module): |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame] | 173 | global missing_functions_nr |
| 174 | |
| 175 | missing_functions_nr = missing_functions_nr + 1 |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 176 | try: |
| 177 | list = missing_functions[module] |
| 178 | list.append(name) |
| 179 | except: |
| 180 | missing_functions[module] = [name] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 181 | |
| 182 | # |
| 183 | # Provide the type generators and destructors for the parameters |
| 184 | # |
| 185 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 186 | def type_convert(str, name, info, module, function, pos): |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 187 | # res = string.replace(str, " ", " ") |
| 188 | # res = string.replace(str, " ", " ") |
| 189 | # res = string.replace(str, " ", " ") |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 190 | res = string.replace(str, " *", "_ptr") |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 191 | # res = string.replace(str, "*", "_ptr") |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 192 | res = string.replace(res, " ", "_") |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 193 | res = string.replace(res, "htmlNode", "xmlNode") |
| 194 | res = string.replace(res, "htmlDoc", "xmlDoc") |
| 195 | res = string.replace(res, "htmlParser", "xmlParser") |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 196 | if res == 'const_char_ptr': |
| 197 | if string.find(name, "file") != -1 or \ |
| 198 | string.find(name, "uri") != -1 or \ |
| 199 | string.find(name, "URI") != -1 or \ |
| 200 | string.find(info, "filename") != -1 or \ |
| 201 | string.find(info, "URI") != -1 or \ |
| 202 | string.find(info, "URL") != -1: |
| 203 | if string.find(function, "Save") != -1: |
| 204 | return('fileoutput') |
| 205 | return('filepath') |
| 206 | if res == 'void_ptr': |
| 207 | if module == 'nanoftp' and name == 'ctx': |
| 208 | return('xmlNanoFTPCtxtPtr') |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 209 | if function == 'xmlNanoFTPNewCtxt': |
| 210 | return('xmlNanoFTPCtxtPtr') |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 211 | if module == 'nanohttp' and name == 'ctx': |
| 212 | return('xmlNanoHTTPCtxtPtr') |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 213 | if function == 'xmlIOHTTPOpenW': |
| 214 | return('xmlNanoHTTPCtxtPtr') |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 215 | if string.find(name, "data") != -1: |
| 216 | return('userdata'); |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 217 | if string.find(name, "user") != -1: |
| 218 | return('userdata'); |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 219 | if res == 'xmlDoc_ptr': |
| 220 | res = 'xmlDocPtr'; |
| 221 | if res == 'xmlNode_ptr': |
| 222 | res = 'xmlNodePtr'; |
| 223 | if res == 'xmlDict_ptr': |
| 224 | res = 'xmlDictPtr'; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 225 | if res == 'xmlNodePtr' and pos != 0: |
| 226 | if (function == 'xmlAddChild' and pos == 2) or \ |
| 227 | (function == 'xmlAddChildList' and pos == 2) or \ |
| 228 | (function == 'xmlAddNextSibling' and pos == 2) or \ |
| 229 | (function == 'xmlAddSibling' and pos == 2) or \ |
| 230 | (function == 'xmlDocSetRootElement' and pos == 2) or \ |
| 231 | (function == 'xmlReplaceNode' and pos == 2) or \ |
| 232 | (function == 'xmlTextMerge') or \ |
| 233 | (function == 'xmlAddPrevSibling' and pos == 2): |
| 234 | return('xmlNodePtr_in'); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 235 | if res == 'const xmlBufferPtr': |
| 236 | res = 'xmlBufferPtr'; |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 237 | if res == 'xmlChar_ptr' and name == 'name' and \ |
| 238 | string.find(function, "EatName") != -1: |
| 239 | return('eaten_name') |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 240 | if res == 'void_ptr*': |
| 241 | res = 'void_ptr_ptr' |
| 242 | if res == 'char_ptr*': |
| 243 | res = 'char_ptr_ptr' |
| 244 | if res == 'xmlChar_ptr*': |
| 245 | res = 'xmlChar_ptr_ptr' |
| 246 | if res == 'const_xmlChar_ptr*': |
| 247 | res = 'const_xmlChar_ptr_ptr' |
| 248 | if res == 'const_char_ptr*': |
| 249 | res = 'const_char_ptr_ptr' |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 250 | |
| 251 | return res |
| 252 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 253 | known_param_types = [] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 254 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 255 | def is_known_param_type(name, rtype): |
| 256 | global test |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 257 | for type in known_param_types: |
| 258 | if type == name: |
| 259 | return 1 |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 260 | for type in generated_param_types: |
| 261 | if type == name: |
| 262 | return 1 |
| 263 | |
| 264 | if name[-3:] == 'Ptr' or name[-4:] == '_ptr': |
| 265 | if rtype[0:6] == 'const ': |
| 266 | crtype = rtype[6:] |
| 267 | else: |
| 268 | crtype = rtype |
| 269 | |
| 270 | test.write(""" |
| 271 | #define gen_nb_%s 1 |
| 272 | static %s gen_%s(int no ATTRIBUTE_UNUSED, int nr ATTRIBUTE_UNUSED) { |
| 273 | return(NULL); |
| 274 | } |
| 275 | static void des_%s(int no ATTRIBUTE_UNUSED, %s val ATTRIBUTE_UNUSED, int nr ATTRIBUTE_UNUSED) { |
| 276 | } |
| 277 | """ % (name, crtype, name, name, rtype)) |
| 278 | add_generated_param_type(name) |
| 279 | return 1 |
| 280 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 281 | return 0 |
| 282 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 283 | # |
| 284 | # Provide the type destructors for the return values |
| 285 | # |
| 286 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 287 | known_return_types = [] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 288 | |
| 289 | def is_known_return_type(name): |
| 290 | for type in known_return_types: |
| 291 | if type == name: |
| 292 | return 1 |
| 293 | return 0 |
| 294 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 295 | # |
| 296 | # Copy the beginning of the C test program result |
| 297 | # |
| 298 | |
| 299 | input = open("testapi.c", "r") |
| 300 | test = open('testapi.c.new', 'w') |
| 301 | |
| 302 | def compare_and_save(): |
| 303 | global test |
| 304 | |
| 305 | test.close() |
| 306 | input = open("testapi.c", "r").read() |
| 307 | test = open('testapi.c.new', "r").read() |
| 308 | if input != test: |
| 309 | os.system("rm testapi.c ; mv testapi.c.new testapi.c") |
| 310 | print("Updated testapi.c") |
| 311 | else: |
| 312 | print("Generated testapi.c is identical") |
| 313 | |
| 314 | line = input.readline() |
| 315 | while line != "": |
| 316 | if line == "/* CUT HERE: everything below that line is generated */\n": |
| 317 | break; |
| 318 | if line[0:15] == "#define gen_nb_": |
| 319 | type = string.split(line[15:])[0] |
| 320 | known_param_types.append(type) |
| 321 | if line[0:19] == "static void desret_": |
| 322 | type = string.split(line[19:], '(')[0] |
| 323 | known_return_types.append(type) |
| 324 | test.write(line) |
| 325 | line = input.readline() |
| 326 | input.close() |
| 327 | |
| 328 | if line == "": |
| 329 | print "Could not find the CUT marker in testapi.c skipping generation" |
| 330 | test.close() |
| 331 | sys.exit(0) |
| 332 | |
| 333 | print("Scanned testapi.c: found %d parameters types and %d return types\n" % ( |
| 334 | len(known_param_types), len(known_return_types))) |
| 335 | test.write("/* CUT HERE: everything below that line is generated */\n") |
| 336 | |
| 337 | |
| 338 | # |
| 339 | # Open the input API description |
| 340 | # |
| 341 | doc = libxml2.readFile('doc/libxml2-api.xml', None, 0) |
| 342 | if doc == None: |
| 343 | print "Failed to load doc/libxml2-api.xml" |
| 344 | sys.exit(1) |
| 345 | ctxt = doc.xpathNewContext() |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 346 | |
| 347 | # |
| 348 | # Generate constructors and return type handling for all enums |
| 349 | # |
| 350 | enums = ctxt.xpathEval("/api/symbols/typedef[@type='enum']") |
| 351 | for enum in enums: |
| 352 | name = enum.xpathEval('string(@name)') |
| 353 | if name == None: |
| 354 | continue; |
| 355 | |
| 356 | if is_known_param_type(name, name) == 0: |
| 357 | values = ctxt.xpathEval("/api/symbols/enum[@type='%s']" % name) |
| 358 | i = 0 |
| 359 | vals = [] |
| 360 | for value in values: |
| 361 | vname = value.xpathEval('string(@name)') |
| 362 | if vname == None: |
| 363 | continue; |
| 364 | i = i + 1 |
| 365 | if i >= 5: |
| 366 | break; |
| 367 | vals.append(vname) |
| 368 | if vals == []: |
| 369 | print "Didn't found any value for enum %s" % (name) |
| 370 | continue |
| 371 | test.write("#define gen_nb_%s %d\n" % (name, len(vals))) |
| 372 | test.write("""static %s gen_%s(int no, int nr ATTRIBUTE_UNUSED) {\n""" % |
| 373 | (name, name)) |
| 374 | i = 1 |
| 375 | for value in vals: |
| 376 | test.write(" if (no == %d) return(%s);\n" % (i, value)) |
| 377 | i = i + 1 |
| 378 | test.write(""" return(0); |
| 379 | } |
| 380 | """); |
| 381 | known_param_types.append(name) |
| 382 | |
| 383 | if is_known_return_type(name) == 0: |
| 384 | test.write("""static void des_%s(int no ATTRIBUTE_UNUSED, %s val ATTRIBUTE_UNUSED, int nr ATTRIBUTE_UNUSED) { |
| 385 | } |
| 386 | static void desret_%s(%s val ATTRIBUTE_UNUSED) { |
| 387 | } |
| 388 | |
| 389 | """ % (name, name, name, name)) |
| 390 | known_return_types.append(name) |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 391 | |
| 392 | # |
| 393 | # Load the interfaces |
| 394 | # |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 395 | headers = ctxt.xpathEval("/api/files/file") |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 396 | for file in headers: |
| 397 | name = file.xpathEval('string(@name)') |
| 398 | if (name == None) or (name == ''): |
| 399 | continue |
| 400 | |
| 401 | # |
| 402 | # Some module may be skipped because they don't really consists |
| 403 | # of user callable APIs |
| 404 | # |
| 405 | if is_skipped_module(name): |
| 406 | continue |
| 407 | |
| 408 | # |
| 409 | # do not test deprecated APIs |
| 410 | # |
| 411 | desc = file.xpathEval('string(description)') |
| 412 | if string.find(desc, 'DEPRECATED') != -1: |
| 413 | print "Skipping deprecated interface %s" % name |
| 414 | continue; |
| 415 | |
| 416 | test.write("#include <libxml/%s.h>\n" % name) |
| 417 | modules.append(name) |
| 418 | |
| 419 | # |
| 420 | # Generate the callers signatures |
| 421 | # |
| 422 | for module in modules: |
| 423 | test.write("static int test_%s(void);\n" % module); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 424 | |
| 425 | # |
| 426 | # Generate the top caller |
| 427 | # |
| 428 | |
| 429 | test.write(""" |
| 430 | /** |
| 431 | * testlibxml2: |
| 432 | * |
| 433 | * Main entry point of the tester for the full libxml2 module, |
| 434 | * it calls all the tester entry point for each module. |
| 435 | * |
| 436 | * Returns the number of error found |
| 437 | */ |
| 438 | static int |
| 439 | testlibxml2(void) |
| 440 | { |
| 441 | int ret = 0; |
| 442 | |
| 443 | """) |
| 444 | |
| 445 | for module in modules: |
| 446 | test.write(" ret += test_%s();\n" % module) |
| 447 | |
| 448 | test.write(""" |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 449 | printf("Total: %d functions, %d tests, %d errors\\n", |
| 450 | function_tests, call_tests, ret); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 451 | return(ret); |
| 452 | } |
| 453 | |
| 454 | """) |
| 455 | |
| 456 | # |
| 457 | # How to handle a function |
| 458 | # |
| 459 | nb_tests = 0 |
| 460 | |
| 461 | def generate_test(module, node): |
| 462 | global test |
| 463 | global nb_tests |
| 464 | nb_cond = 0 |
| 465 | no_gen = 0 |
| 466 | |
| 467 | name = node.xpathEval('string(@name)') |
| 468 | if is_skipped_function(name): |
| 469 | return |
| 470 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 471 | # |
| 472 | # check we know how to handle the args and return values |
| 473 | # and store the informations for the generation |
| 474 | # |
| 475 | try: |
| 476 | args = node.xpathEval("arg") |
| 477 | except: |
| 478 | args = [] |
| 479 | t_args = [] |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 480 | n = 0 |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 481 | for arg in args: |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 482 | n = n + 1 |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 483 | rtype = arg.xpathEval("string(@type)") |
| 484 | if rtype == 'void': |
| 485 | break; |
| 486 | info = arg.xpathEval("string(@info)") |
| 487 | nam = arg.xpathEval("string(@name)") |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 488 | type = type_convert(rtype, nam, info, module, name, n) |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 489 | if is_known_param_type(type, rtype) == 0: |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 490 | add_missing_type(type, name); |
| 491 | no_gen = 1 |
| 492 | t_args.append((nam, type, rtype, info)) |
| 493 | |
| 494 | try: |
| 495 | rets = node.xpathEval("return") |
| 496 | except: |
| 497 | rets = [] |
| 498 | t_ret = None |
| 499 | for ret in rets: |
| 500 | rtype = ret.xpathEval("string(@type)") |
| 501 | info = ret.xpathEval("string(@info)") |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 502 | type = type_convert(rtype, 'return', info, module, name, 0) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 503 | if rtype == 'void': |
| 504 | break |
| 505 | if is_known_return_type(type) == 0: |
| 506 | add_missing_type(type, name); |
| 507 | no_gen = 1 |
| 508 | t_ret = (type, rtype, info) |
| 509 | break |
| 510 | |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame] | 511 | test.write(""" |
| 512 | static int |
| 513 | test_%s(void) { |
| 514 | int ret = 0; |
| 515 | |
| 516 | """ % (name)) |
| 517 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 518 | if no_gen == 1: |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 519 | add_missing_functions(name, module) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 520 | test.write(""" |
| 521 | /* missing type support */ |
| 522 | return(ret); |
| 523 | } |
| 524 | |
| 525 | """) |
| 526 | return |
| 527 | |
| 528 | try: |
| 529 | conds = node.xpathEval("cond") |
| 530 | for cond in conds: |
| 531 | test.write("#ifdef %s\n" % (cond.get_content())) |
| 532 | nb_cond = nb_cond + 1 |
| 533 | except: |
| 534 | pass |
| 535 | |
| 536 | # Declare the memory usage counter |
| 537 | no_mem = is_skipped_memcheck(name) |
| 538 | if no_mem == 0: |
| 539 | test.write(" int mem_base;\n"); |
| 540 | |
| 541 | # Declare the return value |
| 542 | if t_ret != None: |
| 543 | test.write(" %s ret_val;\n" % (t_ret[1])) |
| 544 | |
| 545 | # Declare the arguments |
| 546 | for arg in t_args: |
| 547 | (nam, type, rtype, info) = arg; |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 548 | if (type[-3:] == 'Ptr' or type[-4:] == '_ptr') and \ |
| 549 | rtype[0:6] == 'const ': |
| 550 | crtype = rtype[6:] |
| 551 | else: |
| 552 | crtype = rtype |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 553 | # add declaration |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 554 | test.write(" %s %s; /* %s */\n" % (crtype, nam, info)) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 555 | test.write(" int n_%s;\n" % (nam)) |
| 556 | test.write("\n") |
| 557 | |
| 558 | # Cascade loop on of each argument list of values |
| 559 | for arg in t_args: |
| 560 | (nam, type, rtype, info) = arg; |
| 561 | # |
| 562 | test.write(" for (n_%s = 0;n_%s < gen_nb_%s;n_%s++) {\n" % ( |
| 563 | nam, nam, type, nam)) |
| 564 | |
| 565 | # log the memory usage |
| 566 | if no_mem == 0: |
| 567 | test.write(" mem_base = xmlMemBlocks();\n"); |
| 568 | |
| 569 | # prepare the call |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 570 | i = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 571 | for arg in t_args: |
| 572 | (nam, type, rtype, info) = arg; |
| 573 | # |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 574 | test.write(" %s = gen_%s(n_%s, %d);\n" % (nam, type, nam, i)) |
| 575 | i = i + 1; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 576 | |
| 577 | # do the call, and clanup the result |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 578 | if extra_pre_call.has_key(name): |
| 579 | test.write(" %s\n"% (extra_pre_call[name])) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 580 | if t_ret != None: |
| 581 | test.write("\n ret_val = %s(" % (name)) |
| 582 | need = 0 |
| 583 | for arg in t_args: |
| 584 | (nam, type, rtype, info) = arg |
| 585 | if need: |
| 586 | test.write(", ") |
| 587 | else: |
| 588 | need = 1 |
| 589 | test.write("%s" % nam); |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 590 | test.write(");\n") |
| 591 | if extra_post_call.has_key(name): |
| 592 | test.write(" %s\n"% (extra_post_call[name])) |
| 593 | test.write(" desret_%s(ret_val);\n" % t_ret[0]) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 594 | else: |
| 595 | test.write("\n %s(" % (name)); |
| 596 | need = 0; |
| 597 | for arg in t_args: |
| 598 | (nam, type, rtype, info) = arg; |
| 599 | if need: |
| 600 | test.write(", ") |
| 601 | else: |
| 602 | need = 1 |
| 603 | test.write("%s" % nam) |
| 604 | test.write(");\n") |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 605 | if extra_post_call.has_key(name): |
| 606 | test.write(" %s\n"% (extra_post_call[name])) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 607 | |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 608 | test.write(" call_tests++;\n"); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 609 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 610 | # Free the arguments |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 611 | i = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 612 | for arg in t_args: |
| 613 | (nam, type, rtype, info) = arg; |
| 614 | # |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 615 | test.write(" des_%s(n_%s, %s, %d);\n" % (type, nam, nam, i)) |
| 616 | i = i + 1; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 617 | |
| 618 | test.write(" xmlResetLastError();\n"); |
| 619 | # Check the memory usage |
| 620 | if no_mem == 0: |
| 621 | test.write(""" if (mem_base != xmlMemBlocks()) { |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 622 | printf("Leak of %%d blocks found in %s", |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 623 | xmlMemBlocks() - mem_base); |
| 624 | ret++; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 625 | """ % (name)); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 626 | for arg in t_args: |
| 627 | (nam, type, rtype, info) = arg; |
| 628 | test.write(""" printf(" %%d", n_%s);\n""" % (nam)) |
| 629 | test.write(""" printf("\\n");\n""") |
| 630 | test.write(" }\n") |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 631 | |
| 632 | for arg in t_args: |
| 633 | test.write(" }\n") |
| 634 | |
| 635 | # |
| 636 | # end of conditional |
| 637 | # |
| 638 | while nb_cond > 0: |
| 639 | test.write("#endif\n") |
| 640 | nb_cond = nb_cond -1 |
| 641 | |
| 642 | nb_tests = nb_tests + 1; |
| 643 | |
| 644 | test.write(""" |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 645 | function_tests++; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 646 | return(ret); |
| 647 | } |
| 648 | |
| 649 | """) |
| 650 | |
| 651 | # |
| 652 | # Generate all module callers |
| 653 | # |
| 654 | for module in modules: |
| 655 | # gather all the functions exported by that module |
| 656 | try: |
| 657 | functions = ctxt.xpathEval("/api/symbols/function[@file='%s']" % (module)) |
| 658 | except: |
| 659 | print "Failed to gather functions from module %s" % (module) |
| 660 | continue; |
| 661 | |
| 662 | # iterate over all functions in the module generating the test |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 663 | i = 0 |
| 664 | nb_tests_old = nb_tests |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 665 | for function in functions: |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 666 | i = i + 1 |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 667 | generate_test(module, function); |
| 668 | |
| 669 | # header |
| 670 | test.write("""static int |
| 671 | test_%s(void) { |
| 672 | int ret = 0; |
| 673 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 674 | printf("Testing %s : %d of %d functions ...\\n"); |
| 675 | """ % (module, module, nb_tests - nb_tests_old, i)) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 676 | |
| 677 | # iterate over all functions in the module generating the call |
| 678 | for function in functions: |
| 679 | name = function.xpathEval('string(@name)') |
| 680 | if is_skipped_function(name): |
| 681 | continue |
| 682 | test.write(" ret += test_%s();\n" % (name)) |
| 683 | |
| 684 | # footer |
| 685 | test.write(""" |
| 686 | if (ret != 0) |
| 687 | printf("Module %s: %%d errors\\n", ret); |
| 688 | return(ret); |
| 689 | } |
| 690 | """ % (module)) |
| 691 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 692 | # |
| 693 | # Generate direct module caller |
| 694 | # |
| 695 | test.write("""static int |
| 696 | test_module(const char *module) { |
| 697 | """); |
| 698 | for module in modules: |
| 699 | test.write(""" if (!strcmp(module, "%s")) return(test_%s());\n""" % ( |
| 700 | module, module)) |
| 701 | test.write(""" return(0); |
| 702 | } |
| 703 | """); |
| 704 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 705 | print "Generated test for %d modules and %d functions" %(len(modules), nb_tests) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 706 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 707 | compare_and_save() |
| 708 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 709 | missing_list = [] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 710 | for missing in missing_types.keys(): |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 711 | if missing == 'va_list' or missing == '...': |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 712 | continue; |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 713 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 714 | n = len(missing_types[missing]) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 715 | missing_list.append((n, missing)) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 716 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 717 | def compare_missing(a, b): |
| 718 | return b[0] - a[0] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 719 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 720 | missing_list.sort(compare_missing) |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame] | 721 | print "Missing support for %d functions and %d types see missing.lst" % (missing_functions_nr, len(missing_list)) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 722 | lst = open("missing.lst", "w") |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 723 | lst.write("Missing support for %d types" % (len(missing_list))) |
| 724 | lst.write("\n") |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 725 | for miss in missing_list: |
| 726 | lst.write("%s: %d :" % (miss[1], miss[0])) |
| 727 | i = 0 |
| 728 | for n in missing_types[miss[1]]: |
| 729 | i = i + 1 |
| 730 | if i > 5: |
| 731 | lst.write(" ...") |
| 732 | break |
| 733 | lst.write(" %s" % (n)) |
| 734 | lst.write("\n") |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 735 | lst.write("\n") |
| 736 | lst.write("\n") |
| 737 | lst.write("Missing support per module"); |
| 738 | for module in missing_functions.keys(): |
| 739 | lst.write("module %s:\n %s\n" % (module, missing_functions[module])) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 740 | |
| 741 | lst.close() |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 742 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 743 | |