Commit graph

21 commits

Author SHA1 Message Date
Laurent Pinchart
5849dd0b17 utils: checkstyle: Add formatter to sort #include statements
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-03-23 13:51:00 +02:00
Kieran Bingham
d15f979ead utils: checkstyle: Add a ShellChecker
Hook the utility 'shellcheck' into our checkstyle helper to
automatically verify shell script additions.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-03-16 15:21:51 +00:00
Nicolas Dufresne
17b3c79409 checkstyle: Add support for checking style on amendments
This introduces a new argument "--amend" and a new special type of
commit "Amendment". It will check the style of changes that are in
the index combined with the changes of the last commit. So this is
the changes that would be applied by "git commit --amend" hence the
name of the argument.

This is needed to implement pre-commit hook.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-01-18 22:40:55 +02:00
Nicolas Dufresne
8bf8442565 checkstyle: Add support for checking style on staged changes
This introduces a new command line "--staged" and a new special type of
commit "StagedChanges". It will check the style of changes that are in
the index, so the changes that would be committed by "git commit".

"--staged" was chosen to match with "git diff --staged" command line.
Other valid name could have been "--index" or "--cached". This was
my personal preference, aliases can be added later. Note that we must
not confuse this with working tree changes, as these changes are not
picked by "git commit".

This feature is needed to implement pre-commit hook.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-01-18 22:05:03 +02:00
Nicolas Dufresne
fef2f02324 checkstyle: Introduce a Commit class
This introduces a Commit class used in the final revlist list. All the
git commands are moved into that class. This class will be used to
introduce new types of commit (StagedChanges and Amendment) needed to implement
pre-commit hook support.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-01-18 22:05:03 +02:00
Nicolas Dufresne
0b86152a57 checkstyle: Exit with 1 status if issues are found
Makes the tool return 1 if there is any potential issues. This is
needed when using this tool for pre-commit hook in order to abort
the commit process.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-01-18 22:05:03 +02:00
Nicolas Dufresne
3e0c32f992 checkstyle: Move from pep8 to pycodestyle
The tool has been renamed in 2016 to make it more obvious what it
is doing. There is no other changes needed on our side.

See https://github.com/PyCQA/pycodestyle/issues/466

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-01-18 22:05:03 +02:00
Kieran Bingham
300654217e utils: checkstyle.py: Support single line hunks
The checkstyle script expects hunks to be declared with a start line and
line count, however the unified diff format [0] declares that a single
line hunk will only have the start line:

> 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.

[0] https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html#Detailed-Unified

Attempting to parse a single line hunk results in the following error:

  File "./utils/checkstyle.py", line 110, in __init__
    raise RuntimeError("Malformed diff hunk header '%s'" % line)
  RuntimeError: Malformed diff hunk header '@@ -1 +1,2 @@

The DiffHunk class only makes use of the start line, and does not
utilise the line count, thus update the regex to make the unused
groups optional.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-01-07 16:37:28 +00:00
Kieran Bingham
32dd1a3b2f utils: checkstyle.py: Fix regex string literal
The checkstyle.py patch has a fault which it identified in itself when
updating the regex string.

  --- utils/checkstyle.py
  +++ utils/checkstyle.py
  #105: : W605 invalid escape sequence '\+'
  +    diff_header_regex = re.compile('@@ -([0-9]+)(,[0-9]+)? \+([0-9]+)(,?[0-9]+)? @@')
  ---
  1 potential style issue detected, please review

This is documented further at:
  https://www.flake8rules.com/rules/W605.html

Update the string literal prefix to declare a raw byte string for the
regex parser.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-01-07 16:05:27 +00:00
Laurent Pinchart
bb5f8cf495 utils: checkstyle.py: Add include checker
Add an include checker to verify usage of the C compatibility headers.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2019-10-23 17:12:34 +03:00
Kieran Bingham
021af795c2 utils: checkstyle.py: Add pep8 checker
Process python additions with pep8 and report any errors that are added.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2019-07-04 21:49:34 +01:00
Laurent Pinchart
91a65e9ee6 utils: checkstyle.py: Add Doxygen formatter
Add a formatter for doxygen comments. In its initial implementation the
formatter ensures that the first word of a \return statement starts with
an uppercase letter.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2019-07-04 14:58:11 +03:00
Laurent Pinchart
6f0c4d0742 utils: checkstyle.py: Add meson.build checker
Add a meson.build checker that warns when tabs are used.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-07-01 02:24:21 +03:00
Laurent Pinchart
bc6b758c71 utils: checkstyle.py: Refactor formatters and checkers support
Introduce two new base classes for the code formatters and style
checkers, with an auto-registration mechanism that automatically uses
all derived classes. This will allow easier addition of new formatters
and checkers.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-07-01 02:24:21 +03:00
Laurent Pinchart
5fb0ea016c utils: checkstyle: Catch LOG() usage without an explicit category
Add support for checkers not related to code formatting to the
checkstyle.py script, and create a first checker that catches usage of
the LOG() macro without an explicit category.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2019-02-13 13:23:34 +02:00
Laurent Pinchart
1369c0b7c3 utils: checkstyle: Add support for clang-format
clang-format produces better results than astyle as it can better match
the libcamera coding style. Default to clang-format over astyle, fall
back to astyle if clang-format isn't found, and add a --formatter
command line option to select a formatter manually.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2019-01-22 14:30:00 +02:00
Kieran Bingham
df7bd278a6 utils: checkstyle: add keep-one-line-blocks
Enable --keep-one-line-blocks to prevent astyle from wanting to move
single inlined blocks to cover 4 lines such as:

-       virtual int init() { return 0; }
+       virtual int init()
+       {
+               return 0;
+       }

Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2018-12-21 15:18:32 +00:00
Laurent Pinchart
3c8886d698 utils: checkstyle.py: Strip trailing white spaces
As astyle doesn't strip trailing white spaces, strip them manually.
Organize the code to allow for new additional formatting steps if
needed.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2018-12-19 14:11:16 +02:00
Laurent Pinchart
cf04a496a6 utils: checkstyle.py: Highlight trailing white space at end of line
In order to facilitate interpretation of diffs, highlight trailing white
space at end of lines with a red background.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2018-12-19 14:11:16 +02:00
Laurent Pinchart
2887934ca0 utils: checkstyle.py: Support execution from non-root directories
The git diff command is invoked with relative paths, which causes git to
fail to locate files when the checkstyle.py script is run from
subdirectories of the git tree. Fix this by prepending the absolute path
to the git tree root directory to the file names.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2018-12-19 14:11:16 +02:00
Laurent Pinchart
8b30bb3185 utils: Add Python-based commit style checker script
checkstyle.py is a reimplementation of checkstyle.sh in Python, that
should be easier to extend with additional features.

Three additional features and enhancements are already implemented:

- While retaining the default behaviour of operating on the HEAD commit,
  a list of commits can also be specified on the command line.

- Correct line numbers are printed in the diff output.

- The index and working tree are not touched, they can be dirty.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2018-12-14 15:34:00 +02:00