|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "librarydata.h" |
| 20 | + |
| 21 | +#include <QDomDocument> |
| 22 | +#include <QDomNode> |
| 23 | +#include <QDomNodeList> |
| 24 | + |
| 25 | +const unsigned int LibraryData::Function::Arg::ANY = ~0U; |
| 26 | + |
| 27 | +LibraryData::LibraryData() |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +static LibraryData::Define loadDefine(const QDomElement &defineElement) |
| 33 | +{ |
| 34 | + LibraryData::Define define; |
| 35 | + define.name = defineElement.attribute("name"); |
| 36 | + define.value = defineElement.attribute("value"); |
| 37 | + return define; |
| 38 | +} |
| 39 | + |
| 40 | +static LibraryData::Function::Arg loadFunctionArg(const QDomElement &functionArgElement) |
| 41 | +{ |
| 42 | + LibraryData::Function::Arg arg; |
| 43 | + if (functionArgElement.attribute("nr") == "any") |
| 44 | + arg.nr = LibraryData::Function::Arg::ANY; |
| 45 | + else |
| 46 | + arg.nr = functionArgElement.attribute("nr").toUInt(); |
| 47 | + for (QDomElement childElement = functionArgElement.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) { |
| 48 | + if (childElement.tagName() == "not-bool") |
| 49 | + arg.notbool = true; |
| 50 | + else if (childElement.tagName() == "not-null") |
| 51 | + arg.notnull = true; |
| 52 | + else if (childElement.tagName() == "not-uninit") |
| 53 | + arg.notuninit = true; |
| 54 | + else if (childElement.tagName() == "strz") |
| 55 | + arg.strz = true; |
| 56 | + else if (childElement.tagName() == "formatstr") |
| 57 | + arg.formatstr = true; |
| 58 | + else if (childElement.tagName() == "valid") |
| 59 | + arg.valid = childElement.text(); |
| 60 | + else if (childElement.tagName() == "minsize") { |
| 61 | + arg.minsize.type = childElement.attribute("type"); |
| 62 | + arg.minsize.arg = childElement.attribute("arg"); |
| 63 | + arg.minsize.arg2 = childElement.attribute("arg2"); |
| 64 | + } |
| 65 | + } |
| 66 | + return arg; |
| 67 | +} |
| 68 | + |
| 69 | +static LibraryData::Function loadFunction(const QDomElement &functionElement) |
| 70 | +{ |
| 71 | + LibraryData::Function function; |
| 72 | + function.name = functionElement.attribute("name"); |
| 73 | + for (QDomElement childElement = functionElement.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) { |
| 74 | + if (childElement.tagName() == "noreturn") |
| 75 | + function.noreturn = (childElement.text() == "true"); |
| 76 | + else if (childElement.tagName() == "pure") |
| 77 | + function.gccPure = true; |
| 78 | + else if (childElement.tagName() == "const") |
| 79 | + function.gccConst = true; |
| 80 | + else if (childElement.tagName() == "leak-ignore") |
| 81 | + function.leakignore = true; |
| 82 | + else if (childElement.tagName() == "use-retval") |
| 83 | + function.useretval = true; |
| 84 | + else if (childElement.tagName() == "formatstr") { |
| 85 | + function.formatstr.scan = childElement.attribute("scan"); |
| 86 | + function.formatstr.secure = childElement.attribute("secure"); |
| 87 | + } else if (childElement.tagName() == "arg") { |
| 88 | + const LibraryData::Function::Arg fa = loadFunctionArg(childElement); |
| 89 | + function.args.append(fa); |
| 90 | + } else { |
| 91 | + int x = 123; |
| 92 | + x++; |
| 93 | + |
| 94 | + } |
| 95 | + } |
| 96 | + return function; |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +static LibraryData::MemoryResource loadMemoryResource(const QDomElement &element) |
| 101 | +{ |
| 102 | + LibraryData::MemoryResource memoryresource; |
| 103 | + memoryresource.type = element.tagName(); |
| 104 | + for (QDomElement childElement = element.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) { |
| 105 | + if (childElement.tagName() == "alloc") { |
| 106 | + LibraryData::MemoryResource::Alloc alloc; |
| 107 | + alloc.init = (childElement.attribute("init", "false") == "true"); |
| 108 | + alloc.name = childElement.text(); |
| 109 | + memoryresource.alloc.append(alloc); |
| 110 | + } else if (childElement.tagName() == "dealloc") |
| 111 | + memoryresource.dealloc.append(childElement.text()); |
| 112 | + else if (childElement.tagName() == "use") |
| 113 | + memoryresource.use.append(childElement.text()); |
| 114 | + } |
| 115 | + return memoryresource; |
| 116 | +} |
| 117 | + |
| 118 | +static LibraryData::PodType loadPodType(const QDomElement &element) |
| 119 | +{ |
| 120 | + LibraryData::PodType podtype; |
| 121 | + podtype.name = element.attribute("name"); |
| 122 | + podtype.size = element.attribute("size"); |
| 123 | + podtype.sign = element.attribute("sign"); |
| 124 | + return podtype; |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +bool LibraryData::open(QIODevice &file) |
| 129 | +{ |
| 130 | + QDomDocument doc; |
| 131 | + if (!doc.setContent(&file)) |
| 132 | + return false; |
| 133 | + |
| 134 | + QDomElement rootElement = doc.firstChildElement("def"); |
| 135 | + for (QDomElement e = rootElement.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) { |
| 136 | + if (e.tagName() == "define") |
| 137 | + defines.append(loadDefine(e)); |
| 138 | + else if (e.tagName() == "function") |
| 139 | + functions.append(loadFunction(e)); |
| 140 | + else if (e.tagName() == "memory" || e.tagName() == "resource") |
| 141 | + memoryresource.append(loadMemoryResource(e)); |
| 142 | + else if (e.tagName() == "podtype") |
| 143 | + podtypes.append(loadPodType(e)); |
| 144 | + } |
| 145 | + |
| 146 | + return true; |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +static QDomElement FunctionElement(QDomDocument &doc, const LibraryData::Function &function) |
| 152 | +{ |
| 153 | + QDomElement functionElement = doc.createElement("function"); |
| 154 | + functionElement.setAttribute("name", function.name); |
| 155 | + if (!function.noreturn) { |
| 156 | + QDomElement e = doc.createElement("noreturn"); |
| 157 | + e.appendChild(doc.createTextNode("false")); |
| 158 | + functionElement.appendChild(e); |
| 159 | + } |
| 160 | + if (function.useretval) |
| 161 | + functionElement.appendChild(doc.createElement("useretval")); |
| 162 | + if (function.leakignore) |
| 163 | + functionElement.appendChild(doc.createElement("leak-ignore")); |
| 164 | + |
| 165 | + // Argument info.. |
| 166 | + foreach(const LibraryData::Function::Arg &arg, function.args) { |
| 167 | + QDomElement argElement = doc.createElement("arg"); |
| 168 | + functionElement.appendChild(argElement); |
| 169 | + if (arg.nr == LibraryData::Function::Arg::ANY) |
| 170 | + argElement.setAttribute("nr", "any"); |
| 171 | + else |
| 172 | + argElement.setAttribute("nr", arg.nr); |
| 173 | + if (arg.notbool) |
| 174 | + argElement.appendChild(doc.createElement("not-bool")); |
| 175 | + if (arg.notnull) |
| 176 | + argElement.appendChild(doc.createElement("not-null")); |
| 177 | + if (arg.notuninit) |
| 178 | + argElement.appendChild(doc.createElement("not-uninit")); |
| 179 | + if (arg.strz) |
| 180 | + argElement.appendChild(doc.createElement("strz")); |
| 181 | + if (arg.formatstr) |
| 182 | + argElement.appendChild(doc.createElement("formatstr")); |
| 183 | + |
| 184 | + if (!arg.valid.isEmpty()) { |
| 185 | + QDomElement e = doc.createElement("valid"); |
| 186 | + e.appendChild(doc.createTextNode(arg.valid)); |
| 187 | + argElement.appendChild(e); |
| 188 | + } |
| 189 | + |
| 190 | + if (!arg.minsize.type.isEmpty()) { |
| 191 | + QDomElement e = doc.createElement("minsize"); |
| 192 | + e.setAttribute("type", arg.minsize.type); |
| 193 | + e.setAttribute("arg", arg.minsize.arg); |
| 194 | + if (!arg.minsize.arg2.isEmpty()) |
| 195 | + e.setAttribute("arg2", arg.minsize.arg2); |
| 196 | + argElement.appendChild(e); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return functionElement; |
| 201 | +} |
| 202 | + |
| 203 | +static QDomElement MemoryResourceElement(QDomDocument &doc, const LibraryData::MemoryResource &mr) |
| 204 | +{ |
| 205 | + QDomElement element = doc.createElement(mr.type); |
| 206 | + foreach(const LibraryData::MemoryResource::Alloc &alloc, mr.alloc) { |
| 207 | + QDomElement e = doc.createElement("alloc"); |
| 208 | + if (alloc.init) |
| 209 | + e.setAttribute("init", "true"); |
| 210 | + e.appendChild(doc.createTextNode(alloc.name)); |
| 211 | + element.appendChild(e); |
| 212 | + } |
| 213 | + foreach(const QString &dealloc, mr.dealloc) { |
| 214 | + QDomElement e = doc.createElement("dealloc"); |
| 215 | + e.appendChild(doc.createTextNode(dealloc)); |
| 216 | + element.appendChild(e); |
| 217 | + } |
| 218 | + foreach(const QString &use, mr.use) { |
| 219 | + QDomElement e = doc.createElement("use"); |
| 220 | + e.appendChild(doc.createTextNode(use)); |
| 221 | + element.appendChild(e); |
| 222 | + } |
| 223 | + return element; |
| 224 | +} |
| 225 | + |
| 226 | + |
| 227 | +QString LibraryData::toString() const |
| 228 | +{ |
| 229 | + QDomDocument doc; |
| 230 | + QDomElement root = doc.createElement("def"); |
| 231 | + doc.appendChild(root); |
| 232 | + root.setAttribute("format","2"); |
| 233 | + |
| 234 | + foreach(const Define &define, defines) { |
| 235 | + QDomElement defineElement = doc.createElement("define"); |
| 236 | + defineElement.setAttribute("name", define.name); |
| 237 | + defineElement.setAttribute("value", define.value); |
| 238 | + root.appendChild(defineElement); |
| 239 | + } |
| 240 | + |
| 241 | + foreach(const Function &function, functions) { |
| 242 | + root.appendChild(FunctionElement(doc, function)); |
| 243 | + } |
| 244 | + |
| 245 | + foreach(const MemoryResource &mr, memoryresource) { |
| 246 | + root.appendChild(MemoryResourceElement(doc, mr)); |
| 247 | + } |
| 248 | + |
| 249 | + foreach(const PodType &podtype, podtypes) { |
| 250 | + QDomElement podtypeElement = doc.createElement("podtype"); |
| 251 | + podtypeElement.setAttribute("name", podtype.name); |
| 252 | + podtypeElement.setAttribute("size", podtype.size); |
| 253 | + podtypeElement.setAttribute("sign", podtype.sign); |
| 254 | + root.appendChild(podtypeElement); |
| 255 | + } |
| 256 | + |
| 257 | + return doc.toString(); |
| 258 | +} |
| 259 | + |
| 260 | + |
0 commit comments