2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
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; @*/
54 /*@constant static char TABOUTCH; @*/
55 # define TABOUTCH '\3'
58 # define TAB fputc (TABCH, LhFile.f);
59 # define TABIN fputc (TABINCH, LhFile.f);
60 # define TABOUT fputc (TABOUTCH, LhFile.f);
63 /*@private@*/ typedef struct
65 /*:open:*/ /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f;
66 /*@reldef@*/ cstring name;
70 static outFile LhFile;
71 static bool needIncludeBool = FALSE;
79 /* static int colpos (int startcol, cstring line); */
81 static cstring lhTypeSpecNode (lclTypeSpecNode p_typespec);
82 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr p_x);
83 static /*@only@*/ cstring lhDeclaratorNode (declaratorNode p_x);
85 static /*:open:*/ /*@dependent@*/ /*@null@*/ FILE *out_open (cstring name, cstring suffix)
86 /*@modifies fileSystem@*/
88 cstring fullname = cstring_concat (name, suffix);
89 FILE *ret = fileTable_openFile (context_fileTable (), fullname, "w+");
90 cstring_free (fullname);
95 lhFunction (lclTypeSpecNode lclTypeSpec, declaratorNode declarator)
100 return cstring_undefined;
102 s = message ("extern %q\1%q;", lhTypeSpecNode (lclTypeSpec),
103 lhDeclaratorNode (declarator));
108 static /*@only@*/ cstring
109 lhDeclaratorNode (declaratorNode x)
111 return (lhTypeExpr (x->type));
114 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr x)
116 cstring s = cstring_undefined; /* print out types in order of appearance in source */
117 paramNodeList params;
120 if (x != (typeExpr) 0)
122 cstring front = cstring_undefined;
123 cstring back = cstring_undefined;
125 for (i = x->wrapped; i >= 1; i--)
127 front = cstring_appendChar (front, '(');
128 back = cstring_appendChar (back, ')');
134 s = message ("%q%s", s, ltoken_getRawString (x->content.base));
137 s = message ("%q*%q", s, lhTypeExpr (x->content.pointer));
140 s = message ("%q%q[%q]", s,
141 lhTypeExpr (x->content.array.elementtype),
142 termNode_unparse (x->content.array.size));
145 s = message ("%q%q (", s, lhTypeExpr (x->content.function.returntype));
146 params = x->content.function.args;
148 if (!paramNodeList_empty (params))
150 s = message ("%q%q", s,
151 paramNodeList_unparseComments (x->content.function.args));
154 s = message ("%q)", s);
157 s = message ("%q%q%q", front, s, back);
161 s = cstring_makeLiteral ("?");
168 lhForwardStruct (ltoken t)
173 lhOutLine (message ("struct %s;", ltoken_unparse (t)));
178 lhForwardUnion (ltoken t)
183 lhOutLine (message ("union %s;", ltoken_unparse (t)));
186 extern /*@only@*/ cstring
190 return cstring_undefined;
192 if (t->kind == TK_EXPOSED)
194 exposedNode n = t->content.exposed;
196 if (n != (exposedNode) 0)
198 if (declaratorInvNodeList_size (n->decls) == 0)
201 ** Forward struct or union declaration
204 return (cstring_appendChar (lhTypeSpecNode (n->type), ';'));
208 cstring s = cstring_undefined;
210 declaratorInvNodeList_elements (n->decls, d)
212 cstring name = declaratorNode_unparse (d->declarator);
213 cstring pname = declaratorNode_unparseCode (d->declarator);
215 s = message ("%q\n# ifndef EXPOSED_TYPE_%q\ntypedef %q %q;\n# endif\n",
216 s, pname, lhTypeSpecNode (n->type), name);
217 } end_declaratorInvNodeList_elements;
224 return cstring_undefined;
227 static /*@only@*/ cstring
228 lhTypeSpecNode (lclTypeSpecNode typespec)
232 return cstring_undefined;
235 return (lclTypeSpecNode_unparseComments (typespec));
239 lhVarDecl (lclTypeSpecNode lclTypeSpec, initDeclNodeList initDecls,
240 qualifierKind qualifier)
246 return cstring_undefined;
248 s = cstring_makeLiteral ("extern");
255 s = message ("%q const", s);
258 s = message ("%q volatile", s);
260 default: /* ignore it */
264 s = message ("%q %q\1", s, lhTypeSpecNode (lclTypeSpec));
266 initDeclNodeList_elements (initDecls, i)
270 s = message ("%q %q", s, declaratorNode_unparse (i->declarator));
275 s = message ("%q, %q", s, declaratorNode_unparse (i->declarator));
277 } end_initDeclNodeList_elements;
279 return (message ("%q;", s));
284 /*@modifies fileSystem@*/
293 int c, col = 0, tabcol = 0;
296 f = out_open (LhFile.name, LH_EXTENSION);
297 llassert (LhFile.f != NULL);
299 fullname = cstring_concat (LhFile.name, LHTMP_EXTENSION);
303 /*@i25534 check this! does it report the right filename? */
304 lldiagmsg (message ("Cannot open lh file for output: %s",
309 fprintf (f, "/* Output from %s */\n", LCL_PARSE_VERSION);
313 while (EOF != (c = getc (LhFile.f)))
321 fprintf (f, "%*s", tabcol, "");
325 check (fputc (' ', f) == (int) ' ');
327 /*@switchbreak@*/ break;
331 /*@switchbreak@*/ break;
335 /*@switchbreak@*/ break;
339 check (fputc (c, f) == (int) c);
340 /*@switchbreak@*/ break;
344 check (fputc (c, f) == (int) c);
345 /*@switchbreak@*/ break;
349 check (fileTable_closeFile (context_fileTable (), f));
350 check (fileTable_closeFile (context_fileTable (), LhFile.f));
352 (void) osd_unlink (fullname);
356 cstring_free (fullname);
360 /* Write an #include of bool.h if we have't done so already. */
364 needIncludeBool = TRUE;
369 ** FUNCTIONAL DESCRIPTION:
371 ** Initialize the .lh file processing.
373 ** FORMAL PARAMETERS:
375 ** source * f: The source file, from which we compute the name of
378 ** bool outputLh: If true, produce a .lh file, otherwise don't.
386 ** The .lh file may be opened.
392 void lhInit (inputStream f) /*@globals undef LhFile; @*/
394 static bool lherror = FALSE;
396 genLh = context_msgLh ();
397 needIncludeBool = FALSE;
404 LhFile.name = LSLRootName (inputStream_fileName (f));
405 LhFile.f = out_open (LhFile.name, LHTMP_EXTENSION);
407 if (LhFile.f == NULL)
412 lclplainerror (message ("Cannot write temporary %s file: %s%s",
421 void lhOutLine (/*@only@*/ cstring s)
425 llassert (LhFile.f != NULL);
427 if (cstring_length (s) > 0)
429 check (fputs (cstring_toCharsSafe (s), LhFile.f) != EOF);
432 check (fputc ('\n', LhFile.f) == (int) '\n');
438 void lhExternals (interfaceNodeList x)
442 llassert (LhFile.f != NULL);
445 ** Need to make sure all standard library includes come first.
448 interfaceNodeList_elements (x, el)
450 if (el->kind == INF_IMPORTS)
452 importNodeList imps = el->content.imports;
454 importNodeList_elements (imps, il)
456 if (il->kind == IMPBRACKET)
458 lhOutLine (message ("# include <%s.h>",
459 ltoken_getRawString (il->val)));
461 } end_importNodeList_elements ;
463 } end_interfaceNodeList_elements;
465 lhOutLine (cstring_makeLiteral ("# include \"bool.h\""));
467 interfaceNodeList_elements (x, el)
469 if (el->kind == INF_IMPORTS)
471 importNodeList imps = el->content.imports;
473 importNodeList_elements (imps, il)
475 if (il->kind != IMPBRACKET)
477 lhOutLine (message ("# include \"%s.h\"",
478 ltoken_getRawString (il->val)));
480 } end_importNodeList_elements ;
482 } end_interfaceNodeList_elements;
484 lhOutLine (cstring_undefined);