]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/arcanist_util/cpp_linter/ArcanistCpplintLinter.php
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / arcanist_util / cpp_linter / ArcanistCpplintLinter.php
1 <?php
2
3 /**
4 * Uses google's cpplint.py to check code. RocksDB team forked this file from
5 * phabricator's /src/lint/linter/ArcanistCpplintLinter.php, and customized it
6 * for its own use.
7 *
8 * You can get it here:
9 * http://google-styleguide.googlecode.com/svn/trunk/cpplint/cpplint.py
10 * @group linter
11 */
12 final class ArcanistCpplintLinter extends ArcanistLinter {
13
14 public function willLintPaths(array $paths) {
15 return;
16 }
17
18 public function getLinterName() {
19 return 'cpplint.py';
20 }
21
22 public function getLintPath() {
23 $bin = 'cpplint.py';
24 // Search under current dir
25 list($err) = exec_manual('which %s/%s', $this->linterDir(), $bin);
26 if (!$err) {
27 return $this->linterDir().'/'.$bin;
28 }
29
30 // Look for globally installed cpplint.py
31 list($err) = exec_manual('which %s', $bin);
32 if ($err) {
33 throw new ArcanistUsageException(
34 "cpplint.py does not appear to be installed on this system. Install ".
35 "it (e.g., with 'wget \"http://google-styleguide.googlecode.com/".
36 "svn/trunk/cpplint/cpplint.py\"') ".
37 "in your .arcconfig to point to the directory where it resides. ".
38 "Also don't forget to chmod a+x cpplint.py!");
39 }
40
41 return $bin;
42 }
43
44 public function lintPath($path) {
45 $bin = $this->getLintPath();
46 $path = $this->rocksdbDir().'/'.$path;
47
48 $f = new ExecFuture("%C $path", $bin);
49
50 list($err, $stdout, $stderr) = $f->resolve();
51
52 if ($err === 2) {
53 throw new Exception("cpplint failed to run correctly:\n".$stderr);
54 }
55
56 $lines = explode("\n", $stderr);
57 $messages = array();
58 foreach ($lines as $line) {
59 $line = trim($line);
60 $matches = null;
61 $regex = '/^[^:]+:(\d+):\s*(.*)\s*\[(.*)\] \[(\d+)\]$/';
62 if (!preg_match($regex, $line, $matches)) {
63 continue;
64 }
65 foreach ($matches as $key => $match) {
66 $matches[$key] = trim($match);
67 }
68 $message = new ArcanistLintMessage();
69 $message->setPath($path);
70 $message->setLine($matches[1]);
71 $message->setCode($matches[3]);
72 $message->setName($matches[3]);
73 $message->setDescription($matches[2]);
74 $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
75 $this->addLintMessage($message);
76 }
77 }
78
79 // The path of this linter
80 private function linterDir() {
81 return dirname(__FILE__);
82 }
83
84 // TODO(kaili) a quick and dirty way to figure out rocksdb's root dir.
85 private function rocksdbDir() {
86 return $this->linterDir()."/../..";
87 }
88 }