From 251c7bde6dddf28e104edf414cd6d9d39cb54863 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Thu, 8 Jan 2026 20:35:16 -0600 Subject: [PATCH] Readded clang-format --- .clang-format | 29 +++++++++++++++----------- run-clang-format.ps1 | 49 ++++++++++++++++++++++++++++++++++++++++++++ run-clang-format.sh | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 run-clang-format.ps1 create mode 100644 run-clang-format.sh diff --git a/.clang-format b/.clang-format index 03ea4a3a..d4450a21 100644 --- a/.clang-format +++ b/.clang-format @@ -1,18 +1,23 @@ IndentWidth: 4 Language: Cpp -AlignAfterOpenBracket: Align -SortIncludes: false -ColumnLimit: 104 -PointerAlignment: Right -AllowShortFunctionsOnASingleLine: false -AllowShortIfStatementsOnASingleLine: false -BinPackArguments: true -BinPackParameters: true -SpaceAfterCStyleCast: true +UseTab: Never +ColumnLimit: 120 +PointerAlignment: Left BreakBeforeBraces: Attach -BreakBeforeTernaryOperators: true -BreakBeforeBinaryOperators: NonAssignment +SpaceAfterCStyleCast: false Cpp11BracedListStyle: false IndentCaseLabels: true +BinPackArguments: true +BinPackParameters: true +AlignAfterOpenBracket: Align +AlignOperands: true +BreakBeforeTernaryOperators: true +BreakBeforeBinaryOperators: None +AllowShortBlocksOnASingleLine: true +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AlignEscapedNewlines: Left AlignTrailingComments: true -UseTab: Never +SortIncludes: false \ No newline at end of file diff --git a/run-clang-format.ps1 b/run-clang-format.ps1 new file mode 100644 index 00000000..28a20c8d --- /dev/null +++ b/run-clang-format.ps1 @@ -0,0 +1,49 @@ +Using Namespace System +$url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.6/LLVM-14.0.6-win64.exe" +$llvmInstallerPath = ".\LLVM-14.0.6-win64.exe" +$clangFormatFilePath = ".\clang-format.exe" +$requiredVersion = "clang-format version 14.0.6" +$currentVersion = "" + +function Test-7ZipInstalled { + $sevenZipPath = "C:\Program Files\7-Zip\7z.exe" + return Test-Path $sevenZipPath -PathType Leaf +} + +if (Test-Path $clangFormatFilePath) { + $currentVersion = & $clangFormatFilePath --version + if (-not ($currentVersion -eq $requiredVersion)) { + # Delete the existing file if the version is incorrect + Remove-Item $clangFormatFilePath -Force + } +} + +if (-not (Test-Path $clangFormatFilePath) -or ($currentVersion -ne $requiredVersion)) { + if (-not (Test-7ZipInstalled)) { + Write-Host "7-Zip is not installed. Please install 7-Zip and run the script again." + exit + } + + $wc = New-Object net.webclient + $wc.Downloadfile($url, $PSScriptRoot + $llvmInstallerPath) + + $sevenZipPath = "C:\Program Files\7-Zip\7z.exe" + $specificFileInArchive = "bin\clang-format.exe" + & "$sevenZipPath" e $llvmInstallerPath $specificFileInArchive + + Remove-Item $llvmInstallerPath -Force +} + +$basePath = (Resolve-Path .).Path +$files = Get-ChildItem -Path $basePath -Recurse -File ` + | Where-Object { ($_.Extension -eq '.c' -or $_.Extension -eq '.cpp' -or ` + (($_.Extension -eq '.h' -or $_.Extension -eq '.hpp') -and ` + (-not ($_.FullName -like "*\src/port\*" -or $_.FullName -like "*\include\*")))) -and ` + (-not ($_.FullName -like "*\assets\*" -or $_.FullName -like "*\build\*")) } + +for ($i = 0; $i -lt $files.Length; $i++) { + $file = $files[$i] + $relativePath = $file.FullName.Substring($basePath.Length + 1) + Write-Host "Formatting [$($i+1)/$($files.Length)] $relativePath" + .\clang-format.exe -i $file.FullName +} diff --git a/run-clang-format.sh b/run-clang-format.sh new file mode 100644 index 00000000..32351e37 --- /dev/null +++ b/run-clang-format.sh @@ -0,0 +1,36 @@ +# this line does quite a bit, so let's break it down +# +# find soh +# use "find" to look in the "soh" directory +# this ensures we don't try to format stuff in the submodules +# +# -type f +# only look for files +# +# -name "*.c" -o -name "*.cpp" +# find all .c and .cpp files +# +# ( -name "*.h" -o -name "*.hpp" ) ! -path "soh/src/**.h" ! -path "soh/include/**.h" +# find all .h and .hpp files that aren't in soh/src or soh/include +# this is because zret decomp only runs clang-format on c files +# https://github.com/zeldaret/mm/blob/b7e5468ca16315a7e322055eff3d97fe980bbc25/format.py#L182 +# +# ! -path "soh/assets/*" +# asset headers are autogenerated, don't fight them +# +# -print0 +# separate paths with NUL bytes, avoiding issues with spaces in paths +# +# | xargs -0 clang-format-14 -i -verbose +# use xargs to take each path we've found +# and pass it as an argument to clang-format +# verbose to print files being formatted and X out of Y status + +# Autodetect the command +if command -v clang-format-14 &> /dev/null; then + CLANG_FORMAT="clang-format-14" +else + CLANG_FORMAT="clang-format" +fi + +find src -type f \( -name "*.c" -o -name "*.cpp" -o \( \( -name "*.h" -o -name "*.hpp" \) ! -path "src/port/*" ! -path "include/*" \) \) ! -path "assets/*" -print0 | xargs -0 $CLANG_FORMAT -i --verbose