diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -89,6 +89,11 @@ C Language Changes in Clang --------------------------- +C2x Feature Support +------------------- + +- Implemented `WG14 N2674 The noreturn attribute `_. + C++ Language Changes in Clang ----------------------------- diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -1209,10 +1209,10 @@ } def CXX11NoReturn : InheritableAttr { - let Spellings = [CXX11<"", "noreturn", 200809>]; + let Spellings = [CXX11<"", "noreturn", 200809>, + C2x<"", "noreturn", 202202>, C2x<"", "_Noreturn", 202202>]; let Subjects = SubjectList<[Function], ErrorDiag>; let Documentation = [CXX11NoReturnDocs]; - let SimpleHandler = 1; } // Similar to CUDA, OpenCL attributes do not receive a [[]] spelling because diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4027,6 +4027,9 @@ "specifying vector types with the 'mode' attribute is deprecated; " "use the 'vector_size' attribute instead">, InGroup; +def warn_deprecated_noreturn_spelling : Warning< + "the '[[_Noreturn]]' attribute spelling is deprecated in C2x; use " + "'[[noreturn]]' instead">, InGroup; def err_complex_mode_vector_type : Error< "type of machine mode does not support base vector types">; def err_enum_mode_vector_type : Error< diff --git a/clang/lib/Headers/stdnoreturn.h b/clang/lib/Headers/stdnoreturn.h --- a/clang/lib/Headers/stdnoreturn.h +++ b/clang/lib/Headers/stdnoreturn.h @@ -13,4 +13,13 @@ #define noreturn _Noreturn #define __noreturn_is_defined 1 +#if __STDC_VERSION__ > 201710L && \ + !defined(_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS) +/* The noreturn macro is deprecated in C2x. */ +#pragma clang deprecated(noreturn) + +/* Including the header file in C2x is also deprecated. */ +#warning "the '' header is deprecated in C2x" +#endif + #endif /* __STDNORETURN_H */ diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2175,6 +2175,16 @@ D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs)); } +static void handleStandardNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &A) { + // The [[_Noreturn]] spelling is deprecated in C2x, so if that was used, + // issue an appropriate diagnostic. + if (!S.getLangOpts().CPlusPlus && + A.getSemanticSpelling() == CXX11NoReturnAttr::C2x_Noreturn) + S.Diag(A.getLoc(), diag::warn_deprecated_noreturn_spelling) << A.getRange(); + + D->addAttr(::new (S.Context) CXX11NoReturnAttr(S.Context, A)); +} + static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { if (!S.getLangOpts().CFProtectionBranch) S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored); @@ -8432,6 +8442,9 @@ case ParsedAttr::AT_NoReturn: handleNoReturnAttr(S, D, AL); break; + case ParsedAttr::AT_CXX11NoReturn: + handleStandardNoReturnAttr(S, D, AL); + break; case ParsedAttr::AT_AnyX86NoCfCheck: handleNoCfCheckAttr(S, D, AL); break; diff --git a/clang/test/AST/ast-dump-lambda.cpp b/clang/test/AST/ast-dump-lambda.cpp --- a/clang/test/AST/ast-dump-lambda.cpp +++ b/clang/test/AST/ast-dump-lambda.cpp @@ -305,7 +305,7 @@ // CHECK-NEXT: | | `-Destructor simple irrelevant trivial needs_implicit // CHECK-NEXT: | |-CXXMethodDecl {{.*}} col:3{{( imported)?}} operator() 'auto () const' inline // CHECK-NEXT: | | |-CompoundStmt {{.*}} -// CHECK-NEXT: | | `-CXX11NoReturnAttr {{.*}} +// CHECK-NEXT: | | `-CXX11NoReturnAttr {{.*}} noreturn // CHECK-NEXT: | |-CXXConversionDecl {{.*}} col:3{{( imported)?}} implicit constexpr operator auto (*)() 'auto (*() const noexcept)()' inline // CHECK-NEXT: | `-CXXMethodDecl {{.*}} col:3{{( imported)?}} implicit __invoke 'auto ()' static inline // CHECK-NEXT: `-CompoundStmt {{.*}} diff --git a/clang/test/Sema/c2x-noreturn.c b/clang/test/Sema/c2x-noreturn.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/c2x-noreturn.c @@ -0,0 +1,64 @@ +// RUN: %clang_cc1 -verify=all,c2x -std=c2x -fsyntax-only %s +// RUN: %clang_cc1 -verify=all -std=c17 -fdouble-square-bracket-attributes -fsyntax-only %s +// RUN: %clang_cc1 -verify=none -Wno-deprecated-attributes -D_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS -std=c2x -fsyntax-only %s +// RUN: %clang_cc1 -verify=none -Wno-deprecated-attributes -D_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS -std=c17 -fdouble-square-bracket-attributes -fsyntax-only %s +// none-no-diagnostics + +// Test preprocessor functionality. +#if !__has_c_attribute(noreturn) +#error "No noreturn attribute support?" +#endif + +#if !__has_c_attribute(_Noreturn) +#error "No _Noreturn attribute support?" +#endif + +#if __has_c_attribute(noreturn) != __has_c_attribute(_Noreturn) || \ + __has_c_attribute(noreturn) != 202202L +#error "Wrong value for __has_c_attribute(noreturn)" +#endif + +// If we're testings with deprecations disabled, we don't care about testing +// the scenarios that trigger errors because we're only interested in the +// deprecation warning behaviors. +#ifndef _CLANG_DISABLE_CRT_DEPRECATION_WARNINGS +// Test that the attribute accepts no args, applies to the correct subject, etc. +[[noreturn(12)]] void func4(void); // all-error {{attribute 'noreturn' cannot have an argument list}} +[[noreturn]] int not_a_func; // all-error {{'noreturn' attribute only applies to functions}} +void func5(void) [[noreturn]]; // all-error {{'noreturn' attribute cannot be applied to types}} +#endif + +_Noreturn void func1(void); // ok, using the function specifier +[[noreturn]] void func2(void); + +// This is deprecated because it's only for compatibility with inclusion of the +// header where the noreturn macro expands to _Noreturn. +[[_Noreturn]] void func3(void); // all-warning {{the '[[_Noreturn]]' attribute spelling is deprecated in C2x; use '[[noreturn]]' instead}} + +// Test the behavior of including +#include // c2x-warning@stdnoreturn.h:* {{the '' header is deprecated in C2x}} + +[[noreturn]] void func6(void); // all-warning {{the '[[_Noreturn]]' attribute spelling is deprecated in C2x; use '[[noreturn]]' instead}} \ + // c2x-warning {{macro 'noreturn' has been marked as deprecated}} \ + // c2x-note@stdnoreturn.h:* {{macro marked 'deprecated' here}} + +void func7 [[noreturn]] (void); // all-warning {{the '[[_Noreturn]]' attribute spelling is deprecated in C2x; use '[[noreturn]]' instead}} \ + // c2x-warning {{macro 'noreturn' has been marked as deprecated}} \ + // c2x-note@stdnoreturn.h:* {{macro marked 'deprecated' here}} + +noreturn void func8(void); // c2x-warning {{macro 'noreturn' has been marked as deprecated}} \ + // c2x-note@stdnoreturn.h:* {{macro marked 'deprecated' here}} + +// Ensure the function specifier form still works +void noreturn func9(void); // c2x-warning {{macro 'noreturn' has been marked as deprecated}} \ + // c2x-note@stdnoreturn.h:* {{macro marked 'deprecated' here}} + +// Test preprocessor functionality after including . +#if !__has_c_attribute(noreturn) // c2x-warning {{macro 'noreturn' has been marked as deprecated}} \ + // c2x-note@stdnoreturn.h:* {{macro marked 'deprecated' here}} +#error "No noreturn attribute support?" +#endif + +#if !__has_c_attribute(_Noreturn) +#error "No _Noreturn attribute support?" +#endif