2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
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 "lclintMacros.nf"
49 /*@constant static char TABCH; @*/
52 /*@constant static char TABINCH; @*/
55 /*@constant static char TABOUTCH; @*/
56 # define TABOUTCH '\3'
59 # define TAB fputc (TABCH, LhFile.f);
60 # define TABIN fputc (TABINCH, LhFile.f);
61 # define TABOUT fputc (TABOUTCH, LhFile.f);
64 /*@private@*/ typedef struct
66 /*@open@*/ /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f;
67 /*@reldef@*/ cstring name;
71 static outFile LhFile;
72 static bool needIncludeBool = FALSE;
80 /* static int colpos (int startcol, cstring line); */
82 static cstring lhTypeSpecNode (lclTypeSpecNode p_typespec);
83 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr p_x);
84 static /*@only@*/ cstring lhDeclaratorNode (declaratorNode p_x);
86 static /*@open@*/ /*@dependent@*/ /*@null@*/ FILE *out_open (cstring name, cstring suffix)
87 /*@modifies fileSystem@*/
89 cstring fullname = cstring_concat (name, suffix);
90 FILE *ret = fopen (cstring_toCharsSafe (fullname), "w+");
91 cstring_free (fullname);
96 lhFunction (lclTypeSpecNode lclTypeSpec, declaratorNode declarator)
101 return cstring_undefined;
103 s = message ("extern %q\1%q;", lhTypeSpecNode (lclTypeSpec),
104 lhDeclaratorNode (declarator));
109 static /*@only@*/ cstring
110 lhDeclaratorNode (declaratorNode x)
112 return (lhTypeExpr (x->type));
115 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr x)
117 cstring s = cstring_undefined; /* print out types in order of appearance in source */
118 paramNodeList params;
121 if (x != (typeExpr) 0)
123 cstring front = cstring_undefined;
124 cstring back = cstring_undefined;
126 for (i = x->wrapped; i >= 1; i--)
128 front = cstring_appendChar (front, '(');
129 back = cstring_appendChar (back, ')');
135 s = message ("%q%s", s, ltoken_getRawString (x->content.base));
138 s = message ("%q*%q", s, lhTypeExpr (x->content.pointer));
141 s = message ("%q%q[%q]", s,
142 lhTypeExpr (x->content.array.elementtype),
143 termNode_unparse (x->content.array.size));
146 s = message ("%q%q (", s, lhTypeExpr (x->content.function.returntype));
147 params = x->content.function.args;
149 if (!paramNodeList_empty (params))
151 s = message ("%q%q", s,
152 paramNodeList_unparseComments (x->content.function.args));
155 s = message ("%q)", s);
158 s = message ("%q%q%q", front, s, back);
162 s = cstring_makeLiteral ("?");
169 lhForwardStruct (ltoken t)
174 lhOutLine (message ("struct %s;", ltoken_unparse (t)));
179 lhForwardUnion (ltoken t)
184 lhOutLine (message ("union %s;", ltoken_unparse (t)));
187 extern /*@only@*/ cstring
191 return cstring_undefined;
193 if (t->kind == TK_EXPOSED)
195 exposedNode n = t->content.exposed;
197 if (n != (exposedNode) 0)
199 if (declaratorInvNodeList_size (n->decls) == 0)
202 ** Forward struct or union declaration
205 return (cstring_appendChar (lhTypeSpecNode (n->type), ';'));
209 cstring s = cstring_undefined;
211 declaratorInvNodeList_elements (n->decls, d)
213 cstring name = declaratorNode_unparse (d->declarator);
214 cstring pname = declaratorNode_unparseCode (d->declarator);
216 s = message ("%q\n# ifndef EXPOSED_TYPE_%q\ntypedef %q %q;\n# endif\n",
217 s, pname, lhTypeSpecNode (n->type), name);
218 } end_declaratorInvNodeList_elements;
225 return cstring_undefined;
228 static /*@only@*/ cstring
229 lhTypeSpecNode (lclTypeSpecNode typespec)
233 return cstring_undefined;
236 return (lclTypeSpecNode_unparseComments (typespec));
240 lhVarDecl (lclTypeSpecNode lclTypeSpec, initDeclNodeList initDecls,
241 qualifierKind qualifier)
247 return cstring_undefined;
249 s = cstring_makeLiteral ("extern");
256 s = message ("%q const", s);
259 s = message ("%q volatile", s);
261 default: /* ignore it */
265 s = message ("%q %q\1", s, lhTypeSpecNode (lclTypeSpec));
267 initDeclNodeList_elements (initDecls, i)
271 s = message ("%q %q", s, declaratorNode_unparse (i->declarator));
276 s = message ("%q, %q", s, declaratorNode_unparse (i->declarator));
278 } end_initDeclNodeList_elements;
280 return (message ("%q;", s));
285 /*@modifies fileSystem@*/
294 int c, col = 0, tabcol = 0;
297 f = out_open (LhFile.name, LH_EXTENSION);
298 llassert (LhFile.f != NULL);
300 fullname = cstring_concat (LhFile.name, LHTMP_EXTENSION);
304 /*@i25534 check this! does it report the right filename? */
305 lldiagmsg (message ("Cannot open lh file for output: %s",
310 fprintf (f, "/* Output from %s */\n", LCL_PARSE_VERSION);
314 while (EOF != (c = getc (LhFile.f)))
322 fprintf (f, "%*s", tabcol, "");
326 check (fputc (' ', f) == (int) ' ');
328 /*@switchbreak@*/ break;
332 /*@switchbreak@*/ break;
336 /*@switchbreak@*/ break;
340 check (fputc (c, f) == (int) c);
341 /*@switchbreak@*/ break;
345 check (fputc (c, f) == (int) c);
346 /*@switchbreak@*/ break;
350 check (fclose (f) == 0);
351 check (fclose (LhFile.f) == 0);
353 (void) osd_unlink (fullname);
357 cstring_free (fullname);
361 /* Write an #include of bool.h if we have't done so already. */
365 needIncludeBool = TRUE;
370 ** FUNCTIONAL DESCRIPTION:
372 ** Initialize the .lh file processing.
374 ** FORMAL PARAMETERS:
376 ** source * f: The source file, from which we compute the name of
379 ** bool outputLh: If true, produce a .lh file, otherwise don't.
387 ** The .lh file may be opened.
393 void lhInit (inputStream f) /*@globals undef LhFile; @*/
395 static bool lherror = FALSE;
397 genLh = context_msgLh ();
398 needIncludeBool = FALSE;
405 LhFile.name = LSLRootName (inputStream_fileName (f));
406 LhFile.f = out_open (LhFile.name, LHTMP_EXTENSION);
408 if (LhFile.f == NULL)
413 lclplainerror (message ("Cannot write temporary %s file: %s%s",
422 void lhOutLine (/*@only@*/ cstring s)
426 llassert (LhFile.f != NULL);
428 if (cstring_length (s) > 0)
430 check (fputs (cstring_toCharsSafe (s), LhFile.f) != EOF);
433 check (fputc ('\n', LhFile.f) == (int) '\n');
439 void lhExternals (interfaceNodeList x)
443 llassert (LhFile.f != NULL);
446 ** Need to make sure all standard library includes come first.
449 interfaceNodeList_elements (x, el)
451 if (el->kind == INF_IMPORTS)
453 importNodeList imps = el->content.imports;
455 importNodeList_elements (imps, il)
457 if (il->kind == IMPBRACKET)
459 lhOutLine (message ("# include <%s.h>",
460 ltoken_getRawString (il->val)));
462 } end_importNodeList_elements ;
464 } end_interfaceNodeList_elements;
466 lhOutLine (cstring_makeLiteral ("# include \"bool.h\""));
468 interfaceNodeList_elements (x, el)
470 if (el->kind == INF_IMPORTS)
472 importNodeList imps = el->content.imports;
474 importNodeList_elements (imps, il)
476 if (il->kind != IMPBRACKET)
478 lhOutLine (message ("# include \"%s.h\"",
479 ltoken_getRawString (il->val)));
481 } end_importNodeList_elements ;
483 } end_interfaceNodeList_elements;
485 lhOutLine (cstring_undefined);