states: tweak debug output
[smatch.git] / c2xml.c
blob67486c779d0664d99125ddb4f0c02c4698598a0f
1 /*
2 * Sparse c2xml
4 * Dumps the parse tree as an xml document
6 * Copyright (C) 2007 Rob Taylor
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <assert.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
35 #include "expression.h"
36 #include "parse.h"
37 #include "scope.h"
38 #include "symbol.h"
40 static xmlDocPtr doc = NULL; /* document pointer */
41 static xmlNodePtr root_node = NULL;/* root node pointer */
42 static int idcount = 0;
44 static void examine_symbol(struct symbol *sym, xmlNodePtr node);
46 static xmlAttrPtr newProp(xmlNodePtr node, const char *name, const char *value)
48 return xmlNewProp(node, BAD_CAST name, BAD_CAST value);
51 static xmlAttrPtr newNumProp(xmlNodePtr node, const char *name, int value)
53 char buf[256];
54 snprintf(buf, 256, "%d", value);
55 return newProp(node, name, buf);
58 static xmlAttrPtr newIdProp(xmlNodePtr node, const char *name, unsigned int id)
60 char buf[256];
61 snprintf(buf, 256, "_%d", id);
62 return newProp(node, name, buf);
65 static xmlNodePtr new_sym_node(struct symbol *sym, const char *name, xmlNodePtr parent)
67 xmlNodePtr node;
68 const char *ident = show_ident(sym->ident);
70 assert(name != NULL);
71 assert(sym != NULL);
72 assert(parent != NULL);
74 node = xmlNewChild(parent, NULL, BAD_CAST "symbol", NULL);
76 newProp(node, "type", name);
78 newIdProp(node, "id", idcount);
80 if (sym->ident && ident)
81 newProp(node, "ident", ident);
82 newProp(node, "file", stream_name(sym->pos.stream));
84 newNumProp(node, "start-line", sym->pos.line);
85 newNumProp(node, "start-col", sym->pos.pos);
87 if (sym->endpos.type) {
88 newNumProp(node, "end-line", sym->endpos.line);
89 newNumProp(node, "end-col", sym->endpos.pos);
90 if (sym->pos.stream != sym->endpos.stream)
91 newProp(node, "end-file", stream_name(sym->endpos.stream));
93 sym->aux = node;
95 idcount++;
97 return node;
100 static inline void examine_members(struct symbol_list *list, xmlNodePtr node)
102 struct symbol *sym;
104 FOR_EACH_PTR(list, sym) {
105 examine_symbol(sym, node);
106 } END_FOR_EACH_PTR(sym);
109 static void examine_modifiers(struct symbol *sym, xmlNodePtr node)
111 const char *modifiers[] = {
112 "auto",
113 "register",
114 "static",
115 "extern",
116 "const",
117 "volatile",
118 "signed",
119 "unsigned",
120 "char",
121 "short",
122 "long",
123 "long-long",
124 "typedef",
125 NULL,
126 NULL,
127 NULL,
128 NULL,
129 NULL,
130 "inline",
131 "addressable",
132 "nocast",
133 "noderef",
134 "accessed",
135 "toplevel",
136 "label",
137 "assigned",
138 "type-type",
139 "safe",
140 "user-type",
141 "force",
142 "explicitly-signed",
143 "bitwise"};
145 int i;
147 if (sym->namespace != NS_SYMBOL)
148 return;
150 /*iterate over the 32 bit bitfield*/
151 for (i=0; i < 32; i++) {
152 if ((sym->ctype.modifiers & 1<<i) && modifiers[i])
153 newProp(node, modifiers[i], "1");
157 static void
158 examine_layout(struct symbol *sym, xmlNodePtr node)
160 examine_symbol_type(sym);
162 newNumProp(node, "bit-size", sym->bit_size);
163 newNumProp(node, "alignment", sym->ctype.alignment);
164 newNumProp(node, "offset", sym->offset);
165 if (is_bitfield_type(sym)) {
166 newNumProp(node, "bit-offset", sym->bit_offset);
170 static void examine_symbol(struct symbol *sym, xmlNodePtr node)
172 xmlNodePtr child = NULL;
173 const char *base;
174 int array_size;
176 if (!sym)
177 return;
178 if (sym->aux) /*already visited */
179 return;
181 if (sym->ident && sym->ident->reserved)
182 return;
184 child = new_sym_node(sym, get_type_name(sym->type), node);
185 examine_modifiers(sym, child);
186 examine_layout(sym, child);
188 if (sym->ctype.base_type) {
189 if ((base = builtin_typename(sym->ctype.base_type)) == NULL) {
190 if (!sym->ctype.base_type->aux) {
191 examine_symbol(sym->ctype.base_type, root_node);
193 xmlNewProp(child, BAD_CAST "base-type",
194 xmlGetProp((xmlNodePtr)sym->ctype.base_type->aux, BAD_CAST "id"));
195 } else {
196 newProp(child, "base-type-builtin", base);
199 if (sym->array_size) {
200 /* TODO: modify get_expression_value to give error return */
201 array_size = get_expression_value(sym->array_size);
202 newNumProp(child, "array-size", array_size);
206 switch (sym->type) {
207 case SYM_STRUCT:
208 case SYM_UNION:
209 examine_members(sym->symbol_list, child);
210 break;
211 case SYM_FN:
212 examine_members(sym->arguments, child);
213 break;
214 case SYM_UNINITIALIZED:
215 newProp(child, "base-type-builtin", builtin_typename(sym));
216 break;
218 return;
221 static struct position *get_expansion_end (struct token *token)
223 struct token *p1, *p2;
225 for (p1=NULL, p2=NULL;
226 !eof_token(token);
227 p2 = p1, p1 = token, token = token->next);
229 if (p2)
230 return &(p2->pos);
231 else
232 return NULL;
235 static void examine_macro(struct symbol *sym, xmlNodePtr node)
237 struct position *pos;
239 /* this should probably go in the main codebase*/
240 pos = get_expansion_end(sym->expansion);
241 if (pos)
242 sym->endpos = *pos;
243 else
244 sym->endpos = sym->pos;
246 new_sym_node(sym, "macro", node);
249 static void examine_namespace(struct symbol *sym)
251 if (sym->ident && sym->ident->reserved)
252 return;
254 switch(sym->namespace) {
255 case NS_MACRO:
256 examine_macro(sym, root_node);
257 break;
258 case NS_TYPEDEF:
259 case NS_STRUCT:
260 case NS_SYMBOL:
261 examine_symbol(sym, root_node);
262 break;
263 case NS_NONE:
264 case NS_LABEL:
265 case NS_ITERATOR:
266 case NS_UNDEF:
267 case NS_PREPROCESSOR:
268 case NS_KEYWORD:
269 break;
270 default:
271 die("Unrecognised namespace type %d",sym->namespace);
276 static int get_stream_id (const char *name)
278 int i;
279 for (i=0; i<input_stream_nr; i++) {
280 if (strcmp(name, stream_name(i))==0)
281 return i;
283 return -1;
286 static inline void examine_symbol_list(const char *file, struct symbol_list *list)
288 struct symbol *sym;
289 int stream_id = get_stream_id (file);
291 if (!list)
292 return;
293 FOR_EACH_PTR(list, sym) {
294 if (sym->pos.stream == stream_id)
295 examine_namespace(sym);
296 } END_FOR_EACH_PTR(sym);
299 int main(int argc, char **argv)
301 struct string_list *filelist = NULL;
302 struct symbol_list *symlist = NULL;
303 char *file;
305 doc = xmlNewDoc(BAD_CAST "1.0");
306 root_node = xmlNewNode(NULL, BAD_CAST "parse");
307 xmlDocSetRootElement(doc, root_node);
309 /* - A DTD is probably unnecessary for something like this
311 dtd = xmlCreateIntSubset(doc, "parse", "http://www.kernel.org/pub/software/devel/sparse/parse.dtd" NULL, "parse.dtd");
313 ns = xmlNewNs (root_node, "http://www.kernel.org/pub/software/devel/sparse/parse.dtd", NULL);
315 xmlSetNs(root_node, ns);
317 symlist = sparse_initialize(argc, argv, &filelist);
319 FOR_EACH_PTR_NOTAG(filelist, file) {
320 examine_symbol_list(file, symlist);
321 sparse_keep_tokens(file);
322 examine_symbol_list(file, file_scope->symbols);
323 examine_symbol_list(file, global_scope->symbols);
324 } END_FOR_EACH_PTR_NOTAG(file);
327 xmlSaveFormatFileEnc("-", doc, "UTF-8", 1);
328 xmlFreeDoc(doc);
329 xmlCleanupParser();
331 return 0;