summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/include/clang/Basic/DiagnosticParseKinds.td3
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td6
-rw-r--r--clang/include/clang/Basic/TokenKinds.def1
-rw-r--r--clang/include/clang/Sema/DeclSpec.h10
-rw-r--r--clang/lib/Headers/CMakeLists.txt1
-rw-r--r--clang/lib/Headers/stdnoreturn.h30
-rw-r--r--clang/lib/Parse/ParseDecl.cpp6
-rw-r--r--clang/lib/Sema/DeclSpec.cpp10
-rw-r--r--clang/lib/Sema/SemaDecl.cpp11
-rw-r--r--clang/test/Headers/c11.c12
-rw-r--r--clang/test/Parser/c11-noreturn.c14
-rw-r--r--clang/test/Sema/return-noreturn.c5
12 files changed, 104 insertions, 5 deletions
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 963263bebd7..4f8c4279779 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/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<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<
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 81212e661c8..328bebe9af4 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/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<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">;
diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def
index cba9898c01a..b4c0b2d97fe 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/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/clang/include/clang/Sema/DeclSpec.h b/clang/include/clang/Sema/DeclSpec.h
index d2b4a7ce306..b232d34c40d 100644
--- a/clang/include/clang/Sema/DeclSpec.h
+++ b/clang/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/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt
index 25e4d903bb7..ae689c37fae 100644
--- a/clang/lib/Headers/CMakeLists.txt
+++ b/clang/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/clang/lib/Headers/stdnoreturn.h b/clang/lib/Headers/stdnoreturn.h
new file mode 100644
index 00000000000..a7a301d7e0b
--- /dev/null
+++ b/clang/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/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 74769bf7319..f9c68c79b9c 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/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/clang/lib/Sema/DeclSpec.cpp b/clang/lib/Sema/DeclSpec.cpp
index fa63c2af38f..35b0736196a 100644
--- a/clang/lib/Sema/DeclSpec.cpp
+++ b/clang/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/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a13d8b3b462..9cf5e73665a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/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/clang/test/Headers/c11.c b/clang/test/Headers/c11.c
new file mode 100644
index 00000000000..1a3d4d41fe8
--- /dev/null
+++ b/clang/test/Headers/c11.c
@@ -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();
diff --git a/clang/test/Parser/c11-noreturn.c b/clang/test/Parser/c11-noreturn.c
new file mode 100644
index 00000000000..7a2fe50f88a
--- /dev/null
+++ b/clang/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/clang/test/Sema/return-noreturn.c b/clang/test/Sema/return-noreturn.c
index 448fce77cd8..5dd6693373a 100644
--- a/clang/test/Sema/return-noreturn.c
+++ b/clang/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();
+}
OpenPOWER on IntegriCloud