2012-11-28 07:09:08 +01:00
|
|
|
<?php
|
2015-08-10 22:58:55 +02:00
|
|
|
// Copyright 2004-present Facebook. All rights reserved.
|
2012-11-28 07:09:08 +01:00
|
|
|
|
|
|
|
class FbcodeCppLinter extends ArcanistLinter {
|
2015-08-10 22:58:55 +02:00
|
|
|
const FLINT = "/home/engshare/tools/flint";
|
2012-11-28 07:09:08 +01:00
|
|
|
const LINT_ERROR = 1;
|
|
|
|
const LINT_WARNING = 2;
|
2015-08-10 22:58:55 +02:00
|
|
|
const LINT_ADVICE = 3;
|
2012-11-28 07:09:08 +01:00
|
|
|
const C_FLAG = "--c_mode=true";
|
2015-08-10 22:58:55 +02:00
|
|
|
|
2012-11-28 07:09:08 +01:00
|
|
|
private $rawLintOutput = array();
|
|
|
|
|
|
|
|
public function willLintPaths(array $paths) {
|
2015-08-11 20:36:12 +02:00
|
|
|
if (!file_exists(self::FLINT)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 07:09:08 +01:00
|
|
|
$futures = array();
|
2015-08-10 22:58:55 +02:00
|
|
|
foreach ($paths as $p) {
|
|
|
|
$lpath = $this->getEngine()->getFilePathOnDisk($p);
|
|
|
|
$lpath_file = file($lpath);
|
|
|
|
if (preg_match('/\.(c)$/', $lpath) ||
|
|
|
|
preg_match('/-\*-.*Mode: C[; ].*-\*-/', $lpath_file[0]) ||
|
|
|
|
preg_match('/vim(:.*)*:\s*(set\s+)?filetype=c\s*:/', $lpath_file[0])
|
|
|
|
) {
|
|
|
|
$futures[$p] = new ExecFuture("%s %s %s 2>&1",
|
|
|
|
self::FLINT, self::C_FLAG,
|
|
|
|
$this->getEngine()->getFilePathOnDisk($p));
|
|
|
|
} else {
|
|
|
|
$futures[$p] = new ExecFuture("%s %s 2>&1",
|
|
|
|
self::FLINT, $this->getEngine()->getFilePathOnDisk($p));
|
2012-11-28 07:09:08 +01:00
|
|
|
}
|
2015-08-10 22:58:55 +02:00
|
|
|
}
|
2012-11-28 07:09:08 +01:00
|
|
|
|
2015-08-10 22:58:55 +02:00
|
|
|
foreach (Futures($futures)->limit(8) as $p => $f) {
|
|
|
|
$this->rawLintOutput[$p] = $f->resolvex();
|
2012-11-28 07:09:08 +01:00
|
|
|
}
|
2015-08-10 22:58:55 +02:00
|
|
|
|
2012-11-28 07:09:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return "FBCPP";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function lintPath($path) {
|
2015-08-10 22:58:55 +02:00
|
|
|
$this->runCppLint($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function runCppLint($path) {
|
2012-11-28 07:09:08 +01:00
|
|
|
$msgs = $this->getCppLintOutput($path);
|
|
|
|
foreach ($msgs as $m) {
|
|
|
|
$this->raiseLintAtLine($m['line'], 0, $m['severity'], $m['msg']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 22:58:55 +02:00
|
|
|
private function adviseOnEachPattern(
|
|
|
|
$path,
|
|
|
|
$regex,
|
|
|
|
$message,
|
|
|
|
$lint_type = self::LINT_ADVICE,
|
|
|
|
$match_idx = 0) {
|
|
|
|
$file_data = $this->getData($path);
|
|
|
|
$matches = array();
|
|
|
|
if (!preg_match_all($regex, $file_data, $matches, PREG_OFFSET_CAPTURE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($matches[$match_idx] as $match) {
|
|
|
|
list($match_str, $offset) = $match;
|
|
|
|
$this->raiseLintAtOffset($offset, $lint_type, $message, $match_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 07:09:08 +01:00
|
|
|
public function getLintSeverityMap() {
|
|
|
|
return array(
|
|
|
|
self::LINT_WARNING => ArcanistLintSeverity::SEVERITY_WARNING,
|
2015-08-10 22:58:55 +02:00
|
|
|
self::LINT_ADVICE => ArcanistLintSeverity::SEVERITY_ADVICE,
|
2012-11-28 07:09:08 +01:00
|
|
|
self::LINT_ERROR => ArcanistLintSeverity::SEVERITY_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLintNameMap() {
|
|
|
|
return array(
|
2015-08-10 22:58:55 +02:00
|
|
|
self::LINT_ADVICE => "CppLint Advice",
|
|
|
|
self::LINT_WARNING => "CppLint Warning",
|
|
|
|
self::LINT_ERROR => "CppLint Error"
|
2012-11-28 07:09:08 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getCppLintOutput($path) {
|
|
|
|
list($output) = $this->rawLintOutput[$path];
|
|
|
|
|
|
|
|
$msgs = array();
|
|
|
|
$current = null;
|
2015-08-10 22:58:55 +02:00
|
|
|
$matches = array();
|
2012-11-28 07:09:08 +01:00
|
|
|
foreach (explode("\n", $output) as $line) {
|
2015-08-10 22:58:55 +02:00
|
|
|
if (preg_match('/.*?:(\d+):(.*)/', $line, $matches)) {
|
2012-11-28 07:09:08 +01:00
|
|
|
if ($current) {
|
|
|
|
$msgs[] = $current;
|
|
|
|
}
|
|
|
|
$line = $matches[1];
|
|
|
|
$text = $matches[2];
|
2015-08-10 22:58:55 +02:00
|
|
|
if (preg_match('/.*Warning.*/', $text)) {
|
|
|
|
$sev = self::LINT_WARNING;
|
|
|
|
} else if (preg_match('/.*Advice.*/', $text)) {
|
|
|
|
$sev = self::LINT_ADVICE;
|
|
|
|
} else {
|
|
|
|
$sev = self::LINT_ERROR;
|
|
|
|
}
|
2012-11-28 07:09:08 +01:00
|
|
|
$current = array('line' => $line,
|
|
|
|
'msg' => $text,
|
|
|
|
'severity' => $sev);
|
|
|
|
} else if ($current) {
|
|
|
|
$current['msg'] .= ' ' . $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($current) {
|
|
|
|
$msgs[] = $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $msgs;
|
|
|
|
}
|
|
|
|
}
|