File: message.c

package info (click to toggle)
ftnchek 3.3.1-7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,684 kB
  • sloc: ansic: 21,908; fortran: 5,748; yacc: 4,071; sh: 3,035; makefile: 895; lisp: 322; f90: 118; perl: 76
file content (554 lines) | stat: -rw-r--r-- 15,284 bytes parent folder | download | duplicates (5)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* $Id: message.c,v 1.12 2004/11/14 16:29:47 moniot Exp $

	Error and warning routines
*/

/*


Copyright (c) 2001 by Robert K. Moniot.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgement: the above permission notice is what is known
as the "MIT License."
*/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "ftnchek.h"

PROTO(PRIVATE void error_message,( const char *filename, LINENO_t lineno,
	COLNO_t colno, const char *s, const char *tag ));
PROTO(PRIVATE void lintstyle_error_message,( const char *filename, LINENO_t lineno,
	COLNO_t colno, const char *s, const char *tag ));
PROTO(PRIVATE void oldstyle_error_message,( const char *filename, LINENO_t lineno,
	COLNO_t colno, const char *s, const char *tag ));

void
#if HAVE_STDC
lex_error(const char *s)
#else /* K&R style */
lex_error(s)
	char *s;
#endif /* HAVE_STDC */
{
	syntax_error(line_num,col_num,s);
}


void
#if HAVE_STDC
yyerror(const char *s)
#else /* K&R style */
yyerror(s)
	char *s;
#endif /* HAVE_STDC */
{
    static int parserror_explanation_given = FALSE;
    syntax_error(line_num,col_num,s);

			/* Novices are often thrown by terminology, so
			   we give them a little help.  This message is
			   only given once per run.
			   Nov 2004: bison has reverted from "parse error"
			   to "syntax error" but we'll keep the explanatory
			   code in case it changes back in future.
			 */
    if( novice_help && ! parserror_explanation_given &&
	strncmp(s,"parse error",sizeof("parse error")-1) == 0 ) {
	error_message((char *)NULL, NO_LINE_NUM, NO_COL_NUM,
    "(A parse error means that I am not able to make sense of this statement,",
		      (char *)NULL );
	msg_tail("because your program has broken some rule of Fortran syntax.)");
	parserror_explanation_given = TRUE;
    }
}


void
#if HAVE_STDC
syntax_error(LINENO_t lineno, COLNO_t colno, const char *s)		/* Syntax error message */
#else /* K&R style */
syntax_error(lineno,colno,s)		/* Syntax error message */
	LINENO_t lineno;
	COLNO_t colno;
	char *s;
#endif /* HAVE_STDC */
{
	++error_count;
	error_message(current_filename,lineno,colno,s,"Error");
}

void
#if HAVE_STDC
warning(LINENO_t lineno, COLNO_t colno, const char *s)  /* Print warning message */
#else /* K&R style */
warning(lineno,colno,s)
	LINENO_t lineno;
	COLNO_t colno;
	char *s;
#endif /* HAVE_STDC */
{
	++warning_count;

	error_message(current_filename,lineno,colno,s,"Warning");
}

void
#if HAVE_STDC
ugly_code(LINENO_t lineno, COLNO_t colno, const char *s) /* -pretty message */
#else /* K&R style */
ugly_code(lineno,colno,s)
	LINENO_t lineno;
	COLNO_t colno;
	char *s;
#endif /* HAVE_STDC */
{
	++warning_count;

	error_message(current_filename,lineno,colno,s,"Possibly misleading appearance");
}

void
#if HAVE_STDC
nonstandard(LINENO_t lineno, COLNO_t colno, int f90, int f95)
#else /* K&R style */
nonstandard(lineno,colno, f90, f95)
     LINENO_t lineno;
     COLNO_t colno;
     int f90, f95;
#endif /* HAVE_STDC */
{
	++warning_count;
	if( f95 ) {
	  error_message(current_filename,lineno,colno,"Syntax deleted in Fortran 95","Warning");
	}
	else {
	  error_message(current_filename,lineno,colno,"Nonstandard syntax","Warning");
	  if( f90 )
	    msg_tail("(not adopted in Fortran 90)");
	}
}

void
#if HAVE_STDC
nonportable(LINENO_t lineno, COLNO_t colno, const char *s) /* Print warning about nonportable construction */
#else /* K&R style */
nonportable(lineno,colno,s) /* Print warning about nonportable construction */
	LINENO_t lineno;
	COLNO_t colno;
	char *s;
#endif /* HAVE_STDC */
{
	++warning_count;
	error_message(current_filename,lineno,colno,s,"Nonportable usage");
}

		/* Routine to print messages from local symtab checking
		   routines.  */

void
#if HAVE_STDC
local_message(const char *filename, LINENO_t lineno, const char *s, const char *tag)
#else /* K&R style */
local_message(filename, lineno, s, tag)
	char *filename;
	LINENO_t lineno;
	char *s;
	char *tag;
#endif /* HAVE_STDC */
{
	error_message(filename,lineno,GLOBAL_NO_COL_NUM,s,tag);
}

		/* Routine to print messages from global checking
		   routines.  Here the filename is given as an
		   argument since it varies with each call.  */

void
#if HAVE_STDC
global_warning(const char *filename, LINENO_t lineno, const char *s)
#else /* K&R style */
global_warning(filename,lineno,s)
	char *filename;
	LINENO_t lineno;
	char *s;
#endif /* HAVE_STDC */
{
	++warning_count;
	error_message(filename,lineno,GLOBAL_NO_COL_NUM,s,"Warning");
}

void
#if HAVE_STDC
global_message(const char *filename, LINENO_t lineno, const char *s)
#else /* K&R style */
global_message(filename,lineno,s)
	char *filename;
	LINENO_t lineno;
	char *s;
#endif /* HAVE_STDC */
{
	++warning_count;
	error_message(filename,lineno,GLOBAL_NO_COL_NUM,s,NULL);
}

/* error_message prints out error messages and warnings.  It
   now comes in two flavors.  If using lintstyle_error_message(),
   messages are produced in style like UNIX lint:

	"main.f", line nn, col nn: Error: your message here

   Otherwise messages by oldstyle_error_message in old ftnchek style:

	Error near line nn col nn file main.f: your message here

   At this time, oldstyle_error_message is used when -novice is
   in effect, lintstyle_error_message otherwise.
*/

PRIVATE int errmsg_col;
	/* Crude macro to give number of digits in line and column numbers.
	   Used by line wrap computation. */
#define NUM_DIGITS(n) ((n)<10?1:((n)<100?2:((n)<1000?3:(n)<10000?4:5)))

PRIVATE void
#if HAVE_STDC
error_message(const char *filename,LINENO_t lineno, COLNO_t colno, const char *s, const char *tag)
#else /* K&R style */
error_message(filename,lineno,colno,s,tag)
	char *filename;
	LINENO_t lineno;
	COLNO_t colno;
	char *s,*tag;
#endif /* HAVE_STDC */
{
  if(novice_help)
    oldstyle_error_message(filename,lineno,colno,s,tag);
  else
    lintstyle_error_message(filename,lineno,colno,s,tag);
}

PRIVATE void
#if HAVE_STDC
lintstyle_error_message(const char *filename,LINENO_t lineno, COLNO_t colno, const char *s, const char *tag)
#else /* K&R style */
lintstyle_error_message(filename,lineno,colno,s,tag)
	char *filename;
	LINENO_t lineno;
	COLNO_t colno;
	char *s,*tag;
#endif /* HAVE_STDC */
{
	COLNO_t icol;
	extern LINENO_t prev_stmt_line_num; /* shared with advance.c */

	errmsg_col=1;		/* Keep track of line length */

			/* Print the character ^ under the column number.
			   But if colno == 0, error occurred in prior line.
			   If colno is NO_COL_NUM, then print message
			   without any column number given.  N.B. colno ==
			   GLOBAL_COL_NUM means this is from local or global
			   message routines, don't flush line out.
			 */

	if(lineno != NO_LINE_NUM && colno != GLOBAL_NO_COL_NUM) {
	    if(colno == NO_COL_NUM) {
		    /* colno == NO_COL_NUM means don't give column number.*/
		(void)flush_line_out(lineno);/* print line if not printed yet */
	    }
	    else if(colno != 0) {
			/* print line if not printed yet */
		if( flush_line_out(lineno) ) {
				/* If it was printed, put ^ under the col */
		    (void)fprintf(list_fd,"\n%8s","");

		    for(icol=1; icol<colno; icol++)
			(void)fprintf(list_fd," ");
		    (void)fprintf(list_fd,"^");
		}
	    }
	    else {		/* colno == 0 */
			/* print line if not printed yet */
		(void)flush_line_out(prev_stmt_line_num);
	    }
	}

	if( filename != (char *)NULL ) {
	    (void)fprintf(list_fd,"\n\"%s\"",filename);
	    errmsg_col += 2+(int)strlen(filename);
	}

	if(lineno != NO_LINE_NUM) { /* nonlocal error-- don't flush */
	    if(colno == NO_COL_NUM) {
		(void)fprintf(list_fd,
		   ", near line %u",lineno);
		errmsg_col += 12+NUM_DIGITS(lineno);
	    }
	    else if(colno == GLOBAL_NO_COL_NUM) {
		(void)fprintf(list_fd,
		   ", line %u",lineno);
		errmsg_col += 7+NUM_DIGITS(lineno);
	    }
	    else if(colno != 0) {
		(void)fprintf(list_fd,
		   ", line %u col %u",lineno,colno);
		errmsg_col += 12+NUM_DIGITS(lineno);
	    }
	    else {		/* colno == 0 */
		(void)fprintf(list_fd,
		   ", near line %u",prev_stmt_line_num);
		errmsg_col += 12+NUM_DIGITS(lineno);
	    }
	}

	if( tag != (char *)NULL ) {
	    msg_tail(":");
	    msg_tail(tag); /* "Warning", "Error", etc. */
	}
	if( s != (char *)NULL ) {
	    msg_tail(":");
	    msg_tail(s); /* now append the message string */
	}
}

				/* Our own style messages */
PRIVATE void
#if HAVE_STDC
oldstyle_error_message(const char *filename,LINENO_t lineno, COLNO_t colno, const char *s, const char *tag)
#else /* K&R style */
oldstyle_error_message(filename,lineno,colno,s,tag)
	char *filename;
	LINENO_t lineno;
	COLNO_t colno;
	char *s,*tag;
#endif /* HAVE_STDC */
{
	COLNO_t icol;
	extern LINENO_t prev_stmt_line_num; /* shared with advance.c */

	errmsg_col=1;		/* Keep track of line length */

			/* Print the character ^ under the column number.
			   But if colno == 0, error occurred in prior line.
			   If colno is NO_COL_NUM, then print message
			   without any column number given.  If tag is NULL,
			   this is an error_report from global checks, so
			   no location information is printed.
			 */

	if( tag == (char *)NULL ) {
	    (void)fprintf(list_fd,"\n");
	}
	else {
	  if(lineno == NO_LINE_NUM) { /* nonlocal error-- don't flush */
	      (void)fprintf(list_fd,"\n%s",tag);
	      errmsg_col += strlen(tag);
	  }
	  else {
	    if(colno == NO_COL_NUM) {
		    /* colno == NO_COL_NUM means don't give column number.*/
		(void)flush_line_out(lineno);/* print line if not printed yet */

		(void)fprintf(list_fd,"\n%s near line %u",tag,lineno);
		errmsg_col += 11+NUM_DIGITS(lineno)+(unsigned)strlen(tag);
	    }
				/* global warnings don't have column numbers
				   but line number is exact.
				 */
	    else if(colno == GLOBAL_NO_COL_NUM) {
		(void)fprintf(list_fd,
			      "\n%s at line %u",tag,lineno);
		errmsg_col += 9+NUM_DIGITS(lineno)+(unsigned)strlen(tag);
	    }
	    else if(colno != 0) {
			/* print line if not printed yet */
		if( flush_line_out(lineno) ) {
				/* If it was printed, put ^ under the col */
		    (void)fprintf(list_fd,"\n%8s","");

		    for(icol=1; icol<colno; icol++)
			(void)fprintf(list_fd," ");
		    (void)fprintf(list_fd,"^");
		}
		(void)fprintf(list_fd,
			      "\n%s near line %u col %u",tag,lineno,colno);
		errmsg_col += 16+NUM_DIGITS(lineno)+NUM_DIGITS(colno)
			+(unsigned)strlen(tag);
	    }
	    else {		/* colno == 0 */
			/* print line if not printed yet */
		(void)flush_line_out(prev_stmt_line_num);
		(void)fprintf(list_fd,
			      "\n%s near line %u",tag,prev_stmt_line_num);
		errmsg_col += 11+NUM_DIGITS(lineno)+(unsigned)strlen(tag);
	    }
	  }


	  if((!full_output	/* Append file name if not listing */
	   || doing_wrapup	/* or if this is a global error message */
	   || (doing_end_proc	/* or a local message referring to inc file */
	       && filename != top_filename)
	   || incdepth > 0)	/* Append include-file name if we are in one */
	     && filename != (char *)NULL ) { /* skip if multi-file message */
	    if(lineno == NO_LINE_NUM) { /* if no line no, preposition needed */
		(void)fprintf(list_fd," in");
		errmsg_col += 3;
	    }
	    (void)fprintf(list_fd," file %s",filename);
	    errmsg_col += 6+(unsigned)strlen(filename);
	  }
	}/*end if(tag != NULL)*/

	if( s != (char *)NULL ) {
	    if( tag != (char *)NULL ) {
		msg_tail(":");
	    }
	    msg_tail(s); /* now append the message string */
	}
}

		/* msg_tail appends string s to current error message.
		   It prints one word at a time, starting a new line
		   when the message gets to be too long for one line.
		 */
void
#if HAVE_STDC
msg_tail(const char *s)
#else /* K&R style */
msg_tail(s)
    char *s;
#endif /* HAVE_STDC */
{
    if( s[0] != '\0' ) {

	int wordstart,wordend,leading_skip,wordchars;


		/* Insert blanks between items.  Exceptions:
		   colon, semicolon, closing paren
		   are used at start of some items and should
		   not be separated from preceding item.
		*/
	if( s[0] != ':' && s[0] != ')' && s[0] != ';' ) {
	    (void)fprintf(list_fd," ");
	    errmsg_col++;
	}

		/* Each iteration of loop prints leading space and the
		   nonspace characters of a word.  Loop invariant: wordstart
		   is index of leading space at start of word, wordend is
		   index of space char following word. */
	wordstart=0;
	while(s[wordstart] != '\0') {
	  leading_skip = TRUE;
	  for(wordend=wordstart; s[wordend] != '\0'; wordend++) {
	    if(leading_skip) {	/* If skipping leading space chars */
	      if(!isspace(s[wordend]))
		leading_skip = FALSE; /* go out of skip mode at nonspace */
	    }
	    else {		/* If scanning word chars */
	      if(isspace(s[wordend]))
		break;		/* quit loop when space char found */
	    }
	  }
	  wordchars = wordend-wordstart;
				/* If word doesn't fit, wrap to next line */
	  if( wrap_column > 0 && (errmsg_col += wordchars) > wrap_column) {
				/* At start of line, replace zero or more
				   blanks by one blank. */
	    (void)fprintf(list_fd,"\n ");
	    while(isspace(s[wordstart])) {
		++wordstart;
		--wordchars;
	    }
	    errmsg_col = wordchars+1;
	  }
				/* Print the word */
	  while(wordstart < wordend) {
	    (void)putc(s[wordstart++],list_fd);
	  }
	}
    }
}


void
#if HAVE_STDC
oops_message(int severity, LINENO_t lineno, COLNO_t colno, const char *s)
#else /* K&R style */
oops_message(severity,lineno,colno,s)
	int severity;
	LINENO_t lineno;
	COLNO_t colno;
	char *s;
#endif /* HAVE_STDC */
{
	(void)fflush(list_fd);
	(void)fprintf(stderr,"\nOops");
	if(lineno != NO_LINE_NUM) {
	  (void)fprintf(stderr," at line %u",lineno);
	  if(colno != NO_COL_NUM)
	    (void)fprintf(stderr," at col %u",colno);
	}
	(void)fprintf(stderr," in file %s",current_filename);
	(void)fprintf(stderr," -- %s",s);
	if(severity == OOPS_FATAL) {
	  (void)fprintf(stderr,"\nFtnchek aborted\n");
	  exit(1);
	}
}

void
#if HAVE_STDC
oops_tail(const char *s)
#else /* K&R style */
oops_tail(s)
	char *s;
#endif /* HAVE_STDC */
{
	(void)fprintf(stderr," %s",s);
}


		/* Routine to convert a long unsigned int to a string.
		   This uses a static array, so only one call can be
		   in action at a time.  Intended for use when msg_tail
		   must print an integer.
		*/
char *
#if HAVE_STDC
ulongtostr(unsigned long num)
#else /* K&R style */
ulongtostr(num)
    unsigned long num;
#endif /* HAVE_STDC */
{
    static char str[MAX_ULONGTOSTR];
    (void)sprintf(str,"%lu",num);
    return str;
}