arcanist-clang-format-linter-0.git20161021/000077500000000000000000000000001300312513000202455ustar00rootroot00000000000000arcanist-clang-format-linter-0.git20161021/.arclint000066400000000000000000000005461300312513000217070ustar00rootroot00000000000000{ "linters": { "text": { "type": "text" }, "merges": { "type": "merge-conflict" }, "phutil-library": { "type": "phutil-library", "include": "(\\.php$)" }, "xhpast": { "type": "xhpast", "include": "(\\.php$)" } } } arcanist-clang-format-linter-0.git20161021/.gitignore000066400000000000000000000000251300312513000222320ustar00rootroot00000000000000.phutil_module_cache arcanist-clang-format-linter-0.git20161021/LICENSE000066400000000000000000000027231300312513000212560ustar00rootroot00000000000000Copyright (c) 2015, Valerii Hiora All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of clang-format-linter nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. arcanist-clang-format-linter-0.git20161021/README.md000066400000000000000000000045571300312513000215370ustar00rootroot00000000000000# clang-format-linter Tired of spending half time of code review on marking code style issues? Here we go - this allows to use `clang-format` as a linter for [arcanist](https://phacility.com/phabricator/arcanist) to enforce style over your C/C++/Objective-C codebase. It's not invasive (yet) and at the moment just suggests to autofix code. ## Installation What you want to get is to make this module to be available for `arcanist`. There are a couple of ways to achieve it depending on your requirements. ### Prerequisites Right now `clang-format` should be installed beforehand. On OS X you can do it through [homebrew](https://brew.sh) `brew install clang-format`. You also have to configure your style in `.clang-format` ([documentation](http://clang.llvm.org/docs/ClangFormatStyleOptions.html)) Best way is to start from a predefined style by dumping an existing style `clang-format -style=LLVM -dump-config > .clang-format` and tune parameters. There is also a wonderful [interactive builder](http://clangformat.com/) available. ### Project-specific installation You can add this repository as a git submodule and in this case `.arcconfig` should look like: ```json { "load": "path/to/submodule" // ... } ``` ### Global installation `arcanist` can load modules from an absolute path. But there is one more trick - it also searches for modules in a directory up one level from itself. It means that you can clone this repository to the same directory where `arcanist` and `libphutil` are located. In the end it should look like this: ```sh > ls arcanist clang-format-linter libphutil ``` In this case you `.arcconfig` should look like ```json { "load": "clang-format-linter" // ... } ``` Another approach is to clone `clang-format-linter` to a fixed location and use absolute path like: ```sh cd ~/.dev-tools git clone https://github.com/vhbit/clang-format-linter ``` ```json { "load": "~/.dev-tools/clang-format-linter" // ... } ``` Both ways of global installation are actually almost equally as in most cases you'd like to have a bootstrapping script for all tools. ## Setup Once installed, linter can be used and configured just as any other `arcanist linter` Here is a simplest `.arclint`: ```json { "linters": { "clang-format": { "type": "clang-format", "include": "(^Source/.*\\.(m|h|mm)$)" }, // ... } } ``` arcanist-clang-format-linter-0.git20161021/__phutil_library_init__.php000066400000000000000000000001041300312513000256210ustar00rootroot00000000000000 2, 'class' => array( 'FnClangFormatLinter' => 'lint/linter/FnClangFormatLinter.php', 'FnCpplintLinter' => 'lint/linter/FnCpplintLinter.php', 'FnGlslangLinter' => 'lint/linter/FnGlslangLinter.php', 'FnSequenceMatcher' => 'utils/FnSequenceMatcher.php', 'FnYAPFLinter' => 'lint/linter/FnYAPFLinter.php', ), 'function' => array(), 'xmap' => array( 'FnClangFormatLinter' => 'ArcanistExternalLinter', 'FnCpplintLinter' => 'ArcanistExternalLinter', 'FnGlslangLinter' => 'ArcanistExternalLinter', 'FnSequenceMatcher' => 'Phobject', 'FnYAPFLinter' => 'ArcanistExternalLinter', ), )); arcanist-clang-format-linter-0.git20161021/lint/000077500000000000000000000000001300312513000212135ustar00rootroot00000000000000arcanist-clang-format-linter-0.git20161021/lint/linter/000077500000000000000000000000001300312513000225105ustar00rootroot00000000000000arcanist-clang-format-linter-0.git20161021/lint/linter/FnClangFormatLinter.php000066400000000000000000000112001300312513000270520ustar00rootroot00000000000000getExecutableCommand()); $matches = array(); $regex = '/^clang-format version (?P\S+)/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { return pht('See http://llvm.org/releases/download.html'); } public function getUpgradeInstructions() { return pht('See http://llvm.org/releases/download.html. If you have the ' . 'right version installed, but it is not symlinked to ' . '$PATH/%s, you can override the path by setting ' . 'lint.%s.bin in ~/.arcrc.', $this->getDefaultBinary(), $this->getLinterConfigurationName()); } public function shouldExpectCommandErrors() { return false; } protected function getMandatoryFlags() { $options = array(); $options[] = sprintf('--style=%s', coalesce($this->style, 'file')); return $options; } public function getLinterConfigurationOptions() { $options = array( 'clang-format.style' => array( 'type' => 'optional string', 'help' => pht( 'Either "file" (to use a .clang-format file in a parent directory '. 'of the file being checked), a clang-format predefined style, or a '. 'JSON dictionary of style options. See the docs.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'clang-format.style': $this->style = $value; return $this; } return parent::setLinterConfigurationValue($key, $value); } protected function getPathArgumentForLinterFuture($path) { $full_path = Filesystem::resolvePath($path); $ret = array($full_path); // The |path| we get fed needs to be made relative to the project_root, // otherwise the |engine| won't recognise it. $relative_path = Filesystem::readablePath( $full_path, $this->getProjectRoot()); $changed = $this->getEngine()->getPathChangedLines($relative_path); if ($changed !== null && count(array_filter($changed)) > 0) { // Convert the ordered set of changed lines to a list of ranges. $changed_lines = array_keys(array_filter($changed)); $ranges = array( array($changed_lines[0], $changed_lines[0]), ); foreach (array_slice($changed_lines, 1) as $line) { $range = last($ranges); if ($range[1] + 1 === $line) { ++$range[1]; $ranges[last_key($ranges)] = $range; } else { $ranges[] = array($line, $line); } } foreach ($ranges as $range) { $ret[] = sprintf('-lines=%d:%d', $range[0], $range[1]); } } return csprintf('%Ls', $ret); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { /* clang-format only returns a non-zero exit code on bad arguments. */ if ($err != 0) return false; $old_lines = phutil_split_lines($this->getData($path)); $new_lines = phutil_split_lines($stdout); $op_codes = id(new FnSequenceMatcher($old_lines, $new_lines))->getOpCodes(); $messages = array(); foreach ($op_codes as $op_code) { list($_op, $i1, $i2, $j1, $j2) = $op_code; $messages[] = id(new ArcanistLintMessage()) ->setBypassChangedLineFiltering(true) ->setPath($path) ->setCode($this->getLinterName()) ->setSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX) ->setName('Formatting suggestion') ->setDescription(pht('%s suggests an alternative formatting.', $this->getInfoName())) ->setLine($i1 + 1) ->setChar(1) ->setOriginalText( implode('', array_slice($old_lines, $i1, $i2 - $i1))) ->setReplacementText( implode('', array_slice($new_lines, $j1, $j2 - $j1))); } return $messages; } }arcanist-clang-format-linter-0.git20161021/lint/linter/FnCpplintLinter.php000066400000000000000000000040721300312513000262770ustar00rootroot00000000000000 $match) { $matches[$key] = trim($match); } $severity = $this->getLintMessageSeverity($matches[3]); $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[1]); $message->setCode($matches[3]); $message->setName($matches[3]); $message->setDescription($matches[2]); $message->setSeverity($severity); $messages[] = $message; } return $messages; } protected function getLintCodeFromLinterConfigurationKey($code) { if (!preg_match('@^[a-z_]+/[a-z_]+$@', $code)) { throw new Exception( pht( 'Unrecognized lint message code "%s". Expected a valid cpplint '. 'lint code like "%s" or "%s".', $code, 'build/include_order', 'whitespace/braces')); } return $code; } } arcanist-clang-format-linter-0.git20161021/lint/linter/FnGlslangLinter.php000066400000000000000000000030431300312513000262520ustar00rootroot00000000000000setPath($path) ->setLine($matches[2]) ->setCode($this->getLinterName()) ->setDescription($matches[3]); if ($matches[1] === 'ERROR') { $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR) ->setName('Syntax error'); } else { $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING) ->setName('Glslang Warning'); } $messages[] = $message; } return $messages; } } arcanist-clang-format-linter-0.git20161021/lint/linter/FnYAPFLinter.php000066400000000000000000000111451300312513000254240ustar00rootroot00000000000000getExecutableCommand()); $matches = array(); $regex = '/^yapf (?P\S+)/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { return pht('pip install yapf'); } protected function getMandatoryFlags() { $options = array(); $options[] = sprintf('--style=%s', coalesce($this->style, 'pep8')); return $options; } public function getLinterConfigurationOptions() { $options = array( 'yapf.style' => array( 'type' => 'optional string', 'help' => pht( 'specify formatting style: either a style name (for example "pep8" '. 'or "google"), or the name of a file with style settings. The '. 'default is pep8 unless a .style.yapf or setup.cfg file located in '. 'one of the parent directories of the source file'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'yapf.style': $this->style = $value; return $this; } return parent::setLinterConfigurationValue($key, $value); } protected function getPathArgumentForLinterFuture($path) { $full_path = Filesystem::resolvePath($path); $ret = array($full_path); // The |path| we get fed needs to be made relative to the project_root, // otherwise the |engine| won't recognise it. $relative_path = Filesystem::readablePath( $full_path, $this->getProjectRoot()); $changed = $this->getEngine()->getPathChangedLines($relative_path); if ($changed !== null) { // Convert the ordered set of changed lines to a list of ranges. $changed_lines = array_keys(array_filter($changed)); $ranges = array( array($changed_lines[0], $changed_lines[0]), ); foreach (array_slice($changed_lines, 1) as $line) { $range = last($ranges); if ($range[1] + 1 === $line) { ++$range[1]; $ranges[last_key($ranges)] = $range; } else { $ranges[] = array($line, $line); } } foreach ($ranges as $range) { $ret[] = sprintf('--lines=%d-%d', $range[0], $range[1]); } } return csprintf('%Ls', $ret); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { if ($err !== 2) { return array(); } $old_lines = phutil_split_lines($this->getData($path)); $new_lines = phutil_split_lines($stdout); $op_codes = id(new FnSequenceMatcher($old_lines, $new_lines))->getOpCodes(); $messages = array(); foreach ($op_codes as $op_code) { list($op, $i1, $i2, $j1, $j2) = $op_code; $li = $i2 - $i1; $lj = $j2 - $j1; $message = id(new ArcanistLintMessage()) ->setPath($path) ->setCode($this->getLinterName()) ->setSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX) ->setName('Formatting suggestion') ->setDescription(pht('%s suggestes an alternative formatting.', $this->getInfoName())) ->setLine($i1 + 1) ->setChar(1) ->setOriginalText( implode('', array_slice($old_lines, $i1, $li))) ->setReplacementText( implode('', array_slice($new_lines, $j1, $lj))); // To work around an issue in YAPF where whitespace reformatting occurrs // outside the requested lines, try to determine if this hunk only // affects leading or trailing whitespace. If so, then we allow arcanist // to ignore it. $is_whitespace_only = true; for ($k = 0; $is_whitespace_only && $k < max($li, $lj); ++$k) { $line_a = $k < $li ? $old_lines[$i1 + $k] : ''; $line_b = $k < $lj ? $new_lines[$j1 + $k] : ''; $is_whitespace_only = trim($line_a) === trim($line_b); } $message->setBypassChangedLineFiltering(!$is_whitespace_only); $messages[] = $message; } return $messages; } } arcanist-clang-format-linter-0.git20161021/utils/000077500000000000000000000000001300312513000214055ustar00rootroot00000000000000arcanist-clang-format-linter-0.git20161021/utils/FnSequenceMatcher.php000066400000000000000000000053751300312513000254700ustar00rootroot00000000000000a = $a; $this->b = $b; } public function getOpCodes() { $op_codes = array(); $file_a = new TempFile(); $file_b = new TempFile(); Filesystem::writeFile($file_a, implode('', $this->a)); Filesystem::writeFile($file_b, implode('', $this->b)); $diff_future = new ExecFuture('diff -U0 %s %s', $file_a, $file_b); list($err, $stdout, $stderr) = $diff_future->resolve(); if (!in_array($err, array(0, 1))) { throw new CommandException( pht('`diff` returned unexpected exit code %d', $err), $diff_future->getCommand(), $err, $stdout, $stderr); } foreach (phutil_split_lines($stdout) as $line) { $matches = null; $regexp = '/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/'; if (!preg_match($regexp, $line, $matches)) { continue; } // We need to: // - normalize indices; the ones we get from `diff` are 1-indexed. // - account for how `diff` represents empty hunks -- essentially, it // attributes the change to the *previous line* (which may be 0!). This // differs from Python's difflib. // // http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html: // If a hunk contains just one line, only its start line number // appears. Otherwise its line numbers look like 'start,count'. An // empty hunk is considered to start at the line that follows the // hunk. // // If a hunk and its context contain two or more lines, its line // numbers look like 'start,count'. Otherwise only its end line // number appears. An empty hunk is considered to end at the line // that precedes the hunk. $i = (int)$matches[1] - 1; $j = (int)$matches[3] - 1; if (isset($matches[2]) && $matches[2] !== '') { $li = (int)$matches[2]; } else { $li = 1; } if (isset($matches[4]) && $matches[4] !== '') { $lj = (int)$matches[4]; } else { $lj = 1; } if (!($li || $lj)) { throw new CommandException( pht('Malformed output from `diff`.'), $diff_future->getCommand(), $err, $stdout, $stderr); } if ($li === 0) { $op_codes[] = array('insert', $i + 1, $i + 1, $j, $j + $lj); } else if ($lj === 0) { $op_codes[] = array('delete', $i, $i + $li, $j, $j); } else { $op_codes[] = array('replace', $i, $i + $li, $j, $j + $lj); } } return $op_codes; } }