This is an archive of the discontinued LLVM Phabricator instance.

[PATCH] Enable clang-tidy misc-static-assert for C11
ClosedPublic

Authored by aaron.ballman on Aug 28 2015, 2:01 PM.

Details

Reviewers
djasper
alexfh
Summary

This patch adds support for the C11 _Static_assert macro to clang-tidy's misc-static-assert checker. There are a few things I'm not overly satisfied with, and welcome suggestions on how to improve, if desired.

  1. If <assert.h> is included, then static_assert() is also fine to use. Presumably, in order for the assert() check to trigger, <assert.h> has to be included, but I don't feel particularly comfortable with that assumption. If there's a way to access a Preprocessor instance from check(), we can use getMacroDefinitionAtLoc() to determine whether static_assert() is defined and a valid replacement for assert(). For right now, I'm always using _Static_assert() in C11 mode to be on the safe side.
  2. My Python skills are rudimentary at best. If there's a better way to update check_clang_tidy.py to handle file extensions, that would be great. This change is required or else the script fails on .c test cases that check fixes because of writing out to a .tmp.cpp file instead of a .tmp.c file conflicts with the language options specified on the RUN line.

~Aaron

Diff Detail

Event Timeline

aaron.ballman retitled this revision from to [PATCH] Enable clang-tidy misc-static-assert for C11.
aaron.ballman updated this object.
aaron.ballman added reviewers: alexfh, djasper.
aaron.ballman added a subscriber: cfe-commits.
alexfh edited edge metadata.Aug 28 2015, 5:15 PM
  1. If <assert.h> is included, then static_assert() is also fine to use. Presumably, in order for the assert() check to trigger, <assert.h> has to be

included, but I don't feel particularly comfortable with that assumption.

Do you have any C11 code that uses assert(), but doesn't include assert.h? If no, I'd suggest relying on this assumption until someone stumbles upon this and files a bug (or fixes this). One more option is to ensure assert.h is always included in the file being fixed (there's IncludeInserter class that can do this).

If there's a way to access a Preprocessor instance from check(),

You can subscribe to PPCallbacks and find out if static_assert is defined after the last include. Or if it's defined and not undefined. But I'd probably just skip this until you see real code that would benefit from this.

we can use getMacroDefinitionAtLoc() to determine whether static_assert() is defined and a valid replacement for assert(). For right now, I'm
always using _Static_assert() in C11 mode to be on the safe side.

  1. My Python skills are rudimentary at best. If there's a better way to update check_clang_tidy.py to handle file extensions, that would be great.

This change is required or else the script fails on .c test cases that check fixes because of writing out to a .tmp.cpp file instead of a .tmp.c file
conflicts with the language options specified on the RUN line.

Alternatively, you could add "-x c" to the test's RUN: line, but your solution is an improvement over this hack. Thanks!

test/clang-tidy/check_clang_tidy.py
50

I'm not sure that c99 is a good default for C code here. The existing .c tests don't rely on this, and the tests you're adding override -std anyway. I'd change this to ['--', '--std=c++11'] if extension == '.cpp' else ['--'].

test/clang-tidy/misc-static-assert-c99.c
1 ↗(On Diff #33461)

Instead of duplicating the test, you could add a second run line in the other test and just verify that clang-tidy doesn't generate any warnings when run with -std=c99. The invariant "no warnings == no fixes" is pretty basic for clang-tidy, so you can rely on it in the tests.

aaron.ballman edited edge metadata.

No longer worrying about static_assert() vs _Static_assert(). Corrected Python script.

aaron.ballman marked an inline comment as done.Aug 31 2015, 7:17 AM
aaron.ballman added inline comments.
test/clang-tidy/misc-static-assert-c99.c
2 ↗(On Diff #33579)

I am not certain how to accomplish this with the python script. Or are you thinking I should run clang-tidy directly and use "| count 0" ?

It seems Phab doesn't give you any context for this comment. For
reference, this is in response to the comment:

Instead of duplicating the test, you could add a second run line in
the other test and just verify that clang-tidy doesn't generate any
warnings when run with -std=c99. The invariant "no warnings == no
fixes" is pretty basic for clang-tidy, so you can rely on it in the
tests.

~Aaron

alexfh added inline comments.Aug 31 2015, 8:41 AM
test/clang-tidy/misc-static-assert-c99.c
2 ↗(On Diff #33579)

I was thinking about testing c11 mode using the script and c99 mode can run clang-tidy directly (without -fix) and use | count 0, | grep -v 'warning:' or anything else with the same meaning.

klimek added a subscriber: klimek.Aug 31 2015, 8:42 AM
klimek added inline comments.
test/clang-tidy/misc-static-assert-c99.c
2 ↗(On Diff #33579)

Phab test.

1 ↗(On Diff #33461)

Phab test 2.

aaron.ballman added inline comments.Aug 31 2015, 8:43 AM
test/clang-tidy/misc-static-assert-c99.c
2 ↗(On Diff #33579)

Phab test.

Only use a single test file with two RUN lines.

alexfh accepted this revision.Aug 31 2015, 2:29 PM
alexfh edited edge metadata.

LG

This revision is now accepted and ready to land.Aug 31 2015, 2:29 PM
aaron.ballman closed this revision.Aug 31 2015, 2:56 PM

Thanks! I've commit in r246494 and r246495.