Parsing support for C11's _Noreturn keyword. No semantics yet.

llvm-svn: 172761
This commit is contained in:
Richard Smith 2013-01-17 22:16:11 +00:00
parent 01a7fba820
commit 0015f09877
12 changed files with 104 additions and 5 deletions

View File

@ -113,6 +113,9 @@ def note_previous_default_assoc : Note<
def ext_c11_alignment : Extension<
"%0 is a C11-specific feature">, InGroup<C11>;
def ext_c11_noreturn : Extension<
"_Noreturn functions are a C11-specific feature">, InGroup<C11>;
def ext_gnu_indirect_goto : Extension<
"use of GNU indirect-goto extension">, InGroup<GNU>;
def ext_gnu_address_of_label : Extension<

View File

@ -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<IgnoredQualifiers>, 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<Main>;
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<Main>;
def err_constexpr_main : Error<
"'main' is not allowed to be declared constexpr">;
def err_main_template_decl : Error<"'main' cannot be a template">;

View File

@ -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)

View File

@ -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);

View File

@ -27,6 +27,7 @@ set(files
stdbool.h
stddef.h
stdint.h
stdnoreturn.h
tgmath.h
tmmintrin.h
varargs.h

View File

@ -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 */

View File

@ -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:

View File

@ -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) {

View File

@ -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());

12
clang/test/Headers/c11.c Normal file
View File

@ -0,0 +1,12 @@
// RUN: %clang -fsyntax-only -Xclang -verify -std=c11 %s
noreturn int f(); // expected-error 1+{{}}
#include <stdnoreturn.h>
#include <stdnoreturn.h>
#include <stdnoreturn.h>
int g();
noreturn int g();
int noreturn g();
int g();

View File

@ -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

View File

@ -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();
}