User-defined types can mix with storage specifiers, so don't
[smatch.git] / obfuscate.c
blobff283f5702c036366d61e4c8267d1f69cba91946
1 /*
2 * Example trivial client program that uses the sparse library
3 * to tokenize, pre-process and parse a C file, and prints out
4 * the results.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
19 #include "lib.h"
20 #include "token.h"
21 #include "parse.h"
22 #include "symbol.h"
23 #include "expression.h"
25 static void emit_blob(struct symbol *sym)
27 int size = sym->bit_size;
28 int alignment = sym->ctype.alignment;
29 const char *name = show_ident(sym->ident);
31 if (size <= 0) {
32 warn(sym->pos, "emitting insized symbol");
33 size = 8;
35 if (size & 7)
36 warn(sym->pos, "emitting symbol of size %d bits\n", size);
37 size = (size+7) >> 3;
38 if (alignment < 1)
39 alignment = 1;
40 if (!(size & (alignment-1))) {
41 switch (alignment) {
42 case 1:
43 printf("unsigned char %s[%d];\n", name, size);
44 return;
45 case 2:
46 printf("unsigned short %s[%d];\n", name, (size+1) >> 1);
47 return;
48 case 4:
49 printf("unsigned int %s[%d];\n", name, (size+3) >> 2);
50 return;
53 printf("unsigned char %s[%d] __attribute__((aligned(%d)));\n",
54 name, size, alignment);
55 return;
58 static void emit_fn(struct symbol *sym)
60 const char *name = show_ident(sym->ident);
61 printf("%s();\n", name);
64 void emit_symbol(struct symbol *sym, void *_parent, int flags)
66 struct symbol *ctype;
68 evaluate_symbol(sym);
69 if (sym->type != SYM_NODE) {
70 warn(sym->pos, "I really want to emit nodes, not pure types!");
71 return;
74 ctype = sym->ctype.base_type;
75 if (!ctype)
76 return;
77 switch (ctype->type) {
78 case SYM_NODE:
79 case SYM_PTR:
80 case SYM_ARRAY:
81 case SYM_STRUCT:
82 case SYM_UNION:
83 case SYM_BASETYPE:
84 emit_blob(sym);
85 return;
86 case SYM_FN:
87 emit_fn(sym);
88 return;
89 default:
90 warn(sym->pos, "what kind of strange node do you want me to emit again?");
91 return;
95 int main(int argc, char **argv)
97 int fd;
98 char *filename = argv[1];
99 struct token *token;
100 struct symbol_list *list = NULL;
102 // Initialize symbol stream first, so that we can add defines etc
103 init_symbols();
105 fd = open(filename, O_RDONLY);
106 if (fd < 0)
107 die("No such file: %s", argv[1]);
109 // Initialize type system
110 init_ctype();
112 // Tokenize the input stream
113 token = tokenize(filename, fd, NULL);
114 close(fd);
116 // Pre-process the stream
117 token = preprocess(token);
119 // Parse the resulting C code
120 translation_unit(token, &list);
122 // Do type evaluation and simplify
123 symbol_iterate(list, emit_symbol, NULL);
125 return 0;