Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create clang-tidy CI #12653

Merged
merged 16 commits into from
Sep 30, 2022
Merged

Create clang-tidy CI #12653

merged 16 commits into from
Sep 30, 2022

Conversation

justinchuby
Copy link
Contributor

@justinchuby justinchuby commented Aug 19, 2022

Update clang-tidy config to prepare for creating a CI workflow to run clang-tidy.
Added clangtidy check in CI

.clang-tidy Outdated
@@ -1,6 +1,6 @@
---
# turn off readability-braces-around-statements to allow single line statement like 'if (x == y) doSomething();'
Checks: '-*,cppcoreguidelines-*,google-*,readability-*,modernize-*,-readability-braces-around-statements,-google-runtime-references,-cppcoreguidelines-pro-type-reinterpret-cast'
Checks: '-*,cppcoreguidelines-*,google-*,readability-*,modernize-*,bugprone-*,-readability-braces-around-statements,-google-runtime-references,-cppcoreguidelines-pro-type-reinterpret-cast'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.clang-tidy file is a yaml file literally, if desired, you can actually make it mulit-line by using a yaml multiple line string, and clang-tidy itself happens to tolerate the \n tokens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for the suggestion. Do you have any thoughts on the kind of checks we should enable? Discussion happened in #12655 but was inconclusive.

@snnn @edgchen1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I've passed this question along to the core team.

Copy link
Contributor

@cloudhan cloudhan Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinchuby I don't have suggestion to enable more checks, instead, I'd suggest to disable more.

  1. modernize-concat-nested-namespaces, becaure we don't use it.
  2. cppcoreguidelines-macro-usage, there are a lot of false-positives like

    Function-like macro 'Foo' used; consider a 'constexpr' template function

  3. readability-identifier-length, clang-tidy is bullshitting here, there are a lot of numerical code rely on short names to imporve readability.
  4. google-readability-todo, Missing username/bug in TODO, which I am not sure we are practicing.
  5. cppcoreguidelines-pro-bounds-array-to-pointer-decay Do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead. But our macros like ORT_RETURN_IF and LOGS are relying on it. We shall fix it, either way.

hint, onnxruntime/core/session/inference_session.cc is full of warning in current setting xDDD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloudhan I added all except for cppcoreguidelines-pro-bounds-array-to-pointer-decay, because it does seem desired from my understanding of your explanation, even though some of our macros have this behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed -google-readability-braces-around-statements because google-readability-braces-around-statements.ShortStatementLines is set to 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is OK to keep cppcoreguidelines-pro-bounds-array-to-pointer-decay, it has its purpose. But I think someone should fix it from source code level. And if we add the magic comment to disable it at the macro definition side, I am not sure if it has effect on macro application site.

@justinchuby justinchuby marked this pull request as ready for review September 20, 2022 14:48
@edgchen1
Copy link
Contributor

https://github.com/microsoft/onnxruntime/actions/runs/3092428102/jobs/5003682677

looking at the build output, it appears that there are no files checked. perhaps the filter should be adjusted?

Run ZedThree/[email protected]

...

Diff from GitHub PR:
[<PatchedFile: .clang-tidy>, <PatchedFile: .github/workflows/lint.yml>]

include: *.[ch], file list now: []
include: *.[ch]xx, file list now: []
include: *.[ch]pp, file list now: []
include: *.[ch]++, file list now: []
include: *.cc, file list now: []
include: *.hh, file list now: []
exclude: , file list now: []
No files to check!

@justinchuby
Copy link
Contributor Author

@edgchen1 I think that's because the lint is only checking diffs!

@edgchen1
Copy link
Contributor

@edgchen1 I think that's because the lint is only checking diffs!

ah ok, that makes sense.

as a side note, we'll probably need to generate a compile_commands.json file to configure clang-tidy properly.

@justinchuby
Copy link
Contributor Author

as a side note, we'll probably need to generate a compile_commands.json file to configure clang-tidy properly.

Absolutely. I haven't figured out how to do that though. @snnn any suggestions?

@edgchen1
Copy link
Contributor

as a side note, we'll probably need to generate a compile_commands.json file to configure clang-tidy properly.

Absolutely. I haven't figured out how to do that though. @snnn any suggestions?

may be able to refer to this as a starting point:

- script: |
python tools/ci_build/build.py \
--build_dir "$(Build.BinariesDirectory)" \
--cmake_generator "Ninja" \
--config Debug \
--build_shared_lib --use_coreml --build_objc \
--cmake_extra_defines CMAKE_EXPORT_COMPILE_COMMANDS=ON \
--update \
--build --parallel
displayName: Generate compile_commands.json

a few notes:

  • --build_objc is probably unnecessary. and --use_coreml enabling the CoreML EP may be as well. that pipeline is specifically analyzing the Objective-C code.
  • it is not ideal as it does a full build of ORT. I think that was done to generate the ONNX protobuf source files. it certainly could be done more efficiently, perhaps by building the corresponding cmake target directly.

also, not sure which EPs we should enable for this. I suppose we can try with only the CPU EP first.

@cloudhan
Copy link
Contributor

Use clangd as C++ language server gives you a uniform editing experience span C/C++ and CUDA and HIP code. clang-tidy and clang-format is also integrated. All you need is the compile_commands.json.

.clang-tidy Outdated Show resolved Hide resolved
Copy link
Contributor Author

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed comments

cloudhan
cloudhan previously approved these changes Sep 21, 2022
Copy link
Contributor

@cloudhan cloudhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will greatly improve my code editing experience. Thanks!

@justinchuby
Copy link
Contributor Author

justinchuby commented Sep 21, 2022

Current command

python tools/ci_build/build.py --build_dir build --update --cmake_extra_defines CMAKE_EXPORT_COMPILE_COMMANDS=ON

@justinchuby
Copy link
Contributor Author

Strange: https://github.com/microsoft/onnxruntime/actions/runs/3099926945/jobs/5019640574#step:8:39

clang-tidy failed with return code 134 and error:
LLVM ERROR: Cannot chdir into "/home/runner/work/onnxruntime/onnxruntime/build/Debug"!

uses: ZedThree/clang-tidy-review@526cbfb043719639f1ebdeedae0cc1eacd219d8f
with:
token: ${{ secrets.github_token }}
build_dir: "build/Debug/compile_commands.json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be the compile_commands.json directory, "build/Debug"?
https://github.com/ZedThree/clang-tidy-review#inputs

Copy link
Contributor Author

@justinchuby justinchuby Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no wonder 😅 . thanks!

@edgchen1
Copy link
Contributor

Strange: https://github.com/microsoft/onnxruntime/actions/runs/3099926945/jobs/5019640574#step:8:39

clang-tidy failed with return code 134 and error: LLVM ERROR: Cannot chdir into "/home/runner/work/onnxruntime/onnxruntime/build/Debug"!

wonder if setting base_dir is needed since compile_commands.json is generated outside of the action:
https://github.com/ZedThree/clang-tidy-review#use-in-a-non-default-location

@justinchuby justinchuby changed the title Create clang-tidy lint Update clang-tidy config Sep 23, 2022
@justinchuby justinchuby changed the title Update clang-tidy config Create clang-tidy CI Sep 23, 2022
@justinchuby
Copy link
Contributor Author

@edgchen1 @cloudhan should

modernize-use-nodiscard be disabled? Otherwise this is ready to merge.

image

@edgchen1
Copy link
Contributor

@edgchen1 @cloudhan should

modernize-use-nodiscard be disabled? Otherwise this is ready to merge.

image

I can see the merits of using [[nodiscard]] more. However, we don't use it much in our existing code. @pranavsharma what do you think?

@pranavsharma
Copy link
Contributor

@edgchen1 @cloudhan should
modernize-use-nodiscard be disabled? Otherwise this is ready to merge.
image

I can see the merits of using [[nodiscard]] more. However, we don't use it much in our existing code. @pranavsharma what do you think?

I think it'll be good to enable this. I've seen before that we ignored Status returns from some functions.

@cloudhan
Copy link
Contributor

nondiscard is great. Better to keep it on and fix them from the code side.

@justinchuby
Copy link
Contributor Author

@edgchen1 PTAL

edgchen1
edgchen1 previously approved these changes Sep 28, 2022
edgchen1
edgchen1 previously approved these changes Sep 28, 2022
@edgchen1
Copy link
Contributor

thanks for setting this up!

--build_dir build \
--update \
--cmake_extra_defines CMAKE_EXPORT_COMPILE_COMMANDS=ON
- name: Generate ONNX protobuf files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I just found out about the build.py --target option. so this could possibly be done in the build.py command above, e.g., build.py ... --build --parallel --target onnx_proto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! For the sake of avoiding running another day of CI tests I merged as is.

@cloudhan
Copy link
Contributor

@justinchuby Just found a new common annoying case:

Floating point literal has suffix 'f', which is not uppercase (fix available)clang-tidy(readability-uppercase-literal-suffix)

I think this should also be disabled.

@justinchuby
Copy link
Contributor Author

@cloudhan PTAL

@justinchuby justinchuby merged commit 402e199 into main Sep 30, 2022
@justinchuby justinchuby deleted the justinchu/clang-tidy branch September 30, 2022 15:05
linnealovespie pushed a commit that referenced this pull request Sep 30, 2022
Update clang-tidy config to prepare for creating a CI workflow to run
clang-tidy.
Added clangtidy check in CI

Co-authored-by: Edward Chen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants