libm: Exclude -Woverflow from -Werror in s_csqrtl.c for gcc47/i386.
[dragonfly.git] / usr.bin / xlint / lint2 / emit2.c
1 /*      $NetBSD: emit2.c,v 1.2 1995/07/03 21:24:44 cgd Exp $    */
2
3 /*
4  * Copyright (c) 1994, 1995 Jochen Pohl
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Jochen Pohl for
18  *      The NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $NetBSD: emit2.c,v 1.2 1995/07/03 21:24:44 cgd Exp $
34  * $DragonFly: src/usr.bin/xlint/lint2/emit2.c,v 1.5 2004/07/07 12:13:26 asmodai Exp $
35  */
36
37 #include <err.h>
38
39 #include "lint2.h"
40
41 static  void    outtype(type_t *);
42 static  void    outdef(hte_t *, sym_t *);
43 static  void    dumpname(hte_t *);
44
45 /*
46  * Write type into the output buffer.
47  */
48 static void
49 outtype(type_t *tp)
50 {
51         int     t, s, na;
52         tspec_t ts;
53         type_t  **ap;
54
55         while (tp != NULL) {
56                 if ((ts = tp->t_tspec) == INT && tp->t_isenum)
57                         ts = ENUM;
58                 switch (ts) {
59                 case CHAR:      t = 'C';        s = '\0';       break;
60                 case SCHAR:     t = 'C';        s = 's';        break;
61                 case UCHAR:     t = 'C';        s = 'u';        break;
62                 case SHORT:     t = 'S';        s = '\0';       break;
63                 case USHORT:    t = 'S';        s = 'u';        break;
64                 case INT:       t = 'I';        s = '\0';       break;
65                 case UINT:      t = 'I';        s = 'u';        break;
66                 case LONG:      t = 'L';        s = '\0';       break;
67                 case ULONG:     t = 'L';        s = 'u';        break;
68                 case QUAD:      t = 'Q';        s = '\0';       break;
69                 case UQUAD:     t = 'Q';        s = 'u';        break;
70                 case FLOAT:     t = 'D';        s = 's';        break;
71                 case DOUBLE:    t = 'D';        s = '\0';       break;
72                 case LDOUBLE:   t = 'D';        s = 'l';        break;
73                 case VOID:      t = 'V';        s = '\0';       break;
74                 case PTR:       t = 'P';        s = '\0';       break;
75                 case ARRAY:     t = 'A';        s = '\0';       break;
76                 case ENUM:      t = 'T';        s = 'e';        break;
77                 case STRUCT:    t = 'T';        s = 's';        break;
78                 case UNION:     t = 'T';        s = 'u';        break;
79                 case FUNC:
80                         if (tp->t_args != NULL && !tp->t_proto) {
81                                 t = 'f';
82                         } else {
83                                 t = 'F';
84                         }
85                         s = '\0';
86                         break;
87                 default:
88                         errx(1, "internal error: outtype() 1");
89                 }
90                 if (tp->t_const)
91                         outchar('c');
92                 if (tp->t_volatile)
93                         outchar('v');
94                 if (s != '\0')
95                         outchar(s);
96                 outchar(t);
97                 if (ts == ARRAY) {
98                         outint(tp->t_dim);
99                 } else if (ts == ENUM || ts == STRUCT || ts == UNION) {
100                         if (tp->t_istag) {
101                                 outint(1);
102                                 outname(tp->t_tag->h_name);
103                         } else if (tp->t_istynam) {
104                                 outint(2);
105                                 outname(tp->t_tynam->h_name);
106                         } else {
107                                 outint(0);
108                         }
109                 } else if (ts == FUNC && tp->t_args != NULL) {
110                         na = 0;
111                         for (ap = tp->t_args; *ap != NULL; ap++)
112                                 na++;
113                         if (tp->t_vararg)
114                                 na++;
115                         outint(na);
116                         for (ap = tp->t_args; *ap != NULL; ap++)
117                                 outtype(*ap);
118                         if (tp->t_vararg)
119                                 outchar('E');
120                 }
121                 tp = tp->t_subt;
122         }
123 }
124
125 /*
126  * Write a definition.
127  */
128 static void
129 outdef(hte_t *hte, sym_t *sym)
130 {
131         /* reset output buffer */
132         outclr();
133
134         /* line number in C source file */
135         outint(0);
136
137         /* this is a definition */
138         outchar('d');
139
140         /* index of file where symbol was defined and line number of def. */
141         outint(0);
142         outchar('.');
143         outint(0);
144
145         /* flags */
146         if (sym->s_va) {
147                 outchar('v');           /* varargs */
148                 outint(sym->s_nva);
149         }
150         if (sym->s_scfl) {
151                 outchar('S');           /* scanflike */
152                 outint(sym->s_nscfl);
153         }
154         if (sym->s_prfl) {
155                 outchar('P');           /* printflike */
156                 outint(sym->s_nprfl);
157         }
158         /* definition or tentative definition */
159         outchar(sym->s_def == DEF ? 'd' : 't');
160         if (TP(sym->s_type)->t_tspec == FUNC) {
161                 if (sym->s_rval)
162                         outchar('r');   /* fkt. has return value */
163                 if (sym->s_osdef)
164                         outchar('o');   /* old style definition */
165         }
166         outchar('u');                   /* used (no warning if not used) */
167
168         /* name */
169         outname(hte->h_name);
170
171         /* type */
172         outtype(TP(sym->s_type));
173 }
174
175 /*
176  * Write the first definition of a name into the lint library.
177  */
178 static void
179 dumpname(hte_t *hte)
180 {
181         sym_t   *sym, *def;
182
183         /* static and undefined symbols are not written */
184         if (hte->h_static || !hte->h_def)
185                 return;
186
187         /*
188          * If there is a definition, write it. Otherwise write a tentative
189          * definition. This is neccessary because more than one tentative
190          * definition is allowed (except with sflag).
191          */
192         def = NULL;
193         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
194                 if (sym->s_def == DEF) {
195                         def = sym;
196                         break;
197                 }
198                 if (sym->s_def == TDEF && def == NULL)
199                         def = sym;
200         }
201         if (def == NULL)
202                 errx(1, "internal error: dumpname() %s", hte->h_name);
203
204         outdef(hte, def);
205 }
206
207 /*
208  * Write a new lint library.
209  */
210 void
211 outlib(const char *name)
212 {
213         /* Open of output file and initialisation of the output buffer */
214         outopen(name);
215
216         /* write name of lint library */
217         outsrc(name);
218
219         /* name of lint lib has index 0 */
220         outclr();
221         outint(0);
222         outchar('s');
223         outstrg(name);
224
225         /* write all definitions with external linkage */
226         forall(dumpname);
227
228         /* close the output */
229         outclose();
230 }