Skip to content

Add clang-format and clang-tidy configuration #4

Description

@codingwithmagga

Description

Establish consistent code formatting and static analysis across the project.
This is required for maintainability, senior-level code review readiness, and potential open-source publication.


clang-format Configuration (.clang-format)

Base style: Google

Modifications

BasedOnStyle: Google
ColumnLimit: 100
IndentWidth: 4
AccessModifierOffset: -4
BreakBeforeBraces: Allman
SortIncludes: true
AllowShortFunctionsOnASingleLine: Empty
DerivePointerAlignment: false
PointerAlignment: Left

clang-tidy Configuration (.clang-tidy)

Enabled Checks

  • bugprone-*
  • modernize-*
  • performance-*
  • readability-*
  • cppcoreguidelines-*
  • misc-*

Disabled Checks

  • modernize-use-trailing-return-type
  • readability-magic-numbers
  • cppcoreguidelines-avoid-magic-numbers

Configuration

Checks: >
  bugprone-*,
  modernize-*,
  performance-*,
  readability-*,
  cppcoreguidelines-*,
  misc-*,
  -modernize-use-trailing-return-type,
  -readability-magic-numbers,
  -cppcoreguidelines-avoid-magic-numbers

WarningsAsErrors: '*'

Additional Scripts

scripts/format.sh

Formats all source files in-place.

#!/usr/bin/env bash
set -e

find . \( -name "*.cpp" -o -name "*.hpp" \) -print0 \
  | xargs -0 clang-format -i

scripts/lint.sh

Runs clang-tidy using compile commands.

#!/usr/bin/env bash
set -e

if [ ! -f build/compile_commands.json ]; then
  echo "compile_commands.json not found. Run CMake with:"
  echo "  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
  exit 1
fi

find src include -name "*.cpp" -o -name "*.hpp" | while read -r file; do
  clang-tidy "$file" -p build
done

scripts/check-format.sh

Fails if formatting is not compliant (for CI use).

#!/usr/bin/env bash
set -e

diff=$(find . \( -name "*.cpp" -o -name "*.hpp" \) -print0 \
  | xargs -0 clang-format \
  | diff -u <(cat) -)

if [ -n "$diff" ]; then
  echo "Formatting issues detected:"
  echo "$diff"
  exit 1
fi

CMake Requirement

Ensure compile commands are exported for clang-tidy:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Acceptance Criteria

  • scripts/format.sh formats all project files consistently
  • scripts/lint.sh produces zero warnings on the initial placeholder code
  • CI pipeline (Issue 3) includes a lint job that fails on style violations
  • A deliberately misformatted commit is rejected by CI
  • .clang-format and .clang-tidy exist at the repository root
  • compile_commands.json is generated via CMake

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions