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);
69 if (x > 0 && fileloc_isDefined (ret))
71 llassertprint (ret->column > x, ("decColumn: %d", x));
79 fileloc_noColumn (fileloc f)
81 if (fileloc_isDefined (f))
83 fileloc ret = fileloc_copy (f);
85 if (fileloc_isDefined (ret))
94 return fileloc_undefined;
99 fileloc_subColumn (fileloc f, int x)
101 if (x > 0 && fileloc_isDefined (f))
103 llassert (f->column > x);
108 fileloc fileloc_copy (fileloc f)
110 if (fileloc_isDefined (f))
112 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
115 ** Legitimate (spurious) errors reported since no copy
119 /*@i3@*/ return f; /* No copy is necessary. */
123 return (fileloc_createPrim (f->kind, f->fid, f->lineno, f->column));
128 return fileloc_undefined;
132 fileloc fileloc_update (/*@only@*/ fileloc old, fileloc fnew)
134 if (fileloc_isUndefined (fnew))
137 return fileloc_undefined;
139 else if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
141 return (fileloc_copy (fnew));
145 old->kind = fnew->kind;
146 old->fid = fnew->fid;
147 old->lineno = fnew->lineno;
148 old->column = fnew->column;
154 fileloc fileloc_updateFileId (/*@only@*/ fileloc old, fileId s)
156 if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
158 return (fileloc_create (s, 1, 1));
162 old->kind = fileId_kind (s);
172 fileloc_free (/*@only@*/ fileloc f)
174 if (fileloc_isDefined (f))
176 if (f != g_currentloc)
178 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
190 ; /* Don't free g_currentloc ever! */
197 fileloc_reallyFree (/*@only@*/ fileloc f)
199 if (fileloc_isDefined (f))
201 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
208 /*@-branchstate@*/ } /*@=branchstate@*/
212 cstring fileloc_getBase (fileloc f)
214 llassert (fileloc_isDefined (f));
216 return (fileTable_fileNameBase (f->fid));
220 fileloc_equal (fileloc f1, fileloc f2)
223 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
224 && ((f1->column == f2->column) &&
225 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
229 fileloc_compare (fileloc f1, fileloc f2)
231 if (fileloc_isUndefined (f1))
233 if (fileloc_isUndefined (f2)) return 0;
237 if (fileloc_isUndefined (f2))
241 INTCOMPARERETURN (f1->fid, f2->fid);
242 /*@noaccess fileId@*/
245 /* drl 8-11-01 fix what I think is a bug
246 lineno should more important than column number*/
248 INTCOMPARERETURN (f1->lineno, f2->lineno);
249 INTCOMPARERETURN (f1->column, f2->column);
255 fileloc_withinLines (fileloc f1, fileloc f2, int n)
258 return (fileloc_isDefined (f1) &&
259 fileloc_isDefined (f2) &&
260 ((f2->lineno <= f1->lineno + n)
261 && (f2->lineno >= f1->lineno)
262 && fileloc_sameFile (f1, f2)));
266 fileloc_lessthan (fileloc f1, fileloc f2)
269 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
270 && ((f1->fid < f2->fid)
271 || ((f1->fid == f2->fid)
272 && ((f1->lineno < f2->lineno)
273 || ((f1->lineno == f2->lineno)
274 && (f1->column < f2->column))))));
279 ** returns true if f1 and f2 are different files,
280 ** or f1 is before f2 in same file
284 fileloc_notAfter (fileloc f1, fileloc f2)
287 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
288 && ((f1->fid != f2->fid)
289 || ((f1->lineno < f2->lineno)
290 || ((f1->lineno == f2->lineno)
291 && (f1->column <= f2->column)))));
292 /*@noaccess fileId@*/
297 fileloc_isStandardLibrary (fileloc f)
299 cstring s = fileloc_getBase (f);
301 return (cstring_equalLit (s, LLSTDLIBS_NAME)
302 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
303 || cstring_equalLit (s, LLUNIXLIBS_NAME)
304 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
305 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
306 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
311 fileloc_sameFileAndLine (fileloc f1, fileloc f2)
313 return (fileloc_sameFile (f1, f2)
314 && (fileloc_isDefined (f1) && fileloc_isDefined (f2)
315 && f1->lineno == f2->lineno));
319 fileloc_sameFile (fileloc f1, fileloc f2)
321 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
322 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
328 return (fileId_equal (f1->fid, f2->fid));
333 fileloc_sameModule (fileloc f1, fileloc f2)
335 if (fileloc_isUndefined (f1))
337 return (fileloc_isUndefined (f2));
339 else if (fileloc_isUndefined (f2))
345 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
346 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
348 return fileloc_sameFile (f1, f2);
352 cstring s1 = fileloc_getBase (f1);
353 cstring s2 = fileloc_getBase (f2);
355 return (cstring_equal (s1, s2));
361 fileloc_sameBaseFile (fileloc f1, fileloc f2)
363 if (fileloc_isUndefined (f1))
365 return (fileloc_isUndefined (f2));
367 else if (fileloc_isUndefined (f2))
373 return (fileId_baseEqual (f1->fid, f2->fid));
377 bool fileloc_isSystemFile (fileloc f1)
379 if (fileloc_isDefined (f1)
380 && !fileloc_isBuiltin (f1)
381 && !fileloc_isExternal (f1))
383 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
389 bool fileloc_isXHFile (fileloc f1)
391 if (fileloc_isDefined (f1)
392 && !fileloc_isBuiltin (f1)
393 && !fileloc_isExternal (f1))
395 DPRINTF (("Fileloc is XH: [%p] %s", f1, fileloc_unparse (f1)));
396 return (fileTable_isXHFile (context_fileTable (), f1->fid));
403 fileloc_almostSameFile (fileloc f1, fileloc f2)
405 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
406 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
412 if (fileId_baseEqual (f1->fid, f2->fid))
416 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
417 || fileTable_isSystemFile (context_fileTable (), f2->fid))
421 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
422 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
424 return (cstring_equal (fileloc_getBase (f1),
425 fileloc_getBase (f2)));
436 fileloc_fromTok (ltoken t)
438 cstring fname = ltoken_fileName (t);
439 fileId fid = fileTable_lookup (context_fileTable (), fname);
442 if (!fileId_isValid (fid))
444 fid = fileTable_addLCLFile (context_fileTable (), fname);
447 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
454 fileloc_createLib (cstring ln)
457 fileId fid = fileTable_lookup (context_fileTable (), ln);
459 if (!fileId_isValid (fid))
461 fid = fileTable_addLibraryFile (context_fileTable (), ln);
464 if (cstring_equalPrefix (ln, cstring_makeLiteralTemp (SYSTEM_LIBDIR)))
469 return (fileloc_createPrim (fk, fid, 0, 0));
472 fileloc fileloc_createRc (cstring name)
474 fileId fid = fileTable_addFile (context_fileTable (), name);
476 return (fileloc_createPrim (FL_RC, fid, 0, 0));
479 fileloc fileloc_createExternal (void)
481 /*@i@*/ return (fileloc_getExternal ());
484 fileloc fileloc_getExternal (void)
486 static /*@owned@*/ fileloc res = fileloc_undefined;
488 if (res == fileloc_undefined)
490 res = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
496 fileloc fileloc_observeBuiltin ()
498 /*@-onlytrans@*/ return (fileloc_getBuiltin ()); /*@=onlytrans@*/
501 fileloc fileloc_getBuiltin ()
503 static /*@owned@*/ fileloc res = fileloc_undefined;
505 if (res == fileloc_undefined)
507 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
514 fileloc_makePreproc (fileloc loc)
516 if (fileloc_isDefined (loc))
518 return (fileloc_createPrim (FL_PREPROC, loc->fid,
519 loc->lineno, loc->column));
522 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
526 fileloc_makePreprocPrevious (fileloc loc)
528 if (fileloc_isDefined (loc))
532 return (fileloc_createPrim (FL_PREPROC, loc->fid,
533 loc->lineno - 1, 0));
537 return (fileloc_createPrim (FL_PREPROC, loc->fid,
542 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
546 fileloc_createBuiltin ()
548 return (fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0));
553 fileloc_createImport (cstring fname, int lineno)
555 fileId fid = fileTable_lookup (context_fileTable (), fname);
557 if (!fileId_isValid (fid))
559 fid = fileTable_addImportFile (context_fileTable (), fname);
562 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
566 static /*@only@*/ fileloc
567 fileloc_createPrim (flkind kind, fileId fid, int line, int col)
569 fileloc f = (fileloc) dmalloc (sizeof (*f));
576 DPRINTF (("Fileloc create: [%p] %s", f, fileloc_unparse (f)));
582 fileloc_createSpec (fileId fid, int line, int col)
584 return (fileloc_createPrim (FL_SPEC, fid, line, col));
588 fileloc fileloc_create (fileId fid, int line, int col)
590 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
593 /*@observer@*/ cstring
594 fileloc_filename (fileloc f)
596 return (fileloc_isDefined (f) ? fileTable_rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
599 /*@only@*/ cstring fileloc_outputFilename (fileloc f)
601 if (fileloc_isDefined (f))
603 return osd_outputPath (fileTable_rootFileName (f->fid));
607 return cstring_makeLiteral ("<unknown>");
612 fileloc_unparseFilename (fileloc f)
614 if (fileloc_isDefined (f))
619 return (message ("load file %q", fileloc_outputFilename (f)));
621 return (cstring_makeLiteral ("# builtin #"));
623 return (message ("import file %q", fileloc_outputFilename (f)));
625 return (cstring_makeLiteral ("<external>"));
627 return (fileloc_outputFilename (f));
630 return cstring_undefined;
634 fileloc_lineno (fileloc f)
636 return (fileloc_isDefined (f) ? f->lineno : -1);
640 fileloc_column (fileloc f)
642 return (fileloc_isDefined (f) ? f->column : -1);
646 fileloc_unparse (fileloc f)
648 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
649 bool htmlFormat = context_getFlag (FLG_HTMLFILEFORMAT);
650 cstring res = cstring_undefined;
652 if (fileloc_isDefined (f))
657 return (cstring_makeLiteral ("Command Line"));
661 res = message ("import file %q(%d)", fileloc_outputFilename (f), f->lineno);
665 res = message ("import file %q:%d", fileloc_outputFilename (f), f->lineno);
672 res = message ("%q(%d)", fileloc_outputFilename (f), f->lineno);
676 res = message ("%q:%d", fileloc_outputFilename (f), f->lineno);
682 res = cstring_makeLiteral ("<external>");
688 if (f->kind == FL_LIB)
690 fname = message ("load file %q", fileloc_outputFilename (f));
694 fname = fileloc_outputFilename (f);
697 if (context_getFlag (FLG_SHOWCOL))
699 if (fileloc_linenoDefined (f))
701 if (fileloc_columnDefined (f))
705 res = message ("%q(%d,%d)", fname, f->lineno, f->column);
709 res = message ("%q:%d:%d", fname, f->lineno, f->column);
716 res = message ("%q(%d)", fname, f->lineno);
720 res = message ("%q:%d", fname, f->lineno);
729 else if (fileloc_linenoDefined (f))
733 res = message ("%q(%d)", fname, f->lineno);
737 res = message ("%q:%d", fname, f->lineno);
747 if (htmlFormat && fileloc_linenoDefined (f))
749 res = message ("<a href=\"#line%d\">%s</a>", f->lineno, res);
754 res = cstring_makeLiteral ("< Location unknown >");
761 fileloc_unparseRaw (cstring fname, int lineno)
763 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
767 return (message ("%q(%d)", osd_outputPath (fname), lineno));
771 return (message ("%q:%d", osd_outputPath (fname), lineno));
776 fileloc_unparseRawCol (cstring fname, int lineno, int col)
778 if (context_getFlag (FLG_SHOWCOL))
780 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
784 return (message ("%q(%d,%d)", osd_outputPath (fname), lineno, col));
788 return (message ("%q:%d:%d", osd_outputPath (fname), lineno, col));
793 return fileloc_unparseRaw (fname, lineno);
797 bool fileloc_isSpecialFile (fileloc f)
799 if (fileloc_isDefined (f)
800 && fileId_isValid (f->fid))
802 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
810 bool fileloc_isHeader (fileloc f)
812 /* returns true if is not a .c file */
814 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
815 && fileId_isHeader (f->fid));
818 bool fileloc_isSpec (fileloc f)
820 return (fileloc_isDefined (f) &&
821 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
824 bool fileloc_isRealSpec (fileloc f)
826 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
829 bool fileloc_isLib (fileloc f)
831 return (fileloc_isDefined (f)
832 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
835 bool fileloc_isStandardLib (fileloc f)
837 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
840 /*@only@*/ cstring fileloc_unparseDirect (fileloc fl)
842 if (fileloc_isDefined (fl))
844 return (message ("%d:%d:%d",
845 /*@access fileId@*/ (int) fl->fid, /*@noaccess fileId@*/
846 fl->lineno, fl->column));
850 return (cstring_makeLiteral ("<undef>"));
854 bool fileloc_isUser (fileloc f)
856 return (fileloc_isDefined (f)
857 && f->kind == FL_NORMAL);