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 # include "splintMacros.nf"
36 ** from the CC man page:
38 ** -Idir Search for #include files whose names do not begin with a
39 ** slash (/) in the following order: (1) in the directory of
40 ** the dir argument, (2) in the directories specified by -I
41 ** options, (3) in the standard directory (/usr/include).
44 /* needed for string literals literals */
48 /*@null@*/ /*@observer@*/ char *name;
49 /*@null@*/ /*@observer@*/ char *describe;
52 static flagcatinfo categories[] =
54 { FK_ABSTRACT, "abstract", "abstraction violations, representation access" } ,
55 { FK_ALIAS, "aliasing", "unexpected or dangerous aliasing" } ,
56 { FK_USE, "alluse", "all declarations are used" } ,
57 { FK_ANSI, "ansi", "violations of constraints imposed by ANSI/ISO standard" } ,
58 { FK_ARRAY, "arrays", "special checking involving arrays" } ,
59 { FK_BOOL, "booleans", "checking and naming of boolean types" } ,
60 { FK_COMMENTS, "comments", "warnings about (normal) comments" } ,
61 { FK_SYNCOMMENTS, "syncomments", "interpretation of annotation comments" } ,
62 { FK_COMPLETE, "complete", "completely defined, used, or specified system" } ,
63 { FK_CONTROL, "controlflow", "suspicious control structures" } ,
64 { FK_DEBUG, "debug", "flags for debugging splint" } ,
65 { FK_DECL, "declarations", "consistency of declarations" } ,
66 { FK_DEF, "definition", "undefined storage errors" } ,
67 { FK_DIRECT, "directories", "set directores" } ,
68 { FK_DISPLAY, "display", "control what is displayed" } ,
69 { FK_EFFECT, "effect", "statements with no effects" } ,
70 { FK_ERRORS, "errors", "control expected errors, summary reporting" } ,
71 { FK_EXPORT, "export", "control what may be exported" } ,
72 { FK_EXPOSURE, "exposure", "representation exposure" } ,
73 { FK_EXTENSIBLE, "extensible", "user-defined checks and annotations" },
74 { FK_FILES, "files", "control system files" } ,
75 { FK_FORMAT, "format", "control format of warning messages" } ,
76 { FK_GLOBALS, "globals", "use of global and file static variables" },
77 { FK_HEADERS, "headers", "control inclusion and generation of header files" },
78 { FK_HELP, "help", "on-line help" },
79 { FK_BOUNDS, "memorybounds", "out-of-bounds memory accesses" },
80 { FK_HINTS, "hints", "control display of warning hints" },
81 { FK_SYSTEMFUNCTIONS, "systemfunctions", "special properties of exit and main" },
82 { FK_IMPLICIT, "implicit", "control implicit annotations and interpretations" } ,
83 { FK_INIT, "initializations", "initialization files" } ,
84 { FK_ITER, "iterators", "checking iterator definitions and uses" } ,
85 { FK_LEAK, "leaks", "memory leaks" } ,
86 { FK_LIBS, "libraries", "loading and dumping of user and standard libraries" } ,
87 { FK_LIMITS, "limits", "violations of set limits" } ,
88 { FK_MACROS, "macros", "expansion, definition and use of macros" },
89 { FK_MEMORY, "memory", "memory management" } ,
90 { FK_MODIFIES, "modification", "modification errors" } ,
91 { FK_NAMES, "names", "naming conventions and limits" } ,
92 { FK_NULL, "null", "misuses of null pointers" } ,
93 { FK_NUMBERS, "numbers", "control type-checking of numeric types" } ,
94 { FK_OPS, "operations", "checking of primitive operations" } ,
95 { FK_PARAMS, "parameters", "function and macro parameters" } ,
96 { FK_SPEED, "performance", "speeding up checking" } ,
97 { FK_POINTER, "pointers", "pointers" } ,
98 { FK_PRED, "predicates", "condition test expressions" } ,
99 { FK_PREFIX, "prefixes", "set naming prefixes and control checking" } ,
100 { FK_PREPROC, "preproc", "defines and undefines for the preprocessor" } ,
101 { FK_PROTOS, "prototypes", "function prototypes" } ,
102 { FK_DEAD, "released", "using storage that has been deallocated" } ,
103 { FK_IGNORERET, "returnvals", "ignored return values" },
104 { FK_SECURITY, "security", "possible security vulnerability" },
105 { FK_SPEC, "specifications", "checks involving .lcl specifications" } ,
106 { FK_SUPPRESS, "suppress", "local and global suppression of messages" } ,
107 { FK_TYPEEQ, "typeequivalence", "control what types are equivalent" } ,
108 { FK_BEHAVIOR, "undefined", "code with undefined or implementation-defined behavior" } ,
109 { FK_UNRECOG, "unrecognized", "unrecognized identifiers" } ,
110 { FK_UNSPEC, "unconstrained", "checking in the presence of unconstrained functions" } ,
111 { FK_WARNUSE, "warnuse", "use of possibly problematic function" } ,
112 { FK_ITS4, "its4", "its4 compatibility flags (report warnings for uses of possibly insecure functions)" } ,
113 { FK_SYNTAX, NULL, NULL } ,
114 { FK_TYPE, NULL, NULL } ,
115 { FK_SECRET, NULL, NULL } ,
116 { FK_OBSOLETE, NULL, NULL } ,
117 { FK_NONE, NULL, NULL } /* must be last */
122 ARG_NUMBER, /* number */
124 ARG_STRING, /* string */
125 ARG_FILE, /* filename (also a string) */
126 ARG_DIRECTORY, /* directory (also a string) */
131 static /*@observer@*/ cstring argcode_unparse (argcode arg)
135 case ARG_STRING: return cstring_makeLiteralTemp ("string");
136 case ARG_FILE: return cstring_makeLiteralTemp ("filename");
137 case ARG_DIRECTORY: return cstring_makeLiteralTemp ("directory");
138 case ARG_PATH: return cstring_makeLiteralTemp ("path");
139 case ARG_NUMBER: return cstring_makeLiteralTemp ("number");
140 case ARG_CHAR: return cstring_makeLiteralTemp ("character");
147 /* Make Microsoft VC++ happy */
148 # pragma warning (disable:4715)
155 bool isSpecial; /* setting this flag may set other flags (or values) */
156 bool isIdem; /* idempotent - always sets to TRUE */
157 bool isGlobal; /* cannot be set locally (using control comments) */
158 bool isModeFlag; /* set by modes */
160 /*@observer@*/ char *flag;
162 /*@observer@*/ /*@null@*/ char *desc;
168 typedef fflag flaglist[];
170 # include "flags.def"
172 /*@iter allFlags (yield observer fflag f); @*/
173 # define allFlags(m_f) \
174 { /*@+enumint@*/ flagcode m_i; for (m_i = 0; m_i < NUMFLAGS; m_i++) { fflag m_f = flags[m_i]; /*@=enumint@*/
175 # define end_allFlags }}
177 static bn_mstring mode_names[] =
179 "weak", "standard", "checks", "strict", NULL,
182 /*@iter allModes (yield bn_mstring modename)@*/
183 # define allModes(m_m) \
184 { int m_ii = 0; while (mstring_isDefined (mode_names[m_ii])) \
185 { bn_mstring m_m = mode_names[m_ii]; m_ii++;
187 # define end_allModes }}
191 static cstring getFlagModeSettings (flagcode p_flag) /*@modifies internalState@*/ ;
192 static cstring describeFlagCode (flagcode p_flag) /*@*/ ;
193 static cstringSList sortedFlags (void) /*@*/ ;
194 static /*@observer@*/ cstring categoryName (flagkind p_kind) /*@*/ ;
196 static flagcode flags_identifyFlagAux (cstring p_s, bool p_quiet) /*@modifies g_warningstream@*/ ;
199 static /*@unused@*/ cstring listModes (void) /*@*/ ;
202 bool flagcode_isSpecialFlag (flagcode f)
204 return (flags[f].isSpecial);
207 bool flagcode_isGlobalFlag (flagcode f)
209 return (flags[f].isGlobal);
212 bool flagcode_isIdemFlag (flagcode f)
214 return (flags[f].isIdem);
217 bool flagcode_isModeFlag (flagcode f)
219 return (flags[f].isModeFlag);
222 bool flagcode_isNameChecksFlag (flagcode f)
224 return (flags[f].main == FK_NAMES);
227 bool flagcode_isHelpFlag (flagcode f)
229 return f == FLG_HELP;
232 bool flagcode_isMessageControlFlag (flagcode f)
235 ** True if opt controls the display of messages.
236 ** These flags must be processed first.
239 return (f == FLG_SHOWSCAN
241 || f == FLG_PARENFILEFORMAT
242 || f == FLG_MESSAGESTREAMSTDERR
243 || f == FLG_MESSAGESTREAMSTDOUT
244 || f == FLG_WARNINGSTREAMSTDERR
245 || f == FLG_WARNINGSTREAMSTDOUT
246 || f == FLG_ERRORSTREAMSTDERR
247 || f == FLG_ERRORSTREAMSTDOUT
248 || f == FLG_MESSAGESTREAM
249 || f == FLG_WARNINGSTREAM
250 || f == FLG_ERRORSTREAM
251 || f == FLG_STREAMOVERWRITE);
255 ** Internal consistency check on the flags.
258 void flags_initMod ()
263 if (flags[code].code != code)
266 "*** ERROR: inconsistent flag %s / %d / %d",
268 flags[code].code, code);
270 llbug (message ("*** ERROR: inconsistent flag %s / %d / %d",
271 cstring_fromChars (flags[code].flag),
272 flags[code].code, code));
285 char *buf = mstring_create (128);
289 if (f.nreported > 0 || f.nsuppressed > 0)
291 int nrep = f.nreported;
292 int nsup = f.nsuppressed;
293 cstring fs = cstring_fill (cstring_fromChars (f.flag), 23);
297 llmsgplain (cstring_makeLiteral
298 ("\nError Type Reported Suppressed\n"
299 "=================== ======== ========="));
303 (void) snprintf (buf, 128, "%s%7d %9d", cstring_toCharsSafe (fs), nrep, nsup);
309 llmsg (cstring_copy (cstring_fromChars (buf)));
315 cstring ts = cstring_fill (cstring_makeLiteralTemp ("Total"), 23);
317 llmsglit (" ======== =========");
319 (void) snprintf (buf, 128, "%s%7d %9d", cstring_toCharsSafe (ts), sumrep, sumsup);
321 llmsgplain (cstring_copy (cstring_fromChars (buf)));
330 flagcode_recordError (flagcode f)
332 if (f != INVALID_FLAG)
334 if (f == FLG_WARNFLAGS)
336 ; /* don't count these */
342 flags[f].nreported = flags[f].nreported + 1;
347 llcontbug (message ("flagcode_recordError: invalid flag: %d", (int) f));
352 flagcode_recordSuppressed (flagcode f)
354 llassertprint (f != INVALID_FLAG, ("flagcode: %s", flagcode_unparse (f)));
357 /*drl bee: ec*/ flags[f].nsuppressed = flags[f].nsuppressed + 1;
361 flagcode_numReported (flagcode f)
363 llassert (f != INVALID_FLAG);
365 return (flags[f].nreported);
368 /*@observer@*/ cstring
369 flagcodeHint (flagcode f)
371 llassert (f != INVALID_FLAG);
374 if (mstring_isDefined (flags[f].hint))
376 return (cstring_fromChars (flags[f].hint));
380 return (cstring_fromChars (flags[f].desc));
384 static int categorySize (flagkind kind) /*@*/
391 if (f.main == kind || f.sub == kind)
400 flagkind identifyCategory (cstring s)
404 for (i = 0; categories[i].kind != FK_NONE; i++)
407 if (mstring_isDefined (categories[i].name))
409 if (cstring_equalLit (s, categories[i].name))
411 return categories[i].kind;
419 static /*@observer@*/ cstring categoryName (flagkind kind)
423 for (i = 0; categories[i].kind != FK_NONE; i++)
426 if (categories[i].kind == kind)
428 return (cstring_fromChars (categories[i].name));
432 return (cstring_makeLiteralTemp ("<No Category>"));
435 static int categoryIndex (flagkind kind)
439 for (i = 0; categories[i].kind != FK_NONE; i++)
442 if (categories[i].kind == kind)
451 void printCategory (flagkind kind)
453 int index = categoryIndex (kind);
455 llassert (index >= 0);
457 llmsg (message ("%s (%d flags)\n\3%s\n\n",
458 cstring_fromChars (categories[index].name),
460 cstring_fromChars (categories[index].describe)));
464 if (f.main == kind || f.sub == kind)
466 llmsg (message (" %s\n\6%q", cstring_fromChars (f.flag),
467 describeFlagCode (f.code)));
473 listAllCategories (void)
477 for (i = 0; categories[i].kind != FK_NONE; i++)
480 flagkind kind = categories[i].kind ;
482 if (categories[i].describe != NULL)
484 llmsg (message ("%s (%d flags)\n\3%s",
487 cstring_fromChars (categories[i].describe)));
493 printAllFlags (bool desc, bool full)
497 cstringSList fl = sortedFlags ();
499 cstringSList_elements (fl, el)
501 /*@i22@*/ /*find out why this is necessary*/
503 tmp = cstring_copy(el);
504 llmsg (message ("%q\n\n", describeFlag (tmp)));
506 } end_cstringSList_elements ;
508 cstringSList_free (fl);
514 if (f.code != INVALID_FLAG && f.main != FK_OBSOLETE)
516 if (mstring_isDefined (f.desc))
520 llmsg (message ("%s --- %s", cstring_fromChars (f.flag),
521 cstring_fromChars (f.desc)));
530 printFlagManual (bool html)
533 ** Prints all flags by category, in order they appear in flags.def
536 flagkind lastCategory = FK_NONE;
540 cstring flagtype = cstring_undefined;
542 if (f.main != lastCategory)
546 llmsg (message ("\n<h4>%s</h4>\n", categoryName (f.main)));
550 llmsg (message ("\n%s\n%s\n",
551 categoryName (f.main),
552 cstring_makeLiteralTemp ("===================================")));
555 lastCategory = f.main;
558 if (f.argtype == ARG_NONE || f.argtype == ARG_SPECIAL)
562 flagname = message ("<tt>%s</tt>", cstring_fromChars (f.flag));
566 flagname = cstring_fromCharsNew (f.flag);
571 if (flagcode_hasString (f.code))
575 flagname = message ("<tt>%s <em><%s></em></tt>",
576 cstring_fromChars (f.flag), argcode_unparse (f.argtype));
580 flagname = message ("%s <%s>", cstring_fromChars (f.flag), argcode_unparse (f.argtype));
583 if (cstring_isDefined (context_getString (f.code)))
587 flagname = message ("%q <font color=\"blue\">[%s]</font>", flagname,
588 context_getString (f.code));
592 flagname = message ("%q [%s]", flagname,
593 context_getString (f.code));
597 else if (f.argtype == ARG_CHAR)
601 flagname = message ("<tt>%s <em><%s></em></tt> <font color=\"blue\">[%c]</font>",
602 cstring_fromChars (f.flag), argcode_unparse (f.argtype),
603 (char) context_getValue (f.code));
607 flagname = message ("%s <%s> [%c]", cstring_fromChars (f.flag), argcode_unparse (f.argtype),
608 (char) context_getValue (f.code));
613 llassert (f.argtype == ARG_NUMBER);
617 flagname = message ("<tt>%s <em><%s></em> <font color=\"blue\">[%d]</font>",
618 cstring_fromChars (f.flag), argcode_unparse (f.argtype),
619 context_getValue (f.code));
623 flagname = message ("%s <%s> [%d]", cstring_fromChars (f.flag), argcode_unparse (f.argtype),
624 context_getValue (f.code));
633 flagtype = message("%q<font color=\"green\">-</font>", flagtype);
637 flagtype = message("%q<->", flagtype);
645 flagtype = message ("%q<font color=\"green\"><em>global</em></font>", flagtype);
649 flagtype = message ("%q<G>", flagtype);
657 flagtype = message ("%q<font color=\"orange\"><em>shortcut</em></font>", flagtype);
661 flagtype = message("%q<S>", flagtype);
669 flagtype = message ("%q mode:<tt>%q</tt>>", flagtype, getFlagModeSettings (f.code));
673 flagtype = message ("%q<M:%q>", flagtype, getFlagModeSettings (f.code));
676 else /* its a plain flag */
680 flagtype = message ("%q plain:<tt>%s</tt>", flagtype,
681 cstring_makeLiteralTemp (context_getFlag (f.code) ? "+" : "-"));
685 flagtype = message ("%q<P:%s>", flagtype,
686 cstring_makeLiteralTemp (context_getFlag (f.code) ? "+" : "-"));
690 llmsg (message ("%s: %s", flagname, flagtype));
694 llgenindentmsgnoloc (cstring_makeLiteral ("<blockquote>"));
697 if (mstring_isDefined (f.hint))
699 llgenindentmsgnoloc (cstring_fromCharsNew (f.hint));
703 llgenindentmsgnoloc (message ("%q.", cstring_capitalize (cstring_fromChars (f.desc))));
708 llgenindentmsgnoloc (cstring_makeLiteral ("</blockquote>"));
714 describeFlagCode (flagcode flag)
716 cstring ret = cstring_undefined;
719 if (flagcode_isInvalid (flag))
721 return (cstring_makeLiteral ("<invalid>"));
724 context_resetAllFlags ();
728 ret = cstring_copy (cstring_fromChars (f.desc));
730 if (f.sub != FK_NONE)
732 ret = message ("%q\nCategories: %s, %s",
734 categoryName (f.main),
735 categoryName (f.sub));
739 if (f.main != FK_NONE)
741 cstring cname = categoryName (f.main);
743 if (cstring_isDefined (cname))
745 ret = message ("%q\nCategory: %s",
753 ret = message ("%q\nMode Settings: %q",
754 ret, getFlagModeSettings (flag));
758 ret = message ("%q\nDefault Setting: %s",
760 cstring_makeLiteralTemp (context_getFlag (flag) ? "+" : "-"));
765 ret = message("%q\nSet globally only", ret);
769 ret = message("%q\nSet locally", ret);
778 ret = message("%q\nNumeric Argument. Default: %d",
780 context_getValue (flag));
783 ret = message("%q\nCharacter Argument. Default: %h",
784 ret, (char) context_getValue (flag));
791 if (cstring_isDefined (context_getString (flag)))
793 ret = message("%q\n%q argument. Default: %s",
795 cstring_capitalize (argcode_unparse (f.argtype)),
796 context_getString (flag));
800 ret = message("%q\n%s argument. No default.",
802 cstring_capitalize (argcode_unparse (f.argtype)));
808 if (mstring_isDefined (f.hint))
810 ret = message("%q\n\3%s", ret, cstring_fromChars (f.hint));
816 static cstring getFlagModeSettings (flagcode flag)
818 cstring res = cstring_undefined;
822 context_setModeNoWarn (cstring_fromChars (mname));
824 res = message ("%q%s", res, cstring_makeLiteralTemp (context_getFlag (flag) ? "+" : "-"));
831 describeFlag (cstring flagname)
833 cstring oflagname = cstring_copy (flagname);
834 flagcode f = flags_identifyFlag (flagname);
836 if (flagcode_isSkip (f))
838 cstring_free (oflagname);
839 return cstring_undefined;
841 else if (flagcode_isValid (f))
843 if (cstring_equal (flagname, oflagname))
845 cstring_free (oflagname);
846 return (message ("%s\n\3%q", flagname, describeFlagCode (f)));
850 return (message ("%q (standardized name: %s)\n\3%q",
851 oflagname, flagname, describeFlagCode (f)));
856 if (flags_isModeName (flagname))
858 cstring_free (oflagname);
861 (message ("%s: predefined mode (see Manual for information)",
866 return (message ("%q: <invalid flag>", oflagname));
874 cstringSList s = cstringSList_new ();
880 s = cstringSList_add (s, cstring_fromChars (f.flag));
884 cstringSList_alphabetize (s);
889 void printAlphaFlags ()
891 cstringSList fl = sortedFlags ();
893 cstringSList_printSpaced (fl, 3, 1, context_getLineLen () - 25);
894 cstringSList_free (fl);
897 /*@observer@*/ cstring
898 flagcode_unparse (flagcode code)
900 if (code == INVALID_FLAG)
902 return cstring_makeLiteralTemp ("<invalid flag>");
905 return cstring_fromChars (flags[code].flag);
909 ** Transforms a flag into its cannonical form.
911 ** The following transformations are made:
917 ** parameter -> param
918 ** unrecognized -> unrecog
920 ** declaration -> decl
921 ** globalias -> (no change)
929 ** unconstrained -> unspec
935 static /*@only@*/ cstring
936 canonicalizeFlag (cstring s)
939 cstring res = cstring_copy (s);
940 static bn_mstring transform[] =
946 "parameter", "param",
947 "unrecognized", "unrecog",
949 "declaration", "decl",
957 "unconstrained", "uncon",
968 while ((current = transform[i]) != NULL)
970 if (cstring_containsLit (res, current))
972 cstring_replaceLit (res, current, transform[i+1]);
977 /* remove whitespace, -'s, and _'s */
978 cstring_stripChars (res, " -_");
983 flags_identifyFlag (cstring s)
985 return flags_identifyFlagAux (s, FALSE);
989 flags_identifyFlagQuiet (cstring s)
991 return flags_identifyFlagAux (s, TRUE);
995 flags_identifyFlagAux (cstring s, bool quiet)
1000 if (cstring_length (s) == 0) {
1001 /* evs 2000-06-25: A malformed flag. */
1002 return INVALID_FLAG;
1005 if (cstring_firstChar (s) == 'I')
1007 return FLG_INCLUDEPATH; /* no space required after -I */
1010 if (cstring_firstChar (s) == 'S')
1012 return FLG_SPECPATH; /* no space required after -S */
1015 if (cstring_firstChar (s) == 'D')
1017 return FLG_DEFINE; /* no space required after -D */
1020 if (cstring_firstChar (s) == 'U')
1022 return FLG_UNDEFINE; /* no space required after -D */
1025 cflag = canonicalizeFlag (s);
1030 if (cstring_equal (cstring_fromChars (f.flag), cflag))
1037 if (res == INVALID_FLAG)
1043 if (cstring_equalLit (cflag, "pred"))
1047 else if (cstring_equalLit (cflag, "modobserverstrict"))
1049 res = FLG_MODOBSERVERUNCON;
1051 else if (cstring_equalLit (cflag, "czechnames"))
1055 else if (cstring_equalLit (cflag, "slovaknames"))
1059 else if (cstring_equalLit (cflag, "czechoslovaknames"))
1061 res = FLG_CZECHOSLOVAK;
1063 else if (cstring_equalLit (cflag, "globunspec")
1064 || cstring_equalLit (cflag, "globuncon"))
1066 res = FLG_GLOBUNSPEC;
1068 else if (cstring_equalLit (cflag, "modglobsunspec")
1069 || cstring_equalLit (cflag, "modglobsuncon")
1070 || cstring_equalLit (cflag, "modglobsnomods"))
1072 res = FLG_MODGLOBSUNSPEC;
1074 else if (cstring_equalLit (cflag, "export"))
1076 res = FLG_EXPORTANY;
1078 else if (cstring_equalLit (cflag, "macrospec"))
1080 res = FLG_MACRODECL;
1082 else if (cstring_equalLit (cflag, "ansireservedlocal"))
1084 res = FLG_ISORESERVEDLOCAL;
1086 else if (cstring_equalLit (cflag, "warnposix"))
1088 res = FLG_WARNPOSIX;
1090 else if (cstring_equalLit (cflag, "defuse"))
1094 else if (cstring_equalLit (cflag, "macroundef"))
1096 res = FLG_MACROUNDEF;
1098 else if (cstring_equalLit (cflag, "showcol"))
1102 else if (cstring_equalLit (cflag, "intbool"))
1106 else if (cstring_equalLit (cflag, "intchar"))
1110 else if (cstring_equalLit (cflag, "intenum"))
1115 ** Backwards compatibility for our American friends...
1118 else if (cstring_equalLit (cflag, "ansilib"))
1122 else if (cstring_equalLit (cflag, "ansistrictlib"))
1124 res = FLG_STRICTLIB;
1126 else if (cstring_equalLit (cflag, "skipansiheaders"))
1128 res = FLG_SKIPISOHEADERS;
1130 else if (cstring_equalLit (cflag, "ansireserved"))
1132 res = FLG_ISORESERVED;
1134 else if (cstring_equalLit (cflag, "ansireservedinternal"))
1136 res = FLG_ISORESERVEDLOCAL;
1143 else if (cstring_equalLit (cflag, "accessunspec"))
1148 (cstring_makeLiteral
1149 ("accessunspec flag is no longer supported. It has been replaced by accessmodule, accessfile and "
1150 "accessfunction to provide more precise control of accessibility "
1151 "of representations. For more information, "
1152 "see splint -help accessmodule"));
1157 else if (cstring_equalLit (cflag, "ansilimits"))
1160 (cstring_makeLiteral
1161 ("ansilimits flag is no longer supported. It has been replaced by ansi89limits and "
1162 "iso99limits to select either the lower translation limits imposed by the ANSI89 "
1163 "standard or the typically higher limits prescribed by ISO C99."));
1167 else if (cstring_equalLit (cflag, "staticmods"))
1172 (cstring_makeLiteral
1173 ("staticmods flag is obsolete. You probably "
1174 "want impcheckmodstatics. For more information, "
1175 "see splint -help impcheckmodstatics"));
1180 else if (cstring_equalLit (cflag, "bool"))
1185 (cstring_makeLiteral ("bool flag is obsolete. It never really "
1186 "made sense in the first place."));
1191 else if (cstring_equalLit (cflag, "shiftsigned"))
1196 (cstring_makeLiteral ("shiftsigned flag is obsolete. You probably "
1197 "want bitwisesigned, shiftnegative or shiftimplementation."));
1202 else if (cstring_equalLit (cflag, "ansi"))
1207 (cstring_makeLiteral ("ansi flag is obsolete. You probably "
1208 "want noparams and/or oldstyle."));
1213 else if (cstring_equalLit (cflag, "usestderr"))
1218 (cstring_makeLiteral
1219 ("usestderr flag is obsolete. This has been replaced "
1220 "by more precise flags for controlling the warning, "
1221 "status message and fatal error streams independently: message-stream-stdout, "
1222 "message-stream-stderr, message-stream <file>, "
1223 "warning-stream-stdout, warning-stream-stderr, warning-stream <file>, "
1224 "error-stream-stdout, error-stream-stderr, error-stream <file>."));
1230 else if (cstring_equalLit (cflag, "stdio"))
1235 (cstring_makeLiteral
1236 ("stdio flag is obsolete. You may "
1237 "want strictlib or one of the gloabls "
1238 "checking flags. For more information, "
1239 "see splint -help strictlib or splint -help flags globals"));
1244 else if (flags_isModeName (cflag))
1246 res = MODENAME_FLAG;
1254 cstring_free (cflag);
1258 void setValueFlag (flagcode opt, cstring arg)
1266 case FLG_INDENTSPACES:
1268 case FLG_EXTERNALNAMELEN:
1269 case FLG_INTERNALNAMELEN:
1270 case FLG_CONTROLNESTDEPTH:
1271 case FLG_STRINGLITERALLEN:
1272 case FLG_NUMSTRUCTFIELDS:
1273 case FLG_NUMENUMMEMBERS:
1274 case FLG_INCLUDENEST:
1276 int val = cstring_toPosInt (arg);
1283 ("Flag %s must be followed by a positive number number. "
1285 flagcode_unparse (opt), arg));
1289 context_setValueAndFlag (opt, val);
1293 case FLG_COMMENTCHAR:
1295 if (cstring_length (arg) != 1)
1299 ("Flag %s should be followed by a single character. Followed by %s",
1300 flagcode_unparse (opt), arg));
1304 context_setCommentMarkerChar (cstring_firstChar (arg));
1312 void setStringFlag (flagcode opt, /*@only@*/ cstring arg)
1318 if (cstring_lastChar (arg) == CONNECTCHAR)
1320 context_setString (opt, arg);
1324 context_setString (opt, cstring_appendChar (arg, CONNECTCHAR));
1330 context_setString (opt, arg);
1339 cstring s = cstring_makeLiteral ("Flag ");
1340 cstringSList sflags = sortedFlags ();
1344 s = message ("%q%9s", s, cstring_fromChars (modename));
1347 s = message ("%q\n", s);
1349 cstringSList_elements (sflags, flagname)
1351 flagcode code = flags_identifyFlag (flagname);
1352 fflag currentflag = flags[code];
1354 if (mstring_isDefined (currentflag.desc) && flagcode_isModeFlag (code))
1356 s = message ("%q\n%27s", s,
1357 cstring_fromChars (currentflag.flag));
1361 context_setMode (cstring_fromChars (modename));
1363 if (context_getFlag (code))
1365 s = message ("%q%9s", s, cstring_makeLiteralTemp ("+"));
1369 s = message ("%q%9s", s, cstring_makeLiteralTemp (" "));
1372 context_resetModeFlags ();
1375 } end_cstringSList_elements;
1377 cstringSList_free (sflags);
1379 s = cstring_appendChar (s, '\n');
1385 static /*@unused@*/ cstring
1388 cstring s = cstring_makeLiteral ("\t");
1393 if (i != 0 && (i % 4 == 0))
1395 s = message ("%q\n\t%15s", s, cstring_fromChars (modename));
1399 s = message ("%q%15s", s, cstring_fromChars (modename));
1409 flags_isModeName (cstring s)
1413 if (mstring_isDefined (modename))
1415 if (cstring_equalLit (s, modename))
1425 extern bool flagcode_hasArgument (flagcode f)
1427 return (flags[f].argtype != ARG_NONE);
1430 extern bool flagcode_hasNumber (flagcode f)
1432 return (flags[f].argtype == ARG_NUMBER);
1435 extern bool flagcode_hasChar (flagcode f)
1437 return (flags[f].argtype == ARG_CHAR);
1440 extern bool flagcode_hasString (flagcode f)
1442 return (flags[f].argtype == ARG_STRING
1443 || flags[f].argtype == ARG_FILE
1444 || flags[f].argtype == ARG_DIRECTORY
1445 || flags[f].argtype == ARG_PATH);
1448 extern int flagcode_valueIndex (flagcode f)
1450 /*@unchecked@*/ static bool initialized = FALSE;
1452 /*@unchecked@*/ static flagcode valueFlags[NUMVALUEFLAGS];
1460 if (flagcode_hasNumber (code) || flagcode_hasChar (code))
1462 llassert (nv < NUMVALUEFLAGS);
1463 DPRINTF (("Value flag: %s [%d]", flagcode_unparse (code), (int) code));
1464 valueFlags[nv] = code;
1469 llassertprint (nv == NUMVALUEFLAGS,
1470 ("Number of value flags: %d (expected %d)",
1471 nv, (int) NUMVALUEFLAGS));
1475 for (i = 0; i < NUMVALUEFLAGS; i++)
1477 /* static valueFlags must be defined */
1480 if (f == valueFlags[i]) /*@=usedef@*/
1486 fprintf (stderr, "Cannot find value flag: %d", (int) f);
1487 exit (EXIT_FAILURE);
1488 /* Cannot do this...might call recursively...
1489 llfatalbug (message ("Cannot fine value flag: %d", (int) f));
1494 extern int flagcode_stringIndex (flagcode f)
1496 /*@unchecked@*/ static bool initialized = FALSE;
1497 /*@unchecked@*/ static flagcode stringFlags[NUMSTRINGFLAGS];
1507 if (flagcode_hasString (code))
1509 llassertprint (nv < NUMSTRINGFLAGS, ("Incorrect number of string flags: %d (need at least %d)", NUMSTRINGFLAGS, nv));
1510 stringFlags[nv] = code;
1515 llassertprint (nv == NUMSTRINGFLAGS,
1516 ("number of string flags: %d (expected %d)",
1517 nv, NUMSTRINGFLAGS));
1521 for (i = 0; i < NUMSTRINGFLAGS; i++)
1524 /*@-usedef@*/ if (f == stringFlags[i]) /*@=usedef@*/
1530 llbug (message ("Bad string flag: %s", flagcode_unparse (f)));
1534 bool flagcode_isNamePrefixFlag (flagcode f)
1538 case FLG_MACROVARPREFIX:
1540 case FLG_ENUMPREFIX:
1541 case FLG_FILESTATICPREFIX:
1542 case FLG_GLOBPREFIX:
1543 case FLG_TYPEPREFIX:
1544 case FLG_EXTERNALPREFIX:
1545 case FLG_LOCALPREFIX:
1546 case FLG_UNCHECKEDMACROPREFIX:
1547 case FLG_CONSTPREFIX:
1548 case FLG_ITERPREFIX:
1549 case FLG_DECLPARAMPREFIX:
1556 static cstring findLarchPathFile (/*@temp@*/ cstring s)
1561 status = osd_getPath (context_getLarchPath (), s, &pathName);
1563 if (status == OSD_FILEFOUND)
1567 else if (status == OSD_FILENOTFOUND)
1570 lldiagmsg (message ("Cannot find file on LARCH_PATH: %s", s));
1572 else if (status == OSD_PATHTOOLONG)
1574 /* Directory and filename are too long. Report error. */
1575 llbuglit ("soure_getPath: Filename plus directory from search path too long");
1582 return cstring_undefined;
1585 static void addLarchPathFile (fileIdList files, /*@temp@*/ cstring s)
1587 cstring pathName = findLarchPathFile (s);
1589 if (cstring_isDefined (pathName))
1591 if (fileTable_exists (context_fileTable (), pathName))
1594 lldiagmsg (message ("File listed multiple times: %s", pathName));
1595 cstring_free (pathName);
1599 fileIdList_add (files, fileTable_addFileOnly (context_fileTable (), pathName));
1605 static void addFile (fileIdList files, /*@only@*/ cstring s)
1607 if (fileTable_exists (context_fileTable (), s))
1610 lldiagmsg (message ("File listed multiple times: %s", s));
1615 fileIdList_add (files, fileTable_addFileOnly (context_fileTable (), s));
1619 static void addXHFile (fileIdList files, /*@temp@*/ cstring s)
1621 cstring pathName = findLarchPathFile (s);
1623 if (cstring_isDefined (pathName))
1625 if (fileTable_exists (context_fileTable (), pathName))
1628 lldiagmsg (message ("File listed multiple times: %s", s));
1632 fileIdList_add (files, fileTable_addXHFile (context_fileTable (), pathName));
1636 cstring_free (pathName);
1640 flags_processFlags (bool inCommandLine,
1643 fileIdList lclfiles,
1645 cstringList *passThroughArgs,
1646 int argc, char **argv)
1649 cstringSList fl = cstringSList_undefined;
1651 for (i = 0; i < argc; i++)
1655 llassert (argv != NULL);
1658 DPRINTF (("process thisarg [%d]: %s", i, thisarg));
1660 if (*thisarg == '-' || *thisarg == '+')
1662 bool set = (*thisarg == '+');
1663 cstring flagname = cstring_fromChars (thisarg + 1); /* skip '-' or '+' */
1664 flagcode opt = flags_identifyFlag (flagname);
1666 DPRINTF (("Flag: %s", flagcode_unparse (opt)));
1668 if (flagcode_isInvalid (opt))
1670 DPRINTF (("Error!"));
1671 voptgenerror (FLG_BADFLAG,
1672 message ("Unrecognized option: %s",
1673 cstring_fromChars (thisarg)),
1676 else if (flagcode_isHelpFlag (opt))
1680 voptgenerror (FLG_BADFLAG,
1681 message ("Help flag must be first on the command line: %s",
1682 cstring_fromChars (thisarg)),
1687 voptgenerror (FLG_BADFLAG,
1688 message ("Help flags can only be used on the command line: %s",
1689 cstring_fromChars (thisarg)),
1693 else if (flagcode_isPassThrough (opt)) /* preprocessor flag: -D or -U */
1696 ** Following space is optional, don't include the -
1699 *passThroughArgs = cstringList_add (*passThroughArgs,
1700 cstring_fromCharsNew (thisarg + 1));
1702 else if (opt == FLG_INCLUDEPATH || opt == FLG_SPECPATH)
1704 cstring dir = cstring_suffix (cstring_fromChars (thisarg), 1); /* skip over I */
1706 DPRINTF (("Directory: %s", dir));
1710 case FLG_INCLUDEPATH:
1711 cppAddIncludeDir (dir);
1712 /*@switchbreak@*/ break;
1715 g_localSpecPath = cstring_toCharsSafe
1717 cstring_fromChars (g_localSpecPath),
1721 /*@switchbreak@*/ break;
1725 else if (flagcode_isModeName (opt))
1727 context_setMode (flagname);
1729 else if (inCommandLine && flagcode_isMessageControlFlag (opt))
1732 ** Processed on first pass
1735 if (flagcode_hasArgument (opt))
1743 ** A normal control flag
1746 context_userSetFlag (opt, set);
1748 if (flagcode_hasArgument (opt))
1750 if (flagcode_hasNumber (opt))
1754 setValueFlag (opt, cstring_fromChars (argv[i]));
1761 ("Flag %s must be followed by a number",
1762 flagcode_unparse (opt)),
1766 else if (flagcode_hasChar (opt))
1770 setValueFlag (opt, cstring_fromChars (argv[i]));
1777 ("Flag %s must be followed by a character",
1778 flagcode_unparse (opt)),
1782 else if (flagcode_hasString (opt)
1783 || opt == FLG_INIT || opt == FLG_OPTF)
1788 Changed this because arg can be freed when it's passed to
1789 lslinit_setInitFile and freeing argv[i] causes a seg fault
1791 cstring arg = cstring_fromCharsNew (argv[i]);
1793 if (opt == FLG_OPTF)
1797 ; /* -f already processed */
1801 (void) rcfiles_read (arg, passThroughArgs, TRUE);
1804 else if (opt == FLG_INIT)
1806 lslinit_setInitFile (inputStream_create
1808 cstring_makeLiteralTemp (LCLINIT_SUFFIX),
1814 DPRINTF (("String flag: %s / %s",
1815 flagcode_unparse (opt), arg));
1816 if (opt == FLG_MTSFILE)
1819 ** arg identifies mts files
1821 cstring tmp = message ("%s%s", arg, MTS_EXTENSION);
1822 addLarchPathFile (mtfiles, tmp);
1824 tmp = message ("%s%s", arg, XH_EXTENSION);
1825 addXHFile (xfiles, tmp);
1830 setStringFlag (opt, cstring_copy (arg));
1839 ("Flag %s must be followed by a string",
1840 flagcode_unparse (opt)),
1851 else /* its a filename */
1853 DPRINTF (("Adding filename: %s", thisarg));
1854 fl = cstringSList_add (fl, cstring_fromChars (thisarg));
1859 ** create lists of C and LCL files
1864 cstringSList_elements (fl, current)
1866 cstring ext = fileLib_getExtension (current);
1868 if (cstring_isUndefined (ext))
1870 /* no extension --- both C and LCL with default extensions */
1872 addFile (cfiles, message ("%s%s", current, C_EXTENSION));
1873 addFile (lclfiles, message ("%s%s", current, LCL_EXTENSION));
1875 else if (cstring_equal (ext, XH_EXTENSION))
1877 addXHFile (xfiles, current);
1879 else if (cstring_equal (ext, PP_EXTENSION))
1881 if (!context_getFlag (FLG_NOPP))
1884 (FLG_FILEEXTENSIONS,
1885 message ("File extension %s used without +nopp flag (will be processed as C source code): %s",
1890 addFile (cfiles, cstring_copy (current));
1892 else if (cstring_equal (ext, LCL_EXTENSION))
1894 addFile (lclfiles, cstring_copy (current));
1896 else if (fileLib_isCExtension (ext))
1898 addFile (cfiles, cstring_copy (current));
1900 else if (cstring_equal (ext, MTS_EXTENSION))
1902 addLarchPathFile (mtfiles, current);
1907 (FLG_FILEEXTENSIONS,
1908 message ("Unrecognized file extension: %s (assuming %s is C source code)",
1912 addFile (cfiles, cstring_copy (current));
1914 } end_cstringSList_elements;
1918 if (cstringSList_size (fl) != 0)
1920 /* Cannot list files in .splintrc files */
1921 voptgenerror (FLG_BADFLAG,
1922 message ("Cannot list files in .splintrc files: %s (probable missing + or -)",
1923 cstringSList_unparse (fl)),
1928 cstringSList_free (fl); /* evans 2002-07-12: why wasn't this reported!?? */