Skip to content

Commit 2fe5b15

Browse files
authored
[Clang][Sema] improve sema check of clang::musttail attribute (llvm#77727)
Call function with no-return attribute generates code with unreachable instruction instead of return and if the call statement has `clang::musttail` attribute, it would crash in `VerifyPass`. Check this condition in Sema ahead. Fix [issue](llvm#76631) Co-authored-by: huqizhi <[email protected]>
1 parent 67f5df7 commit 2fe5b15

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ Bug Fixes in This Version
768768
- Fix crashes when using the binding decl from an invalid structured binding.
769769
Fixes (`#67495 <https://github.com/llvm/llvm-project/issues/67495>`_) and
770770
(`#72198 <https://github.com/llvm/llvm-project/issues/72198>`_)
771+
- Fix assertion failure when call noreturn-attribute function with musttail
772+
attribute.
773+
Fixes (`#76631 <https://github.com/llvm/llvm-project/issues/76631>`_)
771774

772775
Bug Fixes to Compiler Builtins
773776
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,8 @@ def err_musttail_scope : Error<
31133113
"cannot perform a tail call from this return statement">;
31143114
def err_musttail_no_variadic : Error<
31153115
"%0 attribute may not be used with variadic functions">;
3116+
def err_musttail_no_return : Error<
3117+
"%0 attribute may not be used with no-return-attribute functions">;
31163118

31173119
def err_nsobject_attribute : Error<
31183120
"'NSObject' attribute is for pointer types only">;

clang/lib/Sema/SemaStmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) {
786786
return false;
787787
}
788788

789+
const auto *CalleeDecl = CE->getCalleeDecl();
790+
if (CalleeDecl && CalleeDecl->hasAttr<CXX11NoReturnAttr>()) {
791+
Diag(St->getBeginLoc(), diag::err_musttail_no_return) << &MTA;
792+
return false;
793+
}
794+
789795
// Caller and callee must match in whether they have a "this" parameter.
790796
if (CallerType.This.isNull() != CalleeType.This.isNull()) {
791797
if (const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {

clang/test/SemaCXX/PR76631.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
2+
3+
[[noreturn]] void throw_int() {
4+
throw int(); // expected-error {{cannot use 'throw' with exceptions disabled}}
5+
}
6+
7+
void throw_int_wrapper() {
8+
[[clang::musttail]] return throw_int(); // expected-error {{'musttail' attribute may not be used with no-return-attribute functions}}
9+
}

0 commit comments

Comments
 (0)