1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
#!/usr/bin/env slsh
() = evalfile ("cmdopt");
() = evalfile ("csv");
private variable Version = "0.1.0";
private variable Output_Fp;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_boseos_info = 0;
private variable Hits_Hash = Assoc_Type[UInt_Type, 0];
private define bos_handler (file, line)
{
variable h = strcat (file, "\t", string(line));
Hits_Hash[h] += 1;
}
private variable Func_Hits = Assoc_Type[UInt_Type, 0];
private define bof_handler (func, file)
{
variable h = strcat (file, "\t", func);
%() = fprintf (stderr, "bof_handler: %S\n", h);
Func_Hits[h] += 1;
}
#ifexists _set_bof_compile_hook
private variable Func_List = Assoc_Type[Int_Type, 0];
private define bof_compile_hook (file, fname)
{
Func_List[strcat (file, "\t", fname)] = 1;
}
private define bos_compile_hook (file, line)
{
Hits_Hash[strcat (file, "\t", string(line))] = 0;
}
#endif
define slcov_enable ()
{
()=_set_bos_handler (&bos_handler);
()=_set_bof_handler (&bof_handler);
_boseos_info = 3;
_bofeof_info = 1;
#ifexists _set_bof_compile_hook
()=_set_bof_compile_hook (&bof_compile_hook);
()=_set_bos_compile_hook (&bos_compile_hook);
#endif
}
define slcov_disable ()
{
()=_set_bos_handler (NULL);
()=_set_eos_handler (NULL);
()=_set_bof_handler (NULL);
_boseos_info = 0;
_bofeof_info = 0;
#ifexists _set_bof_compile_hook
()=_set_bof_compile_hook (NULL);
()=_set_bos_compile_hook (NULL);
#endif
}
private define make_absolute_filename (file)
{
file = path_concat (getcwd(), file);
file = strreplace (file, "/./", "/");
ifnot (is_substr (file, "../")) return file;
variable components = strchop (file, '/', 0);
file = "/";
foreach (components)
{
variable c = ();
if (c == "..")
file = path_dirname (file);
else
file = path_concat (file, c);
}
return file;
}
private define output_trace_file (fp, csv)
{
variable file = NULL, i;
_for i (0, length (csv.file)-1, 1)
{
if (file != csv.file[i])
{
if (file != NULL) () = fputs ("end_of_record\n", fp);
() = fputs ("TN:\n", fp);
file = csv.file[i];
% Use an absolute pathname to faciliate merging output files
() = fprintf (fp, "SF:%s\n", make_absolute_filename (file));
variable jj, j = where ((csv.file == file) and (csv.function != ""));
foreach jj (j)
() = fprintf (fp, "FN:%d,%s\n", csv.lineno[jj], csv.function[jj]);
foreach jj (j)
{
() = fprintf (fp, "FNDA:%d,%s\n", csv.fhits[jj], csv.function[jj]);
}
() = fprintf (fp, "FNF:%d\n", length(j));
() = fprintf (fp, "FNH:%d\n", length(j)); % FIXME
}
variable h = csv.hits[i];
if ((h == -1) || ((h == 0) && (csv.function[i] != ""))) continue;
() = fprintf (fp, "DA:%d,%d\n", csv.lineno[i], h);
}
if (file != NULL) () = fputs ("end_of_record\n", fp);
}
define slcov_write_report (fp, use_gcov)
{
variable s = struct
{
file = {}, lineno = {}, hits = {},
};
variable key, val, fields, file, files = Assoc_Type[Int_Type];
foreach key, val (Hits_Hash)
{
fields = strchop (key, '\t', 0);
file = fields[0];
list_append (s.file, file);
list_append (s.lineno, atoi (fields[1]));
list_append (s.hits, val);
files[file] = 1;
}
s.file = list_to_array (s.file);
s.lineno = list_to_array (s.lineno);
s.hits = list_to_array (s.hits);
variable csv = struct
{
file = String_Type[0], lineno = Int_Type[0],
hits = Int_Type[0], function = String_Type[0],
fhits = Int_Type[0],
};
foreach file (assoc_get_keys (files))
{
if (file == __FILE__) continue;
variable fpin = fopen (file, "r");
if (fpin == NULL)
{
files[file] = 0;
continue;
}
variable i,
fre = `^[^%"]*define[ \t]+\([a-zA-Z0-9_]+\)`,
line, lines = fgetslines(fpin),
num = length(lines),
hits = -1[Int_Type[num]],
funcs = [""][Int_Type[num]],
fhits = Int_Type[num];
#ifnexists _set_bos_compile_hook
variable stmt_res =
[
`^[^#%]*[-([@!+/*&|<>^=]`,
`^[ \t]*continue\>`,
`^[ \t]*break\>`,
`^[ \t]*return\>`,
`^[ \t]*throw\>`,
];
variable not_stmt_res =
[
`^[ \t]*[]0-9"'[{}+-/*&!=|]`, % continuation
`^[ \ta-z0-9_]+,`, % continuation
`^["%]*\\\$`, % continuation of multiline string
`^[ \t]*([a-z0-9_, \t]*)[ \t][^=]`, % matches (a,b,..) [^=]
`^[ \t]*try\>`,
`^[ \t]*return[ \t]*;`, % return;
];
#endif
() = fclose (fpin);
_for i (0, num-1, 1)
{
line = lines[i];
variable matches = string_matches (line, fre);
if (matches != NULL)
{
variable func = matches[1];
#ifexists _set_bof_compile_hook
ifnot (Func_List[file + "\t" + func])
continue; % not compiled, probably #ifdef'd out
#endif
hits[where (funcs == func)] = -1; % probably a forward declaration
funcs[i] = func;
fhits[i] = Func_Hits[file + "\t" + func];
continue;
}
#ifnexists _set_bos_compile_hook
variable re;
foreach re (stmt_res)
{
if (string_match (line, re))
{
hits[i] = 0;
% False positive check
foreach re (not_stmt_res)
{
if (string_match (line, re))
{
hits[i] = -1;
break;
}
}
break;
}
}
#endif
}
i = where (s.file == file);
hits[s.lineno[i]-1] = s.hits[i];
csv.file = [csv.file, [file][Int_Type[num]]];
csv.lineno = [csv.lineno, [1:num]];
csv.hits = [csv.hits, hits];
csv.function = [csv.function, funcs];
csv.fhits = [csv.fhits, fhits];
}
struct_filter (csv, where ((csv.hits != -1) or (csv.function != "")));
if (use_gcov)
output_trace_file (fp, csv);
else
csv_writecol (fp, csv);
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
private define _slcov_version ()
{
() = fprintf (stderr, "slcov version %s; S-Lang version: %s\n",
Version, _slang_version_string);
}
private define _slcov_usage ()
{
variable fp = stderr;
() = fprintf (fp, "Usage: %s [options] script args...\n", path_basename (__argv[0]));
() = fprintf (fp, "Options:\n");
variable opts =
[
" -f|--func <function> Name of Function to call [default: slsh_main]\n",
" -o|--output <file> Name of output file [default: <script>.slcov]\n",
" -v|--version Print version information\n",
" -h|--help Print this message\n"
];
variable opt;
foreach opt (opts)
() = fputs (opt, fp);
() = fputs ("\
After running this script, use the genhtml script to produce html output:\n\
genhtml -o /var/www/html/dir -t 'title' --num-spaces 8 OUTPUT-FILE\n\
",
fp);
exit (1);
}
private define _slcov_cmdopt_error (msg)
{
() = fprintf (stderr, "%s\n", msg);
_slcov_usage ();
}
private variable Use_Gcov = 1;
private define _slcov_write_report ()
{
slcov_write_report (Output_Fp, Use_Gcov);
() = fclose (Output_Fp);
}
private define _slcov_main ()
{
variable output = NULL;
variable func = "slsh_main";
variable line_by_line = 0;
variable opts = cmdopt_new (&_slcov_cmdopt_error);
opts.add("f|funct", &func; type="string");
opts.add("o|output",&output; type="string");
opts.add("v|version", &_slcov_version),
opts.add("h|help", &_slcov_usage);
variable i = opts.process (__argv, 1);
if (func == "")
_slcov_usage ();
if (i >= __argc)
_slcov_usage ();
variable main = strtok (func, " \t(;")[0];
variable main_args = strtrim (substrbytes (func, 1+strlen(main), -1));
variable depth = _stkdepth();
try
{
if (strlen (main)) eval (main_args);
}
catch AnyError:
{
() = fprintf (stderr, "Error parsing args of %s\n", func);
exit (1);
}
main_args = __pop_args (_stkdepth ()-depth);
variable script = __argv[i];
if (not path_is_absolute (script))
script = path_concat (getcwd (), script);
variable st = stat_file (script);
if (st == NULL)
{
() = fprintf (stderr, "Unable to stat %s -- did you specify its path?\n", script);
exit (1);
}
if (not stat_is ("reg", st.st_mode))
() = fprintf (stderr, "*** Warning %s is not a regular file\n", script);
if (output == NULL)
output = strcat (path_basename_sans_extname (script), ".slcov");
variable fp = fopen (output, "w");
if (fp == NULL)
{
() = fprintf (stderr, "Unable to open code coverage output file %s\n");
exit (1);
}
() = fprintf (fp, "# slcov report for:\n");
() = fprintf (fp, "# %S\n", strjoin (__argv, " "));
Output_Fp = fp;
variable has_at_exit = 0;
atexit (&_slcov_write_report);
__set_argc_argv (__argv[[i:]]);
slcov_enable ();
() = evalfile (script);
variable ref = __get_reference (main);
if (ref != NULL)
(@ref) (__push_args (main_args));
else if (main != "slsh_main")
() = fprintf (stderr, "*** Warning: %s is not defined\n", main);
}
_slcov_main ();
exit (0);
|