Skip to content

Commit 0281f6d

Browse files
author
huqizhi
committed
[Clang][SemaCXX] improve sema check of clang::musttail attribute
1 parent 17c062c commit 0281f6d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

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)