From de03c15ad92b44a4be11507ca2501bb9dd014dce Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 17 Jan 2013 22:16:11 +0000 Subject: [PATCH] Parsing support for C11's _Noreturn keyword. No semantics yet. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172761 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticParseKinds.td | 3 +++ include/clang/Basic/DiagnosticSemaKinds.td | 6 ++++- include/clang/Basic/TokenKinds.def | 1 + include/clang/Sema/DeclSpec.h | 10 ++++++- lib/Headers/CMakeLists.txt | 1 + lib/Headers/stdnoreturn.h | 30 +++++++++++++++++++++ lib/Parse/ParseDecl.cpp | 6 +++++ lib/Sema/DeclSpec.cpp | 10 ++++++- lib/Sema/SemaDecl.cpp | 11 ++++++-- test/Headers/c11.c | 12 +++++++++ test/Parser/c11-noreturn.c | 14 ++++++++++ test/Sema/return-noreturn.c | 5 ++++ 12 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 lib/Headers/stdnoreturn.h create mode 100644 test/Headers/c11.c create mode 100644 test/Parser/c11-noreturn.c diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index 963263bebd..4f8c427977 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -113,6 +113,9 @@ def note_previous_default_assoc : Note< def ext_c11_alignment : Extension< "%0 is a C11-specific feature">, InGroup; +def ext_c11_noreturn : Extension< + "_Noreturn functions are a C11-specific feature">, InGroup; + def ext_gnu_indirect_goto : Extension< "use of GNU indirect-goto extension">, InGroup; def ext_gnu_address_of_label : Extension< diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 81212e661c..328bebe9af 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -214,6 +214,8 @@ def warn_use_out_of_scope_declaration : Warning< "use of out-of-scope declaration of %0">; def err_inline_non_function : Error< "'inline' can only appear on functions">; +def err_noreturn_non_function : Error< + "'_Noreturn' can only appear on functions">; def warn_qual_return_type : Warning< "'%0' type qualifier%s1 on return type %plural{1:has|:have}1 no effect">, InGroup, DefaultIgnore; @@ -381,11 +383,13 @@ def note_strncat_wrong_size : Note< "the terminating null byte">; /// main() -// static/inline main() are not errors in C, just in C++. +// static main() is not an error in C, just in C++. def warn_static_main : Warning<"'main' should not be declared static">, InGroup
; def err_static_main : Error<"'main' is not allowed to be declared static">; def err_inline_main : Error<"'main' is not allowed to be declared inline">; +def ext_noreturn_main : ExtWarn< + "'main' is not allowed to be declared _Noreturn">, InGroup
; def err_constexpr_main : Error< "'main' is not allowed to be declared constexpr">; def err_main_template_decl : Error<"'main' cannot be a template">; diff --git a/include/clang/Basic/TokenKinds.def b/include/clang/Basic/TokenKinds.def index cba9898c01..b4c0b2d97f 100644 --- a/include/clang/Basic/TokenKinds.def +++ b/include/clang/Basic/TokenKinds.def @@ -260,6 +260,7 @@ KEYWORD(_Bool , KEYNOCXX) KEYWORD(_Complex , KEYALL) KEYWORD(_Generic , KEYALL) KEYWORD(_Imaginary , KEYALL) +KEYWORD(_Noreturn , KEYALL) KEYWORD(_Static_assert , KEYALL) KEYWORD(__func__ , KEYALL) KEYWORD(__objc_yes , KEYALL) diff --git a/include/clang/Sema/DeclSpec.h b/include/clang/Sema/DeclSpec.h index d2b4a7ce30..b232d34c40 100644 --- a/include/clang/Sema/DeclSpec.h +++ b/include/clang/Sema/DeclSpec.h @@ -325,6 +325,7 @@ private: unsigned FS_inline_specified : 1; unsigned FS_virtual_specified : 1; unsigned FS_explicit_specified : 1; + unsigned FS_noreturn_specified : 1; // friend-specifier unsigned Friend_specified : 1; @@ -367,7 +368,7 @@ private: SourceLocation TSTNameLoc; SourceRange TypeofParensRange; SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc; - SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc; + SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc; SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc; WrittenBuiltinSpecs writtenBS; @@ -409,6 +410,7 @@ public: FS_inline_specified(false), FS_virtual_specified(false), FS_explicit_specified(false), + FS_noreturn_specified(false), Friend_specified(false), Constexpr_specified(false), StorageClassSpecAsWritten(SCS_unspecified), @@ -518,6 +520,9 @@ public: bool isExplicitSpecified() const { return FS_explicit_specified; } SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; } + bool isNoreturnSpecified() const { return FS_noreturn_specified; } + SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; } + void ClearFunctionSpecs() { FS_inline_specified = false; FS_inlineLoc = SourceLocation(); @@ -525,6 +530,8 @@ public: FS_virtualLoc = SourceLocation(); FS_explicit_specified = false; FS_explicitLoc = SourceLocation(); + FS_noreturn_specified = false; + FS_noreturnLoc = SourceLocation(); } /// \brief Return true if any type-specifier has been found. @@ -611,6 +618,7 @@ public: bool setFunctionSpecInline(SourceLocation Loc); bool setFunctionSpecVirtual(SourceLocation Loc); bool setFunctionSpecExplicit(SourceLocation Loc); + bool setFunctionSpecNoreturn(SourceLocation Loc); bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, unsigned &DiagID); diff --git a/lib/Headers/CMakeLists.txt b/lib/Headers/CMakeLists.txt index 25e4d903bb..ae689c37fa 100644 --- a/lib/Headers/CMakeLists.txt +++ b/lib/Headers/CMakeLists.txt @@ -27,6 +27,7 @@ set(files stdbool.h stddef.h stdint.h + stdnoreturn.h tgmath.h tmmintrin.h varargs.h diff --git a/lib/Headers/stdnoreturn.h b/lib/Headers/stdnoreturn.h new file mode 100644 index 0000000000..a7a301d7e0 --- /dev/null +++ b/lib/Headers/stdnoreturn.h @@ -0,0 +1,30 @@ +/*===---- stdnoreturn.h - Standard header for noreturn macro ---------------=== + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + *===-----------------------------------------------------------------------=== + */ + +#ifndef __STDNORETURN_H +#define __STDNORETURN_H + +#define noreturn _Noreturn +#define __noreturn_is_defined 1 + +#endif /* __STDNORETURN_H */ diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 74769bf731..f9c68c79b9 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2619,6 +2619,11 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, case tok::kw_explicit: isInvalid = DS.setFunctionSpecExplicit(Loc); break; + case tok::kw__Noreturn: + if (!getLangOpts().C11) + Diag(Loc, diag::ext_c11_noreturn); + isInvalid = DS.setFunctionSpecNoreturn(Loc); + break; // alignment-specifier case tok::kw__Alignas: @@ -3878,6 +3883,7 @@ bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { case tok::kw_inline: case tok::kw_virtual: case tok::kw_explicit: + case tok::kw__Noreturn: // friend keyword. case tok::kw_friend: diff --git a/lib/Sema/DeclSpec.cpp b/lib/Sema/DeclSpec.cpp index fa63c2af38..35b0736196 100644 --- a/lib/Sema/DeclSpec.cpp +++ b/lib/Sema/DeclSpec.cpp @@ -329,7 +329,8 @@ unsigned DeclSpec::getParsedSpecifiers() const { if (hasTypeSpecifier()) Res |= PQ_TypeSpecifier; - if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) + if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified || + FS_noreturn_specified) Res |= PQ_FunctionSpecifier; return Res; } @@ -734,6 +735,13 @@ bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc) { return false; } +bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc) { + // '_Noreturn _Noreturn' is ok. + FS_noreturn_specified = true; + FS_noreturnLoc = Loc; + return false; +} + bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, unsigned &DiagID) { if (Friend_specified) { diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index a13d8b3b46..9cf5e73665 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -4097,6 +4097,10 @@ void Sema::DiagnoseFunctionSpecifiers(Declarator& D) { if (D.getDeclSpec().isExplicitSpecified()) Diag(D.getDeclSpec().getExplicitSpecLoc(), diag::err_explicit_non_function); + + if (D.getDeclSpec().isNoreturnSpecified()) + Diag(D.getDeclSpec().getNoreturnSpecLoc(), + diag::err_noreturn_non_function); } NamedDecl* @@ -6429,9 +6433,10 @@ static SourceRange getResultSourceRange(const FunctionDecl *FD) { void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { // C++11 [basic.start.main]p3: A program that declares main to be inline, // static or constexpr is ill-formed. - // C99 6.7.4p4: In a hosted environment, the inline function specifier - // shall not appear in a declaration of main. + // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall + // appear in a declaration of main. // static main is not an error under C99, but we should warn about it. + // We accept _Noreturn main as an extension. if (FD->getStorageClass() == SC_Static) Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus ? diag::err_static_main : diag::warn_static_main) @@ -6439,6 +6444,8 @@ void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { if (FD->isInlineSpecified()) Diag(DS.getInlineSpecLoc(), diag::err_inline_main) << FixItHint::CreateRemoval(DS.getInlineSpecLoc()); + if (DS.isNoreturnSpecified()) + Diag(DS.getNoreturnSpecLoc(), diag::ext_noreturn_main); if (FD->isConstexpr()) { Diag(DS.getConstexprSpecLoc(), diag::err_constexpr_main) << FixItHint::CreateRemoval(DS.getConstexprSpecLoc()); diff --git a/test/Headers/c11.c b/test/Headers/c11.c new file mode 100644 index 0000000000..1a3d4d41fe --- /dev/null +++ b/test/Headers/c11.c @@ -0,0 +1,12 @@ +// RUN: %clang -fsyntax-only -Xclang -verify -std=c11 %s + +noreturn int f(); // expected-error 1+{{}} + +#include +#include +#include + +int g(); +noreturn int g(); +int noreturn g(); +int g(); diff --git a/test/Parser/c11-noreturn.c b/test/Parser/c11-noreturn.c new file mode 100644 index 0000000000..7a2fe50f88 --- /dev/null +++ b/test/Parser/c11-noreturn.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -std=c11 -fsyntax-only -verify %s +// RUN: not %clang_cc1 -pedantic -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-EXT %s + +_Noreturn int f(); +int _Noreturn f(); // expected-note {{previous}} +int f _Noreturn(); // expected-error {{expected ';'}} expected-error 2{{}} +int f() _Noreturn; // expected-error {{expected ';'}} expected-warning {{does not declare anything}} + +_Noreturn char c1; // expected-error {{'_Noreturn' can only appear on functions}} +char _Noreturn c2; // expected-error {{'_Noreturn' can only appear on functions}} + +typedef _Noreturn int g(); // expected-error {{'_Noreturn' can only appear on functions}} + +// CHECK-EXT: _Noreturn functions are a C11-specific feature diff --git a/test/Sema/return-noreturn.c b/test/Sema/return-noreturn.c index 448fce77cd..5dd6693373 100644 --- a/test/Sema/return-noreturn.c +++ b/test/Sema/return-noreturn.c @@ -35,3 +35,8 @@ void __attribute__((noreturn)) test4() { test2_positive(); } + +// FIXME: do not warn here. +_Noreturn void test5() { // expected-warning {{could be declared with attribute 'noreturn'}} + test2_positive(); +} -- 2.40.0