From 030247be18c84118d79725433b2dcf1e3b064d1a Mon Sep 17 00:00:00 2001 From: newell Date: Tue, 8 Oct 2024 15:19:07 +0800 Subject: [PATCH] add pre-commit hooks for code formatting Co-authored-by: newell Co-committed-by: newell --- README.md | 10 ++++++++++ flake.nix | 1 + src/.clang-format | 32 ++++++++++++++++++++++++++++++++ src/.clippy.toml | 1 + src/.pre-commit-config.yaml | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 src/.clang-format create mode 100644 src/.clippy.toml create mode 100644 src/.pre-commit-config.yaml diff --git a/README.md b/README.md index f270c31..55807e4 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,16 @@ Notes: - Firmware type must be either ``runtime`` for DRTIO-less or DRTIO master variants, or ``satman`` for DRTIO satellite. - If the board is connected to the local machine, use the ``local_run.sh`` script. +Pre-Commit Hooks +---------------- + +You are strongly recommended to use the provided pre-commit hooks to automatically reformat files and check for non-optimal Rust/C/C++ practices. Run `pre-commit install` to install the hook and `pre-commit` will automatically run `cargo fmt`, `cargo clippy`, and `clang-format` for you. + +Several things to note: + +- If `cargo fmt`, `cargo clippy`, or `clang-format` returns an error, the pre-commit hook will fail. You should fix all errors before trying to commit again. +- If `cargo fmt` or `clang-format` reformats some files, the pre-commit hook will also fail. You should review the changes and, if satisfied, try to commit again. + License ------- diff --git a/flake.nix b/flake.nix index 28e164f..100f90d 100644 --- a/flake.nix +++ b/flake.nix @@ -385,6 +385,7 @@ artiqpkgs.artiq artiqpkgs.vivado binutils-arm + pre-commit ]; XARGO_RUST_SRC = "${rust}/lib/rustlib/src/rust/library"; CLANG_EXTRA_INCLUDE_DIR = "${llvmPackages_11.clang-unwrapped.lib}/lib/clang/11.1.0/include"; diff --git a/src/.clang-format b/src/.clang-format new file mode 100644 index 0000000..9fc7f42 --- /dev/null +++ b/src/.clang-format @@ -0,0 +1,32 @@ +BasedOnStyle: LLVM + +Language: Cpp +Standard: Cpp11 + +AccessModifierOffset: -1 +AlignEscapedNewlines: Left +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortFunctionsOnASingleLine: Inline +BinPackParameters: false +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: AfterColon +BreakInheritanceList: AfterColon +ColumnLimit: 120 +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ContinuationIndentWidth: 4 +DerivePointerAlignment: false +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +MaxEmptyLinesToKeep: 1 +PointerAlignment: Left +ReflowComments: true +SortIncludes: false +SortUsingDeclarations: true +SpaceAfterTemplateKeyword: false +SpacesBeforeTrailingComments: 2 +TabWidth: 4 +UseTab: Never diff --git a/src/.clippy.toml b/src/.clippy.toml new file mode 100644 index 0000000..434d402 --- /dev/null +++ b/src/.clippy.toml @@ -0,0 +1 @@ +doc-valid-idents = ["CPython", "NumPy", ".."] \ No newline at end of file diff --git a/src/.pre-commit-config.yaml b/src/.pre-commit-config.yaml new file mode 100644 index 0000000..abe1608 --- /dev/null +++ b/src/.pre-commit-config.yaml @@ -0,0 +1,32 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks + +default_stages: [commit] + +repos: + - repo: local + hooks: + - id: cargo-fmt + name: artiq-zynq cargo format + entry: nix + language: system + types: [file, rust] + pass_filenames: false + description: Runs cargo fmt on the codebase. + args: [develop, -c, cargo, fmt, --manifest-path, src/Cargo.toml, --all] + - id: cargo-clippy + name: artiq-zynq cargo clippy + entry: nix + language: system + types: [file, rust] + pass_filenames: false + description: Runs cargo clippy on the codebase. + args: [develop, -c, cargo, clippy, --manifest-path, src/Cargo.toml, --tests] + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v19.1.0 + hooks: + - id: clang-format + name: artiq-zynq clang-format + description: Runs clang-format on the codebase. + files: \.(cpp|h|hpp|c)$ + args: [-style=file, -fallback-style=none, -assume-filename=src/.clang-format]