2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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
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 "splintMacros.nf"
38 # include "fileIdList.h"
42 static /*@only@*/ fileloc fileloc_createPrim (flkind p_kind, fileId p_fid, int p_line, int p_col);
44 static flkind fileId_kind (fileId s)
46 cstring fname = fileTable_rootFileName (s);
48 if (fileLib_isLCLFile (fname))
52 else if (cstring_equalPrefix (fname, cstring_makeLiteralTemp (SYSTEM_LIBDIR)))
63 fileloc_decColumn (fileloc f, int x)
65 fileloc ret = fileloc_copy (f);
67 if (x > 0 && fileloc_isDefined (ret))
69 llassertprint (ret->column > x, ("decColumn: %d", x));
77 fileloc_noColumn (fileloc f)
79 if (fileloc_isDefined (f))
81 fileloc ret = fileloc_copy (f);
83 if (fileloc_isDefined (ret))
92 return fileloc_undefined;
97 fileloc_subColumn (fileloc f, int x)
99 if (x > 0 && fileloc_isDefined (f))
101 llassert (f->column > x);
106 fileloc fileloc_copy (fileloc f)
108 if (fileloc_isDefined (f))
110 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
113 ** Legitimate (spurious) errors reported since no copy
117 /*@i3@*/ return f; /* No copy is necessary. */
121 return (fileloc_createPrim (f->kind, f->fid, f->lineno, f->column));
126 return fileloc_undefined;
130 fileloc fileloc_update (/*@only@*/ fileloc old, fileloc fnew)
132 if (fileloc_isUndefined (fnew))
135 return fileloc_undefined;
137 else if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
139 return (fileloc_copy (fnew));
143 old->kind = fnew->kind;
144 old->fid = fnew->fid;
145 old->lineno = fnew->lineno;
146 old->column = fnew->column;
152 fileloc fileloc_updateFileId (/*@only@*/ fileloc old, fileId s)
154 if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
156 return (fileloc_create (s, 1, 1));
160 old->kind = fileId_kind (s);
170 fileloc_free (/*@only@*/ fileloc f)
172 if (fileloc_isDefined (f))
174 if (f != g_currentloc)
176 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
188 ; /* Don't free g_currentloc ever! */
195 fileloc_reallyFree (/*@only@*/ fileloc f)
197 if (fileloc_isDefined (f))
199 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
206 /*@-branchstate@*/ } /*@=branchstate@*/
210 cstring fileloc_getBase (fileloc f)
212 llassert (fileloc_isDefined (f));
214 return (fileTable_fileNameBase (f->fid));
218 fileloc_equal (fileloc f1, fileloc f2)
221 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
222 && ((f1->column == f2->column) &&
223 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
227 fileloc_compare (fileloc f1, fileloc f2)
229 if (fileloc_isUndefined (f1))
231 if (fileloc_isUndefined (f2)) return 0;
235 if (fileloc_isUndefined (f2))
239 INTCOMPARERETURN (f1->fid, f2->fid);
240 /*@noaccess fileId@*/
243 /* drl 8-11-01 fix what I think is a bug
244 lineno should more important than column number*/
246 INTCOMPARERETURN (f1->lineno, f2->lineno);
247 INTCOMPARERETURN (f1->column, f2->column);
253 fileloc_withinLines (fileloc f1, fileloc f2, int n)
256 return (fileloc_isDefined (f1) &&
257 fileloc_isDefined (f2) &&
258 ((f2->lineno <= f1->lineno + n)
259 && (f2->lineno >= f1->lineno)
260 && fileloc_sameFile (f1, f2)));
264 fileloc_lessthan (fileloc f1, fileloc f2)
267 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
268 && ((f1->fid < f2->fid)
269 || ((f1->fid == f2->fid)
270 && ((f1->lineno < f2->lineno)
271 || ((f1->lineno == f2->lineno)
272 && (f1->column < f2->column))))));
277 ** returns true if f1 and f2 are different files,
278 ** or f1 is before f2 in same file
282 fileloc_notAfter (fileloc f1, fileloc f2)
285 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
286 && ((f1->fid != f2->fid)
287 || ((f1->lineno < f2->lineno)
288 || ((f1->lineno == f2->lineno)
289 && (f1->column <= f2->column)))));
290 /*@noaccess fileId@*/
295 fileloc_isStandardLibrary (fileloc f)
297 cstring s = fileloc_getBase (f);
299 return (cstring_equalLit (s, LLSTDLIBS_NAME)
300 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
301 || cstring_equalLit (s, LLUNIXLIBS_NAME)
302 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
303 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
304 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
309 fileloc_sameFileAndLine (fileloc f1, fileloc f2)
311 return (fileloc_sameFile (f1, f2)
312 && (fileloc_isDefined (f1) && fileloc_isDefined (f2)
313 && f1->lineno == f2->lineno));
317 fileloc_sameFile (fileloc f1, fileloc f2)
319 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
320 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
326 return (fileId_equal (f1->fid, f2->fid));
331 fileloc_sameModule (fileloc f1, fileloc f2)
333 if (fileloc_isUndefined (f1))
335 return (fileloc_isUndefined (f2));
337 else if (fileloc_isUndefined (f2))
343 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
344 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
346 return fileloc_sameFile (f1, f2);
350 cstring s1 = fileloc_getBase (f1);
351 cstring s2 = fileloc_getBase (f2);
353 return (cstring_equal (s1, s2));
359 fileloc_sameBaseFile (fileloc f1, fileloc f2)
361 if (fileloc_isUndefined (f1))
363 return (fileloc_isUndefined (f2));
365 else if (fileloc_isUndefined (f2))
371 return (fileId_baseEqual (f1->fid, f2->fid));
375 bool fileloc_isSystemFile (fileloc f1)
377 if (fileloc_isDefined (f1)
378 && !fileloc_isBuiltin (f1)
379 && !fileloc_isExternal (f1))
381 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
387 bool fileloc_isXHFile (fileloc f1)
389 if (fileloc_isDefined (f1)
390 && !fileloc_isBuiltin (f1)
391 && !fileloc_isExternal (f1))
393 DPRINTF (("Fileloc is XH: [%p] %s", f1, fileloc_unparse (f1)));
394 return (fileTable_isXHFile (context_fileTable (), f1->fid));
401 fileloc_almostSameFile (fileloc f1, fileloc f2)
403 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
404 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
410 if (fileId_baseEqual (f1->fid, f2->fid))
414 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
415 || fileTable_isSystemFile (context_fileTable (), f2->fid))
419 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
420 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
422 return (cstring_equal (fileloc_getBase (f1),
423 fileloc_getBase (f2)));
434 fileloc_fromTok (ltoken t)
436 cstring fname = ltoken_fileName (t);
437 fileId fid = fileTable_lookup (context_fileTable (), fname);
440 if (!fileId_isValid (fid))
442 fid = fileTable_addLCLFile (context_fileTable (), fname);
445 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
452 fileloc_createLib (cstring ln)
455 fileId fid = fileTable_lookup (context_fileTable (), ln);
457 if (!fileId_isValid (fid))
459 fid = fileTable_addLibraryFile (context_fileTable (), ln);
462 if (cstring_equalPrefix (ln, cstring_makeLiteralTemp (SYSTEM_LIBDIR)))
467 return (fileloc_createPrim (fk, fid, 0, 0));
470 fileloc fileloc_createRc (cstring name)
472 fileId fid = fileTable_addFile (context_fileTable (), name);
474 return (fileloc_createPrim (FL_RC, fid, 0, 0));
477 fileloc fileloc_createExternal (void)
479 /*@i@*/ return (fileloc_getExternal ());
482 fileloc fileloc_getExternal (void)
484 static /*@owned@*/ fileloc res = fileloc_undefined;
486 if (res == fileloc_undefined)
488 res = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
494 fileloc fileloc_observeBuiltin ()
496 /*@-onlytrans@*/ return (fileloc_getBuiltin ()); /*@=onlytrans@*/
499 fileloc fileloc_getBuiltin ()
501 static /*@owned@*/ fileloc res = fileloc_undefined;
503 if (res == fileloc_undefined)
505 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
512 fileloc_makePreproc (fileloc loc)
514 if (fileloc_isDefined (loc))
516 return (fileloc_createPrim (FL_PREPROC, loc->fid,
517 loc->lineno, loc->column));
520 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
524 fileloc_makePreprocPrevious (fileloc loc)
526 if (fileloc_isDefined (loc))
530 return (fileloc_createPrim (FL_PREPROC, loc->fid,
531 loc->lineno - 1, 0));
535 return (fileloc_createPrim (FL_PREPROC, loc->fid,
540 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
544 fileloc_createBuiltin ()
546 return (fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0));
551 fileloc_createImport (cstring fname, int lineno)
553 fileId fid = fileTable_lookup (context_fileTable (), fname);
555 if (!fileId_isValid (fid))
557 fid = fileTable_addImportFile (context_fileTable (), fname);
560 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
564 static /*@only@*/ fileloc
565 fileloc_createPrim (flkind kind, fileId fid, int line, int col)
567 fileloc f = (fileloc) dmalloc (sizeof (*f));
574 DPRINTF (("Fileloc create: [%p] %s", f, fileloc_unparse (f)));
580 fileloc_createSpec (fileId fid, int line, int col)
582 return (fileloc_createPrim (FL_SPEC, fid, line, col));
586 fileloc fileloc_create (fileId fid, int line, int col)
588 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
591 /*@observer@*/ cstring
592 fileloc_filename (fileloc f)
594 return (fileloc_isDefined (f) ? fileTable_rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
597 /*@only@*/ cstring fileloc_outputFilename (fileloc f)
599 if (fileloc_isDefined (f))
601 return osd_outputPath (fileTable_rootFileName (f->fid));
605 return cstring_makeLiteral ("<unknown>");
610 fileloc_unparseFilename (fileloc f)
612 if (fileloc_isDefined (f))
617 return (message ("load file %q", fileloc_outputFilename (f)));
619 return (cstring_makeLiteral ("# builtin #"));
621 return (message ("import file %q", fileloc_outputFilename (f)));
623 return (cstring_makeLiteral ("<external>"));
625 return (fileloc_outputFilename (f));
628 return cstring_undefined;
632 fileloc_lineno (fileloc f)
634 return (fileloc_isDefined (f) ? f->lineno : -1);
638 fileloc_column (fileloc f)
640 return (fileloc_isDefined (f) ? f->column : -1);
644 fileloc_unparse (fileloc f)
646 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
648 if (fileloc_isDefined (f))
653 return (cstring_makeLiteral ("Command Line"));
657 return (message ("import file %q(%d)", fileloc_outputFilename (f), f->lineno));
661 return (message ("import file %q:%d", fileloc_outputFilename (f), f->lineno));
666 return (message ("%q(%d)", fileloc_outputFilename (f), f->lineno));
670 return (message ("%q:%d", fileloc_outputFilename (f), f->lineno));
673 return (cstring_makeLiteral ("<external>"));
678 if (f->kind == FL_LIB)
680 fname = message ("load file %q", fileloc_outputFilename (f));
684 fname = fileloc_outputFilename (f);
687 if (context_getFlag (FLG_SHOWCOL))
689 if (fileloc_linenoDefined (f))
691 if (fileloc_columnDefined (f))
695 return (message ("%q(%d,%d)", fname, f->lineno, f->column));
699 return (message ("%q:%d:%d", fname, f->lineno, f->column));
706 return (message ("%q(%d)", fname, f->lineno));
710 return (message ("%q:%d", fname, f->lineno));
717 else if (fileloc_linenoDefined (f))
721 return (message ("%q(%d)", fname, f->lineno));
725 return (message ("%q:%d", fname, f->lineno));
736 return (cstring_makeLiteral ("< Location unknown >"));
740 fileloc_unparseRaw (cstring fname, int lineno)
742 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
746 return (message ("%q(%d)", osd_outputPath (fname), lineno));
750 return (message ("%q:%d", osd_outputPath (fname), lineno));
755 fileloc_unparseRawCol (cstring fname, int lineno, int col)
757 if (context_getFlag (FLG_SHOWCOL))
759 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
763 return (message ("%q(%d,%d)", osd_outputPath (fname), lineno, col));
767 return (message ("%q:%d:%d", osd_outputPath (fname), lineno, col));
772 return fileloc_unparseRaw (fname, lineno);
776 bool fileloc_isSpecialFile (fileloc f)
778 if (fileloc_isDefined (f)
779 && fileId_isValid (f->fid))
781 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
789 bool fileloc_isHeader (fileloc f)
791 /* returns true if is not a .c file */
793 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
794 && fileId_isHeader (f->fid));
797 bool fileloc_isSpec (fileloc f)
799 return (fileloc_isDefined (f) &&
800 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
803 bool fileloc_isRealSpec (fileloc f)
805 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
808 bool fileloc_isLib (fileloc f)
810 return (fileloc_isDefined (f)
811 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
814 bool fileloc_isStandardLib (fileloc f)
816 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
819 /*@only@*/ cstring fileloc_unparseDirect (fileloc fl)
821 if (fileloc_isDefined (fl))
823 return (message ("%d:%d:%d",
824 /*@access fileId@*/ (int) fl->fid, /*@noaccess fileId@*/
825 fl->lineno, fl->column));
829 return (cstring_makeLiteral ("<undef>"));
833 bool fileloc_isUser (fileloc f)
835 return (fileloc_isDefined (f)
836 && f->kind == FL_NORMAL);