Code Monkey home page Code Monkey logo

clang-tidy-division-by-non-constant's Introduction

clang-tidy: division by non-constant

A Clang tidy that finds all divisions by non-constant values in your C/C++ codebase. It's useful for hunting down possible runtime division-by-zero exceptions if you haven't been proactive about guarding them in your codebase before. Many thanks to Kristen Shaker for her wonderful talk explaining how to do this, go check it out!

Usage

This tidy is distributed as a patch file against llvm-project commit 0c423af59c971ddf1aa12d94529edf8293608157 although it should work against future commits too.

Execute the following commands to apply it and build a custom version of clang-tidy:

git clone --depth 1 "https://github.com/llvm/llvm-project"
cd llvm-project
patch -p0 < /path/to/clang-tidy-division-by-non-constant/division-by-non-constant.patch
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm
make -j$(nproc) clang-tidy

Then to use it on your CMake-based project:

export PATH=/path/to/llvm-project/build/bin:$PATH
cmake -DCMAKE_C_CLANG_TIDY="clang-tidy;-checks=bugprone-division-by-non-constant" -DCMAKE_CXX_CLANG_TIDY="clang-tidy;-checks=bugprone-division-by-non-constant" .
make -j$(nproc)

Known Issues

The tidy can be overzealous when the right-hand side of the division operation involves function calls, for example:

static const int x = 2;

int y = 4 / pow(x, 2);

This will be registered as a division by a non-constant value even though the right-hand side is constant in practice.

The tidy is underzealous when the right-hand side of the division operation is a run-time constant, for example:

int get_from_user()
{
    int x;

    cin >> x;
    
    return x;
}

int main()
{
    const int x = get_from_user();

    int y = 4 / x;

    return 0;
}

This will not be registered by the tidy, even though the right-hand side is variable at runtime and this may result in a division-by-zero.

As far as I know it isn't possible to fix either of these in a tidy: the abstract syntax tree simply doesn't contain enough information to determine true compile-time constness. A possible future project would be to implement this project as a Clang static analysis checker which does have that power.

Credit & License

Developed by Amini Allight and licensed under the Apache License 2.0 with LLVM exceptions, matching the license used by the LLVM project itself.

clang-tidy-division-by-non-constant's People

Contributors

amini-allight avatar

Stargazers

Antonio Gonzalez  avatar ConfuSomu avatar Aeva & Watch Gallery 2 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.