Turn clang-tidy warnings and fixes to comments in your pull request

Overview

clang-tidy pull request comments

clang-tidy-8 support clang-tidy-9 support clang-tidy-10 support clang-tidy-11 support clang-tidy-12 support

A GitHub Action to post clang-tidy warnings and suggestions as review comments on your pull request.

action preview

What

platisd/clang-tidy-pr-comments is a GitHub Action that utilizes the exported fixes of clang-tidy for your C++ project and posts them as code review comments in the related pull request.

If clang-tidy has a concrete recommendation on how you should modify your code to fix the issue that's detected, then it will be presented as a suggested change that can be committed directly. Alternatively, the offending line will be highlighted along with a description of the warning.

The GitHub Action can be configured to request changes if clang-tidy warnings are found or merely leave a comment without blocking the pull request from being merged. It should fail only if it has been misconfigured by you, due to a bug (please contact me if that's the case) or the GitHub API acting up.

Please note the following:

  • It will not run clang-tidy for you. You are responsible for doing that and then supply the Action with the path to your generated report (see examples below).
    Specifically, the YAML report that includes the fixes is generated via the -export-fixes argument to the run-clang-tidy utility script. Alternatively, you may use --export-fixes with clang-tidy itself and then, in both cases, specify the path where you would like the report to be exported.
    The very same path should be supplied to the GitHub Action.
  • It will only comment on files and lines changed in the pull request. This is due to GitHub not allowing comments on other files outside the pull request diff. This means that there may be more warnings in your project. Make sure you fix them before starting to use this Action to ensure new warnings will not be introduced in the future.
  • Having the Action leave comments on every update of the pull request may result in spam. Specifically, existing comments are not taken into consideration and will be repeated every time the Action is triggered, as long as the violation has not been amended. If you see this becoming an issue, consider triggering it manually with a comment instead (see example below).

Supported clang-tidy versions

YAML files containing generated fixes by the following clang-tidy versions are currently supported:

  • clang-tidy-8
  • clang-tidy-9
  • clang-tidy-10
  • clang-tidy-11
  • clang-tidy-12

How

Since this action comments on files changed in pull requests, naturally, it can be only run on pull_request events. That being said, if it happens to be triggered in a different context, e.g. a push event, it will not run and fail softly by returning a success code.

A basic configuration for the platisd/clang-tidy-pr-comments action can be seen below:

name: Static analysis

on: pull_request

jobs:
  clang-tidy:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Build project and/or unit tests
        run: ./your-build-script.sh
      - name: Run clang-tidy
        run: ./your-clang-tidy-script.sh --fixes-path fixes.yaml
      - name: Run clang-tidy-pr-comments action
        uses: platisd/clang-tidy-pr-comments@master
        with:
          # The GitHub token (or a personal access token)
          github_token: ${{ secrets.GITHUB_TOKEN }}
          # The path to the clang-tidy fixes generated previously
          clang_tidy_fixes: fixes.yaml
          # Optionally set to true if you want the Action to request
          # changes in case warnings are found
          request_changes: true
          # Optionally set the number of comments per review
          # to avoid GitHub API timeouts for heavily loaded
          # pull requests
          suggestions_per_comment: 10

If you want to trigger the Action manually, i.e. by leaving a comment with a particular keyword in the pull request, then you can try the following:

name: Static analysis

# Don't trigger it on pull_request events but issue_comment instead
on: issue_comment

jobs:
  clang-tidy:
    # Trigger the job only when someone comments: run_clang_tidy
    if: github.event.issue.pull_request && contains(github.event.comment.body, 'run_clang_tidy')
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Build project and/or unit tests
        run: ./your-build-script.sh
      - name: Run clang-tidy
        run: ./your-clang-tidy-script.sh --fixes-path fixes.yaml
      - name: Run clang-tidy-pr-comments action
        uses: platisd/clang-tidy-pr-comments@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          clang_tidy_fixes: fixes.yaml
Comments
  • How to contribute to this project?

    How to contribute to this project?

    Hello, thank you for developing this project! Although I just started to take a look at this, it looks so nice. :smile:

    So I'd like to contribute to this project and I believe I can contribute mainly 3 points. How should I contribute? Could I send PRs for these?

    1. Release Management

    By adding CI scripts like this, if I push a tag, image

    this CI will run, https://github.com/kenji-miyake/clang-tidy-pr-comments/runs/5168564498?check_suite_focus=true

    and a new release is prepared, image

    and after I publish it, the major version tag is created as well. https://github.com/kenji-miyake/clang-tidy-pr-comments/runs/5168578795?check_suite_focus=true image

    Why doing so is here: https://github.com/actions/toolkit/blob/2f164000dcd42fb08287824a3bc3030dbed33687/docs/action-versioning.md

    2. Other CI scripts

    It seems some files are not properly formatted. image

    By installing pre-commit.ci and other tools, this can be improved and it will reduce your maintenance cost.

    Please see this project for more details: https://github.com/autowarefoundation/autoware-github-actions/

    3. Bug investigations/fixes

    While I'm testing this action, it seems I caught a bug. I can investigate the cause and fix it. https://github.com/kenji-miyake/autoware.universe/runs/5168447074?check_suite_focus=true image

    -> clang_tidy_fixes should be under the checked-out workspace?

    opened by kenji-miyake 16
  • Error appending replacements in multi-line comments

    Error appending replacements in multi-line comments

    With #38, suggestions including multiple replacements where tried to be tackled. However, right now, replacements text is only concatenated and the length is being summed, which is a bit too simple, resulting in potentially unusable comments.

    Consider the following example: Screenshot 2022-12-12 at 16 50 26

    Diagnostics:
    - BuildDirectory: /home/runner/work/orbitprofiler/orbitprofiler/build/src/DataViews
      DiagnosticMessage:
        FileOffset: 1269
        FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
        Message: invalid case style for local variable 'resultingColumns_'
        Replacements:
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1269
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1292
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1335
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1420
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1503
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1581
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1663
          ReplacementText: resulting_columns
        - FilePath: /home/runner/work/orbitprofiler/orbitprofiler/src/DataViews/CallstackDataView.cpp
          Length: 17
          Offset: 1762
          ReplacementText: resulting_columns
      DiagnosticName: readability-identifier-naming
      Level: Warning
    MainSourceFile: ''
    
    opened by florian-kuebler 13
  • multiline code-suggestions

    multiline code-suggestions

    Hi,

    Might be that this request is not relevant or that the problem lies somewhere else. We (UltiMaker Cura) recently start using your GitHub action, for our custom-made linting tool. We wrote a small Python module which performs linting on a json file which contains definition for 3D printers used in Cura. Since we have a lot of community contributions we want provide the community contributors with some automatic feedback on their PR's

    The output of this linting tool follows the same scheme as clang-tidy. See the example below:

    Diagnostics:
    -   DiagnosticMessage:
            FileOffset: 238
            FilePath: /home/jspijker/cura_wp/Cura/resources/extruders/koonovo_kn5_extruder_1.def.json
            Message: 'Overriding machine_nozzle_offset_x with the same value (0.0) as defined in parent definition: fdmextruder'
            Replacements:
            -   FilePath: /home/jspijker/cura_wp/Cura/resources/extruders/koonovo_kn5_extruder_1.def.json
                Length: 52
                Offset: 246
                ReplacementText: ''
        DiagnosticName: diagnostic-definition-redundant-override
        Level: Warning
    -   DiagnosticMessage:
            FileOffset: 299
            FilePath: /home/jspijker/cura_wp/Cura/resources/extruders/koonovo_kn5_extruder_1.def.json
            Message: 'Overriding machine_nozzle_offset_y with the same value (0.0) as defined in parent definition: fdmextruder'
            Replacements:
            -   FilePath: /home/jspijker/cura_wp/Cura/resources/extruders/koonovo_kn5_extruder_1.def.json
                Length: 52
                Offset: 307
                ReplacementText: ''
        DiagnosticName: diagnostic-definition-redundant-override
        Level: Warning
    -   DiagnosticMessage:
            FileOffset: 360
            FilePath: /home/jspijker/cura_wp/Cura/resources/extruders/koonovo_kn5_extruder_1.def.json
            Message: 'Overriding machine_nozzle_size with the same value (0.4) as defined in parent definition: fdmextruder'
            Replacements:
            -   FilePath: /home/jspijker/cura_wp/Cura/resources/extruders/koonovo_kn5_extruder_1.def.json
                Length: 48
                Offset: 368
                ReplacementText: ''
    

    However some of our suggested fixes are fixes over multiple lines, which this Action seems to have some trouble with, at least when we use it in conjunction with our custom printer-linter. It could be that the fixes suggested by clang-tidy are always confined to a single line so it isn't an issue for normal usage. But it would really help us if this action handles multi line suggestion comments

    image The suggestion in the image would result in an ill-formed json

    opened by jellespijker 10
  • Wrong suggestions

    Wrong suggestions

    Sometimes I get incorrect suggestions. For example, for this PR the suggestion is:

    -	auto findIteratorFor = [](const InterfaceState* state, const Interface& interface) {
    +	auto findItfind_iterator_forconst InterfaceState* state, const Interface& interface) {
    

    instead of the following one, which is correctly produced by clang-apply-replacements from the provided fixes file:

    -	auto findIteratorFor = [](const InterfaceState* state, const Interface& interface) {
    +	auto find_iterator_for = [](const InterfaceState* state, const Interface& interface) {
    

    Looks like the replacement is shifted by 6 characters in the suggestion.

    opened by rhaschke 5
  • Add release CI workflows

    Add release CI workflows

    As explained in #17,

    • github-release.yaml will prepare the release when a tag is published.
    • actions-tagger.yaml will generate a v1/v2/v3 tag when a release is published.
    opened by kenji-miyake 5
  • Example projects using clang-tidy-pr-comments

    Example projects using clang-tidy-pr-comments

    Great project! Are there any real life example project on github which are using this github action? If yes please add them to the readme it would help developers to decide if they want to use it and promote this tool more. Thanks in advance!

    opened by vkresch 3
  • 1.3.0 Seems to break warning without suggested replacement

    1.3.0 Seems to break warning without suggested replacement

    Just noticed my actions are failing with the following message, my guess is that it breaks when there are no suggested replacements. I will try to make a minimal example if you need it.

    Traceback (most recent call last):
      File "/action/run_action.py", line 4[12], in <module>
        sys.exit(main())
      File "/action/run_action.py", line 268, in main
        suggestion_end = suggestion_begin + diagnostic["ReplacementLength"]
    KeyError: 'ReplacementLength'
    
    opened by Summon528 3
  • Bug: warnings not correctly recognized

    Bug: warnings not correctly recognized

    In this build, clang-tidy found some warnings (and fixes):

    Run cat /home/runner/work/industrial_ci/industrial_ci/.work/clang-tidy-fixes.yml
    Diagnostics:
    - DiagnosticName: readability-identifier-naming
      FileOffset: 26
      FilePath: /home/runner/work/industrial_ci/industrial_ci/.work/target_ws/src/test_clang_tidy/src/test_clang_tidy.cpp
      Message: invalid case style for variable 'a'
      Replacements:
      - {FilePath: /home/runner/work/industrial_ci/industrial_ci/.work/target_ws/src/test_clang_tidy/src/test_clang_tidy.cpp,
        Length: 1, Offset: 26, ReplacementText: A}
    MainSourceFile: ''
    

    but the action reports: No warnings found in files changed in this pull request

    opened by rhaschke 3
  • Configuration examples not workng

    Configuration examples not workng

    To start using clang-tidy-pr-comments in a project, I copied the example code in "Using this Action to safely perform analysis of pull requests from forks". I kept getting fatal git errors in git diff -U0 HEAD^.

    I found a solution that I hope will be added to the examples to help others. action/checkout@v2 (and v3) can use fetch-depth: 2 to enable enough context for git diff to work.

    opened by skrobinson 3
  • A more strict regex extracting diff info is required

    A more strict regex extracting diff info is required

    One of my test failed with an index out of range error (see logs here). The patches include changes on something like this:

    +    // @@@@@@@
    +    // test
    +    // @@@@@@@
    +    /*
    

    And the regex at https://github.com/platisd/clang-tidy-pr-comments/blob/6f016d02636cd7122e972a64220efd8769f9568a/run_action.py#L135-L137 matches this pattern, not giving out diff info, resulting in error. I suggest replacing it with a more strict regex match, maybe like @@ -\d+,\d+ \+\d+,\d+ @@.

    opened by caic99 3
  • Failed to match codes between repo and `fixes.yml`.

    Failed to match codes between repo and `fixes.yml`.

    In my action, the running log shows that "No warnings found in files changed in this pull request". This error also happened in #17 .

    @kenji-miyake All file paths in your fixes.yaml begin with /__w/... (probably because clang-tidy is running self-hosted) while this action expects the GitHub directory layout (/home/runner/...), so it can't normalize paths and is not able to find all these files from your PR. Originally posted by @oleg-derevenetz in https://github.com/platisd/clang-tidy-pr-comments/issues/17#issuecomment-1038393661

    Thanks @oleg-derevenetz points out that there is another file system in a custom actions container.

    I wonder is there a proper way to avoid modifications on fixes.yaml? Maybe GitHub environment variable GITHUB_WORKSPACE helps, creating a symlink from from /home/runner/... to /__w/... .

    opened by caic99 3
  • Ambiguous expression in example workflow config

    Ambiguous expression in example workflow config

    https://github.com/platisd/clang-tidy-pr-comments/blob/10dea67599cd21bff5391960fe080aa6fd3a164b/README.md?plain=1#L114-L117

    Here, the comment says this is "the number of comments per review", but the variable name gives us "suggestions per comment". I suppose the latter one is actually what you mean.

    opened by caic99 1
  • Support Checks API

    Support Checks API

    It would be cool if instead of regular comments, the fixes could be suggested as part of the Checks API.

    There's a more informative picture here: https://docs.github.com/en/developers/apps/guides/creating-ci-tests-with-the-checks-api#step-25-updating-the-check-run-with-ci-test-results

    opened by pqn 2
Releases(1.3.2)
Owner
Dimitris Platis
Software Engineer & Maker. Currently with a special interest in the fields of Embedded Systems, Internet of things and Robotics.
Dimitris Platis
Fixes mojibake and other glitches in Unicode text, after the fact.

ftfy: fixes text for you >>> print(fix_encoding("(ง'⌣')ง")) (ง'⌣')ง Full documentation: https://ftfy.readthedocs.org Testimonials “My life is li

Luminoso Technologies, Inc. 3.4k Dec 29, 2022
Fixes mojibake and other glitches in Unicode text, after the fact.

ftfy: fixes text for you >>> print(fix_encoding("(ง'⌣')ง")) (ง'⌣')ง Full documentation: https://ftfy.readthedocs.org Testimonials “My life is li

Luminoso Technologies, Inc. 2.9k Feb 17, 2021
Prithivida 690 Jan 4, 2023
Fidibo.com comments Sentiment Analyser

Fidibo.com comments Sentiment Analyser Introduction This project first asynchronously grab Fidibo.com books comment data using grabber.py and then sav

Iman Kermani 3 Apr 15, 2022
Generate product descriptions, blogs, ads and more using GPT architecture with a single request to TextCortex API a.k.a Hemingwai

TextCortex - HemingwAI Generate product descriptions, blogs, ads and more using GPT architecture with a single request to TextCortex API a.k.a Hemingw

TextCortex AI 27 Nov 28, 2022
A python script to prefab your scripts/text files, and re create them with ease and not have to open your browser to copy code or write code yourself

Scriptfab - What is it? A python script to prefab your scripts/text files, and re create them with ease and not have to open your browser to copy code

DevNugget 3 Jul 28, 2021
This simple Python program calculates a love score based on your and your crush's full names in English

This simple Python program calculates a love score based on your and your crush's full names in English. There is no logic or reason in the calculation behind the love score. The calculation could have been anything different from what's shown in this code.

p.katekomol 1 Jan 24, 2022
Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code.

textgenrnn Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code, or quickly tr

Max Woolf 4.8k Dec 30, 2022
Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code.

textgenrnn Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code, or quickly tr

Max Woolf 4.3k Feb 18, 2021
Share constant definitions between programming languages and make your constants constant again

Introduction Reconstant lets you share constant and enum definitions between programming languages. Constants are defined in a yaml file and converted

Natan Yellin 47 Sep 10, 2022
RIDE automatically creates the package and boilerplate OOP Python node scripts as per your needs

RIDE: ROS IDE RIDE automatically creates the package and boilerplate OOP Python code for nodes as per your needs (RIDE is not an IDE, but even ROS isn

Jash Mota 20 Jul 14, 2022
simpleT5 is built on top of PyTorch-lightning⚡️ and Transformers🤗 that lets you quickly train your T5 models.

Quickly train T5 models in just 3 lines of code + ONNX support simpleT5 is built on top of PyTorch-lightning ⚡️ and Transformers ?? that lets you quic

Shivanand Roy 220 Dec 30, 2022
A linter to manage all your python exceptions and try/except blocks (limited only for those who like dinosaurs).

Manage your exceptions in Python like a PRO Currently in BETA. Inspired by this blog post. I shared the building process of this tool here. “For those

Guilherme Latrova 353 Dec 31, 2022
This project converts your human voice input to its text transcript and to an automated voice too.

Human Voice to Automated Voice & Text Introduction: In this project, whenever you'll speak, it will turn your voice into a robot voice and furthermore

Hassan Shahzad 3 Oct 15, 2021
Proquabet - Convert your prose into proquints and then you essentially have Vogon poetry

Proquabet Turn your prose into a constant stream of encrypted and meaningless-so

Milo Fultz 2 Oct 10, 2022
Understand Text Summarization and create your own summarizer in python

Automatic summarization is the process of shortening a text document with software, in order to create a summary with the major points of the original document. Technologies that can make a coherent summary take into account variables such as length, writing style and syntax.

Sreekanth M 1 Oct 18, 2022
Write Alphabet, Words and Sentences with your eyes.

The-Next-Gen-AI-Eye-Writer The Eye tracking Technique has become one of the most popular techniques within the human and computer interaction era, thi

Rohan Kasabe 2 Apr 5, 2022