Make our "__builtin_va_arg()" thing a bit closer to real.
[smatch.git] / obfuscate.c
blob806fa30b7533b534b4a59e739172838d978e647f
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 warning(sym->pos, "emitting insized symbol");
33 size = 8;
35 if (size & 7)
36 warning(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)
66 struct symbol *ctype;
68 evaluate_symbol(sym);
69 if (sym->type != SYM_NODE) {
70 warning(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 warning(sym->pos, "what kind of strange node do you want me to emit again?");
91 return;
95 static void emit_symbol_list(struct symbol_list *list)
97 struct symbol *sym;
99 FOR_EACH_PTR(list, sym) {
100 emit_symbol(sym);
101 } END_FOR_EACH_PTR(sym);
104 int main(int argc, char **argv)
106 int fd;
107 char *filename = argv[1];
108 struct token *token;
109 struct symbol_list *list = NULL;
111 // Initialize symbol stream first, so that we can add defines etc
112 init_symbols();
114 fd = open(filename, O_RDONLY);
115 if (fd < 0)
116 die("No such file: %s", filename);
118 // Initialize type system
119 init_ctype();
121 // Tokenize the input stream
122 token = tokenize(filename, fd, NULL, includepath);
123 close(fd);
125 // Pre-process the stream
126 token = preprocess(token);
128 // Parse the resulting C code
129 translation_unit(token, &list);
131 // Do type evaluation and simplify
132 emit_symbol_list(list);
134 return 0;