name: "Code analysis (clang)"

on:
  push:
    branches:
    - 'dev'
    - 'stable'
  pull_request:
    branches:
    - 'dev'
    - 'release-*'

jobs:
  build:
    runs-on: ubuntu-20.04
    steps:
    - name: Checkout repository
      uses: actions/checkout@v2
      with:
          submodules: recursive

    - name: Update package database
      run: sudo apt --assume-yes update

    - name: Install wget
      run: sudo apt --assume-yes install wget

    - name: Install automatic llvm (stable branch)
      run: sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

    - name: Install clang-tools-11
      run: sudo apt --assume-yes install clang-tools-11

    - name: Install python
      run: sudo apt --assume-yes install python3-wheel python3-setuptools

    - name: Install meson ninja
      run: pip3 install --user meson ninja

    - name: Install git
      run: sudo apt --assume-yes install git

    - name: Install npm
      run: sudo apt --assume-yes install npm

    - name: Install microsoft/sarif-multitool
      run: npm install @microsoft/sarif-multitool

    - name: Analysis
      run: |
        export PATH=${HOME}/Library/Python/3.8/bin:${HOME}/Library/Python/3.9/bin:${HOME}/.local/bin:${PATH}
        scan-build-11 -v -sarif -o reports "meson build && meson compile -C build"

    - name: Exclude external lib
      run: |
        grep -Rl 'rizin/shlr/capstone' reports | xargs rm -fv
        grep -Rl 'rizin/shlr/sdb' reports | xargs rm -fv
        grep -Rl 'rizin/shlr/tree-sitter' reports | xargs rm -fv

    - name: Merge all files
      run: npx @microsoft/sarif-multitool merge reports/**/*.sarif

    - name: Upload SARIF file
      uses: github/codeql-action/upload-sarif@v1
      with:
        sarif_file: merged.sarif
