|
| 1 | +<?php |
| 2 | +$target = $_SERVER['argv'][1]; |
| 3 | + |
| 4 | +// clang warning based unused variables fixer. |
| 5 | +// |
| 6 | +// make 2> error.log |
| 7 | +// grep "unused variables" error.log > error2.log |
| 8 | +// php fixer.php <path.c> |
| 9 | +$data = file_get_contents("error2.log"); |
| 10 | +$result = array(); |
| 11 | +foreach (explode("\n", $data) as $line) { |
| 12 | + if (preg_match("!(.+?):(\d+):(\d+): warning: unused variable '(.+?)'!", $line, $match)) { |
| 13 | + $filename = basename($match[1]); |
| 14 | + $line = $match[2]; |
| 15 | + $columns = $match[3]; |
| 16 | + $name = $match[4]; |
| 17 | + |
| 18 | + if ($filename == $target) { |
| 19 | + $result[$filename][] = array( |
| 20 | + "line" => $line, |
| 21 | + "columns" => $columns, |
| 22 | + "name" => $name, |
| 23 | + ); |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +//var_dump($result); |
| 29 | +$file = file_get_contents($target); |
| 30 | +$lines = explode("\n", $file); |
| 31 | +if (!isset($result[$target])) { |
| 32 | + echo "nothing"; |
| 33 | + exit; |
| 34 | +} |
| 35 | +foreach ($result[$target] as $value) { |
| 36 | + //echo $lines[$value['line']-1] . PHP_EOL; |
| 37 | + |
| 38 | + $line = $lines[$value['line']-1]; |
| 39 | + $l = parseLine($line); |
| 40 | + $l->remove($value['name']); |
| 41 | + $hhh = $l->toLine(); |
| 42 | + $lines[$value['line']-1] = $hhh; |
| 43 | +} |
| 44 | +$buffer = ""; |
| 45 | +for ($i = 0; $i < count($lines); $i++) { |
| 46 | + $line = $lines[$i]; |
| 47 | + if ($line == "==========REMOVED==========") { |
| 48 | + continue; |
| 49 | + } |
| 50 | + $buffer .= $line; |
| 51 | + if ($i + 1 < count($lines)) { |
| 52 | + $buffer .= "\n"; |
| 53 | + } |
| 54 | +} |
| 55 | +//echo $buffer;exit; |
| 56 | +file_put_contents($target, $buffer); |
| 57 | + |
| 58 | +function parseLine($line) |
| 59 | +{ |
| 60 | + for ($i = 0; $i < strlen($line); $i++) { |
| 61 | + if ($line[$i] == "\t") { |
| 62 | + continue; |
| 63 | + } else { |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $l = new Line($i); |
| 69 | + $arg = substr($line, $i); |
| 70 | + $type = null; |
| 71 | + $args = array(); |
| 72 | + $tmp = null; |
| 73 | + $base = null; |
| 74 | + foreach (explode(",", $arg) as $arg2) { |
| 75 | + $tokens = preg_split("/(\s+|;)/", $arg2); |
| 76 | + |
| 77 | + $cnt = count($tokens); |
| 78 | + $state = 0; |
| 79 | + for ($i = 0; $i < $cnt; $i++) { |
| 80 | + if ($state == 0) { |
| 81 | + if (empty($tokens[$i])) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + if (!$type) { |
| 85 | + if ($tokens[$i] != "const") { |
| 86 | + $type = $tokens[$i]; |
| 87 | + $state = 1; |
| 88 | + if (!$tmp) { |
| 89 | + $tmp = new Variable(); |
| 90 | + $tmp->setType($tokens[$i]); |
| 91 | + $base = clone $tmp; |
| 92 | + } |
| 93 | + } else { |
| 94 | + $tmp = new Variable(); |
| 95 | + $tmp->is_const = 1; |
| 96 | + $tmp->setType($tokens[$i+1]); |
| 97 | + $i++; |
| 98 | + $state = 1; |
| 99 | + $base = clone $tmp; |
| 100 | + } |
| 101 | + } else { |
| 102 | + $state = 1; |
| 103 | + $tmp = clone $base; |
| 104 | + $tmp->setName($tokens[$i]); |
| 105 | + } |
| 106 | + } else if ($state == 1) { |
| 107 | + if ($tokens[$i] == "=") { |
| 108 | + $state = 2; |
| 109 | + } else { |
| 110 | + $tmp->setName($tokens[$i]); |
| 111 | + } |
| 112 | + } else if ($state == 2) { |
| 113 | + $tmp->setDefault($tokens[$i]); |
| 114 | + $l->add($tmp); |
| 115 | + unset($tmp); |
| 116 | + $state = 0; |
| 117 | + } |
| 118 | + } |
| 119 | + if (isset($tmp)) { |
| 120 | + $l->add($tmp); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return $l; |
| 125 | +} |
| 126 | + |
| 127 | +class Line |
| 128 | +{ |
| 129 | + public $indent; |
| 130 | + public $variables = array(); |
| 131 | + |
| 132 | + public function __construct($indent) |
| 133 | + { |
| 134 | + $this->indent = $indent; |
| 135 | + } |
| 136 | + |
| 137 | + public function add(Variable $var) |
| 138 | + { |
| 139 | + $this->variables[] = $var; |
| 140 | + } |
| 141 | + |
| 142 | + public function remove($name) |
| 143 | + { |
| 144 | + foreach ($this->variables as $o => $var) { |
| 145 | + if ($var->name == $name) { |
| 146 | + unset($this->variables[$o]); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public function isEmpty() |
| 152 | + { |
| 153 | + if (count($this->variables)) { |
| 154 | + return false; |
| 155 | + } else { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public function toLine() |
| 161 | + { |
| 162 | + $type = null; |
| 163 | + $buffer = str_repeat("\t", $this->indent); |
| 164 | + $values = array(); |
| 165 | + foreach ($this->variables as $var) { |
| 166 | + if (!$type) { |
| 167 | + if ($var->getNameWithPtr() === "") { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + $type = $var->type; |
| 172 | + if ($var->is_const) { |
| 173 | + $buffer .= "const "; |
| 174 | + } |
| 175 | + $buffer .= sprintf("%s ", $var->type); |
| 176 | + |
| 177 | + if ($var->default !== NULL) { |
| 178 | + $values[] = sprintf("%s = %s", $var->getNameWithPtr(), $var->default); |
| 179 | + } else { |
| 180 | + $values[] = $var->getNameWithPtr(); |
| 181 | + } |
| 182 | + } else { |
| 183 | + if ($var->getNameWithPtr() === "") { |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + if ($var->default !== NULL) { |
| 189 | + $values[] = sprintf("%s = %s", $var->getNameWithPtr(), $var->default); |
| 190 | + } else { |
| 191 | + $values[] = $var->getNameWithPtr(); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + if (count($values)) { |
| 197 | + $buffer .= join(", ", $values); |
| 198 | + } else { |
| 199 | + return "==========REMOVED=========="; |
| 200 | + } |
| 201 | + if (strlen($buffer) > $this->indent) { |
| 202 | + $buffer .= ";"; |
| 203 | + } |
| 204 | + |
| 205 | + $check = trim($buffer, " \t"); |
| 206 | + if (!$check) { |
| 207 | + return "==========REMOVED=========="; |
| 208 | + } |
| 209 | + |
| 210 | + return $buffer; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +class Variable |
| 215 | +{ |
| 216 | + public $name; |
| 217 | + public $type; |
| 218 | + public $default; |
| 219 | + public $is_static; |
| 220 | + public $is_const; |
| 221 | + public $ptr = 0; |
| 222 | + |
| 223 | + public function __construct() |
| 224 | + { |
| 225 | + } |
| 226 | + |
| 227 | + public function setType($type) |
| 228 | + { |
| 229 | + $this->type = $type; |
| 230 | + } |
| 231 | + |
| 232 | + public function setName($name) |
| 233 | + { |
| 234 | + $ptr = 0; |
| 235 | + for ($i =0; $i < strlen($name); $i++) { |
| 236 | + if ($name[$i] == '*') { |
| 237 | + $ptr++; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + $this->name = substr($name, $ptr); |
| 242 | + if ($ptr) { |
| 243 | + $this->ptr = $ptr; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + public function setPtr($ptr) |
| 248 | + { |
| 249 | + $this->ptr = $ptr; |
| 250 | + } |
| 251 | + |
| 252 | + public function setDefault($default) |
| 253 | + { |
| 254 | + $this->default = $default; |
| 255 | + } |
| 256 | + |
| 257 | + public function getNameWithPtr() |
| 258 | + { |
| 259 | + if ($this->name) { |
| 260 | + return str_repeat("*", $this->ptr) . $this->name; |
| 261 | + } else { |
| 262 | + return ""; |
| 263 | + } |
| 264 | + } |
| 265 | +} |
0 commit comments