| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | CHECKS="*" |
| 4 | ERRORS="*" |
| 5 | |
| 6 | # Can't fix this, because winsock has different HANDLE type than posix. |
| 7 | # Still good to occasionally look at. |
| 8 | ERRORS="$ERRORS,-google-readability-casting" |
| 9 | |
| 10 | # TODO(iphydf): Fix these. |
| 11 | CHECKS="$CHECKS,-clang-analyzer-nullability.NullableDereferenced" |
| 12 | CHECKS="$CHECKS,-clang-analyzer-nullability.NullablePassedToNonnull" |
| 13 | CHECKS="$CHECKS,-clang-analyzer-nullability.NullPassedToNonnull" |
| 14 | |
| 15 | # Need to investigate or disable and document these. |
| 16 | # ========================================================= |
| 17 | |
| 18 | # TODO(iphydf): Fix these. |
| 19 | CHECKS="$CHECKS,-readability-suspicious-call-argument" |
| 20 | CHECKS="$CHECKS,-cppcoreguidelines-avoid-goto,-hicpp-avoid-goto" |
| 21 | CHECKS="$CHECKS,-bugprone-incorrect-roundings" |
| 22 | |
| 23 | # TODO(iphydf): Fix this by making more functions set error code enums. |
| 24 | CHECKS="$CHECKS,-bugprone-switch-missing-default-case" |
| 25 | |
| 26 | # TODO(iphydf): We might want some of these. For the ones we don't want, add a |
| 27 | # comment explaining why not. |
| 28 | CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding" |
| 29 | CHECKS="$CHECKS,-hicpp-signed-bitwise" |
| 30 | |
| 31 | # TODO(iphydf): Maybe fix these? |
| 32 | CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result" |
| 33 | CHECKS="$CHECKS,-bugprone-integer-division" |
| 34 | CHECKS="$CHECKS,-misc-no-recursion" |
| 35 | |
| 36 | # TODO(iphydf): Only happens in bootstrap_daemon. Fix it. |
| 37 | CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables" |
| 38 | |
| 39 | # TODO(iphydf): Probably fix these. |
| 40 | CHECKS="$CHECKS,-cert-err33-c,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers" |
| 41 | |
| 42 | # Documented disabled checks. We don't want these for sure. |
| 43 | # ========================================================= |
| 44 | |
| 45 | # We want to decay many arrays to pointers. In C, we do that all the time. |
| 46 | CHECKS="$CHECKS,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay" |
| 47 | |
| 48 | # enum{} breaks comparisons and arithmetic in C++. |
| 49 | CHECKS="$CHECKS,-modernize-macro-to-enum" |
| 50 | |
| 51 | # For most things, we do want this, but for some we want to ensure (with msan) |
| 52 | # that struct members are actually initialised with useful non-zero values. |
| 53 | # Initialising them by default takes away that validation. |
| 54 | CHECKS="$CHECKS,-cppcoreguidelines-pro-type-member-init,-hicpp-member-init" |
| 55 | |
| 56 | # https://stackoverflow.com/questions/58672959/why-does-clang-tidy-say-vsnprintf-has-an-uninitialized-va-list-argument |
| 57 | CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized" |
| 58 | |
| 59 | # We pass a lot of ints around, so many function parameters are some kind of |
| 60 | # int type that can be converted from another int type. We won't be getting |
| 61 | # away from that anytime soon. |
| 62 | CHECKS="$CHECKS,-bugprone-easily-swappable-parameters" |
| 63 | |
| 64 | # Callback handlers often don't use all their parameters. There's |
| 65 | # IgnoreVirtual, but that doesn't work for C-style callbacks. |
| 66 | CHECKS="$CHECKS,-misc-unused-parameters" |
| 67 | |
| 68 | # We sometimes use #if 0. |
| 69 | CHECKS="$CHECKS,-readability-avoid-unconditional-preprocessor-if" |
| 70 | |
| 71 | # We have better macro hygiene checks with tokstyle. We can never pass macro |
| 72 | # arguments that require parentheses inside the macro. |
| 73 | CHECKS="$CHECKS,-bugprone-macro-parentheses" |
| 74 | |
| 75 | # We don't use memcpy_s. |
| 76 | CHECKS="$CHECKS,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling" |
| 77 | |
| 78 | # __attribute__((nonnull)) causes this warning on defensive null checks. |
| 79 | CHECKS="$CHECKS,-clang-diagnostic-pointer-bool-conversion" |
| 80 | CHECKS="$CHECKS,-clang-diagnostic-tautological-pointer-compare" |
| 81 | |
| 82 | # Conflicts with "Variable is assigned a value that is never used." |
| 83 | # [unreadVariable] |
| 84 | CHECKS="$CHECKS,-cppcoreguidelines-init-variables" |
| 85 | |
| 86 | # Short variable names are used quite a lot, and we don't consider them a |
| 87 | # readability issue. |
| 88 | CHECKS="$CHECKS,-readability-identifier-length" |
| 89 | |
| 90 | # Altera checks are for GPUs (OpenCL). Our code doesn't run on GPUs. |
| 91 | CHECKS="$CHECKS,-altera-id-dependent-backward-branch" |
| 92 | CHECKS="$CHECKS,-altera-struct-pack-align" |
| 93 | CHECKS="$CHECKS,-altera-unroll-loops" |
| 94 | |
| 95 | # We target systems other than Android, so we don't want to use non-standard |
| 96 | # functions from the Android libc. |
| 97 | CHECKS="$CHECKS,-android-cloexec-accept" |
| 98 | CHECKS="$CHECKS,-android-cloexec-fopen" |
| 99 | |
| 100 | # This catches all the feature test macros (_POSIX_SOURCE etc.). |
| 101 | CHECKS="$CHECKS,-bugprone-reserved-identifier" |
| 102 | CHECKS="$CHECKS,-cert-dcl37-c" |
| 103 | CHECKS="$CHECKS,-cert-dcl51-cpp" |
| 104 | |
| 105 | # Too restrictive. This makes sense if the branch clone is very large, but not |
| 106 | # if it's a single line. It can make the code less readable. |
| 107 | CHECKS="$CHECKS,-bugprone-branch-clone" |
| 108 | |
| 109 | # We intentionally send some not null-terminated strings in tests and use it for |
| 110 | # the toxencryptsave magic number. |
| 111 | CHECKS="$CHECKS,-bugprone-not-null-terminated-result" |
| 112 | |
| 113 | # We don't want default labels in enum switches. |
| 114 | CHECKS="$CHECKS,-hicpp-multiway-paths-covered" |
| 115 | |
| 116 | # This can make readability quite a bit worse when the 2 cases look very |
| 117 | # similar. |
| 118 | CHECKS="$CHECKS,-llvm-else-after-return" |
| 119 | CHECKS="$CHECKS,-readability-else-after-return" |
| 120 | |
| 121 | # We need 'return;' in empty functions because cimple won't allow empty |
| 122 | # functions otherwise. |
| 123 | CHECKS="$CHECKS,-readability-redundant-control-flow" |
| 124 | |
| 125 | # These are incredibly annoying, because things like |
| 126 | # uint16_t a = 0, b = 1, c = a > b ? a : b; |
| 127 | # ^ |
| 128 | # Trip the checker, which is true, because of integer promotion, but also not |
| 129 | # very helpful as a diagnostic. |
| 130 | CHECKS="$CHECKS,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions" |
| 131 | |
| 132 | # Mistakenly thinks that |
| 133 | # const int a = 0, b = 1; |
| 134 | # assert(a < b); |
| 135 | # is a constant expression in C (it is in C++ though, which is probably why it's |
| 136 | # mistaken), suggesting to replace 'assert()' with 'static_assert()' in cases |
| 137 | # where that won't work. |
| 138 | # |
| 139 | # There are ways to make 'static_assert()' work, but they are rather annoying -- |
| 140 | # they are somewhat ugly, hurting the readability, and some are error-prone: |
| 141 | # |
| 142 | # - Turning 'a' and 'b' into enum constants would make it work, but this falls |
| 143 | # apart if the enum types are compared against non-enums down the line |
| 144 | # error: enumerated and non-enumerated type in conditional expression [-Werror=extra] |
| 145 | # |
| 146 | # - Turning 'a' and 'b' into pre-processor macros is the only option left, but |
| 147 | # #defines and #undefs in the middle of a function hurt the readability and |
| 148 | # are less idiomatic than simply using 'const int'. |
| 149 | CHECKS="$CHECKS,-cert-dcl03-c,-hicpp-static-assert,-misc-static-assert" |
| 150 | |
| 151 | # Doesn't consider use of preprocessor macros as needing a header, breaking |
| 152 | # struct definitions that depend on size macros from e.g. crypto_core.h. |
| 153 | CHECKS="$CHECKS,-misc-include-cleaner" |
| 154 | |
| 155 | # A bunch of checks only valid for C++, we turn off for C. |
| 156 | # ========================================================= |
| 157 | |
| 158 | # We don't use Google's int typedefs. |
| 159 | CHECKS="$CHECKS,-google-runtime-int" |
| 160 | # We write C code, so we use C arrays. |
| 161 | CHECKS="$CHECKS,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays" |
| 162 | # C loops are ok. This one tells us to use range-for. |
| 163 | CHECKS="$CHECKS,-modernize-loop-convert" |
| 164 | # No auto in C. |
| 165 | CHECKS="$CHECKS,-hicpp-use-auto,-modernize-use-auto" |
| 166 | # Only C style casts in C. |
| 167 | CHECKS="$CHECKS,-cppcoreguidelines-pro-type-cstyle-cast" |
| 168 | # We use malloc (for now), and MISRA checks this too. |
| 169 | CHECKS="$CHECKS,-cppcoreguidelines-no-malloc,-hicpp-no-malloc" |
| 170 | # No owning_ptr<> in C. |
| 171 | CHECKS="$CHECKS,-cppcoreguidelines-owning-memory" |
| 172 | # void foo(void) is good in C. |
| 173 | CHECKS="$CHECKS,-modernize-redundant-void-arg" |
| 174 | # No using-typedefs in C. |
| 175 | CHECKS="$CHECKS,-modernize-use-using" |
| 176 | # No namespaces in C. |
| 177 | CHECKS="$CHECKS,-misc-use-anonymous-namespace" |
| 178 | # No trailing return type in C. |
| 179 | CHECKS="$CHECKS,-modernize-use-trailing-return-type" |
| 180 | # No <cstdint> and friends in C. |
| 181 | CHECKS="$CHECKS,-hicpp-deprecated-headers,-modernize-deprecated-headers" |
| 182 | # We use varargs for logging (we could reconsider, but right now we do). |
| 183 | CHECKS="$CHECKS,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-cert-dcl50-cpp" |
| 184 | # We do want to use the array index operator, even when the index is non-constant. |
| 185 | CHECKS="$CHECKS,-cppcoreguidelines-pro-bounds-constant-array-index" |
| 186 | # We don't want to use pointer arithmetic, but this one includes array index |
| 187 | # operators, which we do want to use. |
| 188 | CHECKS="$CHECKS,-cppcoreguidelines-pro-bounds-pointer-arithmetic" |
| 189 | # Can't use constexpr, yet. C will have it eventually, but it'll be years |
| 190 | # until we can use it across all the compilers. |
| 191 | CHECKS="$CHECKS,-cppcoreguidelines-macro-usage" |
| 192 | # These are all very C++ and/or LLVM specific. |
| 193 | CHECKS="$CHECKS,-llvmlibc-*" |
| 194 | |
| 195 | # We use unions in C, and can't use std::variant or boost::variant. |
| 196 | CHECKS="$CHECKS,-cppcoreguidelines-pro-type-union-access" |
| 197 | |
| 198 | set -eux |
| 199 | |
| 200 | DIRS=( |
| 201 | other/bootstrap_daemon/src |
| 202 | other |
| 203 | toxav |
| 204 | toxcore |
| 205 | toxcore/events |
| 206 | toxencryptsave |
| 207 | ) |
| 208 | |
| 209 | copy_filesscip-shell shell shell-project 0.0.0 copy_files().() { |
| 210 | findscip-shell system . . find. "${DIRS[@]}" \ |
| 211 | -maxdepth 1 -type d -exec mkdir -p "$1/{}" \; |
| 212 | findscip-shell system . . find. "${DIRS[@]}" \ |
| 213 | -maxdepth 1 -name "*.[ch]" -exec cp "{}" "$1/{}" \; |
| 214 | } |
| 215 | |
| 216 | runscip-shell shell shell-project 0.0.0 run().() { |
| 217 | echoscip-shell system . . echo. "Running clang-tidy in variant '$*'" |
| 218 | EXTRA_ARGS=("$@") |
| 219 | for i in "${!EXTRA_ARGS[@]}"; do |
| 220 | EXTRA_ARGS[$i]="--extra-arg=${EXTRA_ARGS[$i]}" |
| 221 | done |
| 222 | lsscip-shell system . . ls. .clang-tidy |
| 223 | copy_filesscip-shell shell shell-project 0.0.0 copy_files(). a |
| 224 | if ! findscip-shell system . . find. "${DIRS[@]}" \ |
| 225 | -maxdepth 1 -name "*.[ch]" -print0 \ |
| 226 | | xarscip-shell system . . xargs.gs -0 -n15 -P"$(nprocscip-shell system . . nproc.)" clang-tidy \ |
| 227 | -p="$PWD/_build" \ |
| 228 | --extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \ |
| 229 | "${EXTRA_ARGS[@]}" \ |
| 230 | --fix \ |
| 231 | --checks="$CHECKS" \ |
| 232 | --warnings-as-errors="$ERRORS" \ |
| 233 | --use-color; then |
| 234 | copy_filesscip-shell shell shell-project 0.0.0 copy_files(). b |
| 235 | colordiff -ru a b |
| 236 | rmscip-shell system . . rm. -rf a b |
| 237 | falsescip-shell system . . false. |
| 238 | fi |
| 239 | rmscip-shell system . . rm. -rf a |
| 240 | } |
| 241 | |
| 242 | cmakescip-shell system . . cmake. . -B_build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 243 | sedscip-shell system . . sed. -i -e 's/-std=c11/-xc++/' _build/compile_commands.json |
| 244 | |
| 245 | . other/analysis/variants.shscip-shell source . . `other/analysis/variants.sh`. |