This document describes the clang-tidy configuration and workflows for enforcing C++ Core Guidelines in the Phlex project.
The project uses clang-tidy to enforce modern C++23 best practices and C++ Core Guidelines compliance.
Core Guidelines (cppcoreguidelines-*):
- Enforces C++ Core Guidelines recommendations
- Ensures proper resource management (RAII)
- Validates special member functions (rule of five)
- Checks for proper use of const and constexpr
- Disabled checks:
avoid-magic-numbers- Too restrictive for scientific codeavoid-c-arrays- C arrays sometimes needed for interoppro-bounds-*- Allow pointer arithmetic where necessarymacro-usage- Macros sometimes necessarynon-private-member-variables-in-classes- Allow public data members in simple structs
Bug Prevention (bugprone-*):
- Detects common programming errors
- Identifies suspicious constructs
- Catches potential undefined behavior
- Disabled checks:
easily-swappable-parameters- Too many false positivesexception-escape- Exception handling sometimes intentional
Security (cert-*):
- CERT C++ Secure Coding Standard compliance
- Security-focused checks for vulnerabilities
Concurrency (concurrency-*):
- Thread safety checks
- Race condition detection
- Mutex usage validation
Modernization (modernize-*):
- Suggests modern C++ features (auto, range-for, nullptr, etc.)
- Recommends C++23 idioms
- Disabled checks:
use-trailing-return-type- Not required for all functions
Performance (performance-*):
- Identifies inefficient code patterns
- Suggests const-ref parameters
- Detects unnecessary copies
Portability (portability-*):
- Cross-platform compatibility checks
- Ensures standard-conforming code
Readability (readability-*):
- Enforces consistent naming conventions
- Checks function complexity
- Validates identifier clarity
- Disabled checks:
function-cognitive-complexity- Too restrictiveidentifier-length- Short names acceptable in contextmagic-numbers- Duplicate of cppcoreguidelines check
Static Analysis (clang-analyzer-*):
- Deep static analysis of code
- Control flow analysis
- Null pointer dereference detection
The configuration enforces consistent naming:
- Namespaces:
lower_case - Classes/Structs/Enums:
lower_case - Functions:
lower_case - Variables/Parameters:
lower_case - Private/Protected/Constant Members:
lower_casewith_suffix - Macros:
UPPER_CASE - Constants/Enum Values:
lower_case - Type Aliases/Typedefs:
lower_case - Template Parameters:
CamelCase
- Line Threshold: 100 lines per function
- Statement Threshold: 50 statements per function
- Branch Threshold: 10 branches per function
Purpose: Automatically check C++ code for Core Guidelines compliance on pull requests
Features:
- Runs on PR to main branch and manual trigger
- Uses clang-tidy via
CMAKE_CXX_CLANG_TIDYduring build - Configures project with compile commands export
- Reports warnings and errors with detailed output
- Uploads clang-tidy fixes YAML and log as artifacts
- Posts inline PR comments for detected issues
How it works:
- Configure the project with
CMAKE_EXPORT_COMPILE_COMMANDS=ONandCMAKE_CXX_CLANG_TIDY='clang-tidy;--export-fixes=clang-tidy-fixes.yaml' - Build the project - clang-tidy runs automatically on each source file during compilation
- Parse output and upload artifacts
- Post inline comments on PR with issue details
How to use:
- Automatically runs on every pull request
- Review the workflow output and inline PR comments for details
- Comment
@phlexbot tidy-fixto attempt automatic fixes
Purpose: Automatically apply clang-tidy fixes when triggered by comment
Features:
- Triggered by
@phlexbot tidy-fix [<check>...]comment on PRs - Optionally specify specific checks to fix
- Attempts to reuse existing fixes from check workflow artifacts
- Falls back to running clang-tidy with
CMAKE_CXX_CLANG_TIDYif needed - Applies fixes using
clang-apply-replacements - Commits and pushes changes automatically
- Posts inline PR comments with remaining issues
How it works:
- Try to download existing
clang-tidy-fixes.yamlfrom check workflow - If not available, configure and build with clang-tidy to generate fixes
- Apply fixes using
clang-apply-replacements - Commit and push any changes
Important notes:
- Not all issues can be auto-fixed (some require manual intervention)
- The workflow will commit whatever clang-tidy can automatically fix
- Review auto-generated changes before merging
- Complex refactorings may need manual implementation
How to use:
- Review clang-tidy check results
- Comment
@phlexbot tidy-fixon the PR - Bot will apply automatic fixes and commit
- Review the changes and manually fix any remaining issues
Enable clang-tidy during compilation using CMAKE_CXX_CLANG_TIDY:
cmake -DCMAKE_CXX_CLANG_TIDY='clang-tidy' -B build -S .
cmake --build buildWhen enabled, clang-tidy runs automatically on every C++ file during compilation, providing immediate feedback.
To export fixes for later application:
cmake -DCMAKE_CXX_CLANG_TIDY='clang-tidy;--export-fixes=clang-tidy-fixes.yaml' -B build -S .
cmake --build build
clang-apply-replacements buildNote: The project sets CMAKE_EXPORT_COMPILE_COMMANDS=ON by default, which generates compile_commands.json for clang-tidy to use.
The clang-tidy workflows complement the existing formatting workflows:
- Format checks:
@phlexbot format- Fixes C++, CMake, Jsonnet, and Markdown formatting - Tidy checks:
@phlexbot tidy-fix- Fixes Core Guidelines violations
Both can be run independently or together as needed.
Enable clang-tidy to run on every file during compilation:
cmake -DCMAKE_CXX_CLANG_TIDY='clang-tidy' -B build -S .
cmake --build build -j $(nproc)This provides immediate feedback as you build, catching issues early.
To generate fixes and apply them:
# Configure with fix export
cmake -DCMAKE_CXX_CLANG_TIDY='clang-tidy;--export-fixes=clang-tidy-fixes.yaml' -B build -S .
# Build (generates fixes)
cmake --build build -j $(nproc)
# Apply fixes
clang-apply-replacements buildFor manual control, you can run clang-tidy directly:
# Check a specific file
clang-tidy -p build phlex/core/framework_graph.cpp
# Apply fixes to a specific file
clang-tidy -p build --fix phlex/core/framework_graph.cpp
# Check all project files
find phlex -name "*.cpp" | xargs clang-tidy -p buildThe project's .clang-tidy configuration is automatically used by:
- clangd (if configured as C++ language server)
- C/C++ extension with clang-tidy integration enabled
To enable real-time clang-tidy feedback in VS Code, add to .vscode/settings.json:
"C_Cpp.codeAnalysis.clangTidy.enabled": true,
"C_Cpp.codeAnalysis.clangTidy.useBuildPath": trueTo adjust checks or settings, edit .clang-tidy:
- Add/remove check categories in the
Checks:section - Modify naming conventions in
CheckOptions - Adjust complexity thresholds
- Enable/disable specific rules
After changes, test locally before committing to ensure the configuration works as expected.