Skip to content

Enable clang-tidy in CI #12213

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

Closed
wants to merge 2 commits into from
Closed

Conversation

goldsborough
Copy link
Contributor

@goldsborough goldsborough commented Sep 30, 2018

At long last, we will have clang-tidy enabled in CI. For a while I thought I could clean up the project enough to enable clang-tidy with all checks enabled, but I figure it's smarter to set up the minimal checks and at least have those in CI. We can fix more going forward.

@ezyang @apaszke

@@ -29,3 +29,18 @@ matrix:
- env: CPP_DOC_CHECK
install: sudo apt-get install -y doxygen
script: cd docs/cpp/source && ./check-doxygen.sh
- env: CLANG_TIDY

This comment was marked as off-topic.

This comment was marked as off-topic.

.travis.yml Outdated
- git submodule update --init --recursive
- mkdir clang-tidy-build
- cd clang-tidy-build
- cmake -DBUILD_TORCH=ON ..

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@ezyang
Copy link
Contributor

ezyang commented Oct 1, 2018

The job doesn't seem to be failing on errors. On https://travis-ci.org/pytorch/pytorch/jobs/435471449 I see:

1.33s$ python tools/clang_tidy.py -c "$(which clang-tidy-6.0)" -d clang-tidy-build -p torch/csrc -r HEAD~1
/home/travis/build/pytorch/pytorch/torch/csrc/jit/interned_strings.h:9:10: error: 'torch/csrc/jit/generated/aten_interned_strings.h' file not found [clang-diagnostic-error]
#include "torch/csrc/jit/generated/aten_interned_strings.h"

@@ -10,6 +10,8 @@ if (NOT MSVC)
set(CMAKE_C_STANDARD 11)
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

CONTRIBUTING.md Outdated
how to install the latest version. Note that newer versions of clang-tidy will
have more checks than older versions. In our CI, we run clang-tidy-6.0.

2. Use our driver script to run clang-tidy over your git revision (you may want

This comment was marked as off-topic.

This comment was marked as off-topic.

$ python tools/clang_tidy.py -d /path/to/build/ -p torch/csrc -r HEAD~1
```

Above, it is assumed you are in the PyTorch root folder. `path/to/build` should

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

// NB: Must be at the top of file to avoid including the deprecated "math.h".
// https://stackoverflow.com/questions/6563810/m-pi-works-with-math-h-but-not-with-cmath-in-visual-studio
#ifdef _MSC_VER
#define _USE_MATH_DEFINES

This comment was marked as off-topic.

This comment was marked as off-topic.

@@ -8,28 +8,35 @@
import sys


DEFAULT_FILE_PATTERN = r".*\.[ch](pp)?"
DEFAULT_FILE_PATTERN = r".*\.c(c|pp)?"

This comment was marked as off-topic.

This comment was marked as off-topic.

"""Executes a shell command."""
assert len(arguments) > 0
try:
output = subprocess.check_output(arguments, stderr=subprocess.STDOUT)

This comment was marked as off-topic.

This comment was marked as off-topic.

@@ -7,6 +7,7 @@ else()
project(torch CXX C)
find_package(Caffe2 REQUIRED)
option(USE_CUDA "Use CUDA" ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

This comment was marked as off-topic.

This comment was marked as off-topic.

output = subprocess.check_output(arguments, stderr=subprocess.STDOUT)
except OSError:
_, e, _ = sys.exc_info()
if verbose:

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

process_name = process_name or arguments[0]
raise RuntimeError("Error executing {}: {}".format(process_name, e))
_, error, _ = sys.exc_info()
message = "Error executing '{}'".format(process_name)

This comment was marked as off-topic.

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

I have quibbles about how the Python script is written, but I'm requesting changes here because it doesn't seem like it is actually flagging errors as failures in CI.

@goldsborough
Copy link
Contributor Author

What you're seeing are clang compiler errors (clang-diagnostic-error). Those are because I don't do a full build of PyTorch since that would take too long in CI. I only generate the .compile_commands.json and clang-tidy will happily lint 99% of code that it can parse and compile. The clang diagnostics can be ignored. It means we're not linting generated files, but I think that's fine. In any case, there is nothing broken here, it's expected. When there is an actual lint error (which I have verified of course), those will make the CI error.

@goldsborough
Copy link
Contributor Author

Now that I think of it, I can just run our gen.py script to generate the missing files. I also do that in our doc CI build. In any case the lint job will take 2 minutes and not 20 minutes or however long it takes to build PyTorch

@goldsborough goldsborough force-pushed the clang-tidy-ci branch 3 times, most recently from f94f3b9 to 5564d8c Compare October 1, 2018 17:12
should be able to install it easily with your package manager, e.g. by writing
`apt-get install clang-tidy` on Ubuntu. See https://apt.llvm.org for details on
how to install the latest version. Note that newer versions of clang-tidy will
have more checks than older versions. In our CI, we run clang-tidy-6.0.

This comment was marked as off-topic.

This comment was marked as off-topic.


set -ex

if [[ ! -d build ]]; then

This comment was marked as off-topic.

This comment was marked as off-topic.

popd

# Generate ATen files.
time python aten/src/ATen/gen.py \

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@@ -8,28 +8,35 @@
import sys

This comment was marked as off-topic.

This comment was marked as off-topic.

@ezyang
Copy link
Contributor

ezyang commented Oct 3, 2018

CR on clang_tidy.py

@ezyang ezyang closed this Oct 3, 2018
@ezyang ezyang reopened this Oct 3, 2018
@ezyang
Copy link
Contributor

ezyang commented Oct 3, 2018

(sorry, accidentally closed the PR with an incorrect comment, please disregard)

@ezyang
Copy link
Contributor

ezyang commented Oct 3, 2018

Message me if you want to chat more about the verbose output situation. I want to understand what's going on here.

Move code into script
@goldsborough
Copy link
Contributor Author

goldsborough commented Oct 3, 2018

Hard to respond to your comments directly this way. I'll fix the code you commented on. I think the only open ended question was "why pass the .yaml file to clang-tidy". Believe me, I've spent many hours fighting clang tidy because it wouldn't pick up my .clang-tidy file. It seems a thing about different clang tidy versions. There's unanswered stackoverflow questions about this too. The best way to ensure that all the config gets picked up is to pass it in directly the way I do.

I'll see how hard it is to create a cmake target to generate the files. I think it's more important to have clang tidy in CI as soon as possible than for this to be 100% clean. We could literally already be finding tons of bugs right now

@goldsborough
Copy link
Contributor Author

No, please don't do this. If you pass the base directory, this will walk recursively into the build directory and .git and grab you all of your build products too. Even if you use it on a subdirectory, it will still grab any build products. Use git-ls-files to get an accurate listing of all files in Git which are checked into source control and can be profitably linted.

Why would you pass the base directory? This gets e.g. torch/csrc as its argument, that would certainly only include source files?

@ezyang
Copy link
Contributor

ezyang commented Oct 3, 2018

Believe me, I've spent many hours fighting clang tidy because it wouldn't pick up my .clang-tidy file. It seems a thing about different clang tidy versions. There's unanswered stackoverflow questions about this too. The best way to ensure that all the config gets picked up is to pass it in directly the way I do.

That's fine, but then it should be commented in the code :)

Why would you pass the base directory? This gets e.g. torch/csrc as its argument, that would certainly only include source files?

I've seen too many programs written using os.walk (it worked fine on my machine!) blow up to be that sympathetic to this argument :) It should work without weird performance cliffs in all cases, or we shouldn't provide it at all.

@ezyang
Copy link
Contributor

ezyang commented Oct 3, 2018

In terms of priorities, the main things I want to see fixed are:

  • Removal of footguns from the Python script (this is mostly from the perspective of, this kind of Python code tends to replicate itself in the codebase if let alone for a while, and I don't want the footguns replicating themselves.)
  • Some resolution on the issue where clang-tidy will silently choke if compile commands are busted without erroring. I'm willing to be convinced this is not a problem in practice, but we've had multiple cases in the past where we silently stopped testing code and if there is something easy we can do now to prevent this from happening, I'd prefer it.

Everything else is non-essential.

@goldsborough goldsborough force-pushed the clang-tidy-ci branch 2 times, most recently from b979fea to 048e9bb Compare October 3, 2018 21:57
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

goldsborough has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

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.

3 participants