lint.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/bash
  2. # Lint script for the project
  3. # Usage: lint.sh [--fix]
  4. set -e
  5. FIX_FLAG=""
  6. if [ "$1" = "--fix" ]; then
  7. FIX_FLAG="--fix-errors"
  8. fi
  9. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  10. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  11. cd "$PROJECT_DIR"
  12. # Submodule and vendored paths to exclude
  13. SUBMODULES="components/u8g2 components/u8g2-hal-esp-idf"
  14. export PATH="$(brew --prefix llvm)/bin:$PATH"
  15. # Ensure clang-tidy is available (install via brew if needed)
  16. if ! command -v clang-tidy &>/dev/null; then
  17. if command -v brew &>/dev/null; then
  18. echo "clang-tidy not found, installing llvm via brew..."
  19. brew install llvm
  20. export PATH="$(brew --prefix llvm)/bin:$PATH"
  21. else
  22. echo "Error: clang-tidy not found and brew is not available to install it"
  23. exit 1
  24. fi
  25. fi
  26. # Ensure compile_commands.json exists
  27. if [ ! -f build/compile_commands.json ]; then
  28. echo "Error: build/compile_commands.json not found. Run a build first (idf.py build)."
  29. exit 1
  30. fi
  31. # Build exclusion args for find
  32. EXCLUDE_ARGS=()
  33. for sm in $SUBMODULES; do
  34. EXCLUDE_ARGS+=(-path "$sm" -prune -o)
  35. done
  36. # Find all C source files in main/ and components/, excluding submodules
  37. SOURCES=$(find main components "${EXCLUDE_ARGS[@]}" \( -name "*.c" -o -name "*.h" \) -print)
  38. echo "=== Running clang-tidy ==="
  39. WARNINGS=0
  40. for file in $SOURCES; do
  41. echo "Checking $file..."
  42. # Run clang-tidy, capture output, filter out ESP-IDF "file not found" errors
  43. # which are expected when running on the host outside the build environment
  44. OUTPUT=$(clang-tidy $FIX_FLAG -p build "$file" 2>&1 || true)
  45. # Filter out lines about missing ESP-IDF/lwip/freertos headers
  46. FILTERED=$(echo "$OUTPUT" | grep -v "file not found \[clang-diagnostic" || true)
  47. # Check if any warnings remain (lines containing ": warning:")
  48. if echo "$FILTERED" | grep -q ": warning:"; then
  49. echo "$FILTERED" | grep -E "(: warning:|: note:)"
  50. WARNINGS=1
  51. fi
  52. done
  53. if [ "$WARNINGS" -ne 0 ]; then
  54. echo ""
  55. echo "clang-tidy found issues"
  56. exit 1
  57. fi
  58. echo "All files pass clang-tidy checks"