From 9618d55cc4576fc0cd07f30219b9ad8d6c04c36e Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 17 Oct 2024 16:13:38 -0400 Subject: [PATCH] fix clang-format for recent PRs and new rules (#212) Note that LLVM does not support proper version matching, since the attempt to use it from cmake apparently failed: https://reviews.llvm.org/D99451. --- .github/workflows/main.yml | 258 +++++++++++++++---------------- CMakeLists.txt | 4 +- lib/Target/CBackend/CBackend.cpp | 22 +-- test/cpp_tests/test_dtor.cpp | 4 +- test/test_cbe.py | 1 + 5 files changed, 147 insertions(+), 142 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1967f716..d5fb4259 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,129 +1,129 @@ -name: Build and Test - -on: - workflow_dispatch: - pull_request: - push: - branches: - - "master" - -permissions: - contents: read - -# This allows a subsequently queued workflow run to interrupt previous runs -concurrency: - group: '${{ github.workflow }} @ ${{ github.ref_protected && github.run_id || github.event.pull_request.number || github.ref }}' - cancel-in-progress: true - -jobs: - build: - strategy: - matrix: - include: - - os: ubuntu-latest - - os: macos-latest - runs-on: ${{ matrix.os }} - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Setup LLVM (Linux) - if: ${{ runner.os == 'Linux' }} - run: | - wget https://apt.llvm.org/llvm.sh - chmod +x llvm.sh - sudo ./llvm.sh 19 all - sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-19 160 - sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-19 160 - sudo update-alternatives --install /usr/bin/lli lli /usr/bin/lli-19 160 - sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-19 160 - - - name: Setup LLVM and GCC (MacOS) - if: ${{ runner.os == 'macOS' }} - run: | - brew install llvm@19 - echo "$(brew --prefix llvm@19)/bin" >> $GITHUB_PATH - echo "CC=$(brew --prefix llvm@19)/bin/clang" >> $GITHUB_ENV - echo "CXX=$(brew --prefix llvm@19)/bin/clang++" >> $GITHUB_ENV - cd /usr/local/bin - ln -s gcc-11 gcc - - - name: Update Python - uses: actions/setup-python@v4 - with: - python-version: '3.x' - - - name: Install PyTest - run: | - python -m pip install --upgrade pip - pip install pytest pytest-xdist - - - name: Check formatting - if: ${{ runner.os == 'Linux' }} - run: find . -iname '*.h' -o -iname '*.cpp' | xargs clang-format -Werror --dry-run --style=LLVM --verbose - - - name: Build - run: | - mkdir build - cmake -S . -B build -DLLVM_INCLUDE_TESTS=On -DLLVM_DIR=/usr/lib/llvm-19/cmake - cmake --build build - - - name: Test - run: | - gcc --version - pytest -n 16 - - # The llvm-dev package doesn't exist for Windows, so we need to build LLVM ourselves. - build-windows: - runs-on: windows-latest - env: - SCCACHE_GHA_ENABLED: "true" - steps: - - name: Checkout LLVM - id: checkout-llvm - uses: actions/checkout@master - with: - repository: llvm/llvm-project - ref: llvmorg-19.1.1 - - - name: Checkout - uses: actions/checkout@v3 - with: - path: ${{ github.workspace }}/llvm/projects/llvm-cbe - - - name: Update Python - uses: actions/setup-python@v4 - with: - python-version: '3.x' - - - name: Install PyTest - run: | - python -m pip install --upgrade pip - pip install pytest pytest-xdist - - - name: Initialize LLVM build cache - uses: mozilla-actions/sccache-action@v0.0.3 - - - name: Build - shell: pwsh - run: | - $vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' - $visualStudioInstallationPath = & $vsWhere -latest -property installationPath - Import-Module (Join-Path $visualStudioInstallationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll') - Enter-VsDevShell -VsInstallPath $visualStudioInstallationPath -DevCmdArguments '-arch=x64 -host_arch=x64' - cd ${{ github.workspace }} - if (!(Test-Path '${{ github.workspace }}\build\')) { mkdir '${{ github.workspace }}\build' } - cmake -S llvm -B build -G Ninja -DLLVM_INCLUDE_TESTS=On -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache - cmake --build build - - - name: Test - shell: pwsh - run: | - $vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' - $visualStudioInstallationPath = & $vsWhere -latest -property installationPath - Import-Module (Join-Path $visualStudioInstallationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll') - Enter-VsDevShell -VsInstallPath $visualStudioInstallationPath -DevCmdArguments '-arch=x64 -host_arch=x64' - cd (Join-Path '${{ github.workspace }}' 'llvm\projects\llvm-cbe') - $env:PATH=$env:Path + ";${{ github.workspace }}\build\bin" - $env:LLVMToolDir="${{ github.workspace }}\build\bin" - pytest -n 16 +name: Build and Test + +on: + workflow_dispatch: + pull_request: + push: + branches: + - "master" + +permissions: + contents: read + +# This allows a subsequently queued workflow run to interrupt previous runs +concurrency: + group: '${{ github.workflow }} @ ${{ github.ref_protected && github.run_id || github.event.pull_request.number || github.ref }}' + cancel-in-progress: true + +jobs: + build: + strategy: + matrix: + include: + - os: ubuntu-latest + - os: macos-latest + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup LLVM (Linux) + if: ${{ runner.os == 'Linux' }} + run: | + wget https://apt.llvm.org/llvm.sh + chmod +x llvm.sh + sudo ./llvm.sh 19 all + sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-19 160 + sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-19 160 + sudo update-alternatives --install /usr/bin/lli lli /usr/bin/lli-19 160 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-19 160 + + - name: Setup LLVM and GCC (MacOS) + if: ${{ runner.os == 'macOS' }} + run: | + brew install llvm@19 gcc@14 + echo "$(brew --prefix llvm@19)/bin" >> $GITHUB_PATH + echo "CC=$(brew --prefix llvm@19)/bin/clang" >> $GITHUB_ENV + echo "CXX=$(brew --prefix llvm@19)/bin/clang++" >> $GITHUB_ENV + cd /usr/local/bin + ln -s `which gcc-14` gcc + + - name: Update Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install PyTest + run: | + python -m pip install --upgrade pip + pip install pytest pytest-xdist + + - name: Check formatting + if: ${{ runner.os == 'Linux' }} + run: find . -iname '*.h' -o -iname '*.cpp' | xargs clang-format -Werror --dry-run --style=LLVM --verbose + + - name: Build + run: | + mkdir build + cmake -S . -B build -DLLVM_INCLUDE_TESTS=On -DLLVM_DIR=/usr/lib/llvm-19/cmake + cmake --build build + + - name: Test + run: | + gcc --version + pytest -n 16 + + # The llvm-dev package doesn't exist for Windows, so we need to build LLVM ourselves. + build-windows: + runs-on: windows-latest + env: + SCCACHE_GHA_ENABLED: "true" + steps: + - name: Checkout LLVM + id: checkout-llvm + uses: actions/checkout@master + with: + repository: llvm/llvm-project + ref: llvmorg-19.1.1 + + - name: Checkout + uses: actions/checkout@v3 + with: + path: ${{ github.workspace }}/llvm/projects/llvm-cbe + + - name: Update Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install PyTest + run: | + python -m pip install --upgrade pip + pip install pytest pytest-xdist + + - name: Initialize LLVM build cache + uses: mozilla-actions/sccache-action@v0.0.3 + + - name: Build + shell: pwsh + run: | + $vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' + $visualStudioInstallationPath = & $vsWhere -latest -property installationPath + Import-Module (Join-Path $visualStudioInstallationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll') + Enter-VsDevShell -VsInstallPath $visualStudioInstallationPath -DevCmdArguments '-arch=x64 -host_arch=x64' + cd ${{ github.workspace }} + if (!(Test-Path '${{ github.workspace }}\build\')) { mkdir '${{ github.workspace }}\build' } + cmake -S llvm -B build -G Ninja -DLLVM_INCLUDE_TESTS=On -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache + cmake --build build + + - name: Test + shell: pwsh + run: | + $vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' + $visualStudioInstallationPath = & $vsWhere -latest -property installationPath + Import-Module (Join-Path $visualStudioInstallationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll') + Enter-VsDevShell -VsInstallPath $visualStudioInstallationPath -DevCmdArguments '-arch=x64 -host_arch=x64' + cd (Join-Path '${{ github.workspace }}' 'llvm\projects\llvm-cbe') + $env:PATH=$env:Path + ";${{ github.workspace }}\build\bin" + $env:LLVMToolDir="${{ github.workspace }}\build\bin" + pytest -n 16 diff --git a/CMakeLists.txt b/CMakeLists.txt index 759c0fad..26c09139 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,8 @@ if (NOT DEFINED LLVM_VERSION_MAJOR) project(llvm-cbe) set (USE_SYSTEM_LLVM 1) - cmake_minimum_required(VERSION 3.4.3) - find_package(LLVM 19 REQUIRED CONFIG) + cmake_minimum_required(VERSION 3.20.0) + find_package(LLVM 19.1 REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") message(STATUS "LLVM_INCLUDE_DIRS: ${LLVM_INCLUDE_DIRS}") diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 90f5741c..a23ffa8f 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -1251,7 +1251,8 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) { Out << " >> "; break; case Instruction::ICmp: - switch ((dyn_cast(CE->getAsInstruction()))->getUnsignedPredicate()) { + switch ((dyn_cast(CE->getAsInstruction())) + ->getUnsignedPredicate()) { case ICmpInst::ICMP_EQ: Out << " == "; break; @@ -1286,8 +1287,8 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) { case Instruction::FCmp: { Out << '('; bool NeedsClosingParens = printConstExprCast(CE); - FCmpInst *CmpInst = dyn_cast(CE->getAsInstruction()); - const auto Pred = CmpInst -> getPredicate(); + FCmpInst *CmpInst = dyn_cast(CE->getAsInstruction()); + const auto Pred = CmpInst->getPredicate(); if (Pred == FCmpInst::FCMP_FALSE) Out << "0"; else if (Pred == FCmpInst::FCMP_TRUE) @@ -4645,7 +4646,7 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, cwriter_assert(cast(retT)->getElementType(0) == elemT); Out << " r.field1 = LLVMMul_sov(8 * sizeof(a), &a, &b, &r.field0);\n"; break; - + case Intrinsic::uadd_sat: // r = (a > XX_MAX - b) ? XX_MAX : a + b cwriter_assert(retT == elemT); @@ -4655,7 +4656,7 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, printLimitValue(*elemIntT, false, true, Out); Out << " : a + b;\n"; break; - + case Intrinsic::sadd_sat: // r = (b > 0 && a > XX_MAX - b) ? XX_MAX : a + b; // r = (b < 0 && a < XX_MIN - b) ? XX_MIN : r; @@ -4671,7 +4672,7 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, printLimitValue(*elemIntT, true, false, Out); Out << " : r;\n"; break; - + case Intrinsic::usub_sat: // r = (a < b) ? XX_MIN : a - b; cwriter_assert(retT == elemT); @@ -4679,7 +4680,7 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, printLimitValue(*elemIntT, false, false, Out); Out << " : a - b;\n"; break; - + case Intrinsic::ssub_sat: // r = (b > 0 && a < XX_MIN + b) ? XX_MIN : a - b; // r = (b < 0 && a > XX_MAX + b) ? XX_MAX : r; @@ -4697,7 +4698,8 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, break; case Intrinsic::ushl_sat: - // There's no poison value handler in llvm-cbe yet, so this code don't consider that. + // There's no poison value handler in llvm-cbe yet, so this code don't + // consider that. // r = (a > (XX_MAX >> b)) ? XX_MAX : a << b; cwriter_assert(retT == elemT); Out << " r = (a > ("; @@ -4708,8 +4710,8 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, break; case Intrinsic::sshl_sat: - // (XX_MAX) = 0111... Therfore, shifting this value by b to the right yields the - // maximum/minimum value that can be shifted without overflow. + // (XX_MAX) = 0111... Therfore, shifting this value by b to the right + // yields the maximum/minimum value that can be shifted without overflow. // r = (a >= 0 && a > (XX_MAX >> b)) ? XX_MAX : a << b; // r = (a < 0 && a < ((XX_MAX >> b) | XX_MIN))) ? XX_MIN : r; cwriter_assert(retT == elemT); diff --git a/test/cpp_tests/test_dtor.cpp b/test/cpp_tests/test_dtor.cpp index fc6bbb3f..f6dbc31e 100644 --- a/test/cpp_tests/test_dtor.cpp +++ b/test/cpp_tests/test_dtor.cpp @@ -7,6 +7,8 @@ class Foo { int main() { int y = 0; - { Foo f(&y); } + { + Foo f(&y); + } return y; } diff --git a/test/test_cbe.py b/test/test_cbe.py index 47c53b1f..c48aae26 100644 --- a/test/test_cbe.py +++ b/test/test_cbe.py @@ -30,6 +30,7 @@ '-Wall', '-Wno-unused-function', '-Wno-unused-variable', + '-Wno-uninitialized', '-Werror', ]