-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Wunreachable-code warns that [[likely]] and [[unlikely]] are unreachable #51445
Comments
assigned to @nico |
This smells somewhat related to #48798 . I haven't looked very deeply, but I wonder how well the CFG facilities support attributed statements. |
Only happens if the if contains a int main(int argc, char* argv[]) { |
I'll look at this for a bit. Here are some useful commands:
The ifs look like:
So there's an AttributedStmt as body that contains the CompoundStmt.
Here's the basic block for the body of the 2nd if:
Here are the two basic blocks for the body of the 1st if:
So the AttributedStmt is inserted at the end of the basic block for currently unknown-to-me reasons (*), and since there's a return in the BB, that really ends up being unreachable in the CFG. Maybe the AttributedStmt's attribute should show up at the start of the CFG BB? Maybe AttributedStmt was added for [[fallthrough]] originally and for that it makes sense at the end? (But [[fallthrough]] shouldn't have children.) Or maybe it just happens to fall out that way because the CFGBuilder builds the CFG backwards. Need to do some more reading. |
Looking through other StmtAttr entries in clang/include/clang/Basic/Attr.td, this incorrectly warns as well, for the same reasons:
|
https://reviews.llvm.org/rG84837d5b5aa0d added
to clang/lib/Sema/AnalysisBasedWarnings.cpp (for -Wimplicit-fallthrough / the FallThrough attr). (Actually I'm not sure why this is needed since addStmt() in CFG.cpp always passes The default VisitStmt() / VisitChildren() in clang/lib/Analysis/CFG.cpp do:
So alwaysAdd() is always true for AttributedStmts and is always called before the children (and adds the AttributedStmt itself to the block), and the children are added in reverse order, and a jump statement child causes the block to be replaced. That explains what we're seeing. Options:
|
For the example from bug 49454:
Without that
With the [[likely]], B3 becomes:
So it adds the full AttributedStmt with the case child to the BB for the case label itself. That's not great. |
patch |
*** Bug #48798 has been marked as a duplicate of this bug. *** |
This seems like a pretty bad bug. |
The fix seems safe enough to add into 13.x, but the issue existed before 13.0 so it feels less pressing to me: https://godbolt.org/z/dqMxTjdca |
[[likely]] is in c++20, which was ratified ~1 year ago. It'll likely become more prevalent over the window where llvm 13 is the active release, so merging makes sense to me. Tom, what's the process for llvm 13.x merges? |
(back to open to track the merge) |
It will get backported automatically once I kick off the 13.0.1 release process. |
*** Bug #48769 has been marked as a duplicate of this bug. *** |
Merged: 2ac023c |
mentioned in issue #51489 |
Extended Description
$ cat foo.cc
int main(int argc, char* argv[]) {
if (argc > 100) [[unlikely]]
return 1;
return 0;
}
$ out/gn/bin/clang -Wunreachable-code -c foo.cc -std=c++20
foo.cc:2:19: warning: code will never be executed [-Wunreachable-code]
if (argc > 100) [[unlikely]]
^~~~~~~~~~~~
That's pretty silly.
(There's also bug 19020; not quite sure if this is a dupe of that or not.)
The text was updated successfully, but these errors were encountered: