aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLinus Torvalds <torvalds@home.transmeta.com>2003-04-02 22:31:32 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-07 21:00:06 -0700
commit504bb50c98a1bfa07a1b89a75a02d77f9f94d49e (patch)
tree024b1a00a8ad6b2a4520d4433faa6460ea674f7c
parent73fa5bd563e8e4ee891f3b40d3ff8778329fe5ea (diff)
downloadsparse-dev-504bb50c98a1bfa07a1b89a75a02d77f9f94d49e.tar.gz
Oops. The preprocessor symbol evaluation printed out the wrong name.
Add "obfuscate" as a stupid back-end. It will generate some really really horrible C output if I get my way. Don't expose "attribute" for type attributes. Use only the double-underscore version
-rw-r--r--Makefile5
-rw-r--r--evaluate.c2
-rw-r--r--obfuscate.c80
-rw-r--r--symbol.c2
4 files changed, 87 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index d96c0bad..f5be9052 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-g -Wall
-PROGRAMS=test-lexing test-parsing
+PROGRAMS=test-lexing test-parsing obfuscate
HEADERS=token.h parse.h lib.h symbol.h scope.h expression.h
@@ -16,6 +16,9 @@ test-lexing: test-lexing.o $(COMMON)
test-parsing: test-parsing.o $(COMMON)
gcc -o $@ $< $(COMMON)
+obfuscate: obfuscate.o $(COMMON)
+ gcc -o $@ $< $(COMMON)
+
evaluate.o: $(HEADERS)
expression.o: $(HEADERS)
lib.o: $(HEADERS)
diff --git a/evaluate.c b/evaluate.c
index 12f90190..7755f0da 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1137,7 +1137,7 @@ long long get_expression_value(struct expression *expr)
case EXPR_SYMBOL: {
struct symbol *sym = expr->symbol;
if (!sym || !sym->ctype.base_type || sym->ctype.base_type->type != SYM_ENUM) {
- warn(expr->pos, "undefined identifier '%s' in constant expression", expr->symbol_name);
+ warn(expr->pos, "undefined identifier '%s' in constant expression", show_ident(expr->symbol_name));
return 0;
}
return sym->value;
diff --git a/obfuscate.c b/obfuscate.c
new file mode 100644
index 00000000..a16d7238
--- /dev/null
+++ b/obfuscate.c
@@ -0,0 +1,80 @@
+/*
+ * Example trivial client program that uses the sparse library
+ * to tokenize, pre-process and parse a C file, and prints out
+ * the results.
+ *
+ * Copyright (C) 2003 Transmeta Corp, all rights reserved.
+ */
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "lib.h"
+#include "token.h"
+#include "parse.h"
+#include "symbol.h"
+#include "expression.h"
+
+static void emit_blob(struct symbol *sym)
+{
+ int bit_size = sym->bit_size;
+
+ if (bit_size & 7)
+ warn(sym->pos, "emitting symbol of size %d\n", bit_size);
+ printf("unsigned char %s[%d]\n", show_ident(sym->ident), bit_size >> 3);
+}
+
+void emit_symbol(struct symbol *sym, void *_parent, int flags)
+{
+ struct symbol *ctype;
+
+ evaluate_symbol(sym);
+ ctype = sym->ctype.base_type;
+ if (!ctype)
+ return;
+ switch (ctype->type) {
+ case SYM_PTR:
+ case SYM_ARRAY:
+ case SYM_STRUCT:
+ case SYM_UNION:
+ case SYM_BASETYPE:
+ emit_blob(sym);
+ return;
+ default:
+ return;
+ }
+}
+
+int main(int argc, char **argv)
+{
+ int fd;
+ char *filename = argv[1];
+ struct token *token;
+ struct symbol_list *list = NULL;
+
+ // Initialize symbol stream first, so that we can add defines etc
+ init_symbols();
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ die("No such file: %s", argv[1]);
+
+ // Tokenize the input stream
+ token = tokenize(filename, fd, NULL);
+ close(fd);
+
+ // Pre-process the stream
+ token = preprocess(token);
+
+ // Parse the resulting C code
+ translation_unit(token, &list);
+
+ // Do type evaluation and simplify
+ symbol_iterate(list, emit_symbol, NULL);
+
+ return 0;
+}
diff --git a/symbol.c b/symbol.c
index edf36814..f1a0e6a9 100644
--- a/symbol.c
+++ b/symbol.c
@@ -307,8 +307,10 @@ struct sym_init {
{ "__typeof", NULL, MOD_TYPEOF },
{ "__typeof__", NULL, MOD_TYPEOF },
+#if 0
{ "attribute", NULL, MOD_ATTRIBUTE },
{ "__attribute", NULL, MOD_ATTRIBUTE },
+#endif
{ "__attribute__", NULL, MOD_ATTRIBUTE },
{ "struct", NULL, MOD_STRUCTOF },