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 /*:private:*/ typedef struct
50 /*@open@*/ /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f
;
51 /*@reldef@*/ cstring name
;
55 static outFile LhFile
;
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
);
70 lhFunction (lclTypeSpecNode lclTypeSpec
, declaratorNode declarator
)
75 return cstring_undefined
;
77 s
= message ("extern %q %q;", lhTypeSpecNode (lclTypeSpec
),
78 lhDeclaratorNode (declarator
));
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 */
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
, ')');
109 s
= message ("%q%s", s
, ltoken_getRawString (x
->content
.base
));
112 s
= message ("%q*%q", s
, lhTypeExpr (x
->content
.pointer
));
115 s
= message ("%q%q[%q]", s
,
116 lhTypeExpr (x
->content
.array
.elementtype
),
117 termNode_unparse (x
->content
.array
.size
));
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
);
132 s
= message ("%q%q%q", front
, s
, back
);
136 s
= cstring_makeLiteral ("?");
143 lhForwardStruct (ltoken t
)
148 lhOutLine (message ("struct %s;", ltoken_unparse (t
)));
153 lhForwardUnion (ltoken t
)
158 lhOutLine (message ("union %s;", ltoken_unparse (t
)));
161 extern /*@only@*/ cstring
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
), ';'));
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
;
199 return cstring_undefined
;
202 static /*@only@*/ cstring
203 lhTypeSpecNode (lclTypeSpecNode typespec
)
207 return cstring_undefined
;
210 return (lclTypeSpecNode_unparseComments (typespec
));
214 lhVarDecl (lclTypeSpecNode lclTypeSpec
, initDeclNodeList initDecls
,
215 qualifierKind qualifier
)
221 return cstring_undefined
;
223 s
= cstring_makeLiteral ("extern");
230 s
= message ("%q const", s
);
233 s
= message ("%q volatile", s
);
235 default: /* ignore it */
239 s
= message ("%q %q ", s
, lhTypeSpecNode (lclTypeSpec
));
241 initDeclNodeList_elements (initDecls
, i
)
245 s
= message ("%q %q", s
, declaratorNode_unparse (i
->declarator
));
250 s
= message ("%q, %q", s
, declaratorNode_unparse (i
->declarator
));
252 } end_initDeclNodeList_elements
;
254 return (message ("%q;", s
));
259 /*@modifies fileSystem@*/
267 llassert (LhFile
.f
!= NULL
);
269 if (LhFile
.f
== NULL
)
271 lldiagmsg (message ("Cannot open lh file for output: %s", LhFile
.name
));
275 check (fprintf (LhFile
.f
, "/* Output from %s */\n", LCL_PARSE_VERSION
) > 0);
276 check (fileTable_closeFile (context_fileTable (), LhFile
.f
));
283 LSLRootName (cstring filespec
)
286 char *result
, *startName
, *tail
;
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';
299 /*@noaccess cstring@*/
302 void lhInit (inputStream f
) /*@globals undef LhFile; @*/
304 static bool lherror
= FALSE
;
306 genLh
= context_msgLh ();
313 LhFile
.name
= cstring_concatFree1 (LSLRootName (inputStream_fileName (f
)),
315 LhFile
.f
= fileTable_openWriteUpdateFile (context_fileTable (), LhFile
.name
);
317 if (LhFile
.f
== NULL
)
322 lclplainerror (message ("Cannot write temporary file: %s",
329 void lhOutLine (/*@only@*/ cstring s
)
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');
347 void lhExternals (interfaceNodeList x
)
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
);