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"
41 static /*@only@*/ fileloc fileloc_createPrim (flkind p_kind, fileId p_fid, int p_line, int p_col) /*@*/ ;
44 ** builtin locs are never free'd
47 static /*@owned@*/ fileloc s_builtinLoc = fileloc_undefined;
48 static /*@owned@*/ fileloc s_externalLoc = fileloc_undefined;
50 void fileloc_destroyMod ()
52 if (fileloc_isDefined (s_builtinLoc))
55 s_builtinLoc = fileloc_undefined;
58 if (fileloc_isDefined (s_externalLoc))
60 sfree (s_externalLoc);
61 s_externalLoc = fileloc_undefined;
65 static flkind fileId_kind (fileId s)
67 cstring fname = fileTable_rootFileName (s);
69 if (fileLib_isLCLFile (fname))
73 else if (cstring_equalPrefix (fname, cstring_makeLiteralTemp (SYSTEM_LIBDIR)))
84 fileloc_decColumn (fileloc f, int x)
86 fileloc ret = fileloc_copy (f);
90 if (x > 0 && fileloc_isDefined (ret))
92 llassertprint (ret->column > x, ("decColumn: %d", x));
100 fileloc_noColumn (fileloc f)
102 if (fileloc_isDefined (f))
104 fileloc ret = fileloc_copy (f);
106 if (fileloc_isDefined (ret))
115 return fileloc_undefined;
120 fileloc_subColumn (fileloc f, int x)
122 if (x > 0 && fileloc_isDefined (f))
124 llassert (f->column > x);
129 fileloc fileloc_copy (fileloc f)
131 if (fileloc_isDefined (f))
133 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
136 ** Legitimate (spurious) errors reported since no copy
140 /*@i3@*/ return f; /* No copy is necessary. */
144 return (fileloc_createPrim (f->kind, f->fid, f->lineno, f->column));
149 return fileloc_undefined;
153 fileloc fileloc_update (/*@only@*/ fileloc old, fileloc fnew)
155 if (fileloc_isUndefined (fnew))
158 return fileloc_undefined;
160 else if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
162 return (fileloc_copy (fnew));
166 old->kind = fnew->kind;
167 old->fid = fnew->fid;
168 old->lineno = fnew->lineno;
169 old->column = fnew->column;
175 fileloc fileloc_updateFileId (/*@only@*/ fileloc old, fileId s)
177 if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
179 return (fileloc_create (s, 1, 1));
183 old->kind = fileId_kind (s);
193 fileloc_free (/*@only@*/ fileloc f)
195 if (fileloc_isDefined (f))
197 if (f != g_currentloc)
199 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
211 ; /* Don't free g_currentloc ever! */
218 fileloc_reallyFree (/*@only@*/ fileloc f)
220 if (fileloc_isDefined (f))
222 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
229 /*@-branchstate@*/ } /*@=branchstate@*/
233 cstring fileloc_getBase (fileloc f)
235 llassert (fileloc_isDefined (f));
237 return (fileTable_fileNameBase (f->fid));
241 fileloc_equal (fileloc f1, fileloc f2)
244 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
245 && ((f1->column == f2->column) &&
246 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
250 fileloc_compare (fileloc f1, fileloc f2)
252 if (fileloc_isUndefined (f1))
254 if (fileloc_isUndefined (f2)) return 0;
258 if (fileloc_isUndefined (f2))
262 INTCOMPARERETURN (f1->fid, f2->fid);
263 /*@noaccess fileId@*/
266 /* drl 8-11-01 fix what I think is a bug
267 lineno should more important than column number*/
269 INTCOMPARERETURN (f1->lineno, f2->lineno);
270 INTCOMPARERETURN (f1->column, f2->column);
276 fileloc_withinLines (fileloc f1, fileloc f2, int n)
279 return (fileloc_isDefined (f1) &&
280 fileloc_isDefined (f2) &&
281 ((f2->lineno <= f1->lineno + n)
282 && (f2->lineno >= f1->lineno)
283 && fileloc_sameFile (f1, f2)));
287 fileloc_lessthan (fileloc f1, fileloc f2)
290 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
291 && ((f1->fid < f2->fid)
292 || ((f1->fid == f2->fid)
293 && ((f1->lineno < f2->lineno)
294 || ((f1->lineno == f2->lineno)
295 && (f1->column < f2->column))))));
300 ** returns true if f1 and f2 are different files,
301 ** or f1 is before f2 in same file
305 fileloc_notAfter (fileloc f1, fileloc f2)
308 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
309 && ((f1->fid != f2->fid)
310 || ((f1->lineno < f2->lineno)
311 || ((f1->lineno == f2->lineno)
312 && (f1->column <= f2->column)))));
313 /*@noaccess fileId@*/
317 fileloc_isStandardLibrary (fileloc f)
319 cstring s = fileloc_getBase (f);
321 return (cstring_equalLit (s, LLSTDLIBS_NAME)
322 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
323 || cstring_equalLit (s, LLUNIXLIBS_NAME)
324 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
325 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
326 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
330 fileloc_sameFileAndLine (fileloc f1, fileloc f2)
332 return (fileloc_sameFile (f1, f2)
333 && (fileloc_isDefined (f1) && fileloc_isDefined (f2)
334 && f1->lineno == f2->lineno));
338 fileloc_sameFile (fileloc f1, fileloc f2)
340 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
341 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
347 return (fileId_equal (f1->fid, f2->fid));
352 fileloc_sameModule (fileloc f1, fileloc f2)
354 if (fileloc_isUndefined (f1))
356 return (fileloc_isUndefined (f2));
358 else if (fileloc_isUndefined (f2))
364 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
365 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
367 return fileloc_sameFile (f1, f2);
371 cstring s1 = fileloc_getBase (f1);
372 cstring s2 = fileloc_getBase (f2);
374 return (cstring_equal (s1, s2));
380 fileloc_sameBaseFile (fileloc f1, fileloc f2)
382 if (fileloc_isUndefined (f1))
384 return (fileloc_isUndefined (f2));
386 else if (fileloc_isUndefined (f2))
392 return (fileId_baseEqual (f1->fid, f2->fid));
396 bool fileloc_isSystemFile (fileloc f1)
398 if (fileloc_isDefined (f1)
399 && !fileloc_isBuiltin (f1)
400 && !fileloc_isExternal (f1))
402 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
408 bool fileloc_isXHFile (fileloc f1)
410 if (fileloc_isDefined (f1)
411 && !fileloc_isBuiltin (f1)
412 && !fileloc_isExternal (f1))
414 DPRINTF (("Fileloc is XH: [%p] %s", f1, fileloc_unparse (f1)));
415 return (fileTable_isXHFile (context_fileTable (), f1->fid));
422 fileloc_almostSameFile (fileloc f1, fileloc f2)
424 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
425 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
431 if (fileId_baseEqual (f1->fid, f2->fid))
435 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
436 || fileTable_isSystemFile (context_fileTable (), f2->fid))
440 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
441 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
443 return (cstring_equal (fileloc_getBase (f1),
444 fileloc_getBase (f2)));
454 fileloc_fromTok (ltoken t)
456 cstring fname = ltoken_fileName (t);
457 fileId fid = fileTable_lookup (context_fileTable (), fname);
460 if (!fileId_isValid (fid))
462 fid = fileTable_addLCLFile (context_fileTable (), fname);
465 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
471 fileloc_createLib (cstring ln)
474 fileId fid = fileTable_lookup (context_fileTable (), ln);
476 if (!fileId_isValid (fid))
478 fid = fileTable_addLibraryFile (context_fileTable (), ln);
481 if (cstring_equalPrefix (ln, cstring_makeLiteralTemp (SYSTEM_LIBDIR)))
486 return (fileloc_createPrim (fk, fid, 0, 0));
489 fileloc fileloc_createRc (cstring name)
491 fileId fid = fileTable_addFile (context_fileTable (), name);
493 return (fileloc_createPrim (FL_RC, fid, 0, 0));
496 fileloc fileloc_createExternal (void)
498 /*@i@*/ return (fileloc_getExternal ());
501 fileloc fileloc_getExternal (void)
503 if (s_externalLoc == fileloc_undefined)
505 s_externalLoc = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
508 return s_externalLoc;
511 fileloc fileloc_observeBuiltin ()
513 /*@-onlytrans@*/ return (fileloc_getBuiltin ()); /*@=onlytrans@*/
516 fileloc fileloc_getBuiltin ()
518 static /*@owned@*/ fileloc res = fileloc_undefined;
520 if (res == fileloc_undefined)
522 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
529 fileloc_makePreproc (fileloc loc)
531 if (fileloc_isDefined (loc))
533 return (fileloc_createPrim (FL_PREPROC, loc->fid,
534 loc->lineno, loc->column));
537 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
541 fileloc_makePreprocPrevious (fileloc loc)
543 if (fileloc_isDefined (loc))
547 return (fileloc_createPrim (FL_PREPROC, loc->fid,
548 loc->lineno - 1, 0));
552 return (fileloc_createPrim (FL_PREPROC, loc->fid,
557 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
560 /* We pretend the result is only, because fileloc_free doesn't free it! */
562 fileloc_createBuiltin ()
564 if (fileloc_isUndefined (s_builtinLoc))
566 s_builtinLoc = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
569 /*@-globstate@*/ /*@-retalias@*/
571 /*@=globstate@*/ /*@=retalias@*/
575 fileloc_createImport (cstring fname, int lineno)
577 fileId fid = fileTable_lookup (context_fileTable (), fname);
579 if (!fileId_isValid (fid))
581 fid = fileTable_addImportFile (context_fileTable (), fname);
584 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
587 static /*@only@*/ fileloc
588 fileloc_createPrim (flkind kind, fileId fid, int line, int col)
590 fileloc f = (fileloc) dmalloc (sizeof (*f));
597 DPRINTF (("Fileloc create: [%p] %s", f, fileloc_unparse (f)));
602 fileloc_createSpec (fileId fid, int line, int col)
604 return (fileloc_createPrim (FL_SPEC, fid, line, col));
607 fileloc fileloc_create (fileId fid, int line, int col)
609 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
612 /*@observer@*/ cstring
613 fileloc_filename (fileloc f)
615 return (fileloc_isDefined (f) ? fileTable_rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
618 /*@only@*/ cstring fileloc_outputFilename (fileloc f)
620 if (fileloc_isDefined (f))
622 if (fileId_isValid (f->fid))
624 return osd_outputPath (fileTable_rootFileName (f->fid));
628 return cstring_makeLiteral ("<invalid>");
633 return cstring_makeLiteral ("<unknown>");
638 fileloc_unparseFilename (fileloc f)
640 if (fileloc_isDefined (f))
645 return (message ("load file %q", fileloc_outputFilename (f)));
647 return (cstring_makeLiteral ("# builtin #"));
649 return (message ("import file %q", fileloc_outputFilename (f)));
651 return (cstring_makeLiteral ("<external>"));
653 return (fileloc_outputFilename (f));
656 return cstring_undefined;
660 fileloc_lineno (fileloc f)
662 return (fileloc_isDefined (f) ? f->lineno : -1);
666 fileloc_column (fileloc f)
668 return (fileloc_isDefined (f) ? f->column : -1);
672 fileloc_unparse (fileloc f)
674 static in_funparse = FALSE;
675 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
676 bool htmlFormat = context_getFlag (FLG_HTMLFILEFORMAT);
677 cstring res = cstring_undefined;
679 /* watch out for recursive calls when debugging... */
680 llassert (!in_funparse);
683 if (fileloc_isDefined (f))
689 res = cstring_makeLiteral ("Command Line");
695 res = message ("import file %q(%d)", fileloc_outputFilename (f), f->lineno);
699 res = message ("import file %q:%d", fileloc_outputFilename (f), f->lineno);
706 res = message ("%q(%d)", fileloc_outputFilename (f), f->lineno);
710 res = message ("%q:%d", fileloc_outputFilename (f), f->lineno);
716 res = cstring_makeLiteral ("<external>");
722 if (f->kind == FL_LIB)
724 fname = message ("load file %q", fileloc_outputFilename (f));
726 if (!context_getFlag (FLG_SHOWLOADLOC))
734 fname = fileloc_outputFilename (f);
737 if (context_getFlag (FLG_SHOWCOL))
739 if (fileloc_linenoDefined (f))
741 if (fileloc_columnDefined (f))
745 res = message ("%q(%d,%d)", fname, f->lineno, f->column);
749 res = message ("%q:%d:%d", fname, f->lineno, f->column);
756 res = message ("%q(%d)", fname, f->lineno);
760 res = message ("%q:%d", fname, f->lineno);
767 /*@-branchstate@*/ /* spurious warnings reporteded because of break above */
770 else if (fileloc_linenoDefined (f))
774 res = message ("%q(%d)", fname, f->lineno);
778 res = message ("%q:%d", fname, f->lineno);
788 if (htmlFormat && fileloc_linenoDefined (f))
790 res = message ("<a href=\"#line%d\">%s</a>", f->lineno, res);
795 res = cstring_makeLiteral ("< Location unknown >");
797 /*@=branchstate@*/ /*@i2523 this is a spurious warning because of the break */
804 fileloc_unparseRaw (cstring fname, int lineno)
806 if (!cstring_isEmpty (fname))
808 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
812 return (message ("%q(%d)", osd_outputPath (fname), lineno));
816 return (message ("%q:%d", osd_outputPath (fname), lineno));
821 return cstring_makeLiteral ("Command Line");
826 fileloc_unparseRawCol (cstring fname, int lineno, int col)
828 if (!cstring_isEmpty (fname))
830 if (context_getFlag (FLG_SHOWCOL))
832 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
836 return (message ("%q(%d,%d)", osd_outputPath (fname), lineno, col));
840 return (message ("%q:%d:%d", osd_outputPath (fname), lineno, col));
845 return fileloc_unparseRaw (fname, lineno);
850 return cstring_makeLiteral ("Command Line");
854 bool fileloc_isSpecialFile (fileloc f)
856 if (fileloc_isDefined (f)
857 && fileId_isValid (f->fid))
859 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
867 bool fileloc_isHeader (fileloc f)
869 /* returns true if is not a .c file */
871 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
872 && fileId_isHeader (f->fid));
875 bool fileloc_isSpec (fileloc f)
877 return (fileloc_isDefined (f) &&
878 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
881 bool fileloc_isRealSpec (fileloc f)
883 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
886 bool fileloc_isLib (fileloc f)
888 return (fileloc_isDefined (f)
889 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
892 bool fileloc_isStandardLib (fileloc f)
894 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
897 /*@only@*/ cstring fileloc_unparseDirect (fileloc fl)
899 if (fileloc_isDefined (fl))
901 return (message ("%d:%d:%d",
902 /*@access fileId@*/ (int) fl->fid, /*@noaccess fileId@*/
903 fl->lineno, fl->column));
907 return (cstring_makeLiteral ("<undef>"));
911 bool fileloc_isUser (fileloc f)
913 return (fileloc_isDefined (f)
914 && f->kind == FL_NORMAL);