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