2013-01-07 08:16:45 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2014-05-12 04:30:36 -07:00
|
|
|
* Uses Cppcheck to do basic checks in a C++ file.
|
2013-01-07 08:16:45 -08:00
|
|
|
*/
|
2014-05-12 04:30:36 -07:00
|
|
|
final class ArcanistCppcheckLinter extends ArcanistExternalLinter {
|
|
|
|
|
|
|
|
public function getInfoName() {
|
2015-09-08 14:52:43 -07:00
|
|
|
return 'C++ linter';
|
2014-05-12 04:30:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfoURI() {
|
|
|
|
return 'http://cppcheck.sourceforge.net';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfoDescription() {
|
2015-05-13 18:05:15 +10:00
|
|
|
return pht(
|
|
|
|
'Use `%s` to perform static analysis on C/C++ code.',
|
|
|
|
'cppcheck');
|
2014-05-12 04:30:36 -07:00
|
|
|
}
|
2013-01-07 08:16:45 -08:00
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'cppcheck';
|
|
|
|
}
|
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
public function getLinterConfigurationName() {
|
|
|
|
return 'cppcheck';
|
2013-01-07 08:16:45 -08:00
|
|
|
}
|
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
public function getDefaultBinary() {
|
2015-05-21 06:57:23 +10:00
|
|
|
return 'cppcheck';
|
2014-05-12 04:30:36 -07:00
|
|
|
}
|
2013-01-07 08:16:45 -08:00
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
public function getVersion() {
|
|
|
|
list($stdout) = execx('%C --version', $this->getExecutableCommand());
|
2013-01-07 08:16:45 -08:00
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
$matches = array();
|
|
|
|
$regex = '/^Cppcheck (?P<version>\d+\.\d+)$/';
|
|
|
|
if (preg_match($regex, $stdout, $matches)) {
|
|
|
|
return $matches['version'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-07 08:16:45 -08:00
|
|
|
}
|
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
public function getInstallInstructions() {
|
2015-05-13 18:05:15 +10:00
|
|
|
return pht(
|
|
|
|
'Install Cppcheck using `%s` or similar.',
|
|
|
|
'apt-get install cppcheck');
|
2014-05-12 04:30:36 -07:00
|
|
|
}
|
2013-01-07 08:16:45 -08:00
|
|
|
|
2015-05-21 06:57:23 +10:00
|
|
|
protected function getDefaultFlags() {
|
|
|
|
return array(
|
|
|
|
'-j2',
|
|
|
|
'--enable=performance,style,portability,information',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
protected function getMandatoryFlags() {
|
|
|
|
return array(
|
|
|
|
'--quiet',
|
|
|
|
'--inline-suppr',
|
|
|
|
'--xml',
|
|
|
|
'--xml-version=2',
|
|
|
|
);
|
|
|
|
}
|
2013-01-07 08:16:45 -08:00
|
|
|
|
2015-01-12 07:21:33 +11:00
|
|
|
public function shouldExpectCommandErrors() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
|
2013-01-07 08:16:45 -08:00
|
|
|
$dom = new DOMDocument();
|
2014-05-12 04:30:36 -07:00
|
|
|
$ok = @$dom->loadXML($stderr);
|
2013-01-07 08:16:45 -08:00
|
|
|
|
2014-05-12 04:30:36 -07:00
|
|
|
if (!$ok) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-07 08:16:45 -08:00
|
|
|
|
|
|
|
$errors = $dom->getElementsByTagName('error');
|
2014-05-12 04:30:36 -07:00
|
|
|
$messages = array();
|
2013-01-07 08:16:45 -08:00
|
|
|
foreach ($errors as $error) {
|
2014-05-12 04:30:36 -07:00
|
|
|
foreach ($error->getElementsByTagName('location') as $location) {
|
|
|
|
$message = new ArcanistLintMessage();
|
|
|
|
$message->setPath($location->getAttribute('file'));
|
|
|
|
$message->setLine($location->getAttribute('line'));
|
|
|
|
$message->setCode('Cppcheck');
|
|
|
|
$message->setName($error->getAttribute('id'));
|
|
|
|
$message->setDescription($error->getAttribute('msg'));
|
|
|
|
|
|
|
|
switch ($error->getAttribute('severity')) {
|
|
|
|
case 'error':
|
|
|
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if ($error->getAttribute('inconclusive')) {
|
|
|
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
|
|
|
|
} else {
|
|
|
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$messages[] = $message;
|
2013-01-07 08:16:45 -08:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 04:30:36 -07:00
|
|
|
|
|
|
|
return $messages;
|
2013-01-07 08:16:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|