00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 # include "lclintMacros.nf"
00037 # include "llbasic.h"
00038 # include "fileIdList.h"
00039 # include "portab.h"
00040
00041 static fileloc fileloc_createPrim (flkind p_kind, fileId p_fid, int p_line, int p_col);
00042
00043 static flkind fileId_kind (fileId s)
00044 {
00045 cstring fname = rootFileName (s);
00046
00047 if (isLCLfile (fname))
00048 {
00049 return (FL_SPEC);
00050 }
00051 else if (cstring_equalPrefix (fname, SYSTEM_LIBDIR))
00052 {
00053 return (FL_STDHDR);
00054 }
00055 else
00056 {
00057 return (FL_NORMAL);
00058 }
00059 }
00060
00061 fileloc
00062 fileloc_decColumn (fileloc f, int x)
00063 {
00064 fileloc ret = fileloc_copy (f);
00065 if (x > 0 && fileloc_isDefined (ret))
00066 {
00067 llassertprint (ret->column > x, ("decColumn: %d", x));
00068 ret->column -= x;
00069 }
00070
00071 return ret;
00072 }
00073
00074 fileloc
00075 fileloc_noColumn (fileloc f)
00076 {
00077 if (fileloc_isDefined (f))
00078 {
00079 fileloc ret = fileloc_copy (f);
00080
00081 if (fileloc_isDefined (ret))
00082 {
00083 ret->column = 0;
00084 }
00085
00086 return ret;
00087 }
00088 else
00089 {
00090 return fileloc_undefined;
00091 }
00092 }
00093
00094 void
00095 fileloc_subColumn (fileloc f, int x)
00096 {
00097 if (x > 0 && fileloc_isDefined (f))
00098 {
00099 llassert (f->column > x);
00100 f->column -= x;
00101 }
00102 }
00103
00104 fileloc fileloc_copy (fileloc f)
00105 {
00106 if (fileloc_isDefined (f))
00107 {
00108 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
00109 {
00110
00111
00112
00113
00114
00115 return f;
00116 }
00117 else
00118 {
00119 return (fileloc_createPrim (f->kind, f->fid, f->lineno, f->column));
00120 }
00121 }
00122 else
00123 {
00124 return fileloc_undefined;
00125 }
00126 }
00127
00128 fileloc fileloc_update ( fileloc old, fileloc fnew)
00129 {
00130 if (fileloc_isUndefined (fnew))
00131 {
00132 fileloc_free (old);
00133 return fileloc_undefined;
00134 }
00135 else if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
00136 {
00137 return (fileloc_copy (fnew));
00138 }
00139 else
00140 {
00141 old->kind = fnew->kind;
00142 old->fid = fnew->fid;
00143 old->lineno = fnew->lineno;
00144 old->column = fnew->column;
00145
00146 return old;
00147 }
00148 }
00149
00150 fileloc fileloc_updateFileId ( fileloc old, fileId s)
00151 {
00152 if (fileloc_isUndefined (old) || fileloc_isBuiltin (old) || fileloc_isExternal (old))
00153 {
00154 return (fileloc_create (s, 1, 1));
00155 }
00156 else
00157 {
00158 old->kind = fileId_kind (s);
00159 old->fid = s;
00160 old->lineno = 1;
00161 old->column = 1;
00162
00163 return old;
00164 }
00165 }
00166
00167 void
00168 fileloc_free ( fileloc f)
00169 {
00170 if (fileloc_isDefined (f))
00171 {
00172 if (f != g_currentloc)
00173 {
00174 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
00175 {
00176 ;
00177 }
00178 else
00179 {
00180 sfree (f);
00181
00182 }
00183 }
00184 else
00185 {
00186 }
00187
00188 }
00189 }
00190
00191 void
00192 fileloc_reallyFree ( fileloc f)
00193 {
00194 if (fileloc_isDefined (f))
00195 {
00196 if (fileloc_isBuiltin (f) || fileloc_isExternal (f))
00197 {
00198 ;
00199 }
00200 else
00201 {
00202 sfree (f);
00203 }
00204 }
00205 }
00206
00207 cstring fileloc_getBase (fileloc f)
00208 {
00209 llassert (fileloc_isDefined (f));
00210
00211 return (fileNameBase (f->fid));
00212 }
00213
00214 bool
00215 fileloc_equal (fileloc f1, fileloc f2)
00216 {
00217 return ((f1 == f2)
00218 || (fileloc_isDefined (f1) && fileloc_isDefined (f2)
00219 && ((f1->column == f2->column) &&
00220 (f1->lineno == f2->lineno) && fileloc_sameFile (f1, f2))));
00221 }
00222
00223 int
00224 fileloc_compare (fileloc f1, fileloc f2)
00225 {
00226 if (fileloc_isUndefined (f1))
00227 {
00228 if (fileloc_isUndefined (f2)) return 0;
00229 return -1;
00230 }
00231
00232 if (fileloc_isUndefined (f2))
00233 return 1;
00234
00235
00236 INTCOMPARERETURN (f1->fid, f2->fid);
00237
00238
00239 INTCOMPARERETURN (f1->column, f2->column);
00240 INTCOMPARERETURN (f1->lineno, f2->lineno);
00241
00242 return 0;
00243 }
00244
00245 bool
00246 fileloc_withinLines (fileloc f1, fileloc f2, int n)
00247 {
00248
00249 return (fileloc_isDefined (f1) &&
00250 fileloc_isDefined (f2) &&
00251 ((f2->lineno <= f1->lineno + n)
00252 && (f2->lineno >= f1->lineno)
00253 && fileloc_sameFile (f1, f2)));
00254 }
00255
00256 bool
00257 fileloc_lessthan (fileloc f1, fileloc f2)
00258 {
00259
00260 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
00261 && ((f1->fid < f2->fid)
00262 || ((f1->fid == f2->fid)
00263 && ((f1->lineno < f2->lineno)
00264 || ((f1->lineno == f2->lineno)
00265 && (f1->column < f2->column))))));
00266
00267 }
00268
00269
00270
00271
00272
00273
00274 bool
00275 fileloc_notAfter (fileloc f1, fileloc f2)
00276 {
00277
00278 return ((fileloc_isDefined (f1) && fileloc_isDefined (f2))
00279 && ((f1->fid != f2->fid)
00280 || ((f1->lineno < f2->lineno)
00281 || ((f1->lineno == f2->lineno)
00282 && (f1->column <= f2->column)))));
00283
00284 }
00285
00286 # ifndef NOLCL
00287 bool
00288 fileloc_isStandardLibrary (fileloc f)
00289 {
00290 cstring s = fileloc_getBase (f);
00291
00292 return (cstring_equalLit (s, LLSTDLIBS_NAME)
00293 || cstring_equalLit (s, LLSTRICTLIBS_NAME)
00294 || cstring_equalLit (s, LLUNIXLIBS_NAME)
00295 || cstring_equalLit (s, LLUNIXSTRICTLIBS_NAME)
00296 || cstring_equalLit (s, LLPOSIXSTRICTLIBS_NAME)
00297 || cstring_equalLit (s, LLPOSIXLIBS_NAME));
00298 }
00299 # endif
00300
00301 bool
00302 fileloc_sameFile (fileloc f1, fileloc f2)
00303 {
00304 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
00305 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
00306 {
00307 return FALSE;
00308 }
00309 else
00310 {
00311 return (fileId_equal (f1->fid, f2->fid));
00312 }
00313 }
00314
00315 bool
00316 fileloc_sameModule (fileloc f1, fileloc f2)
00317 {
00318 if (fileloc_isUndefined (f1))
00319 {
00320 return (fileloc_isUndefined (f2));
00321 }
00322 else if (fileloc_isUndefined (f2))
00323 {
00324 return (FALSE);
00325 }
00326 else
00327 {
00328 if (fileloc_isBuiltin (f1) || fileloc_isBuiltin (f2)
00329 || fileloc_isExternal (f1) || fileloc_isExternal (f2))
00330 {
00331 return fileloc_sameFile (f1, f2);
00332 }
00333 else
00334 {
00335 cstring s1 = fileloc_getBase (f1);
00336 cstring s2 = fileloc_getBase (f2);
00337
00338 return (cstring_equal (s1, s2));
00339 }
00340 }
00341 }
00342
00343 bool
00344 fileloc_sameBaseFile (fileloc f1, fileloc f2)
00345 {
00346 if (fileloc_isUndefined (f1))
00347 {
00348 return (fileloc_isUndefined (f2));
00349 }
00350 else if (fileloc_isUndefined (f2))
00351 {
00352 return (FALSE);
00353 }
00354 else
00355 {
00356 return (fileId_baseEqual (f1->fid, f2->fid));
00357 }
00358 }
00359
00360 bool fileloc_isSystemFile (fileloc f1)
00361 {
00362 if (fileloc_isDefined (f1)
00363 && !fileloc_isBuiltin (f1)
00364 && !fileloc_isExternal (f1))
00365 {
00366 return (fileTable_isSystemFile (context_fileTable (), f1->fid));
00367 }
00368
00369 return FALSE;
00370 }
00371
00372 bool
00373 fileloc_almostSameFile (fileloc f1, fileloc f2)
00374 {
00375 if ((fileloc_isUndefined (f1) || (fileloc_isUndefined (f2))
00376 || (fileloc_isLib (f1)) || (fileloc_isLib (f2))))
00377 {
00378 return FALSE;
00379 }
00380 else
00381 {
00382 if (fileId_baseEqual (f1->fid, f2->fid))
00383 {
00384 return TRUE;
00385 }
00386 else if (fileTable_isSystemFile (context_fileTable (), f1->fid)
00387 || fileTable_isSystemFile (context_fileTable (), f2->fid))
00388 {
00389 return TRUE;
00390 }
00391 else if (fileTable_isSpecialFile (context_fileTable (), f1->fid)
00392 || fileTable_isSpecialFile (context_fileTable (), f2->fid))
00393 {
00394 return (cstring_equal (fileloc_getBase (f1),
00395 fileloc_getBase (f2)));
00396 }
00397 else
00398 {
00399 return FALSE;
00400 }
00401 }
00402 }
00403
00404 # ifndef NOLCL
00405 fileloc
00406 fileloc_fromTok (ltoken t)
00407 {
00408 cstring fname = ltoken_fileName (t);
00409 fileId fid = fileTable_lookup (context_fileTable (), fname);
00410 fileloc fl;
00411
00412 if (!fileId_isValid (fid))
00413 {
00414 fid = fileTable_addLCLFile (context_fileTable (), fname);
00415 }
00416
00417 fl = fileloc_create (fid, (int) ltoken_getLine (t), (int) ltoken_getCol (t));
00418
00419 return (fl);
00420 }
00421 # endif
00422
00423 fileloc
00424 fileloc_createLib (cstring ln)
00425 {
00426 flkind fk = FL_LIB;
00427 fileId fid = fileTable_lookup (context_fileTable (), ln);
00428
00429 if (!fileId_isValid (fid))
00430 {
00431 fid = fileTable_addLibraryFile (context_fileTable (), ln);
00432 }
00433
00434 if (cstring_equalPrefix (ln, SYSTEM_LIBDIR))
00435 {
00436 fk = FL_STDLIB;
00437 }
00438
00439 return (fileloc_createPrim (fk, fid, 0, 0));
00440 }
00441
00442 fileloc fileloc_createRc (cstring name)
00443 {
00444 fileId fid = fileTable_addFile (context_fileTable (), name);
00445
00446 return (fileloc_createPrim (FL_RC, fid, 0, 0));
00447 }
00448
00449 fileloc fileloc_createExternal (void)
00450 {
00451 return (fileloc_getExternal ());
00452 }
00453
00454 fileloc fileloc_getExternal (void)
00455 {
00456 static fileloc res = fileloc_undefined;
00457
00458 if (res == fileloc_undefined)
00459 {
00460 res = fileloc_createPrim (FL_EXTERNAL, fileId_invalid, 0, 0);
00461 }
00462
00463 return res;
00464 }
00465
00466 fileloc fileloc_observeBuiltin ()
00467 {
00468 return (fileloc_getBuiltin ());
00469 }
00470
00471 fileloc fileloc_getBuiltin ()
00472 {
00473 static fileloc res = fileloc_undefined;
00474
00475 if (res == fileloc_undefined)
00476 {
00477 res = fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0);
00478 }
00479
00480 return res;
00481 }
00482
00483 fileloc
00484 fileloc_makePreproc (fileloc loc)
00485 {
00486 if (fileloc_isDefined (loc))
00487 {
00488 return (fileloc_createPrim (FL_PREPROC, loc->fid,
00489 loc->lineno, loc->column));
00490 }
00491
00492 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
00493 }
00494
00495 fileloc
00496 fileloc_makePreprocPrevious (fileloc loc)
00497 {
00498 if (fileloc_isDefined (loc))
00499 {
00500 if (loc->lineno > 1)
00501 {
00502 return (fileloc_createPrim (FL_PREPROC, loc->fid,
00503 loc->lineno - 1, 0));
00504 }
00505 else
00506 {
00507 return (fileloc_createPrim (FL_PREPROC, loc->fid,
00508 loc->lineno, 0));
00509 }
00510 }
00511
00512 return (fileloc_createPrim (FL_PREPROC, fileId_invalid, 0, 0));
00513 }
00514
00515 fileloc
00516 fileloc_createBuiltin ()
00517 {
00518 return (fileloc_createPrim (FL_BUILTIN, fileId_invalid, 0, 0));
00519 }
00520
00521 # ifndef NOLCL
00522 fileloc
00523 fileloc_createImport (cstring fname, int lineno)
00524 {
00525 fileId fid = fileTable_lookup (context_fileTable (), fname);
00526
00527 if (!fileId_isValid (fid))
00528 {
00529 fid = fileTable_addImportFile (context_fileTable (), fname);
00530 }
00531
00532 return (fileloc_createPrim (FL_IMPORT, fid, lineno, 0));
00533 }
00534 # endif
00535
00536 static fileloc
00537 fileloc_createPrim (flkind kind, fileId fid, int line, int col)
00538 {
00539 fileloc f = (fileloc) dmalloc (sizeof (*f));
00540
00541 f->kind = kind;
00542 f->fid = fid;
00543 f->lineno = line;
00544 f->column = col;
00545
00546 return (f);
00547 }
00548
00549 # ifndef NOLCL
00550 fileloc
00551 fileloc_createSpec (fileId fid, int line, int col)
00552 {
00553 return (fileloc_createPrim (FL_SPEC, fid, line, col));
00554 }
00555 # endif
00556
00557 fileloc fileloc_create (fileId fid, int line, int col)
00558 {
00559 return (fileloc_createPrim (fileId_kind (fid), fid, line, col));
00560 }
00561
00562 cstring
00563 fileloc_filename (fileloc f)
00564 {
00565 return (fileloc_isDefined (f)
00566 ? rootFileName (f->fid) : cstring_makeLiteralTemp ("<unknown>"));
00567 }
00568
00569 cstring
00570 fileloc_unparseFilename (fileloc f)
00571 {
00572 if (fileloc_isDefined (f))
00573 {
00574 switch (f->kind)
00575 {
00576 case FL_LIB:
00577 return (message ("load file %s", fileloc_filename (f)));
00578 case FL_BUILTIN:
00579 return (cstring_makeLiteral ("# builtin #"));
00580 case FL_IMPORT:
00581 return (message ("import file %s", fileloc_filename (f)));
00582 case FL_EXTERNAL:
00583 return (cstring_makeLiteral ("<external>"));
00584 default:
00585 return (cstring_copy (fileloc_filename (f)));
00586 }
00587 }
00588 return cstring_undefined;
00589 }
00590
00591 int
00592 fileloc_lineno (fileloc f)
00593 {
00594 return (fileloc_isDefined (f) ? f->lineno : -1);
00595 }
00596
00597 int
00598 fileloc_column (fileloc f)
00599 {
00600 return (fileloc_isDefined (f) ? f->column : -1);
00601 }
00602
00603 cstring
00604 fileloc_unparse (fileloc f)
00605 {
00606 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
00607
00608 if (fileloc_isDefined (f))
00609 {
00610 switch (f->kind)
00611 {
00612 case FL_LIB:
00613 return (message ("load file %s", rootFileName (f->fid)));
00614 case FL_BUILTIN:
00615 return (cstring_makeLiteral ("Command Line"));
00616 case FL_IMPORT:
00617 if (parenFormat)
00618 {
00619 return (message ("import file %s(%d)", rootFileName (f->fid), f->lineno));
00620 }
00621 else
00622 {
00623 return (message ("import file %s:%d", rootFileName (f->fid), f->lineno));
00624 }
00625 case FL_PREPROC:
00626 if (parenFormat)
00627 {
00628 return (message ("%s(%d)", rootFileName (f->fid), f->lineno));
00629 }
00630 else
00631 {
00632 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
00633 }
00634 case FL_EXTERNAL:
00635 return (cstring_makeLiteral ("<external>"));
00636 default:
00637 if (context_getFlag (FLG_SHOWCOL))
00638 {
00639 if (fileloc_linenoDefined (f))
00640 {
00641 if (fileloc_columnDefined (f))
00642 {
00643 if (parenFormat)
00644 {
00645 return (message ("%s(%d,%d)",
00646 rootFileName (f->fid),
00647 f->lineno, f->column));
00648 }
00649 else
00650 {
00651 return (message ("%s:%d:%d",
00652 rootFileName (f->fid),
00653 f->lineno, f->column));
00654 }
00655 }
00656 else
00657 {
00658 if (parenFormat)
00659 {
00660 return (message ("%s(%d)", rootFileName (f->fid), f->lineno));
00661 }
00662 else
00663 {
00664 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
00665 }
00666 }
00667 }
00668 return (cstring_copy (rootFileName (f->fid)));
00669 }
00670 else if (fileloc_linenoDefined (f))
00671 {
00672 if (parenFormat)
00673 {
00674 return (message ("%s(%d)",
00675 rootFileName (f->fid), f->lineno));
00676 }
00677 else
00678 {
00679 return (message ("%s:%d", rootFileName (f->fid), f->lineno));
00680 }
00681 }
00682 else
00683 {
00684 return (cstring_copy (rootFileName (f->fid)));
00685 }
00686 }
00687 }
00688 return (cstring_makeLiteral ("< Location unknown >"));
00689 }
00690
00691 cstring
00692 fileloc_unparseRaw (cstring fname, int lineno)
00693 {
00694 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
00695
00696 if (parenFormat)
00697 {
00698 return (message ("%s(%d)", fname, lineno));
00699 }
00700 else
00701 {
00702 return (message ("%s:%d", fname, lineno));
00703 }
00704 }
00705
00706 cstring
00707 fileloc_unparseRawCol (cstring fname, int lineno, int col)
00708 {
00709 if (context_getFlag (FLG_SHOWCOL))
00710 {
00711 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
00712
00713 if (parenFormat)
00714 {
00715 return (message ("%s(%d,%d)", fname, lineno, col));
00716 }
00717 else
00718 {
00719 return (message ("%s:%d:%d", fname, lineno, col));
00720 }
00721 }
00722 else
00723 {
00724 return fileloc_unparseRaw (fname, lineno);
00725 }
00726 }
00727
00728 bool fileloc_isSpecialFile (fileloc f)
00729 {
00730 if (fileloc_isDefined (f)
00731 && fileId_isValid (f->fid))
00732 {
00733 return (fileTable_isSpecialFile (context_fileTable (), f->fid));
00734 }
00735 else
00736 {
00737 return FALSE;
00738 }
00739 }
00740
00741 bool fileloc_isHeader (fileloc f)
00742 {
00743
00744
00745 return (fileloc_isDefined (f) && fileId_isValid (f->fid)
00746 && fileId_isHeader (f->fid));
00747 }
00748
00749 bool fileloc_isSpec (fileloc f)
00750 {
00751 return (fileloc_isDefined (f) &&
00752 (f->kind == FL_LIB || f->kind == FL_STDLIB || f->kind == FL_SPEC));
00753 }
00754
00755 bool fileloc_isRealSpec (fileloc f)
00756 {
00757 return (fileloc_isDefined (f) && (f->kind == FL_SPEC));
00758 }
00759
00760 bool fileloc_isRealLib (fileloc f)
00761 {
00762 return (fileloc_isDefined (f) && f->kind == FL_LIB);
00763 }
00764
00765 bool fileloc_isLib (fileloc f)
00766 {
00767 return (fileloc_isDefined (f)
00768 && (f->kind == FL_LIB || f->kind == FL_STDHDR || f->kind == FL_STDLIB));
00769 }
00770
00771 bool fileloc_isStandardLib (fileloc f)
00772 {
00773 return (fileloc_isDefined (f) && f->kind == FL_STDLIB);
00774 }
00775
00776 cstring fileloc_unparseDirect (fileloc fl)
00777 {
00778 if (fileloc_isDefined (fl))
00779 {
00780 return (message ("%d:%d:%d",
00781 (int) fl->fid,
00782 fl->lineno, fl->column));
00783 }
00784 else
00785 {
00786 return (cstring_makeLiteral ("<undef>"));
00787 }
00788 }
00789
00790 bool fileloc_isUser (fileloc f)
00791 {
00792 return (fileloc_isDefined (f)
00793 && f->kind == FL_NORMAL);
00794 }
00795
00796
00797
00798