Don't bother with memset() on allocations, the chunking
[smatch.git] / obfuscate.c
blob203fcb43fed378d573b89b70655717e4f3ed82e5
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.
8 * Licensed under the Open Software License version 1.1
9 */
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <unistd.h>
16 #include <fcntl.h>
18 #include "lib.h"
19 #include "token.h"
20 #include "parse.h"
21 #include "symbol.h"
22 #include "expression.h"
24 static void emit_blob(struct symbol *sym)
26 int size = sym->bit_size;
27 int alignment = sym->ctype.alignment;
28 const char *name = show_ident(sym->ident);
30 if (size <= 0) {
31 warn(sym->pos, "emitting insized symbol");
32 size = 8;
34 if (size & 7)
35 warn(sym->pos, "emitting symbol of size %d bits\n", size);
36 size = (size+7) >> 3;
37 if (alignment < 1)
38 alignment = 1;
39 if (!(size & (alignment-1))) {
40 switch (alignment) {
41 case 1:
42 printf("unsigned char %s[%d];\n", name, size);
43 return;
44 case 2:
45 printf("unsigned short %s[%d];\n", name, (size+1) >> 1);
46 return;
47 case 4:
48 printf("unsigned int %s[%d];\n", name, (size+3) >> 2);
49 return;
52 printf("unsigned char %s[%d] __attribute__((aligned(%d)));\n",
53 name, size, alignment);
54 return;
57 static void emit_fn(struct symbol *sym)
59 const char *name = show_ident(sym->ident);
60 printf("%s();\n", name);
63 void emit_symbol(struct symbol *sym, void *_parent, int flags)
65 struct symbol *ctype;
67 evaluate_symbol(sym);
68 if (sym->type != SYM_NODE) {
69 warn(sym->pos, "I really want to emit nodes, not pure types!");
70 return;
73 ctype = sym->ctype.base_type;
74 if (!ctype)
75 return;
76 switch (ctype->type) {
77 case SYM_NODE:
78 case SYM_PTR:
79 case SYM_ARRAY:
80 case SYM_STRUCT:
81 case SYM_UNION:
82 case SYM_BASETYPE:
83 emit_blob(sym);
84 return;
85 case SYM_FN:
86 emit_fn(sym);
87 return;
88 default:
89 warn(sym->pos, "what kind of strange node do you want me to emit again?");
90 return;
94 int main(int argc, char **argv)
96 int fd;
97 char *filename = argv[1];
98 struct token *token;
99 struct symbol_list *list = NULL;
101 // Initialize symbol stream first, so that we can add defines etc
102 init_symbols();
104 fd = open(filename, O_RDONLY);
105 if (fd < 0)
106 die("No such file: %s", argv[1]);
108 // Tokenize the input stream
109 token = tokenize(filename, fd, NULL);
110 close(fd);
112 // Pre-process the stream
113 token = preprocess(token);
115 // Parse the resulting C code
116 translation_unit(token, &list);
118 // Do type evaluation and simplify
119 symbol_iterate(list, emit_symbol, NULL);
121 return 0;