diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..424543b --- /dev/null +++ b/.clang-format @@ -0,0 +1,87 @@ +Language: Cpp +Standard: c++20 +BasedOnStyle: LLVM + +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +ContinuationIndentWidth: 4 +IndentCaseLabels: true +IndentPPDirectives: BeforeHash +NamespaceIndentation: None +AccessModifierOffset: -4 +IndentRequiresClause: true +RequiresClausePosition: OwnLine + +ColumnLimit: 100 +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLoopsOnASingleLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortLambdasOnASingleLine: Inline +AlwaysBreakTemplateDeclarations: Yes +BreakConstructorInitializers: BeforeColon +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeConceptDeclarations: Always +PackConstructorInitializers: NextLine +InsertNewlineAtEOF: true +SeparateDefinitionBlocks: Always + +DerivePointerAlignment: false +PointerAlignment: Left +ReferenceAlignment: Left +QualifierAlignment: Left + +SpaceAfterCStyleCast: true +SpaceBeforeParens: ControlStatements +SpacesInAngles: Never +SpaceBeforeCpp11BracedList: false +SpaceBeforeRangeBasedForLoopColon: true + +SortIncludes: CaseSensitive +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^<[a-z_]+>$' + Priority: 1 + - Regex: '^$' + Priority: 2 + - Regex: '^<(lua|lauxlib|lualib)\.h>$' + Priority: 3 + - Regex: '^<.*\.h>$' + Priority: 4 + - Regex: '^ + -*, + bugprone-*, + cert-*, + clang-analyzer-*, + concurrency-*, + cppcoreguidelines-*, + misc-*, + modernize-*, + performance-*, + portability-*, + readability-*, + -bugprone-easily-swappable-parameters, + -cert-err33-c, + -cppcoreguidelines-avoid-c-arrays, + -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-avoid-non-const-global-variables, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-pro-type-vararg, + -misc-const-correctness, + -misc-include-cleaner, + -misc-no-recursion, + -misc-non-private-member-variables-in-classes, + -modernize-avoid-c-arrays, + -modernize-use-trailing-return-type, + -performance-enum-size, + -portability-avoid-pragma-once, + -readability-braces-around-statements, + -readability-identifier-length, + -readability-implicit-bool-conversion, + -readability-magic-numbers, + -readability-named-parameter, + -readability-uppercase-literal-suffix + +WarningsAsErrors: '*' +HeaderFilterRegex: '(^|/)(app/src/main/cpp|include|src|modules)/.*\.(h|hpp|hxx)$' +ExcludeHeaderFilterRegex: '(^|/)(build|external|third_party|vendor)/' +FormatStyle: file +UseColor: true + +CheckOptions: + - key: readability-identifier-naming.NamespaceCase + value: lower_case + - key: readability-identifier-naming.ClassCase + value: CamelCase + - key: readability-identifier-naming.StructCase + value: CamelCase + - key: readability-identifier-naming.TemplateParameterCase + value: CamelCase + - key: readability-identifier-naming.FunctionCase + value: lower_case + - key: readability-identifier-naming.VariableCase + value: lower_case + - key: readability-identifier-naming.PrivateMemberSuffix + value: '_' + - key: readability-identifier-naming.ProtectedMemberSuffix + value: '_' + - key: readability-identifier-naming.EnumCase + value: CamelCase + - key: readability-identifier-naming.EnumConstantCase + value: CamelCase + - key: readability-identifier-naming.MacroDefinitionCase + value: UPPER_CASE + - key: modernize-use-nullptr.NullMacros + value: 'NULL' + - key: modernize-loop-convert.MaxCopySize + value: '16' + - key: modernize-pass-by-value.IncludeStyle + value: google + - key: readability-function-cognitive-complexity.Threshold + value: '35' + - key: readability-function-size.LineThreshold + value: '150' + - key: readability-function-size.StatementThreshold + value: '200' + - key: performance-move-const-arg.CheckTriviallyCopyableMove + value: 'true' + - key: cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor + value: 'true' diff --git a/.github/workflows/clang-quality.yml b/.github/workflows/clang-quality.yml new file mode 100644 index 0000000..edb2570 --- /dev/null +++ b/.github/workflows/clang-quality.yml @@ -0,0 +1,46 @@ +name: Clang Quality Audit + +'on': + push: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + clang-quality: + runs-on: ubuntu-24.04 + timeout-minutes: 30 + + steps: + - name: Check out project + uses: actions/checkout@v5 + + - name: Install Clang workflow tools + shell: bash + run: | + set -euo pipefail + sudo apt-get update + sudo apt-get install --yes clang clang-format clang-tidy clang-tools fish fd-find ninja-build + sudo ln -sf "$(command -v fdfind)" /usr/local/bin/fd + + - name: Configure compilation database + shell: bash + run: | + set -euo pipefail + cmake -S . -B build -G Ninja \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + + - name: Build and test + shell: bash + run: | + set -euo pipefail + cmake --build build --parallel 2 + ctest --test-dir build --output-on-failure + + - name: Run full Clang quality audit + continue-on-error: true + shell: bash + run: fish tools/clang-check.fish --full diff --git a/AGENTS.md b/AGENTS.md index 6175f5f..8e705a5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -180,3 +180,20 @@ command and reason. Never claim a test passed without its output. - End with changed files, tests run, failures, warnings, and package-version consequences. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/CHANGELOG.md b/CHANGELOG.md index 23cabe8..9ff0035 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ## Unreleased +- Add repository-wide Clang formatting, Clang-Tidy, Static Analyzer, and GitHub Actions quality gates. Package agent policies inherit the root configuration without changing immutable package content. + - Advance GUI to `0.6.0-dev.15` with determinate progress bars, lifetime-safe button groups and radio behavior, drawable-or-glyph button content, imported libGDX progress styles, portable tests, documentation, and showcase coverage. diff --git a/README.md b/README.md index 4633692..cf947f2 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,10 @@ edge/corner resizing, minimum sizes, and viewport constraints. Its packaged skin fixture uses CC0 Kenney UI Pack images and libGDX-compatible split and padding metadata. +## Clang quality + +GitHub Actions performs repository-wide clang-format, Clang-Tidy, and Clang Static Analyzer checks. Full local execution is optional on constrained Termux devices. Agents must run narrow checks when practical and treat the GitHub quality gate as authoritative. + ## Build packages Package sources retain the public layout consumed by Squared Project diff --git a/packages/squared-application/AGENTS.md b/packages/squared-application/AGENTS.md index f4e40d9..ea73c82 100644 --- a/packages/squared-application/AGENTS.md +++ b/packages/squared-application/AGENTS.md @@ -79,3 +79,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-backend-sdl2-opengl/AGENTS.md b/packages/squared-backend-sdl2-opengl/AGENTS.md index a4ab0c5..4b77e7a 100644 --- a/packages/squared-backend-sdl2-opengl/AGENTS.md +++ b/packages/squared-backend-sdl2-opengl/AGENTS.md @@ -80,3 +80,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-data/AGENTS.md b/packages/squared-data/AGENTS.md index b381c39..308ae53 100644 --- a/packages/squared-data/AGENTS.md +++ b/packages/squared-data/AGENTS.md @@ -79,3 +79,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-graphics/AGENTS.md b/packages/squared-graphics/AGENTS.md index b9f9575..be66b5d 100644 --- a/packages/squared-graphics/AGENTS.md +++ b/packages/squared-graphics/AGENTS.md @@ -79,3 +79,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-graphics2d/AGENTS.md b/packages/squared-graphics2d/AGENTS.md index 1da421f..48d354e 100644 --- a/packages/squared-graphics2d/AGENTS.md +++ b/packages/squared-graphics2d/AGENTS.md @@ -80,3 +80,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-gui-file-picker/AGENTS.md b/packages/squared-gui-file-picker/AGENTS.md index 96e0b96..b598bda 100644 --- a/packages/squared-gui-file-picker/AGENTS.md +++ b/packages/squared-gui-file-picker/AGENTS.md @@ -10,3 +10,21 @@ Run the file-picker host test, package documentation test, full CMake workflow, Doxygen, Graphviz validation, and package build verification before release. Never overwrite an immutable `.sq` coordinate or stage/publish without explicit authorization. + + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-gui/AGENTS.md b/packages/squared-gui/AGENTS.md index 58e3fe2..386e1b1 100644 --- a/packages/squared-gui/AGENTS.md +++ b/packages/squared-gui/AGENTS.md @@ -82,3 +82,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-holoDisk/AGENTS.md b/packages/squared-holoDisk/AGENTS.md index 5523509..16d204b 100644 --- a/packages/squared-holoDisk/AGENTS.md +++ b/packages/squared-holoDisk/AGENTS.md @@ -80,3 +80,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-math/AGENTS.md b/packages/squared-math/AGENTS.md index bee9b8a..28b9f04 100644 --- a/packages/squared-math/AGENTS.md +++ b/packages/squared-math/AGENTS.md @@ -78,3 +78,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-messaging/AGENTS.md b/packages/squared-messaging/AGENTS.md index 4716375..bab815e 100644 --- a/packages/squared-messaging/AGENTS.md +++ b/packages/squared-messaging/AGENTS.md @@ -81,3 +81,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-scene2d/AGENTS.md b/packages/squared-scene2d/AGENTS.md index e151fbf..1eb2e0d 100644 --- a/packages/squared-scene2d/AGENTS.md +++ b/packages/squared-scene2d/AGENTS.md @@ -80,3 +80,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/packages/squared-time/AGENTS.md b/packages/squared-time/AGENTS.md index 885b4fc..4678c3a 100644 --- a/packages/squared-time/AGENTS.md +++ b/packages/squared-time/AGENTS.md @@ -80,3 +80,20 @@ results, warnings, and anything that could not run. Before any package version change, present the immutable-version and downstream-coordinate impact to the user. + +## Mandatory Clang workflow + +This package inherits the repository-root `.clang-format`, `.clang-tidy`, and `AGENTS.md`. +Whenever project-owned C or C++ files in this package change, agents must configure a compilation +database and run narrow formatter and Clang-Tidy checks on touched files when practical. The +repository-wide `tools/clang-check.fish --full` audit runs in GitHub Actions and reports existing +baseline debt. It must run for releases, package-version milestones, public API changes, and broad +refactors, but remains advisory until the repository-wide baseline is clean. + +New warnings in files touched by the task are actionable failures. Existing unrelated baseline +debt does not block an otherwise successful build or task. Agents must report narrow-check results +and the latest CI audit result. Missing tools or compilation commands must be reported rather than +silently skipped. Global warning suppression is forbidden; any necessary suppression must be +narrow, justified, documented, and reported. Generated, vendored, and third-party source remains +excluded unless explicitly owned by the task. + diff --git a/tools/clang-check.fish b/tools/clang-check.fish new file mode 100755 index 0000000..176d811 --- /dev/null +++ b/tools/clang-check.fish @@ -0,0 +1,174 @@ +#!/usr/bin/env fish + +set -l fix 0 +set -l full 0 + +for argument in $argv + switch $argument + case --fix + set fix 1 + case --full + set full 1 + case --help -h + echo "usage: tools/clang-check.fish [--fix] [--full]" + exit 0 + case '*' + echo "error: unknown argument: $argument" >&2 + exit 2 + end +end + +set -l root (pwd) +while not test -f "$root/.clang-tidy" + set -l parent (dirname "$root") + if test "$parent" = "$root" + echo "error: could not find project root containing .clang-tidy" >&2 + exit 2 + end + set root "$parent" +end +cd "$root" + +set -l required clang-format clang-tidy fd python3 +if test $full -eq 1 + set -a required scan-build cmake +end +for command_name in $required + if not command -q $command_name + echo "error: required command is unavailable: $command_name" >&2 + exit 2 + end +end + +set -l compile_database "" +if set -q CLANG_COMPILE_DB + set compile_database "$CLANG_COMPILE_DB" +else + for candidate in \ + compile_commands.json \ + build/compile_commands.json \ + native-build/compile_commands.json + if test -f "$candidate" + set compile_database "$candidate" + break + end + end +end + +if test -z "$compile_database"; or not test -f "$compile_database" + echo "error: compile_commands.json is required" >&2 + echo "configure CMake with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" >&2 + echo "or set CLANG_COMPILE_DB to its path" >&2 + exit 2 +end + +set compile_database (realpath "$compile_database") +set -l compile_directory (dirname "$compile_database") +set -l overall 0 + +set -l format_files (fd --type f \ + --extension c --extension cc --extension cpp --extension cxx \ + --extension h --extension hh --extension hpp --extension hxx \ + --exclude build --exclude native-build --exclude external \ + --exclude third_party --exclude vendor --exclude generated .) + +if test (count $format_files) -eq 0 + echo "error: no project-owned C/C++ files were found" >&2 + exit 2 +end + +echo "project root: $root" +echo "compile database: $compile_database" +echo "project-owned format files: "(count $format_files) +echo +echo "== clang-format ==" + +if test $fix -eq 1 + clang-format -i $format_files + or set overall 1 +else + clang-format --dry-run --Werror $format_files + or set overall 1 +end + +echo +echo "== clang-tidy ==" +set -l translation_list (mktemp) +python3 -c ' +import json +import os +import sys + +database, root = sys.argv[1], os.path.realpath(sys.argv[2]) +excluded = ("build", "native-build", "external", "third_party", "vendor", "generated") +seen = set() +with open(database, encoding="utf-8") as stream: + commands = json.load(stream) +for command in commands: + filename = command.get("file") + directory = command.get("directory", root) + if not filename: + continue + absolute = os.path.realpath(filename if os.path.isabs(filename) else os.path.join(directory, filename)) + try: + relative = os.path.relpath(absolute, root) + except ValueError: + continue + if relative.startswith(".." + os.sep) or relative == "..": + continue + if any(part in excluded for part in relative.split(os.sep)): + continue + if os.path.splitext(relative)[1].lower() not in {".c", ".cc", ".cpp", ".cxx"}: + continue + if absolute not in seen: + seen.add(absolute) + print(absolute) +' "$compile_database" "$root" >$translation_list + +set -l translation_units (string split \n (string collect <$translation_list)) +rm -f "$translation_list" + +if test (count $translation_units) -eq 0 + echo "error: compilation database contains no project-owned translation units" >&2 + exit 2 +end + +echo "project-owned translation units: "(count $translation_units) +set -l translation_index 0 +for translation_unit in $translation_units + set translation_index (math $translation_index + 1) + echo "[clang-tidy $translation_index/"(count $translation_units)"] $translation_unit" + if test $fix -eq 1 + clang-tidy -p "$compile_directory" --fix --format-style=file "$translation_unit" + else + clang-tidy -p "$compile_directory" "$translation_unit" + end + or set overall 1 +end + +if test $full -eq 1 + echo + echo "== Clang Static Analyzer ==" + if set -q CLANG_FULL_BUILD_COMMAND + fish -c "$CLANG_FULL_BUILD_COMMAND" + or set overall 1 + else if test -f CMakeLists.txt + set -l analyzer_build build/clang-analyzer + scan-build --status-bugs cmake -S . -B "$analyzer_build" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + and scan-build --status-bugs cmake --build "$analyzer_build" --clean-first + or set overall 1 + else + echo "error: --full requires CLANG_FULL_BUILD_COMMAND for this project layout" >&2 + set overall 1 + end +end + +echo +if test $overall -eq 0 + echo "ALL CLANG CHECKS PASSED" +else + echo "CLANG CHECKS FAILED" >&2 +end +exit $overall