Make the "noderef" attribute work right.
[smatch.git] / obfuscate.c
blob06486f1efc529127ba5680c5b00207db33c94b34
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 // Tokenize the input stream
110 token = tokenize(filename, fd, NULL);
111 close(fd);
113 // Pre-process the stream
114 token = preprocess(token);
116 // Parse the resulting C code
117 translation_unit(token, &list);
119 // Do type evaluation and simplify
120 symbol_iterate(list, emit_symbol, NULL);
122 return 0;