mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-26 00:32:41 +01:00
Modernize ArcanistCppcheckLinter
.
Summary: Ref T2039. Make the `ArcanistCppcheckLinter` compatible with `.arclint`. Test Plan: Wrote and executed unit tests. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: epriestley, Korvin Maniphest Tasks: T2039 Differential Revision: https://secure.phabricator.com/D9068
This commit is contained in:
parent
54c377448d
commit
315314425e
7 changed files with 142 additions and 98 deletions
|
@ -45,6 +45,7 @@ phutil_register_library_map(array(
|
||||||
'ArcanistConfigurationManager' => 'configuration/ArcanistConfigurationManager.php',
|
'ArcanistConfigurationManager' => 'configuration/ArcanistConfigurationManager.php',
|
||||||
'ArcanistCoverWorkflow' => 'workflow/ArcanistCoverWorkflow.php',
|
'ArcanistCoverWorkflow' => 'workflow/ArcanistCoverWorkflow.php',
|
||||||
'ArcanistCppcheckLinter' => 'lint/linter/ArcanistCppcheckLinter.php',
|
'ArcanistCppcheckLinter' => 'lint/linter/ArcanistCppcheckLinter.php',
|
||||||
|
'ArcanistCppcheckLinterTestCase' => 'lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php',
|
||||||
'ArcanistCpplintLinter' => 'lint/linter/ArcanistCpplintLinter.php',
|
'ArcanistCpplintLinter' => 'lint/linter/ArcanistCpplintLinter.php',
|
||||||
'ArcanistCpplintLinterTestCase' => 'lint/linter/__tests__/ArcanistCpplintLinterTestCase.php',
|
'ArcanistCpplintLinterTestCase' => 'lint/linter/__tests__/ArcanistCpplintLinterTestCase.php',
|
||||||
'ArcanistDiffChange' => 'parser/diff/ArcanistDiffChange.php',
|
'ArcanistDiffChange' => 'parser/diff/ArcanistDiffChange.php',
|
||||||
|
@ -225,7 +226,8 @@ phutil_register_library_map(array(
|
||||||
'ArcanistConduitLinter' => 'ArcanistLinter',
|
'ArcanistConduitLinter' => 'ArcanistLinter',
|
||||||
'ArcanistConfigurationDrivenLintEngine' => 'ArcanistLintEngine',
|
'ArcanistConfigurationDrivenLintEngine' => 'ArcanistLintEngine',
|
||||||
'ArcanistCoverWorkflow' => 'ArcanistBaseWorkflow',
|
'ArcanistCoverWorkflow' => 'ArcanistBaseWorkflow',
|
||||||
'ArcanistCppcheckLinter' => 'ArcanistLinter',
|
'ArcanistCppcheckLinter' => 'ArcanistExternalLinter',
|
||||||
|
'ArcanistCppcheckLinterTestCase' => 'ArcanistArcanistLinterTestCase',
|
||||||
'ArcanistCpplintLinter' => 'ArcanistExternalLinter',
|
'ArcanistCpplintLinter' => 'ArcanistExternalLinter',
|
||||||
'ArcanistCpplintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
|
'ArcanistCpplintLinterTestCase' => 'ArcanistArcanistLinterTestCase',
|
||||||
'ArcanistDiffParserTestCase' => 'ArcanistTestCase',
|
'ArcanistDiffParserTestCase' => 'ArcanistTestCase',
|
||||||
|
|
|
@ -1,121 +1,110 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses cppcheck to do basic checks in a cpp file
|
* Uses Cppcheck to do basic checks in a C++ file.
|
||||||
*
|
|
||||||
* You can get it here:
|
|
||||||
* http://cppcheck.sourceforge.net/
|
|
||||||
*
|
|
||||||
* @group linter
|
|
||||||
*/
|
*/
|
||||||
final class ArcanistCppcheckLinter extends ArcanistLinter {
|
final class ArcanistCppcheckLinter extends ArcanistExternalLinter {
|
||||||
|
|
||||||
|
public function getInfoName() {
|
||||||
|
return 'Cppcheck';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInfoURI() {
|
||||||
|
return 'http://cppcheck.sourceforge.net';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInfoDescription() {
|
||||||
|
return pht('Use `cppcheck` to perform static analysis on C/C++ code.');
|
||||||
|
}
|
||||||
|
|
||||||
public function getLinterName() {
|
public function getLinterName() {
|
||||||
return 'cppcheck';
|
return 'cppcheck';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLintOptions() {
|
public function getLinterConfigurationName() {
|
||||||
$config = $this->getEngine()->getConfigurationManager();
|
return 'cppcheck';
|
||||||
// You will for sure want some options. The below default tends to be ok
|
}
|
||||||
return $config->getConfigFromAnySource(
|
|
||||||
|
public function getDefaultBinary() {
|
||||||
|
$prefix = $this->getDeprecatedConfiguration('lint.cppcheck.prefix');
|
||||||
|
$bin = $this->getDeprecatedConfiguration('lint.cppcheck.bin', 'cppcheck');
|
||||||
|
|
||||||
|
if ($prefix) {
|
||||||
|
return $prefix.'/'.$bin;
|
||||||
|
} else {
|
||||||
|
return $bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getVersion() {
|
||||||
|
list($stdout) = execx('%C --version', $this->getExecutableCommand());
|
||||||
|
|
||||||
|
$matches = array();
|
||||||
|
$regex = '/^Cppcheck (?P<version>\d+\.\d+)$/';
|
||||||
|
if (preg_match($regex, $stdout, $matches)) {
|
||||||
|
return $matches['version'];
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInstallInstructions() {
|
||||||
|
return pht('Install Cpplint using `apt-get install cpplint` or similar.');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getMandatoryFlags() {
|
||||||
|
return array(
|
||||||
|
'--quiet',
|
||||||
|
'--inline-suppr',
|
||||||
|
'--xml',
|
||||||
|
'--xml-version=2',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getDefaultFlags() {
|
||||||
|
return $this->getDeprecatedConfiguration(
|
||||||
'lint.cppcheck.options',
|
'lint.cppcheck.options',
|
||||||
'-j2 --inconclusive --enable=performance,style,portability,information');
|
array('-j2', '--enable=performance,style,portability,information'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLintPath() {
|
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
|
||||||
$config = $this->getEngine()->getConfigurationManager();
|
|
||||||
$prefix = $config->getConfigFromAnySource('lint.cppcheck.prefix');
|
|
||||||
$bin = $config->getConfigFromAnySource('lint.cppcheck.bin', 'cppcheck');
|
|
||||||
|
|
||||||
if ($prefix !== null) {
|
|
||||||
if (!Filesystem::pathExists($prefix.'/'.$bin)) {
|
|
||||||
throw new ArcanistUsageException(
|
|
||||||
"Unable to find cppcheck binary in a specified directory. Make ".
|
|
||||||
"sure that 'lint.cppcheck.prefix' and 'lint.cppcheck.bin' keys are ".
|
|
||||||
"set correctly. If you'd rather use a copy of cppcheck installed ".
|
|
||||||
"globally, you can just remove these keys from your .arcconfig.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return csprintf("%s/%s", $prefix, $bin);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look for globally installed cppcheck
|
|
||||||
list($err) = exec_manual('which %s', $bin);
|
|
||||||
if ($err) {
|
|
||||||
throw new ArcanistUsageException(
|
|
||||||
"cppcheck does not appear to be installed on this system. Install ".
|
|
||||||
"it (from http://cppcheck.sourceforge.net/) or configure ".
|
|
||||||
"'lint.cppcheck.prefix' in your .arcconfig to point to the ".
|
|
||||||
"directory where it resides."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $bin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function lintPath($path) {
|
|
||||||
$bin = $this->getLintPath();
|
|
||||||
$options = $this->getLintOptions();
|
|
||||||
|
|
||||||
list($rc, $stdout, $stderr) = exec_manual(
|
|
||||||
"%C %C --inline-suppr --xml-version=2 -q %s",
|
|
||||||
$bin,
|
|
||||||
$options,
|
|
||||||
$this->getEngine()->getFilePathOnDisk($path));
|
|
||||||
|
|
||||||
if ($rc === 1) {
|
|
||||||
throw new Exception("cppcheck failed to run correctly:\n".$stderr);
|
|
||||||
}
|
|
||||||
|
|
||||||
$dom = new DOMDocument();
|
$dom = new DOMDocument();
|
||||||
libxml_clear_errors();
|
$ok = @$dom->loadXML($stderr);
|
||||||
if ($dom->loadXML($stderr) === false || libxml_get_errors()) {
|
|
||||||
throw new ArcanistUsageException('cppcheck Linter failed to load ' .
|
|
||||||
'output. Something happened when running cppcheck. ' .
|
|
||||||
"Output:\n$stderr" .
|
|
||||||
"\nTry running lint with --trace flag to get more details.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!$ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$errors = $dom->getElementsByTagName('error');
|
$errors = $dom->getElementsByTagName('error');
|
||||||
|
$messages = array();
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
$loc_node = $error->getElementsByTagName('location');
|
foreach ($error->getElementsByTagName('location') as $location) {
|
||||||
if (!$loc_node) {
|
$message = new ArcanistLintMessage();
|
||||||
continue;
|
$message->setPath($location->getAttribute('file'));
|
||||||
}
|
$message->setLine($location->getAttribute('line'));
|
||||||
$location = $loc_node->item(0);
|
$message->setCode('Cppcheck');
|
||||||
if (!$location) {
|
$message->setName($error->getAttribute('id'));
|
||||||
continue;
|
$message->setDescription($error->getAttribute('msg'));
|
||||||
}
|
|
||||||
$file = $location->getAttribute('file');
|
|
||||||
if ($file != Filesystem::resolvePath($path)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$line = $location->getAttribute('line');
|
|
||||||
|
|
||||||
$id = $error->getAttribute('id');
|
switch ($error->getAttribute('severity')) {
|
||||||
$severity = $error->getAttribute('severity');
|
case 'error':
|
||||||
$msg = $error->getAttribute('msg');
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
|
||||||
$inconclusive = $error->getAttribute('inconclusive');
|
break;
|
||||||
$verbose_msg = $error->getAttribute('verbose');
|
|
||||||
|
|
||||||
$severity_code = ArcanistLintSeverity::SEVERITY_WARNING;
|
default:
|
||||||
if ($inconclusive) {
|
if ($error->getAttribute('inconclusive')) {
|
||||||
$severity_code = ArcanistLintSeverity::SEVERITY_ADVICE;
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
|
||||||
} else if (stripos($severity, 'error') !== false) {
|
} else {
|
||||||
$severity_code = ArcanistLintSeverity::SEVERITY_ERROR;
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$messages[] = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = new ArcanistLintMessage();
|
|
||||||
$message->setPath($path);
|
|
||||||
$message->setLine($line);
|
|
||||||
$message->setCode($severity);
|
|
||||||
$message->setName($id);
|
|
||||||
$message->setDescription($msg);
|
|
||||||
$message->setSeverity($severity_code);
|
|
||||||
|
|
||||||
$this->addLintMessage($message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
12
src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php
Normal file
12
src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class ArcanistCppcheckLinterTestCase
|
||||||
|
extends ArcanistArcanistLinterTestCase {
|
||||||
|
|
||||||
|
public function testCppcheckLint() {
|
||||||
|
return $this->executeTestsInDirectory(
|
||||||
|
dirname(__FILE__).'/cppcheck/',
|
||||||
|
new ArcanistCppcheckLinter());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
src/lint/linter/__tests__/cppcheck/file1.lint-test
Normal file
9
src/lint/linter/__tests__/cppcheck/file1.lint-test
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char a[10];
|
||||||
|
a[10] = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
~~~~~~~~~~
|
||||||
|
error:4:
|
||||||
|
warning:4:
|
|
@ -0,0 +1,7 @@
|
||||||
|
void f() {
|
||||||
|
char arr[5];
|
||||||
|
// cppcheck-suppress arrayIndexOutOfBounds
|
||||||
|
// cppcheck-suppress unreadVariable
|
||||||
|
arr[10] = 0;
|
||||||
|
}
|
||||||
|
~~~~~~~~~~
|
5
src/lint/linter/__tests__/cppcheck/ok.lint-test
Normal file
5
src/lint/linter/__tests__/cppcheck/ok.lint-test
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
~~~~~~~~~~
|
20
src/lint/linter/__tests__/cppcheck/zblair.lint-test
Normal file
20
src/lint/linter/__tests__/cppcheck/zblair.lint-test
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/**
|
||||||
|
* Taken from http://www.slideshare.net/zblair/cppcheck-10316379
|
||||||
|
*/
|
||||||
|
void foo(char* str) {
|
||||||
|
char* buf = new char[8];
|
||||||
|
strcpy(buf, str);
|
||||||
|
|
||||||
|
FILE* file = fopen("out.txt", "w");
|
||||||
|
if (!file)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (char* c = buf; *c; ++c)
|
||||||
|
fputc((int)*c, file);
|
||||||
|
|
||||||
|
delete buf;
|
||||||
|
}
|
||||||
|
~~~~~~~~~~
|
||||||
|
error:10:
|
||||||
|
error:15:
|
||||||
|
error:16:
|
Loading…
Reference in a new issue