This is an archive of the discontinued LLVM Phabricator instance.

Teach clang-tidy how to -Werror checks.
ClosedPublic

Authored by alexfh on Dec 15 2015, 7:50 AM.

Details

Diff Detail

Event Timeline

jroelofs updated this revision to Diff 42856.Dec 15 2015, 7:50 AM
jroelofs retitled this revision from to Teach clang-tidy how to -Werror checks..
jroelofs updated this object.
jroelofs added a reviewer: aaron.ballman.
jroelofs added a subscriber: cfe-commits.
aaron.ballman accepted this revision.Dec 15 2015, 8:38 AM
aaron.ballman added a reviewer: alexfh.

Aside from two tiny nits, LGTM. Thank you for working on this, it's a great feature! I think we should add something to the release notes to make sure it gets called out.

tools/llvm-project/extra/clang-tidy/ClangTidy.cpp
156

Method can be const.

tools/llvm-project/extra/test/clang-tidy/werrors-plural.cpp
7

One of these tests should complete the -Werrors= chunk just to be sure it's getting printed properly.

This revision is now accepted and ready to land.Dec 15 2015, 8:38 AM
jroelofs added inline comments.Dec 15 2015, 8:46 AM
tools/llvm-project/extra/clang-tidy/ClangTidy.cpp
119

hardcoding happens here ^

156

ok

tools/llvm-project/extra/test/clang-tidy/werrors-plural.cpp
7

Not sure what you mean here. Should I be printing the -Werrors argument as the user wrote it? (it isn't currently... this is just a hardcoded string).

aaron.ballman added inline comments.Dec 15 2015, 8:50 AM
tools/llvm-project/extra/test/clang-tidy/werrors-plural.cpp
7

Ah, I didn't pick up on that. What is the = for, then?

alexfh requested changes to this revision.Dec 15 2015, 9:33 AM
alexfh edited edge metadata.

Jonathan, can you explain what specific use case does this patch address? Why one severity level of native clang-tidy warnings (the current situation) is not enough, and two levels are enough?

This revision now requires changes to proceed.Dec 15 2015, 9:33 AM

Jonathan, can you explain what specific use case does this patch address? Why one severity level of native clang-tidy warnings (the current situation) is not enough, and two levels are enough?

I have out-of-tree checkers for a very strange out-of-tree target. Some of the checks are on the level of "this should break the build because it cannot possibly work on this target" and others on the level of "tell me about it, but don't force me to fix it". All of these checks are things that don't even remotely apply to other targets.

If you're wondering why I haven't hacked up Clang's sema to enforce these constraints, see again: out-of-tree backend... maintaining OOT changes there is expected to be very difficult. Clang-tidy however provides a very nice framework where they can be kept neatly off to the side, away from most of the merge hassles.

It'd be nice not to have to run clang-tidy twice & parse its output in order to achieve all of that, hence this patch.

tools/llvm-project/extra/test/clang-tidy/werrors-plural.cpp
7

I considered that part of the name of the flag.

I can drop that if you think it looks better. Or print out the whole thing as the user wrote it, if you think that's useful (though -checks= and -Werrors= lines are probably pretty long...).

aaron.ballman added inline comments.Dec 15 2015, 10:59 AM
tools/llvm-project/extra/test/clang-tidy/werrors-plural.cpp
7

I would just drop the =; it seems like that suggests there should be more text there. That would also be consistent with the way clang reports -Werror diagnostics (error: unused variable 'i' [-Werror,-Wunused-variable])

Jonathan, can you explain what specific use case does this patch address? Why one severity level of native clang-tidy warnings (the current situation) is not enough, and two levels are enough?

I have out-of-tree checkers for a very strange out-of-tree target. Some of the checks are on the level of "this should break the build because it cannot possibly work on this target" and others on the level of "tell me about it, but don't force me to fix it". All of these checks are things that don't even remotely apply to other targets.

Thank you for the explanation. One more question: do you need to define Werrors differently in different directories?

If you're wondering why I haven't hacked up Clang's sema to enforce these constraints, see again: out-of-tree backend... maintaining OOT changes there is expected to be very difficult.

No, a sane person wouldn't suggest maintaining a local patch for Clang as a good solution ;)

Clang-tidy however provides a very nice framework where they can be kept neatly off to the side, away from most of the merge hassles.

It's one of the goals of clang-tidy to provide an easy way to maintain out-of-tree checks.

It'd be nice not to have to run clang-tidy twice & parse its output in order to achieve all of that, hence this patch.

Agreed, I want to ensure though, that this is the right approach. In particular, I wonder whether a way to assign labels or severities to clang-tidy diagnostics would be better. Another question is whether we can reuse something from the Clang diagnostic subsystem to map warning levels.

Jonathan, can you explain what specific use case does this patch address? Why one severity level of native clang-tidy warnings (the current situation) is not enough, and two levels are enough?

I have out-of-tree checkers for a very strange out-of-tree target. Some of the checks are on the level of "this should break the build because it cannot possibly work on this target" and others on the level of "tell me about it, but don't force me to fix it". All of these checks are things that don't even remotely apply to other targets.

Thank you for the explanation. One more question: do you need to define Werrors differently in different directories?

Yeah, for things like "if you do this, you will get terribad performance", it's useful to be able to let the user's build system control them independently with that kind of granularity. In some places it's acceptable to fail these checks (i.e. while porting code to this target), and in others it's mission-critical.

I could see this being useful in other contexts too. For example, hypothetically, one could imagine turning on the llvm-include-order check for all of llvm, and then making it -Werror for subdirectories where it has been cleaned up, ensuring monotonic progress.

If you're wondering why I haven't hacked up Clang's sema to enforce these constraints, see again: out-of-tree backend... maintaining OOT changes there is expected to be very difficult.

No, a sane person wouldn't suggest maintaining a local patch for Clang as a good solution ;)

:)

Clang-tidy however provides a very nice framework where they can be kept neatly off to the side, away from most of the merge hassles.

It's one of the goals of clang-tidy to provide an easy way to maintain out-of-tree checks.

Seems to be working out very well on my end... thanks for designing it that way!

It'd be nice not to have to run clang-tidy twice & parse its output in order to achieve all of that, hence this patch.

Agreed, I want to ensure though, that this is the right approach. In particular, I wonder whether a way to assign labels or severities to clang-tidy diagnostics would be better. Another question is whether we can reuse something from the Clang diagnostic subsystem to map warning levels.

For my purposes, this kind of trimodal { "it's definitely broken, and cannot possibly work", "hey, have you considered this?", "the tool didn't spot any problems" } granularity seems like the right fit, and lines up with Clang's diagnostics: notes usually stay notes, remarks stay remarks, warnings are sometimes errors and other times just warnings, and errors are errors.

Something else to think about: grouping a la -Wpedantic/-Wall/-Weverything might be useful. Some of that can be achieved by giving structure to the names of the checks, i.e.:

  • footarget-perf-check1
  • footarget-perf-check2
  • footarget-correctness-check1
  • footarget-correctness-check2
  • footarget-portability-check1

    (pardon my vagueness here)

But that kind of breaks down when you start to talk about putting existing upstream checks into these groups, because then you have to write things like: `-checks='-*,footarget-portability-*,google-runtime-int-std' (off the cuff example). Not sure if that's what you meant by "labels", but I see that as being orthogonal to the question of what a user should be forced to take action on.

tools/llvm-project/extra/test/clang-tidy/werrors-plural.cpp
7

ok, sounds good.

Thank you for explaining. The use case seems to be important enough to support it. And the solution seems to be good for now. A few concerns:

  1. Werrors isn't a good name for this. The only reason why a similar thing is called -Werror in compilers is that they use -W for warnings. I'd suggest TreatAsErrors, WarningsAsErrors or anything similar (with the proper casing change for the command line argument).
  2. Documentation is missing.

Thanks for the review! I'll rework this a bit early next week.

jroelofs updated this revision to Diff 44648.Jan 12 2016, 9:35 AM
jroelofs edited edge metadata.

Address review comments.

jroelofs marked 9 inline comments as done.Jan 12 2016, 9:36 AM

Thank you for explaining. The use case seems to be important enough to support it. And the solution seems to be good for now. A few concerns:

  1. Werrors isn't a good name for this. The only reason why a similar thing is called -Werror in compilers is that they use -W for warnings. I'd suggest TreatAsErrors, WarningsAsErrors or anything similar (with the proper casing change for the command line argument).

Renamed.

  1. Documentation is missing.

Working on that now.

jroelofs updated this revision to Diff 44650.Jan 12 2016, 9:54 AM
jroelofs edited edge metadata.

Added docs.

Looks good with a couple of nits. I'll be happy to submit your patch once you address the comments.

Also, please include more context in your patches. See http://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface.

clang-tidy/ClangTidy.cpp
156 ↗(On Diff #44650)

I'd call it getWarningsAsErrorsCount()

clang-tidy/ClangTidyOptions.cpp
130 ↗(On Diff #44650)

This line exceeds column limit. Please run clang-format on the changed files.

jroelofs updated this revision to Diff 44759.Jan 13 2016, 9:24 AM

clang-format the patch, and rename method.

jroelofs accepted this revision.Jan 13 2016, 9:45 AM
jroelofs added a reviewer: jroelofs.
jroelofs marked 2 inline comments as done.

Thanks for the review! Committed r257642.

jroelofs accepted this revision.Jan 13 2016, 12:20 PM
jroelofs abandoned this revision.

Hmm. There's no "close revision" button. Abandoning it in lieu of closing it.

alexfh commandeered this revision.Jan 13 2016, 4:53 PM
alexfh reclaimed this revision.
alexfh removed a reviewer: alexfh.
This revision is now accepted and ready to land.Jan 13 2016, 4:53 PM
alexfh closed this revision.Jan 13 2016, 4:53 PM
alexfh edited reviewers, added: alexfh; removed: jroelofs.
alexfh edited edge metadata.Jan 13 2016, 4:57 PM
alexfh added a subscriber: jroelofs.

Hmm. There's no "close revision" button. Abandoning it in lieu of closing it.

Weird. That's probably because I've not marked it "Accepted". Now I tried a few other actions on the revision and it now shows as authored by myself. Really weird.

Anyway, thank you for working on this!

njames93 added inline comments.
clang-tidy/tool/ClangTidyMain.cpp
362 ↗(On Diff #44759)

Was there any specific reason for returning the error count instead of returning 1. It results in undefined behaviour on POSIX shells if a value is returned outside the range of 0<=n<=255. See https://bugs.llvm.org/show_bug.cgi?id=46305