2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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'
58 /*@constant static observer char *LHTMP_SUFFIX; @*/
59 # define LHTMP_SUFFIX ".lh_tmp"
62 # define TAB fputc (TABCH, LhFile.f);
63 # define TABIN fputc (TABINCH, LhFile.f);
64 # define TABOUT fputc (TABOUTCH, LhFile.f);
69 /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f;
70 /*@reldef@*/ char *name;
74 static outFile LhFile;
75 static bool needIncludeBool = FALSE;
83 /* static int colpos (int startcol, cstring line); */
85 static cstring lhTypeSpecNode (lclTypeSpecNode p_typespec);
86 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr p_x);
87 static /*@only@*/ cstring lhDeclaratorNode (declaratorNode p_x);
89 static /*@dependent@*/ /*@null@*/ FILE *out_open (char *name, char *suffix) /*@modifies fileSystem@*/
91 char *fullname = (char *)
92 mstring_create (size_toInt (strlen (name) + strlen (suffix)));
95 strcpy (fullname, name);
96 strcat (fullname, suffix);
97 ret = fopen (fullname, "w+");
103 lhFunction (lclTypeSpecNode lclTypeSpec, declaratorNode declarator)
108 return cstring_undefined;
110 s = message ("extern %q\1%q;", lhTypeSpecNode (lclTypeSpec),
111 lhDeclaratorNode (declarator));
116 static /*@only@*/ cstring
117 lhDeclaratorNode (declaratorNode x)
119 return (lhTypeExpr (x->type));
122 static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr x)
124 cstring s = cstring_undefined; /* print out types in order of appearance in source */
125 paramNodeList params;
128 if (x != (typeExpr) 0)
130 cstring front = cstring_undefined;
131 cstring back = cstring_undefined;
133 for (i = x->wrapped; i >= 1; i--)
135 front = cstring_appendChar (front, '(');
136 back = cstring_appendChar (back, ')');
142 s = message ("%q%s", s, ltoken_getRawString (x->content.base));
145 s = message ("%q*%q", s, lhTypeExpr (x->content.pointer));
148 s = message ("%q%q[%q]", s,
149 lhTypeExpr (x->content.array.elementtype),
150 termNode_unparse (x->content.array.size));
153 s = message ("%q%q (", s, lhTypeExpr (x->content.function.returntype));
154 params = x->content.function.args;
156 if (!paramNodeList_empty (params))
158 s = message ("%q%q", s,
159 paramNodeList_unparseComments (x->content.function.args));
162 s = message ("%q)", s);
165 s = message ("%q%q%q", front, s, back);
169 s = cstring_makeLiteral ("?");
176 lhForwardStruct (ltoken t)
181 lhOutLine (message ("struct %s;", ltoken_unparse (t)));
186 lhForwardUnion (ltoken t)
191 lhOutLine (message ("union %s;", ltoken_unparse (t)));
194 extern /*@only@*/ cstring
198 return cstring_undefined;
200 if (t->kind == TK_EXPOSED)
202 exposedNode n = t->content.exposed;
204 if (n != (exposedNode) 0)
206 if (declaratorInvNodeList_size (n->decls) == 0)
209 ** Forward struct or union declaration
212 return (cstring_appendChar (lhTypeSpecNode (n->type), ';'));
216 cstring s = cstring_undefined;
218 declaratorInvNodeList_elements (n->decls, d)
220 cstring name = declaratorNode_unparse (d->declarator);
221 cstring pname = declaratorNode_unparseCode (d->declarator);
223 s = message ("%q\n# ifndef EXPOSED_TYPE_%q\ntypedef %q %q;\n# endif\n",
224 s, pname, lhTypeSpecNode (n->type), name);
225 } end_declaratorInvNodeList_elements;
232 return cstring_undefined;
235 static /*@only@*/ cstring
236 lhTypeSpecNode (lclTypeSpecNode typespec)
240 return cstring_undefined;
243 return (lclTypeSpecNode_unparseComments (typespec));
247 lhVarDecl (lclTypeSpecNode lclTypeSpec, initDeclNodeList initDecls,
248 qualifierKind qualifier)
254 return cstring_undefined;
256 s = cstring_makeLiteral ("extern");
263 s = message ("%q const", s);
266 s = message ("%q volatile", s);
268 default: /* ignore it */
272 s = message ("%q %q\1", s, lhTypeSpecNode (lclTypeSpec));
274 initDeclNodeList_elements (initDecls, i)
278 s = message ("%q %q", s, declaratorNode_unparse (i->declarator));
283 s = message ("%q, %q", s, declaratorNode_unparse (i->declarator));
285 } end_initDeclNodeList_elements;
287 return (message ("%q;", s));
292 /*@modifies fileSystem@*/
301 int c, col = 0, tabcol = 0;
305 fullname = mstring_create
306 (size_toInt (strlen (LhFile.name) + strlen (LHTMP_SUFFIX)));
308 f = out_open (LhFile.name, LH_SUFFIX);
309 llassert (LhFile.f != NULL);
311 strcpy (fullname, LhFile.name);
312 strcat (fullname, LHTMP_SUFFIX);
316 lldiagmsg (message ("Cannot open lh file for output: %s",
317 cstring_fromChars (fullname)));
321 fprintf (f, "/* Output from %s */\n", LCL_PARSE_VERSION);
325 while (EOF != (c = getc (LhFile.f)))
333 fprintf (f, "%*s", tabcol, "");
337 check (fputc (' ', f) == (int) ' ');
339 /*@switchbreak@*/ break;
343 /*@switchbreak@*/ break;
347 /*@switchbreak@*/ break;
351 check (fputc (c, f) == (int) c);
352 /*@switchbreak@*/ break;
356 check (fputc (c, f) == (int) c);
357 /*@switchbreak@*/ break;
361 check (fclose (f) == 0);
362 check (fclose (LhFile.f) == 0);
364 (void) osd_unlink (fullname);
372 /* Write an #include of bool.h if we have't done so already. */
376 needIncludeBool = TRUE;
381 ** FUNCTIONAL DESCRIPTION:
383 ** Initialize the .lh file processing.
385 ** FORMAL PARAMETERS:
387 ** source * f: The source file, from which we compute the name of
390 ** bool outputLh: If true, produce a .lh file, otherwise don't.
398 ** The .lh file may be opened.
404 void lhInit (tsource * f) /*@globals undef LhFile; @*/
406 static bool lherror = FALSE;
408 genLh = context_msgLh ();
409 needIncludeBool = FALSE;
416 LhFile.name = LSLRootName (tsource_fileName (f));
417 LhFile.f = out_open (LhFile.name, LHTMP_SUFFIX);
419 if (LhFile.f == NULL)
424 lclplainerror (message ("Cannot write .lh file: %s.lh",
425 cstring_fromChars (LhFile.name)));
431 void lhOutLine (/*@only@*/ cstring s)
435 llassert (LhFile.f != NULL);
437 if (cstring_length (s) > 0)
439 check (fputs (cstring_toCharsSafe (s), LhFile.f) != EOF);
442 check (fputc ('\n', LhFile.f) == (int) '\n');
448 void lhExternals (interfaceNodeList x)
452 llassert (LhFile.f != NULL);
455 ** Need to make sure all standard library includes come first.
458 interfaceNodeList_elements (x, el)
460 if (el->kind == INF_IMPORTS)
462 importNodeList imps = el->content.imports;
464 importNodeList_elements (imps, il)
466 if (il->kind == IMPBRACKET)
468 lhOutLine (message ("# include <%s.h>",
469 ltoken_getRawString (il->val)));
471 } end_importNodeList_elements ;
473 } end_interfaceNodeList_elements;
475 lhOutLine (cstring_makeLiteral ("# include \"bool.h\""));
477 interfaceNodeList_elements (x, el)
479 if (el->kind == INF_IMPORTS)
481 importNodeList imps = el->content.imports;
483 importNodeList_elements (imps, il)
485 if (il->kind != IMPBRACKET)
487 lhOutLine (message ("# include \"%s.h\"",
488 ltoken_getRawString (il->val)));
490 } end_importNodeList_elements ;
492 } end_interfaceNodeList_elements;
494 lhOutLine (cstring_undefined);