linters
This commit is contained in:
85
CLAUDE.md
85
CLAUDE.md
@@ -117,82 +117,37 @@ xvfb-run -a -s "-screen 0 1280x720x24" ./projecte_2026
|
||||
|
||||
---
|
||||
|
||||
## Static Analysis Tools (Linters)
|
||||
## Static Analysis & Formatting (CMake targets)
|
||||
|
||||
This project uses two complementary static analysis tools for code quality:
|
||||
|
||||
### clang-tidy (Modernization & Best Practices)
|
||||
|
||||
**Purpose:** Detects style issues, modernization opportunities, and potential bugs. Best for refactoring and applying C++20 best practices.
|
||||
|
||||
**IMPORTANT:** Always run on specific files, NOT on the entire project, to avoid long execution times and errors in unrelated files.
|
||||
Los linters y el formateador están integrados como targets de CMake. Todos excluyen `source/external/` y los headers SPIR-V generados (`*_spv.h`).
|
||||
|
||||
```bash
|
||||
# Analyze a specific file (RECOMMENDED)
|
||||
tools/linter/run_clang-tidy.sh source/game/entities/player.cpp
|
||||
|
||||
# Analyze multiple specific files
|
||||
tools/linter/run_clang-tidy.sh source/game/entities/player.cpp source/game/entities/enemy.cpp
|
||||
|
||||
# Apply fixes automatically to a specific file
|
||||
tools/linter/run_clang-tidy.sh --fix source/game/entities/player.cpp
|
||||
|
||||
# Analyze entire project (SLOW, may have errors in defaults.hpp)
|
||||
tools/linter/run_clang-tidy.sh
|
||||
# Desde el directorio build/
|
||||
cmake --build . --target tidy # clang-tidy sobre todo el código (sin fixes)
|
||||
cmake --build . --target tidy-fix # clang-tidy aplicando fixes automáticos
|
||||
cmake --build . --target cppcheck # cppcheck (warning/style/performance/portability, --std=c++20)
|
||||
cmake --build . --target format # clang-format -i sobre todo el código
|
||||
cmake --build . --target format-check # clang-format en modo dry-run (CI)
|
||||
```
|
||||
|
||||
**Known False Positives:**
|
||||
- `defaults.hpp`: May report errors in constant definitions
|
||||
- `readability-magic-numbers` / `cppcoreguidelines-avoid-magic-numbers`: Acceptable for game constants (block sizes, speeds, etc.)
|
||||
Los targets se definen en [CMakeLists.txt](CMakeLists.txt) (sección *STATIC ANALYSIS TARGETS*) y se generan únicamente si las herramientas correspondientes (`clang-tidy`, `clang-format`, `cppcheck`) están instaladas en el sistema.
|
||||
|
||||
### cppcheck (Bug Detection & Memory Safety)
|
||||
**Falsos positivos conocidos de clang-tidy:**
|
||||
- `defaults.hpp`: puede reportar errores en definiciones de constantes.
|
||||
- `readability-magic-numbers` / `cppcoreguidelines-avoid-magic-numbers`: aceptables para constantes de juego (tamaños de bloque, velocidades, etc).
|
||||
|
||||
**Purpose:** Detects bugs, memory leaks, undefined behavior, and style issues. Complementary to clang-tidy.
|
||||
**Notas sobre cppcheck:**
|
||||
- Avisos `unusedStructMember` son habituales en estructuras de datos accedidas vía serialización/yaml o por código `#ifdef _DEBUG`; revisar antes de borrar nada.
|
||||
- El target usa la lista explícita de fuentes (excluyendo `source/external/` y `*_spv.h`), no `--project=compile_commands.json`. Si añades flags nuevos, actualiza el target en [CMakeLists.txt](CMakeLists.txt).
|
||||
- Por defecto cppcheck solo evalúa 12 combinaciones de `#ifdef`. Para analizar todas, añadir `--force` (mucho más lento).
|
||||
|
||||
**Para análisis puntual de un solo fichero**, llamar la herramienta directamente:
|
||||
|
||||
```bash
|
||||
# Warning, style, and performance analysis (RECOMMENDED for daily use)
|
||||
tools/linter/run_cppcheck.sh -w --path source/game/entities/player.cpp
|
||||
|
||||
# Exhaustive analysis (slower, more thorough)
|
||||
tools/linter/run_cppcheck.sh -a --path source/game/entities/
|
||||
|
||||
# Detect unused functions (whole project analysis)
|
||||
tools/linter/run_cppcheck.sh -u
|
||||
|
||||
# Analyze entire project with warning level
|
||||
tools/linter/run_cppcheck.sh -w
|
||||
clang-tidy -p build source/game/entities/player.cpp
|
||||
cppcheck --enable=warning,style --std=c++20 -I source source/game/entities/player.cpp
|
||||
```
|
||||
|
||||
**Output:** Results are saved in `tools/linter/cppcheck-result-*.txt`
|
||||
|
||||
**Known False Positives:**
|
||||
- `unusedFunction`: May report false positives for callback functions or public API methods
|
||||
- `passedByValue`: Acceptable for small types like SDL_FRect in game code
|
||||
- `constParameter`: Not always applicable for API consistency
|
||||
|
||||
### When to Use Each Tool
|
||||
|
||||
| Scenario | Tool | Reason |
|
||||
|----------|------|--------|
|
||||
| **Daily refactoring** | clang-tidy | Fast, auto-fixable issues |
|
||||
| **Before committing** | Both | Comprehensive quality check |
|
||||
| **After major changes** | cppcheck -a | Deep bug analysis |
|
||||
| **Hunting memory leaks** | cppcheck | Specialized detection |
|
||||
| **Code modernization** | clang-tidy --fix | Automatic C++20 upgrades |
|
||||
|
||||
### Integration with Development Workflow
|
||||
|
||||
When refactoring code (especially with `/refactor-class` command):
|
||||
1. Make structural changes
|
||||
2. **Compile** to verify syntax
|
||||
3. **Run clang-tidy** (without --fix) to identify issues
|
||||
4. **Run cppcheck -w** to catch bugs
|
||||
5. Review and fix legitimate issues
|
||||
6. Apply automatic fixes if appropriate
|
||||
7. Recompile and test
|
||||
|
||||
**Note:** The `/refactor-class` command automatically runs both linters after compilation and provides intelligent analysis of results.
|
||||
|
||||
---
|
||||
|
||||
## Important Code Consistency Rules
|
||||
|
||||
Reference in New Issue
Block a user