Skip to content

[Clang][Sema] improve sema check of clang::musttail attribute #77727

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 1 commit into from
Jan 17, 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
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ Bug Fixes in This Version
- Fix crashes when using the binding decl from an invalid structured binding.
Fixes (`#67495 <https://github.com/llvm/llvm-project/issues/67495>`_) and
(`#72198 <https://github.com/llvm/llvm-project/issues/72198>`_)
- Fix assertion failure when call noreturn-attribute function with musttail
attribute.
Fixes (`#76631 <https://github.com/llvm/llvm-project/issues/76631>`_)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3113,6 +3113,8 @@ def err_musttail_scope : Error<
"cannot perform a tail call from this return statement">;
def err_musttail_no_variadic : Error<
"%0 attribute may not be used with variadic functions">;
def err_musttail_no_return : Error<
"%0 attribute may not be used with no-return-attribute functions">;

def err_nsobject_attribute : Error<
"'NSObject' attribute is for pointer types only">;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) {
return false;
}

const auto *CalleeDecl = CE->getCalleeDecl();
if (CalleeDecl && CalleeDecl->hasAttr<CXX11NoReturnAttr>()) {
Diag(St->getBeginLoc(), diag::err_musttail_no_return) << &MTA;
return false;
}

// Caller and callee must match in whether they have a "this" parameter.
if (CallerType.This.isNull() != CalleeType.This.isNull()) {
if (const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
Expand Down
9 changes: 9 additions & 0 deletions clang/test/SemaCXX/PR76631.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s

[[noreturn]] void throw_int() {
throw int(); // expected-error {{cannot use 'throw' with exceptions disabled}}
}

void throw_int_wrapper() {
[[clang::musttail]] return throw_int(); // expected-error {{'musttail' attribute may not be used with no-return-attribute functions}}
}