PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/framework/lib/labels.php

http://phc.googlecode.com/
PHP | 298 lines | 231 code | 38 blank | 29 comment | 34 complexity | 98774ccf1602f107e2164593ed0da129 MD5 | raw file
Possible License(s): GPL-2.0, 0BSD, BSD-3-Clause, Unlicense, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <?php
  2. /*
  3. * phc -- the open source PHP compiler
  4. * See doc/license/README.license for licensing information
  5. *
  6. * Functions and data to parse and maniupulate label files
  7. */
  8. $subject_dir = "test/subjects/";
  9. $label_file = "test/subjects/labels";
  10. $third_party_label_file = "test/subjects/3rdparty/labels";
  11. $default_labels = array("long", "non-interpretable", "size-neutral");
  12. $non_default_labels = array("short", "interpretable", "size-dependent");
  13. $labels = array_merge($default_labels, $non_default_labels);
  14. $opposite_label = array(
  15. "long" => "short", "short" => "long",
  16. "non-interpretable" => "interpretable", "interpretable" => "non-interpretable",
  17. "size-neutral" => "size-dependent", "size-dependent" => "size-neutral");
  18. $exceptions = array();
  19. $label_struct = create_label_struct ($subject_dir, $label_file, $third_party_label_file);
  20. if (!$opt_long)
  21. {
  22. $label_struct = strip_long_files($label_struct);
  23. }
  24. function strip_long_files ($label_struct)
  25. {
  26. global $labels;
  27. $long_files = $label_struct{"long"};
  28. foreach($labels as $label)
  29. {
  30. // remove the long files from
  31. $label_struct{$label} = array_values (array_diff($label_struct{$label}, $long_files));
  32. }
  33. return $label_struct;
  34. }
  35. /* Returns true if this is a long test, and we're only doing short tests */
  36. function skip_3rdparty ($filename)
  37. {
  38. global $opt_long;
  39. return (!$opt_long && preg_match ("#/3rdparty/#", $filename));
  40. }
  41. function create_label_struct ($directory, $label_filename, $third_party_filename)
  42. {
  43. global $default_labels;
  44. global $non_default_labels;
  45. global $opposite_label;
  46. global $labels;
  47. global $exceptions;
  48. global $opt_one;
  49. $files = get_all_scripts_in_dir ($directory);
  50. // labelled files is a table indexed by filename, containing tables indexed
  51. // by default labels, which are set to 1 or 0 for default and non-default
  52. // respectively
  53. foreach ($default_labels as $default)
  54. {
  55. foreach($files as $filename) { $labelled_files{$filename}{$default} = 1; }
  56. }
  57. foreach($files as $filename)
  58. {
  59. $labelled_files{$filename}{"non-interpretable"} = "check";
  60. }
  61. // parse the file
  62. $lines = file($label_filename);
  63. if (file_exists ($third_party_filename))
  64. $third_party_lines = file ($third_party_filename);
  65. else
  66. $third_party_lines = array ();
  67. foreach($lines as $line)
  68. {
  69. $line = preg_replace("/#.*$/", "", $line); // remove comments
  70. $line = trim($line); // remove superfluous whitespace
  71. if ($line == "") continue; // skip blank lines
  72. process_label_file_line ($line, $files, $labelled_files);
  73. }
  74. foreach($third_party_lines as $line)
  75. {
  76. $line = preg_replace("/#.*$/", "", $line); // remove comments
  77. $line = trim($line); // remove superfluous whitespace
  78. if ($line == "") continue; // skip blank lines
  79. process_label_file_line ("3rdparty/".$line, $files, $labelled_files);
  80. }
  81. # if -O is provided, remove all other files
  82. if ($opt_one)
  83. {
  84. foreach ($labelled_files as $key => $value)
  85. {
  86. if ($key !== $opt_one)
  87. {
  88. unset ($labelled_files[$key]);
  89. $files = array ($opt_one);
  90. }
  91. }
  92. }
  93. # init the label struct
  94. foreach ($labels as $label)
  95. {
  96. $label_struct{$label} = array();
  97. }
  98. # go over the labelled_files, and make an table indexed by label
  99. foreach ($files as $filename)
  100. {
  101. // If you have a ton of 3rd party files, dont spend time checking them
  102. // all, unless your actually going to use them
  103. if (skip_3rdparty ($filename))
  104. {
  105. $labelled_files{$filename}{"non-interpretable"} = 0;
  106. }
  107. else
  108. {
  109. phc_assert(isset($labelled_files{$filename}), "file not found");
  110. // check the interpretable
  111. if ($labelled_files{$filename}{"non-interpretable"} === "check")
  112. {
  113. phc_assert (check_for_plugin ("tools/purity_test"), "purity not available");
  114. global $phc, $plugin_dir;
  115. if (`$phc --run $plugin_dir/tools/purity_test.la $filename 2>&1` == "")
  116. {
  117. log_status ("pure", "", $filename, "");
  118. $labelled_files{$filename}{"non-interpretable"} = 0;
  119. }
  120. else
  121. {
  122. log_status ("impure", "", $filename, "");
  123. $labelled_files{$filename}{"non-interpretable"} = 1;
  124. }
  125. }
  126. }
  127. foreach ($default_labels as $label)
  128. {
  129. if ($labelled_files{$filename}{$label})
  130. {
  131. array_push ($label_struct{$label}, $filename);
  132. }
  133. else
  134. {
  135. array_push ($label_struct{$opposite_label{$label}}, $filename);
  136. }
  137. }
  138. }
  139. // sort and generally fix up the arrays
  140. foreach ($labels as $label)
  141. {
  142. sort($label_struct{$label});
  143. }
  144. return $label_struct;
  145. }
  146. function process_label_file_line ($line, $files, &$labelled_files)
  147. {
  148. global $default_labels;
  149. global $non_default_labels;
  150. global $opposite_label;
  151. global $labels;
  152. global $exceptions;
  153. // split into file and labels
  154. $split = preg_split("/\s+/", $line);
  155. $pattern = array_shift ($split);
  156. $pattern = "^test/subjects/$pattern";
  157. $matches = preg_grep ("!$pattern!", $files);
  158. /* allow that pattern, as 3rdparty directory may be empty */
  159. if ($pattern !== "^test/subjects/3rdparty/.*")
  160. {
  161. phc_assert(count($matches), "pattern !$pattern! matches no files");
  162. }
  163. // add to data structure
  164. foreach($split as $label)
  165. {
  166. foreach ($matches as $filename)
  167. {
  168. if (in_array($label, $default_labels))
  169. {
  170. $labelled_files{$filename}{$label} = 1;
  171. }
  172. else if (in_array($label, $non_default_labels))
  173. {
  174. $labelled_files{$filename}{$opposite_label{$label}} = 0;
  175. }
  176. else if (preg_match ("/no-(.*)/", $label, $exception_matches))
  177. {
  178. $exceptions{$filename}[] = $exception_matches[1];
  179. }
  180. else if ($label == "check-interpretable")
  181. {
  182. $labelled_files{$filename}{"non-interpretable"} = "check";
  183. }
  184. else
  185. {
  186. $label_names = join(", ", $labels);
  187. phc_unreachable ("Label file error: $label not a valid label (must be in $label_names)");
  188. }
  189. }
  190. }
  191. }
  192. function get_scripts_labelled($label)
  193. {
  194. global $label_struct;
  195. phc_assert($label_struct !== null, "label structure not yet initialized");
  196. return $label_struct{$label};
  197. }
  198. function get_interpretable_scripts()
  199. {
  200. return get_scripts_labelled("interpretable");
  201. }
  202. function get_non_interpretable_scripts()
  203. {
  204. return get_scripts_labelled("non-interpretable");
  205. }
  206. function is_labelled($script, $label)
  207. {
  208. global $label_struct;
  209. phc_assert($label_struct !== null, "label structure not yet initialized");
  210. # this doesnt return a simple true/false, so wrap it
  211. if (array_search($script, $label_struct{$label}) === false)
  212. {
  213. return false;
  214. }
  215. else
  216. {
  217. return true;
  218. }
  219. }
  220. // returns a list of all php scripts
  221. function get_all_scripts()
  222. {
  223. global $label_struct;
  224. phc_assert($label_struct !== null, "label structure not yet initialized");
  225. // returns an array of arrays
  226. $all_files = array_values($label_struct);
  227. // merge into a single array
  228. $result = array();
  229. foreach($all_files as $array)
  230. {
  231. $result = array_merge($result, $array);
  232. }
  233. $result = array_unique($result);
  234. return $result;
  235. }
  236. // returns a list of all the php files within a directory (recursively)
  237. function get_all_scripts_in_dir($directory)
  238. {
  239. phc_assert($directory != '', "Cant search blank directory");
  240. phc_assert(preg_match("/\/$/", $directory), "directory '$directory' must end in a '/'");
  241. $command = "find -L $directory -name \"*.php\"";
  242. $result = explode ("\n", trim (`$command`));
  243. if (count ($result) == 1 && $result[0] == "")
  244. return array ();
  245. # I have no idea why, but on OSX `find` returns two forward-slashes in some
  246. # cases, and PHP won't open directories structured like that.
  247. foreach($result as &$line)
  248. $line = preg_replace("!//!", "/", $line);
  249. return $result;
  250. }
  251. function get_all_plugins ()
  252. {
  253. global $base_dir;
  254. $command = "find $base_dir/plugins -name \"*.cpp\"";
  255. $result = preg_split ("/\n/", trim (`$command`));
  256. if (count ($result) == 1 && $result[0] == "")
  257. return array ();
  258. return $result;
  259. }
  260. ?>