Get comparison sizes right.
[smatch.git] / check.c
blob4b79e0bf9c49cffd1fac2e06889fb4e1225222cb
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-2004 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 "allocate.h"
21 #include "token.h"
22 #include "parse.h"
23 #include "symbol.h"
24 #include "expression.h"
25 #include "linearize.h"
27 static int context_increase(struct basic_block *bb)
29 int sum = 0;
30 struct instruction *insn;
32 FOR_EACH_PTR(bb->insns, insn) {
33 if (insn->opcode == OP_CONTEXT)
34 sum += insn->increment;
35 } END_FOR_EACH_PTR(insn);
36 return sum;
39 static int imbalance(struct entrypoint *ep, struct basic_block *bb, int entry, int exit, const char *why)
41 if (Wcontext) {
42 struct symbol *sym = ep->name;
43 warning(bb->pos, "context imbalance in '%s' - %s", show_ident(sym->ident), why);
45 return -1;
48 static int check_bb_context(struct entrypoint *ep, struct basic_block *bb, int entry, int exit);
50 static int check_children(struct entrypoint *ep, struct basic_block *bb, int entry, int exit)
52 struct instruction *insn;
53 struct basic_block *child;
55 insn = last_instruction(bb->insns);
56 if (!insn)
57 return 0;
58 if (insn->opcode == OP_RET)
59 return entry != exit ? imbalance(ep, bb, entry, exit, "wrong count at exit") : 0;
61 FOR_EACH_PTR(bb->children, child) {
62 if (check_bb_context(ep, child, entry, exit))
63 return -1;
64 } END_FOR_EACH_PTR(child);
65 return 0;
68 static int check_bb_context(struct entrypoint *ep, struct basic_block *bb, int entry, int exit)
70 if (!bb)
71 return 0;
72 if (bb->context == entry)
73 return 0;
75 /* Now that's not good.. */
76 if (bb->context >= 0)
77 return imbalance(ep, bb, entry, bb->context, "different lock contexts for basic block");
79 bb->context = entry;
80 entry += context_increase(bb);
81 if (entry < 0)
82 return imbalance(ep, bb, entry, exit, "unexpected unlock");
84 return check_children(ep, bb, entry, exit);
87 static void check_context(struct entrypoint *ep)
89 struct symbol *sym = ep->name;
91 if (verbose && ep->entry->bb->needs) {
92 pseudo_t pseudo;
93 FOR_EACH_PTR(ep->entry->bb->needs, pseudo) {
94 if (pseudo->type != PSEUDO_ARG)
95 warning(sym->pos, "%s: possible uninitialized variable (%s)",
96 show_ident(sym->ident), show_pseudo(pseudo));
97 } END_FOR_EACH_PTR(pseudo);
100 check_bb_context(ep, ep->entry->bb, sym->ctype.in_context, sym->ctype.out_context);
103 static void check_symbols(struct symbol_list *list)
105 struct symbol *sym;
107 FOR_EACH_PTR(list, sym) {
108 struct entrypoint *ep;
110 expand_symbol(sym);
111 ep = linearize_symbol(sym);
112 if (ep)
113 check_context(ep);
114 } END_FOR_EACH_PTR(sym);
117 int main(int argc, char **argv)
119 // Expand, linearize and show it.
120 check_symbols(sparse(argc, argv));
121 return 0;