]> andersk Git - splint.git/blame - src/fileloc.c
REmoved non-working test from make file and changed .expects to reflect the new messa...
[splint.git] / src / fileloc.c
CommitLineData
616915dd 1/*
2** LCLint - annotation-assisted static program checker
28bf4b0b 3** Copyright (C) 1994-2001 University of Virginia,
616915dd 4** Massachusetts Institute of Technology
5**
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.
10**
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.
15**
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.
19**
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
23*/
24/*
25** fileloc.c
26*/
27/*
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().
34 */
35
36# include "lclintMacros.nf"
37# include "llbasic.h"
38# include "fileIdList.h"
39# include "portab.h"
40
41static /*@only@*/ fileloc fileloc_createPrim (flkind p_kind, fileId p_fid, int p_line, int p_col);
42
43static flkind fileId_kind (fileId s)
44{
45 cstring fname = rootFileName (s);
46
28bf4b0b 47 if (fileLib_isLCLFile (fname))
616915dd 48 {
49 return (FL_SPEC);
50 }
51 else if (cstring_equalPrefix (fname, SYSTEM_LIBDIR))
52 {
53 return (FL_STDHDR);
54 }
55 else
56 {
57 return (FL_NORMAL);
58 }
59}
60
61fileloc
62fileloc_decColumn (fileloc f, int x)
63{
64 fileloc ret = fileloc_copy (f);
65 if (x > 0 && fileloc_isDefined (ret))
66 {
67 llassertprint (ret->column > x, ("decColumn: %d", x));
68 ret->column -= x;
69 }
70
71 return ret;
72}
73
74fileloc
75fileloc_noColumn (fileloc f)
76{
77 if (fileloc_isDefined (f))
78 {
79 fileloc ret = fileloc_copy (f);
80
81 if (fileloc_isDefined (ret))
82 {
83 ret->column = 0;
84 }
85
86 return ret;
87 }
88 else
89 {
90 return fileloc_undefined;
91 }
92}
93
94void
95fileloc_subColumn (fileloc f, int x)
96{
97 if (x > 0 && fileloc_isDefined (f))
98 {
99 llassert (f->column > x);
100 f->column -= x;
101 }
102}
103
104fileloc fileloc_copy (fileloc f)
105{
106 if (fileloc_isDefined (f))
107 {
108 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
109 {
110 /*
111 ** Legitimate (spurious) errors reported since no copy
112 ** is made.
113 */
114
115 /*@i3@*/ return f; /* No copy is necessary. */
116 }
117 else
118 {
119 return (fileloc_createPrim (f->kind, f->fid, f->lineno, f->column));
120 }
121 }
122 else
123 {
124 return fileloc_undefined;
125 }
126}
127
128fileloc fileloc_update (/*@only@*/ fileloc old, fileloc fnew)
129{
130 if (fileloc_isUndefined (fnew))
131 {
132 fileloc_free (old);
133 return fileloc_undefined;
134 }
135 else if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
136 {
137 return (fileloc_copy (fnew));
138 }
139 else
140 {
141 old->kind = fnew->kind;
142 old->fid = fnew->fid;
143 old->lineno = fnew->lineno;
144 old->column = fnew->column;
145
146 return old;
147 }
148}
149
150fileloc fileloc_updateFileId (/*@only@*/ fileloc old, fileId s)
151{
152 if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
153 {
154 return (fileloc_create (s, 1, 1));
155 }
156 else
157 {
158 old->kind = fileId_kind (s);
159 old->fid = s;
160 old->lineno = 1;
161 old->column = 1;
162
163 return old;
164 }
165}
166
167void
168fileloc_free (/*@only@*/ fileloc f)
169{
170 if (fileloc_isDefined (f))
171 {
172 if (f != g_currentloc)
173 {
174 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
175 {
176 ; /* don't free */
177 }
178 else
179 {
ccf0a4a8 180 sfree (f);
616915dd 181 /*@-branchstate@*/
182 }
183 }
184 else
185 {
ccf0a4a8 186 ; /* Don't free g_currentloc ever! */
187 }
616915dd 188 /*@=branchstate@*/
189 }
190}
191
192void
193fileloc_reallyFree (/*@only@*/ fileloc f)
194{
195 if (fileloc_isDefined (f))
196 {
197 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
198 {
199 ; /* don't free */
200 }
201 else
202 {
203 sfree (f);
204 /*@-branchstate@*/ } /*@=branchstate@*/
205 }
206}
207
208cstring fileloc_getBase (fileloc f)
209{
210 llassert (fileloc_isDefined (f));
211
212 return (fileNameBase (f->fid));
213}
214
215bool
216fileloc_equal (fileloc f1, fileloc f2)
217{
218 return ((f1 == f2)
219 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
220 && ((f1->column == f2->column) &&
221 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
222}
223
224int
225fileloc_compare (fileloc f1, fileloc f2)
226{
227 if (fileloc_isUndefined (f1))
228 {
229 if (fileloc_isUndefined (f2)) return 0;
230 return -1;
231 }
232
233 if (fileloc_isUndefined (f2))
234 return 1;
235
236 /*@access fileId@*/
237 INTCOMPARERETURN (f1->fid, f2->fid);
238 /*@noaccess fileId@*/
239
240 INTCOMPARERETURN (f1->column, f2->column);
241 INTCOMPARERETURN (f1->lineno, f2->lineno);
242
243 return 0;
244}
245
246bool
247fileloc_withinLines (fileloc f1, fileloc f2, int n)
248{
249
250 return (fileloc_isDefined (f1) &&
251 fileloc_isDefined (f2) &&
252 ((f2->lineno <= f1->lineno + n)
253 && (f2->lineno >= f1->lineno)
254 && fileloc_sameFile (f1, f2)));
255}
256
257bool
258fileloc_lessthan (fileloc f1, fileloc f2)
259{
260 /*@access fileId*/
261 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
262 && ((f1->fid < f2->fid)
263 || ((f1->fid == f2->fid)
264 && ((f1->lineno < f2->lineno)
265 || ((f1->lineno == f2->lineno)
266 && (f1->column < f2->column))))));
267 /*@noaccess fileId*/
268}
269
270/*
271** returns true if f1 and f2 are different files,
272** or f1 is before f2 in same file
273*/
274
275bool
276fileloc_notAfter (fileloc f1, fileloc f2)
277{
278 /*@access fileId*/
279 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
280 && ((f1->fid != f2->fid)
281 || ((f1->lineno < f2->lineno)
282 || ((f1->lineno == f2->lineno)
283 && (f1->column <= f2->column)))));
284 /*@noaccess fileId@*/
285}
286
287# ifndef NOLCL
288bool
289fileloc_isStandardLibrary (fileloc f)
290{
291 cstring s = fileloc_getBase (f);
292
293 return (cstring_equalLit (s, LLSTDLIBS_NAME)
294 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
295 || cstring_equalLit (s, LLUNIXLIBS_NAME)
296 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
297 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
298 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
299}
300# endif
301
28bf4b0b 302bool
303fileloc_sameFileAndLine (fileloc f1, fileloc f2)
304{
305 return (fileloc_sameFile (f1, f2)
306 && (fileloc_isDefined (f1) && fileloc_isDefined (f2)
307 && f1->lineno == f2->lineno));
308}
309
616915dd 310bool
311fileloc_sameFile (fileloc f1, fileloc f2)
312{
313 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
314 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
315 {
316 return FALSE;
317 }
318 else
319 {
320 return (fileId_equal (f1->fid, f2->fid));
321 }
322}
323
324bool
325fileloc_sameModule (fileloc f1, fileloc f2)
326{
327 if (fileloc_isUndefined (f1))
328 {
329 return (fileloc_isUndefined (f2));
330 }
331 else if (fileloc_isUndefined (f2))
332 {
333 return (FALSE);
334 }
335 else
336 {
337 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
338 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
339 {
340 return fileloc_sameFile (f1, f2);
341 }
342 else
343 {
344 cstring s1 = fileloc_getBase (f1);
345 cstring s2 = fileloc_getBase (f2);
346
347 return (cstring_equal (s1, s2));
348 }
349 }
350}
351
352bool
353fileloc_sameBaseFile (fileloc f1, fileloc f2)
354{
355 if (fileloc_isUndefined (f1))
356 {
357 return (fileloc_isUndefined (f2));
358 }
359 else if (fileloc_isUndefined (f2))
360 {
361 return (FALSE);
362 }
363 else
364 {
365 return (fileId_baseEqual (f1->fid, f2->fid));
366 }
367}
368
369bool fileloc_isSystemFile (fileloc f1)
370{
371 if (fileloc_isDefined (f1)
372 && !fileloc_isBuiltin (f1)
373 && !fileloc_isExternal (f1))
374 {
375 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
376 }
377
378 return FALSE;
379}
380
28bf4b0b 381bool fileloc_isXHFile (fileloc f1)
382{
383 if (fileloc_isDefined (f1)
384 && !fileloc_isBuiltin (f1)
385 && !fileloc_isExternal (f1))
386 {
ccf0a4a8 387 DPRINTF (("Fileloc is XH: [%p] %s", f1, fileloc_unparse (f1)));
28bf4b0b 388 return (fileTable_isXHFile (context_fileTable (), f1->fid));
389 }
390
391 return FALSE;
392}
393
616915dd 394bool
395fileloc_almostSameFile (fileloc f1, fileloc f2)
396{
397 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
398 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
399 {
400 return FALSE;
401 }
402 else
403 {
404 if (fileId_baseEqual (f1->fid, f2->fid))
405 {
406 return TRUE;
407 }
408 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
409 || fileTable_isSystemFile (context_fileTable (), f2->fid))
410 {
411 return TRUE;
412 }
413 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
414 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
415 {
416 return (cstring_equal (fileloc_getBase (f1),
417 fileloc_getBase (f2)));
418 }
419 else
420 {
421 return FALSE;
422 }
423 }
424}
425
426# ifndef NOLCL
427/*@only@*/ fileloc
428fileloc_fromTok (ltoken t)
429{
430 cstring fname = ltoken_fileName (t);
431 fileId fid = fileTable_lookup (context_fileTable (), fname);
432 fileloc fl;
433
434 if (!fileId_isValid (fid))
435 {
436 fid = fileTable_addLCLFile (context_fileTable (), fname);
437 }
438
439 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
440
441 return (fl);
442}
443# endif
444
445/*@only@*/ fileloc
446fileloc_createLib (cstring ln)
447{
448 flkind fk = FL_LIB;
449 fileId fid = fileTable_lookup (context_fileTable (), ln);
450
451 if (!fileId_isValid (fid))
452 {
453 fid = fileTable_addLibraryFile (context_fileTable (), ln);
454 }
455
456 if (cstring_equalPrefix (ln, SYSTEM_LIBDIR))
457 {
458 fk = FL_STDLIB;
459 }
460
461 return (fileloc_createPrim (fk, fid, 0, 0));
462}
463
464fileloc fileloc_createRc (cstring name)
465{
466 fileId fid = fileTable_addFile (context_fileTable (), name);
467
468 return (fileloc_createPrim (FL_RC, fid, 0, 0));
469}
470
471fileloc fileloc_createExternal (void)
472{
473 /*@i@*/ return (fileloc_getExternal ());
474}
475
476fileloc fileloc_getExternal (void)
477{
478 static /*@owned@*/ fileloc res = fileloc_undefined;
479
480 if (res == fileloc_undefined)
481 {
482 res = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
483 }
484
485 return res;
486}
487
488fileloc fileloc_observeBuiltin ()
489{
490 /*@-onlytrans@*/ return (fileloc_getBuiltin ()); /*@=onlytrans@*/
491}
492
493fileloc fileloc_getBuiltin ()
494{
495 static /*@owned@*/ fileloc res = fileloc_undefined;
496
497 if (res == fileloc_undefined)
498 {
499 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
500 }
501
502 return res;
503}
504
505fileloc
506fileloc_makePreproc (fileloc loc)
507{
508 if (fileloc_isDefined (loc))
509 {
510 return (fileloc_createPrim (FL_PREPROC, loc->fid,
511 loc->lineno, loc->column));
512 }
513
514 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
515}
516
517fileloc
518fileloc_makePreprocPrevious (fileloc loc)
519{
520 if (fileloc_isDefined (loc))
521 {
522 if (loc->lineno > 1)
523 {
524 return (fileloc_createPrim (FL_PREPROC, loc->fid,
525 loc->lineno - 1, 0));
526 }
527 else
528 {
529 return (fileloc_createPrim (FL_PREPROC, loc->fid,
530 loc->lineno, 0));
531 }
532 }
533
534 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
535}
536
537/*@only@*/ fileloc
538fileloc_createBuiltin ()
539{
540 return (fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0));
541}
542
543# ifndef NOLCL
544/*@only@*/ fileloc
545fileloc_createImport (cstring fname, int lineno)
546{
547 fileId fid = fileTable_lookup (context_fileTable (), fname);
548
549 if (!fileId_isValid (fid))
550 {
551 fid = fileTable_addImportFile (context_fileTable (), fname);
552 }
553
554 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
555}
556# endif
557
558static /*@only@*/ fileloc
559fileloc_createPrim (flkind kind, fileId fid, int line, int col)
560{
561 fileloc f = (fileloc) dmalloc (sizeof (*f));
562
563 f->kind = kind;
564 f->fid = fid;
565 f->lineno = line;
566 f->column = col;
28bf4b0b 567
ccf0a4a8 568 DPRINTF (("Fileloc create: [%p] %s", f, fileloc_unparse (f)));
616915dd 569 return (f);
570}
571
572# ifndef NOLCL
573/*@only@*/ fileloc
574fileloc_createSpec (fileId fid, int line, int col)
575{
576 return (fileloc_createPrim (FL_SPEC, fid, line, col));
577}
578# endif
579
580fileloc fileloc_create (fileId fid, int line, int col)
581{
582 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
583}
584
585/*@observer@*/ cstring
586fileloc_filename (fileloc f)
587{
588 return (fileloc_isDefined (f)
589 ? rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
590}
591
592cstring
593fileloc_unparseFilename (fileloc f)
594{
595 if (fileloc_isDefined (f))
596 {
597 switch (f->kind)
598 {
599 case FL_LIB:
600 return (message ("load file %s", fileloc_filename (f)));
601 case FL_BUILTIN:
602 return (cstring_makeLiteral ("# builtin #"));
603 case FL_IMPORT:
604 return (message ("import file %s", fileloc_filename (f)));
605 case FL_EXTERNAL:
606 return (cstring_makeLiteral ("<external>"));
607 default:
608 return (cstring_copy (fileloc_filename (f)));
609 }
610 }
611 return cstring_undefined;
612}
613
614int
615fileloc_lineno (fileloc f)
616{
617 return (fileloc_isDefined (f) ? f->lineno : -1);
618}
619
620int
621fileloc_column (fileloc f)
622{
623 return (fileloc_isDefined (f) ? f->column : -1);
624}
625
626/*@only@*/ cstring
627fileloc_unparse (fileloc f)
628{
629 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
630
631 if (fileloc_isDefined (f))
632 {
28bf4b0b 633 switch (f->kind)
616915dd 634 {
616915dd 635 case FL_BUILTIN:
636 return (cstring_makeLiteral ("Command Line"));
637 case FL_IMPORT:
638 if (parenFormat)
639 {
640 return (message ("import file %s(%d)", rootFileName (f->fid), f->lineno));
641 }
642 else
643 {
644 return (message ("import file %s:%d", rootFileName (f->fid), f->lineno));
645 }
646 case FL_PREPROC:
647 if (parenFormat)
648 {
649 return (message ("%s(%d)", rootFileName (f->fid), f->lineno));
650 }
651 else
652 {
653 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
654 }
655 case FL_EXTERNAL:
656 return (cstring_makeLiteral ("<external>"));
657 default:
28bf4b0b 658 {
659 cstring fname;
660
661 if (f->kind == FL_LIB)
662 {
663 fname = message ("load file %s", rootFileName (f->fid));
664 cstring_markOwned (fname); /*@i32 memory leak...@*/
665 }
666 else
667 {
668 fname = rootFileName (f->fid);
669 }
670
671 if (context_getFlag (FLG_SHOWCOL))
672 {
673 if (fileloc_linenoDefined (f))
674 {
675 if (fileloc_columnDefined (f))
676 {
677 if (parenFormat)
678 {
679 return (message ("%s(%d,%d)", fname, f->lineno, f->column));
680 }
681 else
682 {
683 return (message ("%s:%d:%d", fname, f->lineno, f->column));
684 }
685 }
686 else
687 {
688 if (parenFormat)
689 {
690 return (message ("%s(%d)", fname, f->lineno));
691 }
692 else
693 {
694 return (message ("%s:%d", fname, f->lineno));
695 }
696 }
697 }
698 return (cstring_copy (fname));
699 }
700 else if (fileloc_linenoDefined (f))
701 {
702 if (parenFormat)
703 {
704 return (message ("%s(%d)", fname, f->lineno));
705 }
706 else
707 {
708 return (message ("%s:%d", fname, f->lineno));
709 }
710 }
711 else
712 {
713 return (cstring_copy (fname));
714 }
715 }
616915dd 716 }
717 }
28bf4b0b 718
616915dd 719 return (cstring_makeLiteral ("< Location unknown >"));
720}
721
722/*@only@*/ cstring
723fileloc_unparseRaw (cstring fname, int lineno)
724{
725 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
726
727 if (parenFormat)
728 {
729 return (message ("%s(%d)", fname, lineno));
730 }
731 else
732 {
733 return (message ("%s:%d", fname, lineno));
734 }
735}
736
737/*@only@*/ cstring
738fileloc_unparseRawCol (cstring fname, int lineno, int col)
739{
740 if (context_getFlag (FLG_SHOWCOL))
741 {
742 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
743
744 if (parenFormat)
745 {
746 return (message ("%s(%d,%d)", fname, lineno, col));
747 }
748 else
749 {
750 return (message ("%s:%d:%d", fname, lineno, col));
751 }
752 }
753 else
754 {
755 return fileloc_unparseRaw (fname, lineno);
756 }
757}
758
759bool fileloc_isSpecialFile (fileloc f)
760{
761 if (fileloc_isDefined (f)
762 && fileId_isValid (f->fid))
763 {
764 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
765 }
766 else
767 {
768 return FALSE;
769 }
770}
771
772bool fileloc_isHeader (fileloc f)
773{
774 /* returns true if is not a .c file */
775
776 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
777 && fileId_isHeader (f->fid));
778}
779
780bool fileloc_isSpec (fileloc f)
781{
782 return (fileloc_isDefined (f) &&
783 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
784}
785
786bool fileloc_isRealSpec (fileloc f)
787{
788 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
789}
790
616915dd 791bool fileloc_isLib (fileloc f)
792{
793 return (fileloc_isDefined (f)
794 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
795}
796
797bool fileloc_isStandardLib (fileloc f)
798{
799 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
800}
801
802/*@only@*/ cstring fileloc_unparseDirect (fileloc fl)
803{
804 if (fileloc_isDefined (fl))
805 {
806 return (message ("%d:%d:%d",
807 /*@access fileId@*/ (int) fl->fid, /*@noaccess fileId@*/
808 fl->lineno, fl->column));
809 }
810 else
811 {
812 return (cstring_makeLiteral ("<undef>"));
813 }
814}
815
816bool fileloc_isUser (fileloc f)
817{
818 return (fileloc_isDefined (f)
819 && f->kind == FL_NORMAL);
820}
821
822
823
824
This page took 0.360136 seconds and 5 git commands to generate.