-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ps1
More file actions
166 lines (144 loc) · 5.66 KB
/
format.ps1
File metadata and controls
166 lines (144 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<#
format.ps1
Formats and lint-fixes the Prefix C codebase with Clang tools.
Usage (from Prefix folder):
powershell -ExecutionPolicy Bypass -File .\format.ps1
#>
$ErrorActionPreference = 'Stop'
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$srcDir = Join-Path $scriptDir "src"
if (-not (Test-Path $srcDir)) {
Write-Error "Could not locate src directory at: $srcDir"
exit 1
}
$clang = Get-Command clang.exe -ErrorAction SilentlyContinue
$clangFormat = Get-Command clang-format.exe -ErrorAction SilentlyContinue
$clangTidy = Get-Command clang-tidy.exe -ErrorAction SilentlyContinue
if (-not $clang) {
Write-Error "clang.exe not found on PATH."
exit 1
}
if (-not $clangFormat) {
Write-Error "clang-format.exe not found on PATH."
exit 1
}
if (-not $clangTidy) {
Write-Error "clang-tidy.exe not found on PATH."
exit 1
}
# Determine clang-tidy checks to use. Priority:
# 1) CLANG_TIDY_CHECKS env var
# 2) repo .clang-tidy file (let clang-tidy find it)
# 3) fix-oriented default checks that stay quiet unless they can rewrite code
$repoTidyFile = Join-Path $scriptDir ".clang-tidy"
$clangTidyChecks = $env:CLANG_TIDY_CHECKS
if (-not $clangTidyChecks) {
if (-not (Test-Path $repoTidyFile)) {
# Default to checks that usually provide mechanical fixes instead of
# flooding the run with non-actionable audit diagnostics.
$clangTidyChecks = "readability-braces-around-statements,readability-isolate-declaration,readability-redundant-control-flow"
} else {
$clangTidyChecks = ""
}
}
$repoClangFormat = Join-Path $scriptDir ".clang-format"
# If repo provides a .clang-format, prefer it; otherwise use an inline
# style that enforces four-space indents and no tabs.
if (Test-Path $repoClangFormat) {
$clangFormatStyle = "file"
$clangTidyFormatArg = "-format-style=file"
} else {
$clangFormatStyle = "{BasedOnStyle: LLVM, IndentWidth: 4, UseTab: Never, ColumnLimit: 120}"
$clangTidyFormatArg = "-format-style=$clangFormatStyle"
}
$roots = @(
(Join-Path $scriptDir "src"),
(Join-Path $scriptDir "ext"),
(Join-Path $scriptDir "lib"),
(Join-Path $scriptDir "tests\helpers")
)
$allC = @()
$allH = @()
foreach ($root in $roots) {
if (-not (Test-Path $root)) {
continue
}
$allC += Get-ChildItem -Path $root -Recurse -File -Filter *.c | ForEach-Object { $_.FullName }
$allH += Get-ChildItem -Path $root -Recurse -File -Filter *.h | ForEach-Object { $_.FullName }
}
$sourceFiles = @($allC | Sort-Object -Unique)
$headerFiles = @($allH | Sort-Object -Unique)
$formatFiles = @($sourceFiles + $headerFiles | Sort-Object -Unique)
if ($sourceFiles.Count -eq 0) {
Write-Error "No C source files found under src/, ext/, lib/, or tests/helpers/."
exit 1
}
if ($formatFiles.Count -eq 0) {
Write-Error "No C/C header files found to format."
exit 1
}
Write-Host "Formatting $($formatFiles.Count) C/C header file(s) with clang-format (IndentWidth=$($clangFormatStyle -replace '^\{|\}$',''))..."
foreach ($file in $formatFiles) {
& $clangFormat.Path "-style=$clangFormatStyle" -i $file
if ($LASTEXITCODE -ne 0) {
Write-Error "clang-format failed for: $file"
exit 1
}
}
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$tidyDbDir = Join-Path $env:TEMP ("prefix-clang-tidy-" + $stamp)
New-Item -ItemType Directory -Path $tidyDbDir -Force | Out-Null
try {
$compileDbPath = Join-Path $tidyDbDir "compile_commands.json"
$compileDb = @()
foreach ($file in $sourceFiles) {
$command = ('"{0}" --driver-mode=cl /std:c17 /I"{1}" /I"{2}" /D_CRT_SECURE_NO_WARNINGS "{3}"' -f $clang.Path, $srcDir, $scriptDir, $file)
$compileDb += [ordered]@{
directory = $scriptDir
file = $file
command = $command
}
}
$compileDb | ConvertTo-Json -Depth 4 | Set-Content -Path $compileDbPath -Encoding UTF8
Write-Host "Lint-fixing $($sourceFiles.Count) C source file(s) with clang-tidy..."
$lintFixFailures = @()
foreach ($file in $sourceFiles) {
$tidyArgs = @('-p', $tidyDbDir)
if ($clangTidyChecks -ne "") { $tidyArgs += "--checks=$clangTidyChecks" }
# Narrow header filter to just project files (src/, ext/, lib/, tests/) to avoid analyzing system headers
$headerFilter = "(src|ext|lib|tests)/"
$tidyArgs += @('-quiet', '-fix', '-fix-errors', '-fix-notes', $clangTidyFormatArg, "-header-filter=$headerFilter", $file)
$tidyOutput = & $clangTidy.Path @tidyArgs 2>&1
if ($LASTEXITCODE -ne 0) {
$lintFixFailures += [pscustomobject]@{
File = $file
Output = (($tidyOutput | ForEach-Object { $_.ToString() }) -join [Environment]::NewLine)
}
}
}
if ($lintFixFailures.Count -gt 0) {
Write-Host "clang-tidy auto-fix failed for $($lintFixFailures.Count) file(s):" -ForegroundColor Red
foreach ($failure in $lintFixFailures) {
Write-Host "--- $($failure.File) ---" -ForegroundColor Red
if ($failure.Output) {
Write-Host $failure.Output -ForegroundColor Red
}
}
Write-Error "clang-tidy auto-fix failed."
exit 1
}
Write-Host "Re-formatting after clang-tidy fixes..."
foreach ($file in $formatFiles) {
& $clangFormat.Path "-style=$clangFormatStyle" -i $file
if ($LASTEXITCODE -ne 0) {
Write-Error "clang-format post-lint failed for: $file"
exit 1
}
}
Write-Host "Formatting and lint auto-fix completed successfully." -ForegroundColor Green
exit 0
} finally {
if (Test-Path $tidyDbDir) {
Remove-Item -Recurse -Force $tidyDbDir -ErrorAction SilentlyContinue
}
}