diff --git a/.github/linters/.clang-format b/.github/linters/.clang-format new file mode 100644 index 00000000..842e0331 --- /dev/null +++ b/.github/linters/.clang-format @@ -0,0 +1,87 @@ +Language: Cpp +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: false +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignConsecutiveDeclarations: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCaseColons: false +AlignTrailingComments: + Kind: Always + OverEmptyLines: 2 +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Empty +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: Custom +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: true + SplitEmptyFunction: false + SplitEmptyRecord: false +#requires clang-format 22 +#BreakAfterOpenBracketFunction: true +#BreakBeforeCloseBracketFunction: true +BreakBeforeBinaryOperators: All +BreakBeforeTernaryOperators: false +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakTemplateDeclarations: Yes +ColumnLimit: 140 +ConstructorInitializerAllOnOneLineOrOnePerLine: true +Cpp11BracedListStyle: false +FixNamespaceComments: true +IndentCaseLabels: false +IndentExternBlock: Indent +IndentPPDirectives: None +IndentWidth: 4 +InsertNewlineAtEOF: true +LineEnding: CRLF +NamespaceIndentation: All +PackConstructorInitializers: Never +PointerAlignment: Left +ReflowComments: true +SortIncludes: false +SpaceAfterCStyleCast: false +#requires clang format 21 +#SpaceAfterOperatorKeyword: true +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +Standard: c++17 +TabWidth: 4 +UseTab: Never diff --git a/.github/linters/.editorconfig-checker.json b/.github/linters/.editorconfig-checker.json index 078fa78e..5f3ecd6a 100644 --- a/.github/linters/.editorconfig-checker.json +++ b/.github/linters/.editorconfig-checker.json @@ -4,5 +4,8 @@ ".git", "LICENSE", "Tests" - ] + ], + "Disable": { + "IndentSize": true + } } diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ef1f283e..cf9444c2 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -26,18 +26,19 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - name: Lint Code Base - uses: super-linter/super-linter@4ce20838b8ab83717e78138c5b3a1407148e0918 # v8.7.0 + uses: super-linter/super-linter@4ce20838b8ab83717e78138c5b3a1407148e0918 # v8.7.0 env: DEFAULT_BRANCH: origin/main GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} IGNORE_GITIGNORED_FILES: true VALIDATE_ALL_CODEBASE: true VALIDATE_CHECKOV: true + VALIDATE_CLANG_FORMAT: true VALIDATE_EDITORCONFIG: true VALIDATE_GITHUB_ACTIONS: true VALIDATE_JSON: true diff --git a/README.md b/README.md index 4252ea54..2e3d1d13 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,13 @@ FOR SECURITY ADVISORIES, see [GitHub](https://github.com/microsoft/DirectXMath/s For a full change history, see [CHANGELOG.md](https://github.com/microsoft/DirectXMath/blob/main/CHANGELOG.md). +* clang-format use for this project must match the version use by GitHub Super-Linter to pass the validation. VS 2022 and VS 2026 come with different versions, so to match use: + +```cmd +winget install --id=LLVM.LLVM --version 21.1.2 +powershell -File build/Format.ps1 -LLVM +``` + * The *directxmath* NuGet package is deprecated. The best way to integrate the latest DirectXMath into your C++ project is using [vcpkg](https://github.com/microsoft/vcpkg/tree/master/ports/directxmath). * The CMake projects require 3.21 or later. VS 2019 users will need to install a standalone version of CMake 3.21 or later and add it to their PATH. diff --git a/XDSP/XDSP.h b/XDSP/XDSP.h index 4864813f..c9998bd2 100644 --- a/XDSP/XDSP.h +++ b/XDSP/XDSP.h @@ -525,6 +525,7 @@ namespace XDSP const size_t uLength = size_t(1) << (uLog2Length - 2); static const unsigned char cSwizzleTable[256] = { + // clang-format off 0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0, 0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0, 0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4, 0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4, 0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8, 0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8, @@ -541,6 +542,7 @@ namespace XDSP 0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7, 0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7, 0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB, 0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB, 0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF, 0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF + // clang-format on }; if ((uLog2Length & 1) == 0) { diff --git a/build/Format.ps1 b/build/Format.ps1 new file mode 100644 index 00000000..819eb5cd --- /dev/null +++ b/build/Format.ps1 @@ -0,0 +1,105 @@ +<# + +.NOTES +Copyright (c) Microsoft Corporation. +Licensed under the MIT License. + +.SYNOPSIS +Runs clang-format on the source code. + +.DESCRIPTION +This is use to clang-format the code using the rules in .github\linters\.clang-format. + +The clang-format version must match the version used by GitHub Super-Linter or false positives can be found. +You can install this version locally using: + +winget install --id=LLVM.LLVM --version 21.1.2 + +.PARAMETER Check +Runs clang-format without modifying the files in place. + +.PARAMETER LLVM +Normally clang-format is found on the path. If you use this switch, then it looks for it in C:\Program Files\LLVM\bin. + +.LINKS +https://github.com/microsoft/DirectXMath + +#> + +[CmdletBinding()] +param( + [switch] $Check, + [Alias('UseLLVM')] + [switch] $LLVM +) + +$ErrorActionPreference = 'Stop' + +$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path +$configFile = Join-Path $repoRoot '.github\linters\.clang-format' + +if (-not (Test-Path -LiteralPath $configFile -PathType Leaf)) { + throw "clang-format configuration was not found: $configFile" +} + +$clangFormatPath = if ($LLVM) { + 'C:\Program Files\LLVM\bin\clang-format.exe' +} +else { + (Get-Command clang-format -ErrorAction SilentlyContinue).Source +} + +if (-not $clangFormatPath -or -not (Test-Path -LiteralPath $clangFormatPath -PathType Leaf)) { + if ($LLVM) { + throw "clang-format was not found at: $clangFormatPath" + } + + throw 'clang-format was not found on PATH.' +} + +# This has to match the version used by GitHub super-linter or results will not match. +$requiredClangFormatVersion = '21.1.2' +$clangFormatVersion = (& $clangFormatPath '--version' 2>&1 | Out-String).Trim() +if ($LASTEXITCODE -ne 0) { + throw "Unable to determine clang-format version." +} + +if ($clangFormatVersion -notmatch "\b$([regex]::Escape($requiredClangFormatVersion))\b") { + throw "clang-format version $requiredClangFormatVersion is required; found: $clangFormatVersion" +} + +$sourceFiles = Get-ChildItem -LiteralPath $repoRoot -Recurse| + Where-Object { + $_.FullName -notmatch '\\(?:\.git|\.vs|build|Tests|vcpkg_installed)\\' -and $_.Extension -in '.c','.cc','.cpp','.cxx','.h','.hh','.hpp','.inl' + } + +if ($sourceFiles.Count -eq 0) { + Write-Output 'No C/C++ source files found.' + exit 0 +} + +$arguments = @( + '--style=file:{0}' -f $configFile +) + +if ($Check) { + $arguments += '--dry-run' + $arguments += '--Werror' +} +else { + $arguments += '-i' +} + +foreach ($sourceFile in $sourceFiles) { + & $clangFormatPath @arguments $sourceFile.FullName + if ($LASTEXITCODE -ne 0) { + throw "clang-format failed for $($sourceFile.FullName)." + } +} + +if ($Check) { + Write-Output "Checked $($sourceFiles.Count) C/C++ source files." +} +else { + Write-Output "Formatted $($sourceFiles.Count) C/C++ source files." +}