~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to src/lint/linter/ArcanistCppcheckLinter.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-11-01 23:20:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20141101232006-mvlnp0cil67tsboe
Tags: upstream-0~git20141101/arcanist
Import upstream version 0~git20141101, component arcanist

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Uses Cppcheck to do basic checks in a C++ file.
 
5
 */
 
6
final class ArcanistCppcheckLinter extends ArcanistExternalLinter {
 
7
 
 
8
  public function getInfoName() {
 
9
    return 'Cppcheck';
 
10
  }
 
11
 
 
12
  public function getInfoURI() {
 
13
    return 'http://cppcheck.sourceforge.net';
 
14
  }
 
15
 
 
16
  public function getInfoDescription() {
 
17
    return pht('Use `cppcheck` to perform static analysis on C/C++ code.');
 
18
  }
 
19
 
 
20
  public function getLinterName() {
 
21
    return 'cppcheck';
 
22
  }
 
23
 
 
24
  public function getLinterConfigurationName() {
 
25
    return 'cppcheck';
 
26
  }
 
27
 
 
28
  public function getDefaultBinary() {
 
29
    $prefix = $this->getDeprecatedConfiguration('lint.cppcheck.prefix');
 
30
    $bin = $this->getDeprecatedConfiguration('lint.cppcheck.bin', 'cppcheck');
 
31
 
 
32
    if ($prefix) {
 
33
      return $prefix.'/'.$bin;
 
34
    } else {
 
35
      return $bin;
 
36
    }
 
37
  }
 
38
 
 
39
  public function getVersion() {
 
40
    list($stdout) = execx('%C --version', $this->getExecutableCommand());
 
41
 
 
42
    $matches = array();
 
43
    $regex = '/^Cppcheck (?P<version>\d+\.\d+)$/';
 
44
    if (preg_match($regex, $stdout, $matches)) {
 
45
      return $matches['version'];
 
46
    } else {
 
47
      return false;
 
48
    }
 
49
  }
 
50
 
 
51
  public function getInstallInstructions() {
 
52
    return pht('Install Cppcheck using `apt-get install cppcheck` or similar.');
 
53
  }
 
54
 
 
55
  protected function getMandatoryFlags() {
 
56
    return array(
 
57
      '--quiet',
 
58
      '--inline-suppr',
 
59
      '--xml',
 
60
      '--xml-version=2',
 
61
    );
 
62
  }
 
63
 
 
64
  protected function getDefaultFlags() {
 
65
    return $this->getDeprecatedConfiguration(
 
66
      'lint.cppcheck.options',
 
67
      array('-j2', '--enable=performance,style,portability,information'));
 
68
  }
 
69
 
 
70
  protected function parseLinterOutput($path, $err, $stdout, $stderr) {
 
71
    $dom = new DOMDocument();
 
72
    $ok = @$dom->loadXML($stderr);
 
73
 
 
74
    if (!$ok) {
 
75
      return false;
 
76
    }
 
77
 
 
78
    $errors = $dom->getElementsByTagName('error');
 
79
    $messages = array();
 
80
    foreach ($errors as $error) {
 
81
      foreach ($error->getElementsByTagName('location') as $location) {
 
82
        $message = new ArcanistLintMessage();
 
83
        $message->setPath($location->getAttribute('file'));
 
84
        $message->setLine($location->getAttribute('line'));
 
85
        $message->setCode('Cppcheck');
 
86
        $message->setName($error->getAttribute('id'));
 
87
        $message->setDescription($error->getAttribute('msg'));
 
88
 
 
89
        switch ($error->getAttribute('severity')) {
 
90
          case 'error':
 
91
            $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
 
92
            break;
 
93
 
 
94
          default:
 
95
            if ($error->getAttribute('inconclusive')) {
 
96
              $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
 
97
            } else {
 
98
              $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
 
99
            }
 
100
            break;
 
101
        }
 
102
 
 
103
        $messages[] = $message;
 
104
      }
 
105
    }
 
106
 
 
107
    return $messages;
 
108
  }
 
109
 
 
110
}