summaryrefslogtreecommitdiffstats
path: root/extras
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 13:59:28 +0800
committerGitHub <noreply@github.com>2024-10-29 13:59:28 +0800
commita729c15e9dce9f5116a38afc66329ab2ca4cea54 (patch)
tree9580072eb0d796b51c04ad5be95230a747709150 /extras
parenta15d770242f88aa4b33cd7d3a97de9c8d86a2315 (diff)
preparation for clang format (#5422)
* Clang-format excludes * Add .clang-format * Don't clang-format in external * Missing includes and forward declarations * Replace wonky include-once macro name * neaten include naming * Add clang-format to formatting script * Add xargs and diff to required binaries * add clang-format to ci formatting check * Add max version check to formatting script * temporarily disable checking formatting for cpp files
Diffstat (limited to 'extras')
-rw-r--r--extras/.clang-format48
-rwxr-xr-xextras/formatting.sh50
2 files changed, 45 insertions, 53 deletions
diff --git a/extras/.clang-format b/extras/.clang-format
deleted file mode 100644
index 81cacff35..000000000
--- a/extras/.clang-format
+++ /dev/null
@@ -1,48 +0,0 @@
----
-BasedOnStyle: LLVM
-IndentWidth: 4
----
-Language: Cpp
-# Force pointers to the type for C++.
-IndentPPDirectives: AfterHash
-DerivePointerAlignment: false
-PointerAlignment: Left
-BinPackArguments: false
-BinPackParameters: false
-ExperimentalAutoDetectBinPacking: false
-AllowAllParametersOfDeclarationOnNextLine: true
-NamespaceIndentation: None
-FixNamespaceComments: true
-AccessModifierOffset: -4
-AlignTrailingComments: false
-ConstructorInitializerIndentWidth: 4
-AlignEscapedNewlinesLeft: true
-PenaltyReturnTypeOnItsOwnLine: 100
-ColumnLimit: 100
-IncludeBlocks: Preserve
-AlignAfterOpenBracket: AlwaysBreak
-IndentCaseBlocks: true
-SortIncludes: true
-SortUsingDeclarations: true
-UseTab: Never
-BreakConstructorInitializers: BeforeComma
-BreakInheritanceList: BeforeComma
-BreakBeforeBraces: Custom
-BraceWrapping:
- AfterCaseLabel: true
- AfterClass: true
- AfterEnum: true
- AfterStruct: true
- AfterUnion: true
- SplitEmptyFunction: false
- AfterControlStatement: Always
- AfterFunction: true
- AfterNamespace: true
- AfterExternBlock: true
- BeforeCatch: true
- BeforeElse: true
- # BeforeLambdaBody: true
- # BeforeWhile: false
- IndentBraces: false
- SplitEmptyFunction: false
- SplitEmptyRecord: false \ No newline at end of file
diff --git a/extras/formatting.sh b/extras/formatting.sh
index a53930eaf..58d7d3509 100755
--- a/extras/formatting.sh
+++ b/extras/formatting.sh
@@ -40,7 +40,8 @@ cd "$source_dir" || exit 1
require_bin() {
local name="$1"
- local required="$2"
+ local min_version="$2"
+ local max_version="${3:-}"
local version
if ! command -v "$name" &>/dev/null; then
@@ -50,16 +51,29 @@ require_bin() {
fi
if [ "$no_version_check" -eq 0 ]; then
- version=$("$name" --version | grep -oP "$name(?:\s+version)?\s+\K\d+\.\d+\.?\d*")
- if ! printf '%s\n%s\n' "$required" "$version" | sort -V -C; then
- echo "$name version $version is too old. Version $required or newer is required."
+ version=$("$name" --version | grep -oP "\d+\.\d+\.?\d*" | head -n1)
+
+ if ! printf '%s\n%s\n' "$min_version" "$version" | sort -V -C; then
+ echo "$name version $version is too old. Version $min_version or newer is required."
missing_bin=1
+ return
+ fi
+
+ if [ -n "$max_version" ]; then
+ if ! printf '%s\n%s\n' "$version" "$max_version" | sort -V -C; then
+ echo "$name version $version is too new. Version less than $max_version is required."
+ missing_bin=1
+ return
+ fi
fi
fi
}
require_bin "git" "1.8"
require_bin "gersemi" "0.17"
+require_bin "xargs" "3"
+require_bin "diff" "2"
+require_bin "clang-format" "17" "18"
if [ "$missing_bin" ]; then
exit 1
@@ -84,7 +98,33 @@ cmake_formatting() {
fi
}
+cpp_formatting() {
+ readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h')
+
+ if [ "$check_only" -eq 1 ]; then
+ local tmpdir
+ tmpdir=$(mktemp -d)
+ trap 'rm -rf "$tmpdir"' EXIT
+
+ printf '%s\n' "${files[@]}" | xargs -P "$(nproc)" -I{} bash -c "
+ mkdir -p \"\$(dirname \"$tmpdir/{}\")\"
+ diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
+ :
+ "
+
+ for file in "${files[@]}"; do
+ if [ -s "$tmpdir/$file" ]; then
+ cat "$tmpdir/$file"
+ exit_code=1
+ fi
+ done
+ else
+ printf '%s\n' "${files[@]}" | xargs -n1 -P "$(nproc)" clang-format -i
+ fi
+}
+
cmake_formatting
+# Disable until we've formatted the code
+# cpp_formatting
exit $exit_code
-