]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/arcanist_util/cpp_linter/FbcodeCppLinter.php
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / rocksdb / arcanist_util / cpp_linter / FbcodeCppLinter.php
1 <?php
2 // Copyright 2004-present Facebook. All rights reserved.
3
4 class FbcodeCppLinter extends ArcanistLinter {
5 const FLINT = "/home/engshare/tools/flint";
6 const LINT_ERROR = 1;
7 const LINT_WARNING = 2;
8 const LINT_ADVICE = 3;
9 const C_FLAG = "--c_mode=true";
10
11 private $rawLintOutput = array();
12
13 public function willLintPaths(array $paths) {
14 if (!file_exists(self::FLINT)) {
15 return;
16 }
17 $futures = array();
18 foreach ($paths as $p) {
19 $lpath = $this->getEngine()->getFilePathOnDisk($p);
20 $lpath_file = file($lpath);
21 if (preg_match('/\.(c)$/', $lpath) ||
22 preg_match('/-\*-.*Mode: C[; ].*-\*-/', $lpath_file[0]) ||
23 preg_match('/vim(:.*)*:\s*(set\s+)?filetype=c\s*:/', $lpath_file[0])
24 ) {
25 $futures[$p] = new ExecFuture("%s %s %s 2>&1",
26 self::FLINT, self::C_FLAG,
27 $this->getEngine()->getFilePathOnDisk($p));
28 } else {
29 $futures[$p] = new ExecFuture("%s %s 2>&1",
30 self::FLINT, $this->getEngine()->getFilePathOnDisk($p));
31 }
32 }
33
34 foreach (Futures($futures)->limit(8) as $p => $f) {
35 $this->rawLintOutput[$p] = $f->resolvex();
36 }
37
38 return;
39 }
40
41 public function getLinterName() {
42 return "FBCPP";
43 }
44
45 public function lintPath($path) {
46 $this->runCppLint($path);
47 }
48
49 private function runCppLint($path) {
50 $msgs = $this->getCppLintOutput($path);
51 foreach ($msgs as $m) {
52 $this->raiseLintAtLine($m['line'], 0, $m['severity'], $m['msg']);
53 }
54 }
55
56 private function adviseOnEachPattern(
57 $path,
58 $regex,
59 $message,
60 $lint_type = self::LINT_ADVICE,
61 $match_idx = 0) {
62 $file_data = $this->getData($path);
63 $matches = array();
64 if (!preg_match_all($regex, $file_data, $matches, PREG_OFFSET_CAPTURE)) {
65 return;
66 }
67
68 foreach ($matches[$match_idx] as $match) {
69 list($match_str, $offset) = $match;
70 $this->raiseLintAtOffset($offset, $lint_type, $message, $match_str);
71 }
72 }
73
74 public function getLintSeverityMap() {
75 return array(
76 self::LINT_WARNING => ArcanistLintSeverity::SEVERITY_WARNING,
77 self::LINT_ADVICE => ArcanistLintSeverity::SEVERITY_ADVICE,
78 self::LINT_ERROR => ArcanistLintSeverity::SEVERITY_ERROR
79 );
80 }
81
82 public function getLintNameMap() {
83 return array(
84 self::LINT_ADVICE => "CppLint Advice",
85 self::LINT_WARNING => "CppLint Warning",
86 self::LINT_ERROR => "CppLint Error"
87 );
88 }
89
90 private function getCppLintOutput($path) {
91 if (!array_key_exists($path, $this->rawLintOutput)) {
92 return array();
93 }
94 list($output) = $this->rawLintOutput[$path];
95
96 $msgs = array();
97 $current = null;
98 $matches = array();
99 foreach (explode("\n", $output) as $line) {
100 if (preg_match('/.*?:(\d+):(.*)/', $line, $matches)) {
101 if ($current) {
102 $msgs[] = $current;
103 }
104 $line = $matches[1];
105 $text = $matches[2];
106 if (preg_match('/.*Warning.*/', $text)) {
107 $sev = self::LINT_WARNING;
108 } else if (preg_match('/.*Advice.*/', $text)) {
109 $sev = self::LINT_ADVICE;
110 } else {
111 $sev = self::LINT_ERROR;
112 }
113 $current = array('line' => $line,
114 'msg' => $text,
115 'severity' => $sev);
116 } else if ($current) {
117 $current['msg'] .= ' ' . $line;
118 }
119 }
120 if ($current) {
121 $msgs[] = $current;
122 }
123
124 return $msgs;
125 }
126 }