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
28 * Modified by Herbert 04/19/97:
29 * - added new include file portab.h.
30 * - added new private function fileloc_filenameForCpp() to handle
31 * filenames belonging to "#line" statements for OS/2 and MSDOS. It
32 * gets called by fileloc_lineMarker() and fileloc_previousLineMarker()
33 * instead of fileloc_unparseFilename().
36 # include "lclintMacros.nf"
38 # include "fileIdList.h"
41 static /*@only@*/ fileloc fileloc_createPrim (flkind p_kind, fileId p_fid, int p_line, int p_col);
43 static flkind fileId_kind (fileId s)
45 cstring fname = rootFileName (s);
47 if (isLCLfile (fname))
51 else if (cstring_equalPrefix (fname, SYSTEM_LIBDIR))
62 fileloc_decColumn (fileloc f, int x)
64 fileloc ret = fileloc_copy (f);
65 if (x > 0 && fileloc_isDefined (ret))
67 llassertprint (ret->column > x, ("decColumn: %d", x));
75 fileloc_noColumn (fileloc f)
77 if (fileloc_isDefined (f))
79 fileloc ret = fileloc_copy (f);
81 if (fileloc_isDefined (ret))
90 return fileloc_undefined;
95 fileloc_subColumn (fileloc f, int x)
97 if (x > 0 && fileloc_isDefined (f))
99 llassert (f->column > x);
104 fileloc fileloc_copy (fileloc f)
106 if (fileloc_isDefined (f))
108 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
111 ** Legitimate (spurious) errors reported since no copy
115 /*@i3@*/ return f; /* No copy is necessary. */
119 return (fileloc_createPrim (f->kind, f->fid, f->lineno, f->column));
124 return fileloc_undefined;
128 fileloc fileloc_update (/*@only@*/ fileloc old, fileloc fnew)
130 if (fileloc_isUndefined (fnew))
133 return fileloc_undefined;
135 else if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
137 return (fileloc_copy (fnew));
141 old->kind = fnew->kind;
142 old->fid = fnew->fid;
143 old->lineno = fnew->lineno;
144 old->column = fnew->column;
150 fileloc fileloc_updateFileId (/*@only@*/ fileloc old, fileId s)
152 if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
154 return (fileloc_create (s, 1, 1));
158 old->kind = fileId_kind (s);
168 fileloc_free (/*@only@*/ fileloc f)
170 if (fileloc_isDefined (f))
172 if (f != g_currentloc)
174 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
192 fileloc_reallyFree (/*@only@*/ fileloc f)
194 if (fileloc_isDefined (f))
196 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
203 /*@-branchstate@*/ } /*@=branchstate@*/
207 cstring fileloc_getBase (fileloc f)
209 llassert (fileloc_isDefined (f));
211 return (fileNameBase (f->fid));
215 fileloc_equal (fileloc f1, fileloc f2)
218 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
219 && ((f1->column == f2->column) &&
220 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
224 fileloc_compare (fileloc f1, fileloc f2)
226 if (fileloc_isUndefined (f1))
228 if (fileloc_isUndefined (f2)) return 0;
232 if (fileloc_isUndefined (f2))
236 INTCOMPARERETURN (f1->fid, f2->fid);
237 /*@noaccess fileId@*/
239 INTCOMPARERETURN (f1->column, f2->column);
240 INTCOMPARERETURN (f1->lineno, f2->lineno);
246 fileloc_withinLines (fileloc f1, fileloc f2, int n)
249 return (fileloc_isDefined (f1) &&
250 fileloc_isDefined (f2) &&
251 ((f2->lineno <= f1->lineno + n)
252 && (f2->lineno >= f1->lineno)
253 && fileloc_sameFile (f1, f2)));
257 fileloc_lessthan (fileloc f1, fileloc f2)
260 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
261 && ((f1->fid < f2->fid)
262 || ((f1->fid == f2->fid)
263 && ((f1->lineno < f2->lineno)
264 || ((f1->lineno == f2->lineno)
265 && (f1->column < f2->column))))));
270 ** returns true if f1 and f2 are different files,
271 ** or f1 is before f2 in same file
275 fileloc_notAfter (fileloc f1, fileloc f2)
278 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
279 && ((f1->fid != f2->fid)
280 || ((f1->lineno < f2->lineno)
281 || ((f1->lineno == f2->lineno)
282 && (f1->column <= f2->column)))));
283 /*@noaccess fileId@*/
288 fileloc_isStandardLibrary (fileloc f)
290 cstring s = fileloc_getBase (f);
292 return (cstring_equalLit (s, LLSTDLIBS_NAME)
293 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
294 || cstring_equalLit (s, LLUNIXLIBS_NAME)
295 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
296 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
297 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
302 fileloc_sameFile (fileloc f1, fileloc f2)
304 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
305 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
311 return (fileId_equal (f1->fid, f2->fid));
316 fileloc_sameModule (fileloc f1, fileloc f2)
318 if (fileloc_isUndefined (f1))
320 return (fileloc_isUndefined (f2));
322 else if (fileloc_isUndefined (f2))
328 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
329 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
331 return fileloc_sameFile (f1, f2);
335 cstring s1 = fileloc_getBase (f1);
336 cstring s2 = fileloc_getBase (f2);
338 return (cstring_equal (s1, s2));
344 fileloc_sameBaseFile (fileloc f1, fileloc f2)
346 if (fileloc_isUndefined (f1))
348 return (fileloc_isUndefined (f2));
350 else if (fileloc_isUndefined (f2))
356 return (fileId_baseEqual (f1->fid, f2->fid));
360 bool fileloc_isSystemFile (fileloc f1)
362 if (fileloc_isDefined (f1)
363 && !fileloc_isBuiltin (f1)
364 && !fileloc_isExternal (f1))
366 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
373 fileloc_almostSameFile (fileloc f1, fileloc f2)
375 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
376 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
382 if (fileId_baseEqual (f1->fid, f2->fid))
386 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
387 || fileTable_isSystemFile (context_fileTable (), f2->fid))
391 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
392 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
394 return (cstring_equal (fileloc_getBase (f1),
395 fileloc_getBase (f2)));
406 fileloc_fromTok (ltoken t)
408 cstring fname = ltoken_fileName (t);
409 fileId fid = fileTable_lookup (context_fileTable (), fname);
412 if (!fileId_isValid (fid))
414 fid = fileTable_addLCLFile (context_fileTable (), fname);
417 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
424 fileloc_createLib (cstring ln)
427 fileId fid = fileTable_lookup (context_fileTable (), ln);
429 if (!fileId_isValid (fid))
431 fid = fileTable_addLibraryFile (context_fileTable (), ln);
434 if (cstring_equalPrefix (ln, SYSTEM_LIBDIR))
439 return (fileloc_createPrim (fk, fid, 0, 0));
442 fileloc fileloc_createRc (cstring name)
444 fileId fid = fileTable_addFile (context_fileTable (), name);
446 return (fileloc_createPrim (FL_RC, fid, 0, 0));
449 fileloc fileloc_createExternal (void)
451 /*@i@*/ return (fileloc_getExternal ());
454 fileloc fileloc_getExternal (void)
456 static /*@owned@*/ fileloc res = fileloc_undefined;
458 if (res == fileloc_undefined)
460 res = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
466 fileloc fileloc_observeBuiltin ()
468 /*@-onlytrans@*/ return (fileloc_getBuiltin ()); /*@=onlytrans@*/
471 fileloc fileloc_getBuiltin ()
473 static /*@owned@*/ fileloc res = fileloc_undefined;
475 if (res == fileloc_undefined)
477 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
484 fileloc_makePreproc (fileloc loc)
486 if (fileloc_isDefined (loc))
488 return (fileloc_createPrim (FL_PREPROC, loc->fid,
489 loc->lineno, loc->column));
492 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
496 fileloc_makePreprocPrevious (fileloc loc)
498 if (fileloc_isDefined (loc))
502 return (fileloc_createPrim (FL_PREPROC, loc->fid,
503 loc->lineno - 1, 0));
507 return (fileloc_createPrim (FL_PREPROC, loc->fid,
512 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
516 fileloc_createBuiltin ()
518 return (fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0));
523 fileloc_createImport (cstring fname, int lineno)
525 fileId fid = fileTable_lookup (context_fileTable (), fname);
527 if (!fileId_isValid (fid))
529 fid = fileTable_addImportFile (context_fileTable (), fname);
532 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
536 static /*@only@*/ fileloc
537 fileloc_createPrim (flkind kind, fileId fid, int line, int col)
539 fileloc f = (fileloc) dmalloc (sizeof (*f));
551 fileloc_createSpec (fileId fid, int line, int col)
553 return (fileloc_createPrim (FL_SPEC, fid, line, col));
557 fileloc fileloc_create (fileId fid, int line, int col)
559 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
562 /*@observer@*/ cstring
563 fileloc_filename (fileloc f)
565 return (fileloc_isDefined (f)
566 ? rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
570 fileloc_unparseFilename (fileloc f)
572 if (fileloc_isDefined (f))
577 return (message ("load file %s", fileloc_filename (f)));
579 return (cstring_makeLiteral ("# builtin #"));
581 return (message ("import file %s", fileloc_filename (f)));
583 return (cstring_makeLiteral ("<external>"));
585 return (cstring_copy (fileloc_filename (f)));
588 return cstring_undefined;
592 fileloc_lineno (fileloc f)
594 return (fileloc_isDefined (f) ? f->lineno : -1);
598 fileloc_column (fileloc f)
600 return (fileloc_isDefined (f) ? f->column : -1);
604 fileloc_unparse (fileloc f)
606 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
608 if (fileloc_isDefined (f))
613 return (message ("load file %s", rootFileName (f->fid)));
615 return (cstring_makeLiteral ("Command Line"));
619 return (message ("import file %s(%d)", rootFileName (f->fid), f->lineno));
623 return (message ("import file %s:%d", rootFileName (f->fid), f->lineno));
628 return (message ("%s(%d)", rootFileName (f->fid), f->lineno));
632 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
635 return (cstring_makeLiteral ("<external>"));
637 if (context_getFlag (FLG_SHOWCOL))
639 if (fileloc_linenoDefined (f))
641 if (fileloc_columnDefined (f))
645 return (message ("%s(%d,%d)",
646 rootFileName (f->fid),
647 f->lineno, f->column));
651 return (message ("%s:%d:%d",
652 rootFileName (f->fid),
653 f->lineno, f->column));
660 return (message ("%s(%d)", rootFileName (f->fid), f->lineno));
664 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
668 return (cstring_copy (rootFileName (f->fid)));
670 else if (fileloc_linenoDefined (f))
674 return (message ("%s(%d)",
675 rootFileName (f->fid), f->lineno));
679 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
684 return (cstring_copy (rootFileName (f->fid)));
688 return (cstring_makeLiteral ("< Location unknown >"));
692 fileloc_unparseRaw (cstring fname, int lineno)
694 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
698 return (message ("%s(%d)", fname, lineno));
702 return (message ("%s:%d", fname, lineno));
707 fileloc_unparseRawCol (cstring fname, int lineno, int col)
709 if (context_getFlag (FLG_SHOWCOL))
711 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
715 return (message ("%s(%d,%d)", fname, lineno, col));
719 return (message ("%s:%d:%d", fname, lineno, col));
724 return fileloc_unparseRaw (fname, lineno);
728 bool fileloc_isSpecialFile (fileloc f)
730 if (fileloc_isDefined (f)
731 && fileId_isValid (f->fid))
733 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
741 bool fileloc_isHeader (fileloc f)
743 /* returns true if is not a .c file */
745 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
746 && fileId_isHeader (f->fid));
749 bool fileloc_isSpec (fileloc f)
751 return (fileloc_isDefined (f) &&
752 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
755 bool fileloc_isRealSpec (fileloc f)
757 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
760 bool fileloc_isRealLib (fileloc f)
762 return (fileloc_isDefined (f) && f->kind == FL_LIB);
765 bool fileloc_isLib (fileloc f)
767 return (fileloc_isDefined (f)
768 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
771 bool fileloc_isStandardLib (fileloc f)
773 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
776 /*@only@*/ cstring fileloc_unparseDirect (fileloc fl)
778 if (fileloc_isDefined (fl))
780 return (message ("%d:%d:%d",
781 /*@access fileId@*/ (int) fl->fid, /*@noaccess fileId@*/
782 fl->lineno, fl->column));
786 return (cstring_makeLiteral ("<undef>"));
790 bool fileloc_isUser (fileloc f)
792 return (fileloc_isDefined (f)
793 && f->kind == FL_NORMAL);