File: lh.c

package info (click to toggle)
splint 3.1.2.dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 12,908 kB
  • ctags: 15,816
  • sloc: ansic: 150,306; yacc: 3,463; sh: 3,426; makefile: 2,218; lex: 412
file content (394 lines) | stat: -rw-r--r-- 8,477 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
** Splint - annotation-assisted static program checker
** Copyright (C) 1994-2003 University of Virginia,
**         Massachusetts Institute of Technology
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2 of the License, or (at your
** option) any later version.
** 
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** General Public License for more details.
** 
** The GNU General Public License is available from http://www.gnu.org/ or
** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
** MA 02111-1307, USA.
**
** For information on splint: info@splint.org
** To report a bug: splint-bug@splint.org
** For more information: http://www.splint.org
*/
/*
** lh.c
**
**  MODULE DESCRIPTION:
**
**      This module contains the I/O routines for writing out the .lh file
**	generated from the .lcl file.
**
**  AUTHORS:
**
**      Gary Feldman, Technical Languages and Environments, DECspec project
**      Yang Meng Tan, MIT.
**
**  CREATION DATE:  9 April 91
**
**	The lh.c module controls formatting policy.
*/

# include "splintMacros.nf"
# include "basic.h"
# include "osd.h"
# include "lh.h"
# include "llmain.h"

/*@constant static char TABCH; @*/
# define TABCH 		' '

/*@constant static char TABINCH; @*/
# define TABINCH 	'\t'


/*:private:*/ typedef struct
{
  /*@open@*/ /*@dependent@*/ /*@null@*/ /*@reldef@*/ FILE *f;
  /*@reldef@*/ cstring name;
} outFile;

static bool genLh;
static outFile LhFile;
static bool needIncludeBool = FALSE;

/*
**
**  FORWARD FUNCTIONS
**
*/

/* static int colpos (int startcol, cstring line); */

static cstring lhTypeSpecNode (lclTypeSpecNode p_typespec);
static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr p_x);
static /*@only@*/ cstring lhDeclaratorNode (declaratorNode p_x);

/*@only@*/ cstring 
lhFunction (lclTypeSpecNode lclTypeSpec, declaratorNode declarator)
{
  cstring s;

  if (!genLh)
    return cstring_undefined;
  
  s = message ("extern %q %q;", lhTypeSpecNode (lclTypeSpec),
	       lhDeclaratorNode (declarator));
  
  return s;
}

static /*@only@*/ cstring
lhDeclaratorNode (declaratorNode x)
{
  return (lhTypeExpr (x->type));
}

static /*@only@*/ cstring lhTypeExpr (/*@null@*/ typeExpr x)
{
  cstring s = cstring_undefined; /* print out types in order of appearance in source */
  paramNodeList params;
  int i;

  if (x != (typeExpr) 0)
    {
      cstring front = cstring_undefined;
      cstring back  = cstring_undefined;

      for (i = x->wrapped; i >= 1; i--)
	{
	  front = cstring_appendChar (front, '(');
	  back  = cstring_appendChar (back, ')');
	}

      switch (x->kind)
	{
	case TEXPR_BASE:
	  s = message ("%q%s", s, ltoken_getRawString (x->content.base));
	  break;
	case TEXPR_PTR:
	  s = message ("%q*%q", s, lhTypeExpr (x->content.pointer));
	  break;
	case TEXPR_ARRAY:
	  s = message ("%q%q[%q]", s, 
		       lhTypeExpr (x->content.array.elementtype),
		       termNode_unparse (x->content.array.size));
	  break;
	case TEXPR_FCN:
	  s = message ("%q%q (", s, lhTypeExpr (x->content.function.returntype));
	  params = x->content.function.args;

	  if (!paramNodeList_empty (params))
	    {
	      s = message ("%q%q", s, 
			   paramNodeList_unparseComments (x->content.function.args));
	    }

	  s = message ("%q)", s);
	  break;
	}
      s = message ("%q%q%q", front, s, back);
    }
  else
    {
      s = cstring_makeLiteral ("?");
    }

  return s;
}

extern void
lhForwardStruct (ltoken t)
{
  if (!genLh)
    return;

  lhOutLine (message ("struct %s;", ltoken_unparse (t)));
}


extern void
lhForwardUnion (ltoken t)
{
  if (!genLh)
    return;

  lhOutLine (message ("union %s;", ltoken_unparse (t)));
}

extern /*@only@*/ cstring 
lhType (typeNode t)
{
  if (!genLh)
    return cstring_undefined;

  if (t->kind == TK_EXPOSED)
    {
      exposedNode n = t->content.exposed;

      if (n != (exposedNode) 0)
	{
	  if (declaratorInvNodeList_size (n->decls) == 0)
	    {
	      /* 
	      ** Forward struct or union declaration
	      */

	      return (cstring_appendChar (lhTypeSpecNode (n->type), ';'));
	    }
	  else
	    {
	      cstring s = cstring_undefined;

	      declaratorInvNodeList_elements (n->decls, d)
		{
		  cstring name = declaratorNode_unparse (d->declarator);
		  cstring pname = declaratorNode_unparseCode (d->declarator); 
		  
		  s = message ("%q\n# ifndef EXPOSED_TYPE_%q\ntypedef %q %q;\n# endif\n", 
			       s, pname, lhTypeSpecNode (n->type), name);
		} end_declaratorInvNodeList_elements;
	      
	      return s;
	    }
	}
    }

  return cstring_undefined;
}

static /*@only@*/ cstring 
lhTypeSpecNode (lclTypeSpecNode typespec)
{
  if (!genLh)
    {
      return cstring_undefined;
    }

  return (lclTypeSpecNode_unparseComments (typespec));
}

/*@only@*/ cstring
lhVarDecl (lclTypeSpecNode lclTypeSpec, initDeclNodeList initDecls,
	   qualifierKind qualifier)
{
  bool first = TRUE;
  cstring s;

  if (!genLh)
    return cstring_undefined;

  s = cstring_makeLiteral ("extern");

  switch (qualifier)
    {
    case QLF_NONE:
      break;
    case QLF_CONST:
      s = message ("%q const", s);
      break;
    case QLF_VOLATILE:
      s = message ("%q volatile", s);
      break;
    default:			/* ignore it */
      break;
    }
  
  s = message ("%q %q ", s, lhTypeSpecNode (lclTypeSpec));

  initDeclNodeList_elements (initDecls, i)
  {
    if (first)
      {
	s = message ("%q %q", s, declaratorNode_unparse (i->declarator));
	first = FALSE;
      }
    else
      {
	s = message ("%q, %q", s, declaratorNode_unparse (i->declarator));
      }
  } end_initDeclNodeList_elements;
  
  return (message ("%q;", s));
}

extern void
lhCleanup (void)
   /*@modifies fileSystem@*/
{
  if (!genLh)
    {
      return;
    }
  else
    {
      llassert (LhFile.f != NULL);
      
      if (LhFile.f == NULL)
	{
	  lldiagmsg (message ("Cannot open lh file for output: %s", LhFile.name));
	}
      else
	{
	  check (fprintf (LhFile.f, "/* Output from %s */\n", LCL_PARSE_VERSION) > 0);
	  check (fileTable_closeFile (context_fileTable (), LhFile.f));
	  LhFile.f = NULL;
	}
    }
}

/* Write an #include of bool.h if we have't done so already.  */
extern void
lhIncludeBool (void)
{
  needIncludeBool = TRUE;
}

void lhInit (inputStream f) /*@globals undef LhFile; @*/
{
  static bool lherror = FALSE;
  
  genLh = context_msgLh ();
  needIncludeBool = FALSE;

  if (!genLh)
    {
      return;
    }
  
  LhFile.name = cstring_concatFree1 (LSLRootName (inputStream_fileName (f)),
				     LH_EXTENSION);
  LhFile.f = fileTable_openWriteUpdateFile (context_fileTable (), LhFile.name);

  if (LhFile.f == NULL)
    {
      genLh = FALSE;
      if (!lherror)
	{
	  lclplainerror (message ("Cannot write temporary file: %s", 
				  LhFile.name));
	  lherror = TRUE;
	}
    } 
} 

void lhOutLine (/*@only@*/ cstring s)
{
  if (genLh)
    {
      llassert (LhFile.f != NULL);
      DPRINTF (("lhOutLine: %s / %s", s, LhFile.name));

      if (cstring_length (s) > 0) 
	{
	  check (fputs (cstring_toCharsSafe (s), LhFile.f) != EOF); 
	}

      check (fputc ('\n', LhFile.f) == (int) '\n'); 
    }

  cstring_free (s);
}

void lhExternals (interfaceNodeList x)
{
  if (genLh)
    {
      llassert (LhFile.f != NULL);

      /*
      ** Need to make sure all standard library includes come first.
      */

      interfaceNodeList_elements (x, el)
	{
	  if (el->kind == INF_IMPORTS)
	    {
	      importNodeList imps = el->content.imports;

	      importNodeList_elements (imps, il)
		{
		  if (il->kind == IMPBRACKET)
		    {
		      lhOutLine (message ("# include <%s.h>", 
					  ltoken_getRawString (il->val)));
		    }
		} end_importNodeList_elements ;
	    }
	} end_interfaceNodeList_elements;

      lhOutLine (cstring_makeLiteral ("# include \"bool.h\""));
      
      interfaceNodeList_elements (x, el)
	{
	  if (el->kind == INF_IMPORTS)
	    {
	      importNodeList imps = el->content.imports;

	      importNodeList_elements (imps, il)
		{
		  if (il->kind != IMPBRACKET)
		    {
		      lhOutLine (message ("# include \"%s.h\"", 
					  ltoken_getRawString (il->val)));
		    }
		} end_importNodeList_elements ;
	    }
	} end_interfaceNodeList_elements;

      lhOutLine (cstring_undefined);
    }
}