Difference between revisions of "Add linters to a project"

From Apertis
Jump to: navigation, search
m (Make sure clang-format is recent)
m (Add a .arclint file: Add clang-format version key)
Line 48: Line 48:
     "clang-format-default": {
     "clang-format-default": {
       "type": "clang-format",
       "type": "clang-format",
      "version": "3.7.0",
       "include": "(^(src|tests|canterbury)/.*\\.(c|h)$)"
       "include": "(^(src|tests|canterbury)/.*\\.(c|h)$)"

Revision as of 11:04, 26 January 2016

To add Phabricator linters to a project (specifically, clang-format-linter):

Install clang-format-linter locally

This should be done on each developer’s machine:

cd ~/.local/bin
git clone https://github.com/pwithnall/morefas-phabricator.git clang-format-linter.git
cd /path/to/arcanist.git/externals/includes
ln -s ~/.local/bin/clang-format-linter.git clang-format-linter
arc linters  # should list ‘clang-format’ as ‘configured’

We are using a custom clone of the original clang-format-linter which adds support for splitting the diff into chunks and adds some more help.

Make sure clang-format is recent

Again, this should be done on each developer’s machine.

Install clang-format 3.7 or later, and make sure that running "clang-format" results in running that version. For instance, in Debian unstable as of January 2016 it's called clang-format-3.7, so you might add ~/bin to your PATH and make a symbolic link:

ln -s /usr/bin/clang-format-3.7 ~/bin/clang-format

Update .arcconfig

Update the .arcconfig file for the project (in git) to add the linter configuration and load the clang-format-linter arcanist extension:

"lint.engine": "ArcanistConfigurationDrivenLintEngine",
"load": [

Add a .clang-format file

clang-format accepts its format options from a hidden file, defined in YAML format. Create .clang-format and commit it to git:

# See https://wiki.apertis.org/Guidelines/Coding_conventions#Code_formatting
BasedOnStyle: GNU
AlwaysBreakAfterDefinitionReturnType: All
BreakBeforeBinaryOperators: None
BinPackParameters: false
SpaceAfterCStyleCast: true

These are the current recommended formatting options from the coding conventions, but may change in future as clang-format evolves or corner cases in the configuration are found and fixed.

Add a .arclint file

To enable the linters to be used with arc lint (and hence with arc diff and git-phab attach), add a .arclint file to the project and commit it to git. This example file contains XML, Python and C linters. Note that you should adjust the include path for the clang-format-default linter to include the source directories in your project, while excluding any generated C or H files.

  "linters": {
    "clang-format-default": {
      "type": "clang-format",
      "version": "3.7.0",
      "include": "(^(src|tests|canterbury)/.*\\.(c|h)$)"
    "pep8-default": {
      "type": "pep8",
      "flags": ["--ignore=E402"],
      "include": "(\\.py$)"
    "xml-default": {
      "type": "xml",
      "include": "(\\.xml$)"

We ignore PEP8 error E402 because the recommended copyright headers, editor modelines and encoding lines push the first import statement below the recommended line number according to PEP8.