|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright (C) 2026 Airbus Defence and Space Romania SRL. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +# Prerequisites: clang-tidy and cppcheck (First time install: sudo apt-get update && sudo apt-get install -y clang-tidy cppcheck) |
| 7 | +# Prerequisite for clang-tidy: CMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 8 | +rm -rf product-mini/platforms/linux/build && mkdir -p product-mini/platforms/linux/build |
| 9 | +cd product-mini/platforms/linux/build |
| 10 | +cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && make -j8 |
| 11 | +cd ../../../../ |
| 12 | + |
| 13 | +# Define the target folders |
| 14 | +TARGET_DIRS="core/iwasm/interpreter \ |
| 15 | + core/iwasm/common/component-model \ |
| 16 | + tests/unit/component*" |
| 17 | + |
| 18 | +# Define the log report file |
| 19 | +REPORT_FILE="code_analysis_report.log" |
| 20 | +echo "--- Running code analysis for: $TARGET_DIRS" |
| 21 | +echo "--- The analysis report will be written into $REPORT_FILE" |
| 22 | + |
| 23 | +: > "$REPORT_FILE" |
| 24 | +exec > "$REPORT_FILE" 2>&1 |
| 25 | + |
| 26 | +echo "--- Running Cppcheck ---" |
| 27 | +cppcheck -q --force --enable=warning,performance,style,portability $TARGET_DIRS |
| 28 | + |
| 29 | +echo "--- Running Clang-Tidy ---" |
| 30 | +FILES=$(find $TARGET_DIRS -name "*.c") |
| 31 | +HEADERS=$(find $TARGET_DIRS -name "*.h") |
| 32 | +$(command -v /opt/wasi-sdk/bin/clang-tidy || echo clang-tidy) -p product-mini/platforms/linux/build --quiet $HEADERS $FILES |
| 33 | + |
| 34 | +TOTAL=$(grep -c ': error:' "$REPORT_FILE" 2>/dev/null || echo 0) |
| 35 | +printf "Total errors: $TOTAL" |
| 36 | + |
| 37 | +for CHECK in \ |
| 38 | + bugprone-narrowing-conversions \ |
| 39 | + bugprone-multi-level-implicit-pointer-conversion \ |
| 40 | + bugprone-null-dereference \ |
| 41 | + bugprone-use-after-move \ |
| 42 | + bugprone-sizeof-expression \ |
| 43 | + clang-analyzer-core \ |
| 44 | + clang-analyzer-security \ |
| 45 | + clang-analyzer-deadcode \ |
| 46 | + cppcoreguidelines-init-variables \ |
| 47 | + cppcoreguidelines-narrowing-conversions \ |
| 48 | + performance-no-int-to-ptr \ |
| 49 | + readability-math-missing-parentheses \ |
| 50 | + concurrency-mt-unsafe \ |
| 51 | + cert-msc30-c \ |
| 52 | + cert-err34-c \ |
| 53 | + readability-redundant-casting; do |
| 54 | + COUNT=$(grep -c "$CHECK" "$REPORT_FILE" 2>/dev/null || echo 0) |
| 55 | + if [ "$COUNT" -gt 0 ]; then |
| 56 | + printf ' %-56s %d\n' "$CHECK:" "$COUNT" | tee -a "$REPORT_FILE" |
| 57 | + fi |
| 58 | +done |
| 59 | + |
| 60 | +echo "--- Analysis complete. Check $REPORT_FILE ---" |
0 commit comments