2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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
27 ** MODULE DESCRIPTION:
29 ** This module contains the I/O routines for writing out the .lh file
30 ** generated from the .lcl file.
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"
48 /*@constant static char TABCH; @*/
51 /*@constant static char TABINCH; @*/
55 /*@private@*/ typedef struct
57 /*:open:*/ /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f;
58 /*@reldef@*/ cstring name;
62 static outFile LhFile;
63 static bool needIncludeBool = FALSE;
71 /* static int colpos (int startcol, cstring line); */
73 static cstring lhTypeSpecNode (lclTypeSpecNode p_typespec);
74 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr p_x);
75 static /*@only@*/ cstring lhDeclaratorNode (declaratorNode p_x);
78 lhFunction (lclTypeSpecNode lclTypeSpec, declaratorNode declarator)
83 return cstring_undefined;
85 s = message ("extern %q %q;", lhTypeSpecNode (lclTypeSpec),
86 lhDeclaratorNode (declarator));
91 static /*@only@*/ cstring
92 lhDeclaratorNode (declaratorNode x)
94 return (lhTypeExpr (x->type));
97 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr x)
99 cstring s = cstring_undefined; /* print out types in order of appearance in source */
100 paramNodeList params;
103 if (x != (typeExpr) 0)
105 cstring front = cstring_undefined;
106 cstring back = cstring_undefined;
108 for (i = x->wrapped; i >= 1; i--)
110 front = cstring_appendChar (front, '(');
111 back = cstring_appendChar (back, ')');
117 s = message ("%q%s", s, ltoken_getRawString (x->content.base));
120 s = message ("%q*%q", s, lhTypeExpr (x->content.pointer));
123 s = message ("%q%q[%q]", s,
124 lhTypeExpr (x->content.array.elementtype),
125 termNode_unparse (x->content.array.size));
128 s = message ("%q%q (", s, lhTypeExpr (x->content.function.returntype));
129 params = x->content.function.args;
131 if (!paramNodeList_empty (params))
133 s = message ("%q%q", s,
134 paramNodeList_unparseComments (x->content.function.args));
137 s = message ("%q)", s);
140 s = message ("%q%q%q", front, s, back);
144 s = cstring_makeLiteral ("?");
151 lhForwardStruct (ltoken t)
156 lhOutLine (message ("struct %s;", ltoken_unparse (t)));
161 lhForwardUnion (ltoken t)
166 lhOutLine (message ("union %s;", ltoken_unparse (t)));
169 extern /*@only@*/ cstring
173 return cstring_undefined;
175 if (t->kind == TK_EXPOSED)
177 exposedNode n = t->content.exposed;
179 if (n != (exposedNode) 0)
181 if (declaratorInvNodeList_size (n->decls) == 0)
184 ** Forward struct or union declaration
187 return (cstring_appendChar (lhTypeSpecNode (n->type), ';'));
191 cstring s = cstring_undefined;
193 declaratorInvNodeList_elements (n->decls, d)
195 cstring name = declaratorNode_unparse (d->declarator);
196 cstring pname = declaratorNode_unparseCode (d->declarator);
198 s = message ("%q\n# ifndef EXPOSED_TYPE_%q\ntypedef %q %q;\n# endif\n",
199 s, pname, lhTypeSpecNode (n->type), name);
200 } end_declaratorInvNodeList_elements;
207 return cstring_undefined;
210 static /*@only@*/ cstring
211 lhTypeSpecNode (lclTypeSpecNode typespec)
215 return cstring_undefined;
218 return (lclTypeSpecNode_unparseComments (typespec));
222 lhVarDecl (lclTypeSpecNode lclTypeSpec, initDeclNodeList initDecls,
223 qualifierKind qualifier)
229 return cstring_undefined;
231 s = cstring_makeLiteral ("extern");
238 s = message ("%q const", s);
241 s = message ("%q volatile", s);
243 default: /* ignore it */
247 s = message ("%q %q ", s, lhTypeSpecNode (lclTypeSpec));
249 initDeclNodeList_elements (initDecls, i)
253 s = message ("%q %q", s, declaratorNode_unparse (i->declarator));
258 s = message ("%q, %q", s, declaratorNode_unparse (i->declarator));
260 } end_initDeclNodeList_elements;
262 return (message ("%q;", s));
267 /*@modifies fileSystem@*/
275 llassert (LhFile.f != NULL);
277 if (LhFile.f == NULL)
279 /*@i25534 check this! does it report the right filename? */
280 lldiagmsg (message ("Cannot open lh file for output: %s", LhFile.name));
284 check (fprintf (LhFile.f, "/* Output from %s */\n", LCL_PARSE_VERSION) > 0);
285 check (fileTable_closeFile (context_fileTable (), LhFile.f));
291 /* Write an #include of bool.h if we have't done so already. */
295 needIncludeBool = TRUE;
298 void lhInit (inputStream f) /*@globals undef LhFile; @*/
300 static bool lherror = FALSE;
302 genLh = context_msgLh ();
303 needIncludeBool = FALSE;
310 LhFile.name = cstring_concatFree1 (LSLRootName (inputStream_fileName (f)),
312 LhFile.f = fileTable_openWriteUpdateFile (context_fileTable (), LhFile.name);
314 if (LhFile.f == NULL)
319 lclplainerror (message ("Cannot write temporary file: %s",
326 void lhOutLine (/*@only@*/ cstring s)
330 llassert (LhFile.f != NULL);
331 DPRINTF (("lhOutLine: %s / %s", s, LhFile.name));
333 if (cstring_length (s) > 0)
335 check (fputs (cstring_toCharsSafe (s), LhFile.f) != EOF);
338 check (fputc ('\n', LhFile.f) == (int) '\n');
344 void lhExternals (interfaceNodeList x)
348 llassert (LhFile.f != NULL);
351 ** Need to make sure all standard library includes come first.
354 interfaceNodeList_elements (x, el)
356 if (el->kind == INF_IMPORTS)
358 importNodeList imps = el->content.imports;
360 importNodeList_elements (imps, il)
362 if (il->kind == IMPBRACKET)
364 lhOutLine (message ("# include <%s.h>",
365 ltoken_getRawString (il->val)));
367 } end_importNodeList_elements ;
369 } end_interfaceNodeList_elements;
371 lhOutLine (cstring_makeLiteral ("# include \"bool.h\""));
373 interfaceNodeList_elements (x, el)
375 if (el->kind == INF_IMPORTS)
377 importNodeList imps = el->content.imports;
379 importNodeList_elements (imps, il)
381 if (il->kind != IMPBRACKET)
383 lhOutLine (message ("# include \"%s.h\"",
384 ltoken_getRawString (il->val)));
386 } end_importNodeList_elements ;
388 } end_interfaceNodeList_elements;
390 lhOutLine (cstring_undefined);