This is an archive of the discontinued LLVM Phabricator instance.

[clang] Tweaked fixit for static assert with no message
ClosedPublic

Authored by njames93 on Oct 8 2020, 12:36 PM.

Details

Summary

If a static assert has a message as the right side of an and condition, suggest a fix it of replacing the '&&' to ','.

static_assert(cond && "Failed Cond") -> static_assert(cond, "Failed cond")

This use case comes up when lazily replacing asserts with static asserts.

Diff Detail

Event Timeline

njames93 created this revision.Oct 8 2020, 12:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 8 2020, 12:36 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
njames93 requested review of this revision.Oct 8 2020, 12:36 PM

I don't see any support for verifying fix-its in the test cases so unsure how i could go about testing this change.

clang/lib/Parse/ParseDeclCXX.cpp
19

I'll undo this, clangd include inserter and clang format making undesired changes

njames93 updated this revision to Diff 297072.Oct 8 2020, 4:04 PM

Remove unnecessary changes

njames93 planned changes to this revision.Oct 10 2020, 8:11 AM

Just noticed this doesn't quite work as expected, will update

njames93 updated this revision to Diff 297434.Oct 10 2020, 6:12 PM

FixIt now properly generated

I don't see any support for verifying fix-its in the test cases so unsure how i could go about testing this change.

We usually test fix-its with -fdiagnostics-parseable-fixits as in the tests here: https://github.com/llvm/llvm-project/tree/main/clang/test/FixIt (not saying the test has to go into the FixIt directory, it should probably live in test/Parser given the changes.)

Btw, this is going to cause merge conflicts with https://reviews.llvm.org/D95396 and I sort of wonder if we should combine the two patches into one given that they're fixing the same code in different ways.

xbolva00 added inline comments.
clang/lib/Parse/ParseDeclCXX.cpp
872

Do we warn for:

static_assert(“my msg”)?

I remember some long time ago bug in llvm itself - assert(“my msg”) - condition expr was missing.

aaron.ballman added inline comments.Feb 20 2021, 5:05 AM
clang/lib/Parse/ParseDeclCXX.cpp
872

We don't, but that would be a reasonable warning to add (not necessarily as part of this patch).

njames93 added inline comments.Feb 21 2021, 5:01 AM
clang/lib/Parse/ParseDeclCXX.cpp
872

There is a clang-tidy check, misc-static-assert, that will flag asserts like that, although it won't flag static asserts of that kind.

njames93 updated this revision to Diff 325284.Feb 21 2021, 5:37 AM

Add tests for fixits.

njames93 updated this revision to Diff 325287.Feb 21 2021, 5:55 AM

Add recompile test to ensure fix-its are correct

aaron.ballman accepted this revision.Feb 22 2021, 7:36 AM

LGTM aside from some nits.

clang/lib/Parse/ParseDeclCXX.cpp
861
863
clang/test/FixIt/fixit-static-assert.cpp
3–4

Any reason not to pass -verify=cxx17 and // cxx17-no-diagnostics?

This revision is now accepted and ready to land.Feb 22 2021, 7:36 AM
njames93 marked an inline comment as done.Feb 22 2021, 8:07 AM
njames93 added inline comments.
clang/test/FixIt/fixit-static-assert.cpp
3–4

I don't know my way around clang testing framework that well.
For the recompile test I think Werror has to be used as verify resulted in an assertion.

njames93 updated this revision to Diff 325452.Feb 22 2021, 8:07 AM
njames93 marked an inline comment as done.

Address comments

aaron.ballman added inline comments.Feb 22 2021, 8:10 AM
clang/test/FixIt/fixit-static-assert.cpp
3–4

Ah, okay, thanks for the explanation.

This revision was landed with ongoing or failed builds.Feb 22 2021, 9:44 AM
This revision was automatically updated to reflect the committed changes.