2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 (fileLib_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))
186 ; /* Don't free g_currentloc ever! */
193 fileloc_reallyFree (/*@only@*/ fileloc f)
195 if (fileloc_isDefined (f))
197 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
204 /*@-branchstate@*/ } /*@=branchstate@*/
208 cstring fileloc_getBase (fileloc f)
210 llassert (fileloc_isDefined (f));
212 return (fileNameBase (f->fid));
216 fileloc_equal (fileloc f1, fileloc f2)
219 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
220 && ((f1->column == f2->column) &&
221 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
225 fileloc_compare (fileloc f1, fileloc f2)
227 if (fileloc_isUndefined (f1))
229 if (fileloc_isUndefined (f2)) return 0;
233 if (fileloc_isUndefined (f2))
237 INTCOMPARERETURN (f1->fid, f2->fid);
238 /*@noaccess fileId@*/
241 /* drl 8-11-01 fix what I think is a bug
242 line no should more important than column number*/
243 INTCOMPARERETURN (f1->lineno, f2->lineno);
245 INTCOMPARERETURN (f1->column, f2->column);
252 fileloc_withinLines (fileloc f1, fileloc f2, int n)
255 return (fileloc_isDefined (f1) &&
256 fileloc_isDefined (f2) &&
257 ((f2->lineno <= f1->lineno + n)
258 && (f2->lineno >= f1->lineno)
259 && fileloc_sameFile (f1, f2)));
263 fileloc_lessthan (fileloc f1, fileloc f2)
266 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
267 && ((f1->fid < f2->fid)
268 || ((f1->fid == f2->fid)
269 && ((f1->lineno < f2->lineno)
270 || ((f1->lineno == f2->lineno)
271 && (f1->column < f2->column))))));
276 ** returns true if f1 and f2 are different files,
277 ** or f1 is before f2 in same file
281 fileloc_notAfter (fileloc f1, fileloc f2)
284 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
285 && ((f1->fid != f2->fid)
286 || ((f1->lineno < f2->lineno)
287 || ((f1->lineno == f2->lineno)
288 && (f1->column <= f2->column)))));
289 /*@noaccess fileId@*/
294 fileloc_isStandardLibrary (fileloc f)
296 cstring s = fileloc_getBase (f);
298 return (cstring_equalLit (s, LLSTDLIBS_NAME)
299 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
300 || cstring_equalLit (s, LLUNIXLIBS_NAME)
301 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
302 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
303 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
308 fileloc_sameFileAndLine (fileloc f1, fileloc f2)
310 return (fileloc_sameFile (f1, f2)
311 && (fileloc_isDefined (f1) && fileloc_isDefined (f2)
312 && f1->lineno == f2->lineno));
316 fileloc_sameFile (fileloc f1, fileloc f2)
318 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
319 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
325 return (fileId_equal (f1->fid, f2->fid));
330 fileloc_sameModule (fileloc f1, fileloc f2)
332 if (fileloc_isUndefined (f1))
334 return (fileloc_isUndefined (f2));
336 else if (fileloc_isUndefined (f2))
342 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
343 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
345 return fileloc_sameFile (f1, f2);
349 cstring s1 = fileloc_getBase (f1);
350 cstring s2 = fileloc_getBase (f2);
352 return (cstring_equal (s1, s2));
358 fileloc_sameBaseFile (fileloc f1, fileloc f2)
360 if (fileloc_isUndefined (f1))
362 return (fileloc_isUndefined (f2));
364 else if (fileloc_isUndefined (f2))
370 return (fileId_baseEqual (f1->fid, f2->fid));
374 bool fileloc_isSystemFile (fileloc f1)
376 if (fileloc_isDefined (f1)
377 && !fileloc_isBuiltin (f1)
378 && !fileloc_isExternal (f1))
380 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
386 bool fileloc_isXHFile (fileloc f1)
388 if (fileloc_isDefined (f1)
389 && !fileloc_isBuiltin (f1)
390 && !fileloc_isExternal (f1))
392 DPRINTF (("Fileloc is XH: [%p] %s", f1, fileloc_unparse (f1)));
393 return (fileTable_isXHFile (context_fileTable (), f1->fid));
400 fileloc_almostSameFile (fileloc f1, fileloc f2)
402 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
403 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
409 if (fileId_baseEqual (f1->fid, f2->fid))
413 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
414 || fileTable_isSystemFile (context_fileTable (), f2->fid))
418 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
419 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
421 return (cstring_equal (fileloc_getBase (f1),
422 fileloc_getBase (f2)));
433 fileloc_fromTok (ltoken t)
435 cstring fname = ltoken_fileName (t);
436 fileId fid = fileTable_lookup (context_fileTable (), fname);
439 if (!fileId_isValid (fid))
441 fid = fileTable_addLCLFile (context_fileTable (), fname);
444 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
451 fileloc_createLib (cstring ln)
454 fileId fid = fileTable_lookup (context_fileTable (), ln);
456 if (!fileId_isValid (fid))
458 fid = fileTable_addLibraryFile (context_fileTable (), ln);
461 if (cstring_equalPrefix (ln, SYSTEM_LIBDIR))
466 return (fileloc_createPrim (fk, fid, 0, 0));
469 fileloc fileloc_createRc (cstring name)
471 fileId fid = fileTable_addFile (context_fileTable (), name);
473 return (fileloc_createPrim (FL_RC, fid, 0, 0));
476 fileloc fileloc_createExternal (void)
478 /*@i@*/ return (fileloc_getExternal ());
481 fileloc fileloc_getExternal (void)
483 static /*@owned@*/ fileloc res = fileloc_undefined;
485 if (res == fileloc_undefined)
487 res = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
493 fileloc fileloc_observeBuiltin ()
495 /*@-onlytrans@*/ return (fileloc_getBuiltin ()); /*@=onlytrans@*/
498 fileloc fileloc_getBuiltin ()
500 static /*@owned@*/ fileloc res = fileloc_undefined;
502 if (res == fileloc_undefined)
504 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
511 fileloc_makePreproc (fileloc loc)
513 if (fileloc_isDefined (loc))
515 return (fileloc_createPrim (FL_PREPROC, loc->fid,
516 loc->lineno, loc->column));
519 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
523 fileloc_makePreprocPrevious (fileloc loc)
525 if (fileloc_isDefined (loc))
529 return (fileloc_createPrim (FL_PREPROC, loc->fid,
530 loc->lineno - 1, 0));
534 return (fileloc_createPrim (FL_PREPROC, loc->fid,
539 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
543 fileloc_createBuiltin ()
545 return (fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0));
550 fileloc_createImport (cstring fname, int lineno)
552 fileId fid = fileTable_lookup (context_fileTable (), fname);
554 if (!fileId_isValid (fid))
556 fid = fileTable_addImportFile (context_fileTable (), fname);
559 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
563 static /*@only@*/ fileloc
564 fileloc_createPrim (flkind kind, fileId fid, int line, int col)
566 fileloc f = (fileloc) dmalloc (sizeof (*f));
573 DPRINTF (("Fileloc create: [%p] %s", f, fileloc_unparse (f)));
579 fileloc_createSpec (fileId fid, int line, int col)
581 return (fileloc_createPrim (FL_SPEC, fid, line, col));
585 fileloc fileloc_create (fileId fid, int line, int col)
587 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
590 /*@observer@*/ cstring
591 fileloc_filename (fileloc f)
593 return (fileloc_isDefined (f)
594 ? rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
598 fileloc_unparseFilename (fileloc f)
600 if (fileloc_isDefined (f))
605 return (message ("load file %s", fileloc_filename (f)));
607 return (cstring_makeLiteral ("# builtin #"));
609 return (message ("import file %s", fileloc_filename (f)));
611 return (cstring_makeLiteral ("<external>"));
613 return (cstring_copy (fileloc_filename (f)));
616 return cstring_undefined;
620 fileloc_lineno (fileloc f)
622 return (fileloc_isDefined (f) ? f->lineno : -1);
626 fileloc_column (fileloc f)
628 return (fileloc_isDefined (f) ? f->column : -1);
632 fileloc_unparse (fileloc f)
634 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
636 if (fileloc_isDefined (f))
641 return (cstring_makeLiteral ("Command Line"));
645 return (message ("import file %s(%d)", rootFileName (f->fid), f->lineno));
649 return (message ("import file %s:%d", rootFileName (f->fid), f->lineno));
654 return (message ("%s(%d)", rootFileName (f->fid), f->lineno));
658 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
661 return (cstring_makeLiteral ("<external>"));
666 if (f->kind == FL_LIB)
668 fname = message ("load file %s", rootFileName (f->fid));
669 cstring_markOwned (fname); /*@i32 memory leak...@*/
673 fname = rootFileName (f->fid);
676 if (context_getFlag (FLG_SHOWCOL))
678 if (fileloc_linenoDefined (f))
680 if (fileloc_columnDefined (f))
684 return (message ("%s(%d,%d)", fname, f->lineno, f->column));
688 return (message ("%s:%d:%d", fname, f->lineno, f->column));
695 return (message ("%s(%d)", fname, f->lineno));
699 return (message ("%s:%d", fname, f->lineno));
703 return (cstring_copy (fname));
705 else if (fileloc_linenoDefined (f))
709 return (message ("%s(%d)", fname, f->lineno));
713 return (message ("%s:%d", fname, f->lineno));
718 return (cstring_copy (fname));
724 return (cstring_makeLiteral ("< Location unknown >"));
728 fileloc_unparseRaw (cstring fname, int lineno)
730 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
734 return (message ("%s(%d)", fname, lineno));
738 return (message ("%s:%d", fname, lineno));
743 fileloc_unparseRawCol (cstring fname, int lineno, int col)
745 if (context_getFlag (FLG_SHOWCOL))
747 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
751 return (message ("%s(%d,%d)", fname, lineno, col));
755 return (message ("%s:%d:%d", fname, lineno, col));
760 return fileloc_unparseRaw (fname, lineno);
764 bool fileloc_isSpecialFile (fileloc f)
766 if (fileloc_isDefined (f)
767 && fileId_isValid (f->fid))
769 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
777 bool fileloc_isHeader (fileloc f)
779 /* returns true if is not a .c file */
781 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
782 && fileId_isHeader (f->fid));
785 bool fileloc_isSpec (fileloc f)
787 return (fileloc_isDefined (f) &&
788 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
791 bool fileloc_isRealSpec (fileloc f)
793 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
796 bool fileloc_isLib (fileloc f)
798 return (fileloc_isDefined (f)
799 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
802 bool fileloc_isStandardLib (fileloc f)
804 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
807 /*@only@*/ cstring fileloc_unparseDirect (fileloc fl)
809 if (fileloc_isDefined (fl))
811 return (message ("%d:%d:%d",
812 /*@access fileId@*/ (int) fl->fid, /*@noaccess fileId@*/
813 fl->lineno, fl->column));
817 return (cstring_makeLiteral ("<undef>"));
821 bool fileloc_isUser (fileloc f)
823 return (fileloc_isDefined (f)
824 && f->kind == FL_NORMAL);