Skip to content

[Clang] avoid adding consteval condition as the last statement to preserve valid CFG #116513

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

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ Improvements to Clang's diagnostics

- Clang now diagnoses ``= delete("reason")`` extension warnings only in pedantic mode rather than on by default. (#GH109311).

- Clang now diagnoses missing return value in functions containing ``if consteval`` (#GH116485).

Improvements to Clang's time-trace
----------------------------------

Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Analysis/CFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3177,11 +3177,14 @@ CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
if (!I->isConsteval())
KnownVal = tryEvaluateBool(I->getCond());

// Add the successors. If we know that specific branches are
// Add the successors. If we know that specific branches are
// unreachable, inform addSuccessor() of that knowledge.
addSuccessor(Block, ThenBlock, /* IsReachable = */ !KnownVal.isFalse());
addSuccessor(Block, ElseBlock, /* IsReachable = */ !KnownVal.isTrue());

if (I->isConsteval())
return Block;

// Add the condition as the last statement in the new block. This may
// create new blocks as the condition may contain control-flow. Any newly
// created blocks will be pointed to be "Block".
Expand Down
31 changes: 30 additions & 1 deletion clang/test/SemaCXX/constexpr-return-non-void-cxx2b.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -Wimplicit-fallthrough -verify %s

constexpr int f() { } // expected-warning {{non-void function does not return a value}}
static_assert(__is_same(decltype([] constexpr -> int { }( )), int)); // expected-warning {{non-void lambda does not return a value}}

consteval int g() { } // expected-warning {{non-void function does not return a value}}
static_assert(__is_same(decltype([] consteval -> int { }( )), int)); // expected-warning {{non-void lambda does not return a value}}

namespace GH116485 {
int h() {
if consteval { }
} // expected-warning {{non-void function does not return a value}}

void i(int x) {
if consteval {
}
switch (x) {
case 1:
i(1);
case 2: // expected-warning {{unannotated fall-through between switch labels}} \
// expected-note {{insert 'break;' to avoid fall-through}}
break;
}
}

constexpr bool j() {
if !consteval { return true; }
} // expected-warning {{non-void function does not return a value in all control paths}} \
// expected-note {{control reached end of constexpr function}}

bool k = j();
constinit bool l = j(); // expected-error {{variable does not have a constant initializer}} \
// expected-note {{required by 'constinit' specifier here}} \
// expected-note {{in call to 'j()'}}

}
Loading