$npx -y skills add mohitmishra786/low-level-dev-skills --skill static-analysisStatic analysis skill for C/C++ codebases. Use when hardening code quality, triaging noisy builds, running clang-tidy, cppcheck, or scan-build, interpreting check categories, suppressing false positives, or integrating static analysis into CI. Activates on queries about clang-tid
| 1 | # Static Analysis |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through selecting, running, and triaging static analysis tools for C/C++ — clang-tidy, cppcheck, and scan-build — including suppression strategies and CI integration. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "How do I run clang-tidy on my project?" |
| 10 | - "What clang-tidy checks should I enable?" |
| 11 | - "cppcheck is reporting false positives — how do I suppress them?" |
| 12 | - "How do I set up scan-build for deeper analysis?" |
| 13 | - "My build is noisy with static analysis warnings" |
| 14 | - "How do I generate compile_commands.json for clang-tidy?" |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Generate compile_commands.json |
| 19 | |
| 20 | clang-tidy requires a compilation database: |
| 21 | |
| 22 | ```bash |
| 23 | # CMake (preferred) |
| 24 | cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 25 | ln -s build/compile_commands.json . |
| 26 | |
| 27 | # Bear (for Make-based projects) |
| 28 | bear -- make |
| 29 | |
| 30 | # compiledb (alternative for Make) |
| 31 | pip install compiledb |
| 32 | compiledb make |
| 33 | ``` |
| 34 | |
| 35 | ### 2. Run clang-tidy |
| 36 | |
| 37 | ```bash |
| 38 | # Single file |
| 39 | clang-tidy src/foo.c -- -std=c11 -I include/ |
| 40 | |
| 41 | # Whole project via compile_commands.json |
| 42 | run-clang-tidy -p build/ -j$(nproc) |
| 43 | |
| 44 | # With specific checks enabled |
| 45 | clang-tidy -checks='bugprone-*,modernize-*,performance-*' src/foo.cpp |
| 46 | |
| 47 | # Apply auto-fixes |
| 48 | clang-tidy -checks='modernize-use-nullptr' -fix src/foo.cpp |
| 49 | ``` |
| 50 | |
| 51 | ### 3. Check category decision tree |
| 52 | |
| 53 | ```text |
| 54 | Goal? |
| 55 | ├── Find real bugs → bugprone-*, clang-analyzer-* |
| 56 | ├── Modernise C++ code → modernize-* |
| 57 | ├── Follow core guidelines → cppcoreguidelines-* |
| 58 | ├── Catch performance issues → performance-* |
| 59 | ├── Security hardening → cert-*, hicpp-* |
| 60 | └── Readability / style → readability-*, llvm-* |
| 61 | ``` |
| 62 | |
| 63 | | Category | Key checks | What it catches | |
| 64 | |----------|-----------|-----------------| |
| 65 | | `bugprone-*` | `use-after-move`, `integer-division`, `suspicious-memset-usage` | Likely bugs | |
| 66 | | `modernize-*` | `use-nullptr`, `use-override`, `use-auto` | C++11/14/17 idioms | |
| 67 | | `cppcoreguidelines-*` | `avoid-goto`, `pro-bounds-*`, `no-malloc` | C++ Core Guidelines | |
| 68 | | `performance-*` | `unnecessary-copy-initialization`, `avoid-endl` | Performance regressions | |
| 69 | | `clang-analyzer-*` | `core.*`, `unix.*`, `security.*` | Path-sensitive bugs | |
| 70 | | `cert-*` | `err34-c`, `str51-cpp` | CERT coding standard | |
| 71 | |
| 72 | ### 4. .clang-tidy configuration file |
| 73 | |
| 74 | ```yaml |
| 75 | # .clang-tidy — place at project root |
| 76 | Checks: > |
| 77 | bugprone-*, |
| 78 | modernize-*, |
| 79 | performance-*, |
| 80 | -modernize-use-trailing-return-type, |
| 81 | -bugprone-easily-swappable-parameters |
| 82 | WarningsAsErrors: 'bugprone-*,clang-analyzer-*' |
| 83 | HeaderFilterRegex: '^(src|include)/.*' |
| 84 | CheckOptions: |
| 85 | - key: modernize-loop-convert.MinConfidence |
| 86 | value: reasonable |
| 87 | - key: readability-identifier-naming.VariableCase |
| 88 | value: camelCase |
| 89 | ``` |
| 90 | |
| 91 | ### 5. Suppress false positives |
| 92 | |
| 93 | ```cpp |
| 94 | // Suppress a single line |
| 95 | int result = riskyOp(); // NOLINT(bugprone-signed-char-misuse) |
| 96 | |
| 97 | // Suppress a block |
| 98 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) |
| 99 | constexpr int BUFFER_SIZE = 4096; |
| 100 | |
| 101 | // Suppress whole function |
| 102 | [[clang::suppress("bugprone-*")]] |
| 103 | void legacy_code() { /* ... */ } |
| 104 | ``` |
| 105 | |
| 106 | Or in `.clang-tidy`: |
| 107 | |
| 108 | ```yaml |
| 109 | # Exclude third-party directories |
| 110 | HeaderFilterRegex: '^(src|include)/.*' |
| 111 | # Disable specific checks |
| 112 | Checks: '-bugprone-easily-swappable-parameters' |
| 113 | ``` |
| 114 | |
| 115 | ### 6. Run cppcheck |
| 116 | |
| 117 | ```bash |
| 118 | # Basic run |
| 119 | cppcheck --enable=all --std=c11 src/ |
| 120 | |
| 121 | # With compile_commands.json |
| 122 | cppcheck --project=build/compile_commands.json |
| 123 | |
| 124 | # Include specific checks and suppress noise |
| 125 | cppcheck --enable=warning,performance,portability \ |
| 126 | --suppress=missingIncludeSystem \ |
| 127 | --suppress=unmatchedSuppression \ |
| 128 | --error-exitcode=1 \ |
| 129 | src/ |
| 130 | |
| 131 | # Generate XML report for CI |
| 132 | cppcheck --xml --xml-version=2 src/ 2> cppcheck-report.xml |
| 133 | ``` |
| 134 | |
| 135 | | `--enable=` value | What it checks | |
| 136 | |-------------------|----------------| |
| 137 | | `warning` | Undefined behaviour, bad practices | |
| 138 | | `performance` | Redundant operations, inefficient patterns | |
| 139 | | `portability` | Non-portable constructs | |
| 140 | | `information` | Configuration and usage notes | |
| 141 | | `all` | Everything above | |
| 142 | |
| 143 | ### 7. Path-sensitive analysis with scan-build |
| 144 | |
| 145 | ```bash |
| 146 | # Intercept a Make build |
| 147 | scan-build make |
| 148 | |
| 149 | # Intercept CMake build |
| 150 | scan-build cmake --build build/ |
| 151 | |
| 152 | # Show HTML report |
| 153 | scan-view /tmp/scan-build-*/ |
| 154 | |
| 155 | # With specific checkers |
| 156 | scan-build -enable-checker security.insecureAPI.gets \ |
| 157 | -enable-checker alpha.unix.cstring.BufferOverlap \ |
| 158 | make |
| 159 | ``` |
| 160 | |
| 161 | scan-build finds deeper bugs than clang-tidy: use-after-free across functions, dead stores from logic errors, null dereferences on complex paths. |
| 162 | |
| 163 | ### 8. CI integration |
| 164 | |
| 165 | ```yaml |
| 166 | # GitHub Actions |
| 167 | - name: Static analysis |
| 168 | run: |