[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/77727 >From 75e9a81a5aa4042197201450fa676b8036a3d2cd Mon Sep 17 00:00:00 2001 From: huqizhi Date: Thu, 11 Jan 2024 13:02:21 +0800 Subject: [PATCH] [Clang][SemaCXX] improve sema check of clang::musttail attribute --- clang/docs/HLSL/FunctionCalls.rst| 2 +- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaStmt.cpp | 6 ++ clang/test/SemaCXX/PR76631.cpp | 9 + 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 clang/test/SemaCXX/PR76631.cpp diff --git a/clang/docs/HLSL/FunctionCalls.rst b/clang/docs/HLSL/FunctionCalls.rst index 996ddd6944b1ce..7317de2163f897 100644 --- a/clang/docs/HLSL/FunctionCalls.rst +++ b/clang/docs/HLSL/FunctionCalls.rst @@ -144,7 +144,7 @@ undefined behavior in HLSL, and any use of the argument after the call is a use of an undefined value which may be illegal in the target (DXIL programs with used or potentially used ``undef`` or ``poison`` values fail validation). -Clang Implementation +Clang Implementation .. note:: diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 1a79892e40030a..1006e7d65dd868 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 21efe25ed84a3d..9e7c8c7e4e8c12 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) { return false; } + const auto *CalleeDecl = CE->getCalleeDecl(); + if (CalleeDecl && CalleeDecl->hasAttr()) { +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(CE->getCalleeDecl())) { diff --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp new file mode 100644 index 00..947fa3fc2635e6 --- /dev/null +++ b/clang/test/SemaCXX/PR76631.cpp @@ -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}} +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/77727 >From cdc04d0b5b934c4dc4c8294dd000539275bf4222 Mon Sep 17 00:00:00 2001 From: huqizhi Date: Thu, 11 Jan 2024 13:02:21 +0800 Subject: [PATCH] [Clang][SemaCXX] improve sema check of clang::musttail attribute --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaStmt.cpp | 6 ++ clang/test/SemaCXX/PR76631.cpp | 9 + 3 files changed, 17 insertions(+) create mode 100644 clang/test/SemaCXX/PR76631.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 1a79892e40030a..1006e7d65dd868 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 21efe25ed84a3d..9e7c8c7e4e8c12 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) { return false; } + const auto *CalleeDecl = CE->getCalleeDecl(); + if (CalleeDecl && CalleeDecl->hasAttr()) { +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(CE->getCalleeDecl())) { diff --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp new file mode 100644 index 00..947fa3fc2635e6 --- /dev/null +++ b/clang/test/SemaCXX/PR76631.cpp @@ -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}} +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
https://github.com/jcsxky edited https://github.com/llvm/llvm-project/pull/77727 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/77727 >From 74fa4ef52b07df154d7b6682323d2da7ff2b12ef Mon Sep 17 00:00:00 2001 From: huqizhi Date: Thu, 11 Jan 2024 13:02:21 +0800 Subject: [PATCH] [Clang][SemaCXX] improve sema check of clang::musttail attribute --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaStmt.cpp | 6 ++ clang/test/SemaCXX/PR76631.cpp | 9 + 3 files changed, 17 insertions(+) create mode 100644 clang/test/SemaCXX/PR76631.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 3884dca59e2f3b..b40befc5c1cfb5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 21efe25ed84a3d..9e7c8c7e4e8c12 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) { return false; } + const auto *CalleeDecl = CE->getCalleeDecl(); + if (CalleeDecl && CalleeDecl->hasAttr()) { +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(CE->getCalleeDecl())) { diff --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp new file mode 100644 index 00..947fa3fc2635e6 --- /dev/null +++ b/clang/test/SemaCXX/PR76631.cpp @@ -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}} +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/77727 >From c554108eeb950c9885291962018ce31233589e8e Mon Sep 17 00:00:00 2001 From: huqizhi Date: Thu, 11 Jan 2024 13:02:21 +0800 Subject: [PATCH] [Clang][SemaCXX] improve sema check of clang::musttail attribute --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaStmt.cpp | 6 ++ clang/test/SemaCXX/PR76631.cpp | 9 + 3 files changed, 17 insertions(+) create mode 100644 clang/test/SemaCXX/PR76631.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 3884dca59e2f3b..b40befc5c1cfb5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 21efe25ed84a3d..9e7c8c7e4e8c12 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) { return false; } + const auto *CalleeDecl = CE->getCalleeDecl(); + if (CalleeDecl && CalleeDecl->hasAttr()) { +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(CE->getCalleeDecl())) { diff --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp new file mode 100644 index 00..ceacf22c76574a --- /dev/null +++ b/clang/test/SemaCXX/PR76631.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s + +[[noreturn]] void throw_int() { + throw int(); +} + +void throw_int_wrapper() { + [[clang::musttail]] return throw_int(); // expected-error {{'musttail' attribute may not be used with no-return-attribute functions}} +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Qizhi Hu (jcsxky) Changes Call function with no-return attribute generate code with unreachable instruction instead of return and if the call statement has `clang::musttail` attribute, it would crash in Verify. Check this condition in Sema. --- Full diff: https://github.com/llvm/llvm-project/pull/77727.diff 3 Files Affected: - (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) - (modified) clang/lib/Sema/SemaStmt.cpp (+6) - (added) clang/test/SemaCXX/PR76631.cpp (+7) ``diff diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 3884dca59e2f3b..b40befc5c1cfb5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 21efe25ed84a3d..9e7c8c7e4e8c12 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) { return false; } + const auto *CalleeDecl = CE->getCalleeDecl(); + if (CalleeDecl && CalleeDecl->hasAttr()) { +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(CE->getCalleeDecl())) { diff --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp new file mode 100644 index 00..df89753bd12580 --- /dev/null +++ b/clang/test/SemaCXX/PR76631.cpp @@ -0,0 +1,7 @@ +[[noreturn]] void throw_int() { + throw int(); +} + +void throw_int_wrapper() { + [[clang::musttail]] return throw_int(); // expected-error {{'musttail' attribute may not be used with no-return-attribute functions}} +} `` https://github.com/llvm/llvm-project/pull/77727 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SemaCXX] improve sema check of clang::musttail attribute (PR #77727)
https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/77727 Call function with no-return attribute generate code with unreachable instruction instead of return and if the call statement has `clang::musttail` attribute, it would crash in Verify. Check this condition in Sema. >From 0a340a46a69f04555bfeaeb14f214f57ebd6b9cd Mon Sep 17 00:00:00 2001 From: huqizhi Date: Thu, 11 Jan 2024 13:02:21 +0800 Subject: [PATCH] [Clang][SemaCXX] improve sema check of clang::musttail attribute --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaStmt.cpp | 6 ++ clang/test/SemaCXX/PR76631.cpp | 7 +++ 3 files changed, 15 insertions(+) create mode 100644 clang/test/SemaCXX/PR76631.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 3884dca59e2f3b..b40befc5c1cfb5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 21efe25ed84a3d..9e7c8c7e4e8c12 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) { return false; } + const auto *CalleeDecl = CE->getCalleeDecl(); + if (CalleeDecl && CalleeDecl->hasAttr()) { +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(CE->getCalleeDecl())) { diff --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp new file mode 100644 index 00..df89753bd12580 --- /dev/null +++ b/clang/test/SemaCXX/PR76631.cpp @@ -0,0 +1,7 @@ +[[noreturn]] void throw_int() { + throw int(); +} + +void throw_int_wrapper() { + [[clang::musttail]] return throw_int(); // expected-error {{'musttail' attribute may not be used with no-return-attribute functions}} +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits