Less permissive syntax for notreached comment.
[splint-patched.git] / src / lh.c
blobd6c8505d91238ad384b84b84116e02c1a06945dd
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
15 **
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
25 ** lh.c
27 ** MODULE DESCRIPTION:
29 ** This module contains the I/O routines for writing out the .lh file
30 ** generated from the .lcl file.
32 ** AUTHORS:
34 ** Gary Feldman, Technical Languages and Environments, DECspec project
35 ** Yang Meng Tan, MIT.
37 ** CREATION DATE: 9 April 91
39 ** The lh.c module controls formatting policy.
42 # include "splintMacros.nf"
43 # include "basic.h"
44 # include "osd.h"
45 # include "lh.h"
48 /*:private:*/ typedef struct
50 /*@open@*/ /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f;
51 /*@reldef@*/ cstring name;
52 } outFile;
54 static bool genLh;
55 static outFile LhFile;
59 ** FORWARD FUNCTIONS
63 /* static int colpos (int startcol, cstring line); */
65 static cstring lhTypeSpecNode (lclTypeSpecNode p_typespec);
66 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr p_x);
67 static /*@only@*/ cstring lhDeclaratorNode (declaratorNode p_x);
69 /*@only@*/ cstring
70 lhFunction (lclTypeSpecNode lclTypeSpec, declaratorNode declarator)
72 cstring s;
74 if (!genLh)
75 return cstring_undefined;
77 s = message ("extern %q %q;", lhTypeSpecNode (lclTypeSpec),
78 lhDeclaratorNode (declarator));
80 return s;
83 static /*@only@*/ cstring
84 lhDeclaratorNode (declaratorNode x)
86 return (lhTypeExpr (x->type));
89 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr x)
91 cstring s = cstring_undefined; /* print out types in order of appearance in source */
92 paramNodeList params;
93 int i;
95 if (x != (typeExpr) 0)
97 cstring front = cstring_undefined;
98 cstring back = cstring_undefined;
100 for (i = x->wrapped; i >= 1; i--)
102 front = cstring_appendChar (front, '(');
103 back = cstring_appendChar (back, ')');
106 switch (x->kind)
108 case TEXPR_BASE:
109 s = message ("%q%s", s, ltoken_getRawString (x->content.base));
110 break;
111 case TEXPR_PTR:
112 s = message ("%q*%q", s, lhTypeExpr (x->content.pointer));
113 break;
114 case TEXPR_ARRAY:
115 s = message ("%q%q[%q]", s,
116 lhTypeExpr (x->content.array.elementtype),
117 termNode_unparse (x->content.array.size));
118 break;
119 case TEXPR_FCN:
120 s = message ("%q%q (", s, lhTypeExpr (x->content.function.returntype));
121 params = x->content.function.args;
123 if (!paramNodeList_empty (params))
125 s = message ("%q%q", s,
126 paramNodeList_unparseComments (x->content.function.args));
129 s = message ("%q)", s);
130 break;
132 s = message ("%q%q%q", front, s, back);
134 else
136 s = cstring_makeLiteral ("?");
139 return s;
142 extern void
143 lhForwardStruct (ltoken t)
145 if (!genLh)
146 return;
148 lhOutLine (message ("struct %s;", ltoken_unparse (t)));
152 extern void
153 lhForwardUnion (ltoken t)
155 if (!genLh)
156 return;
158 lhOutLine (message ("union %s;", ltoken_unparse (t)));
161 extern /*@only@*/ cstring
162 lhType (typeNode t)
164 if (!genLh)
165 return cstring_undefined;
167 if (t->kind == TK_EXPOSED)
169 exposedNode n = t->content.exposed;
171 if (n != (exposedNode) 0)
173 if (declaratorInvNodeList_size (n->decls) == 0)
176 ** Forward struct or union declaration
179 return (cstring_appendChar (lhTypeSpecNode (n->type), ';'));
181 else
183 cstring s = cstring_undefined;
185 declaratorInvNodeList_elements (n->decls, d)
187 cstring name = declaratorNode_unparse (d->declarator);
188 cstring pname = declaratorNode_unparseCode (d->declarator);
190 s = message ("%q\n# ifndef EXPOSED_TYPE_%q\ntypedef %q %q;\n# endif\n",
191 s, pname, lhTypeSpecNode (n->type), name);
192 } end_declaratorInvNodeList_elements;
194 return s;
199 return cstring_undefined;
202 static /*@only@*/ cstring
203 lhTypeSpecNode (lclTypeSpecNode typespec)
205 if (!genLh)
207 return cstring_undefined;
210 return (lclTypeSpecNode_unparseComments (typespec));
213 /*@only@*/ cstring
214 lhVarDecl (lclTypeSpecNode lclTypeSpec, initDeclNodeList initDecls,
215 qualifierKind qualifier)
217 bool first = TRUE;
218 cstring s;
220 if (!genLh)
221 return cstring_undefined;
223 s = cstring_makeLiteral ("extern");
225 switch (qualifier)
227 case QLF_NONE:
228 break;
229 case QLF_CONST:
230 s = message ("%q const", s);
231 break;
232 case QLF_VOLATILE:
233 s = message ("%q volatile", s);
234 break;
235 default: /* ignore it */
236 break;
239 s = message ("%q %q ", s, lhTypeSpecNode (lclTypeSpec));
241 initDeclNodeList_elements (initDecls, i)
243 if (first)
245 s = message ("%q %q", s, declaratorNode_unparse (i->declarator));
246 first = FALSE;
248 else
250 s = message ("%q, %q", s, declaratorNode_unparse (i->declarator));
252 } end_initDeclNodeList_elements;
254 return (message ("%q;", s));
257 extern void
258 lhCleanup (void)
259 /*@modifies fileSystem@*/
261 if (!genLh)
263 return;
265 else
267 llassert (LhFile.f != NULL);
269 if (LhFile.f == NULL)
271 lldiagmsg (message ("Cannot open lh file for output: %s", LhFile.name));
273 else
275 check (fprintf (LhFile.f, "/* Output from %s */\n", LCL_PARSE_VERSION) > 0);
276 check (fileTable_closeFile (context_fileTable (), LhFile.f));
277 LhFile.f = NULL;
282 static cstring
283 LSLRootName (cstring filespec)
285 /*@access cstring@*/
286 char *result, *startName, *tail;
287 size_t nameLength;
289 llassert (cstring_isDefined (filespec));
290 tail = strrchr (filespec, CONNECTCHAR);
291 startName = (tail == NULL ? filespec : &tail[1]);
292 tail = strrchr (startName, '.');
293 nameLength = (tail == NULL ? strlen (startName)
294 : size_fromInt (tail - startName));
295 result = dmalloc (nameLength + 1);
296 strncpy (result, startName, nameLength);
297 result[(int) nameLength] = '\0';
298 return result;
299 /*@noaccess cstring@*/
302 void lhInit (inputStream f) /*@globals undef LhFile; @*/
304 static bool lherror = FALSE;
306 genLh = context_msgLh ();
308 if (!genLh)
310 return;
313 LhFile.name = cstring_concatFree1 (LSLRootName (inputStream_fileName (f)),
314 LH_EXTENSION);
315 LhFile.f = fileTable_openWriteUpdateFile (context_fileTable (), LhFile.name);
317 if (LhFile.f == NULL)
319 genLh = FALSE;
320 if (!lherror)
322 lclplainerror (message ("Cannot write temporary file: %s",
323 LhFile.name));
324 lherror = TRUE;
329 void lhOutLine (/*@only@*/ cstring s)
331 if (genLh)
333 llassert (LhFile.f != NULL);
334 DPRINTF (("lhOutLine: %s / %s", s, LhFile.name));
336 if (cstring_length (s) > 0)
338 check (fputs (cstring_toCharsSafe (s), LhFile.f) != EOF);
341 check (fputc ('\n', LhFile.f) == (int) '\n');
344 cstring_free (s);
347 void lhExternals (interfaceNodeList x)
349 if (genLh)
351 llassert (LhFile.f != NULL);
354 ** Need to make sure all standard library includes come first.
357 interfaceNodeList_elements (x, el)
359 if (el->kind == INF_IMPORTS)
361 importNodeList imps = el->content.imports;
363 importNodeList_elements (imps, il)
365 if (il->kind == IMPBRACKET)
367 lhOutLine (message ("# include <%s.h>",
368 ltoken_getRawString (il->val)));
370 } end_importNodeList_elements ;
372 } end_interfaceNodeList_elements;
374 lhOutLine (cstring_makeLiteral ("# include \"bool.h\""));
376 interfaceNodeList_elements (x, el)
378 if (el->kind == INF_IMPORTS)
380 importNodeList imps = el->content.imports;
382 importNodeList_elements (imps, il)
384 if (il->kind != IMPBRACKET)
386 lhOutLine (message ("# include \"%s.h\"",
387 ltoken_getRawString (il->val)));
389 } end_importNodeList_elements ;
391 } end_interfaceNodeList_elements;
393 lhOutLine (cstring_undefined);