[cfe-commits] r172761 - in /cfe/trunk: include/clang/Basic/DiagnosticParseKinds.td include/clang/Basic/DiagnosticSemaKinds.td include/clang/Basic/TokenKinds.def include/clang/Sema/DeclSpec.h lib/Headers/CMakeLists.txt lib/Headers/stdnoreturn.h lib/Parse/ParseDecl.cpp lib/Sema/DeclSpec.cpp lib/Sema/SemaDecl.cpp test/Headers/c11.c test/Parser/c11-noreturn.c test/Sema/return-noreturn.c

Richard Smith richard-llvm at metafoo.co.uk
Thu Jan 17 14:16:12 PST 2013


Author: rsmith
Date: Thu Jan 17 16:16:11 2013
New Revision: 172761

URL: http://llvm.org/viewvc/llvm-project?rev=172761&view=rev
Log:
Parsing support for C11's _Noreturn keyword. No semantics yet.

Added:
    cfe/trunk/lib/Headers/stdnoreturn.h
    cfe/trunk/test/Headers/c11.c
    cfe/trunk/test/Parser/c11-noreturn.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Basic/TokenKinds.def
    cfe/trunk/include/clang/Sema/DeclSpec.h
    cfe/trunk/lib/Headers/CMakeLists.txt
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/lib/Sema/DeclSpec.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/return-noreturn.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Thu Jan 17 16:16:11 2013
@@ -113,6 +113,9 @@
 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<

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Jan 17 16:16:11 2013
@@ -214,6 +214,8 @@
   "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 @@
   "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">;

Modified: cfe/trunk/include/clang/Basic/TokenKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TokenKinds.def?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TokenKinds.def (original)
+++ cfe/trunk/include/clang/Basic/TokenKinds.def Thu Jan 17 16:16:11 2013
@@ -260,6 +260,7 @@
 KEYWORD(_Complex                    , KEYALL)
 KEYWORD(_Generic                    , KEYALL)
 KEYWORD(_Imaginary                  , KEYALL)
+KEYWORD(_Noreturn                   , KEYALL)
 KEYWORD(_Static_assert              , KEYALL)
 KEYWORD(__func__                    , KEYALL)
 KEYWORD(__objc_yes                  , KEYALL)

Modified: cfe/trunk/include/clang/Sema/DeclSpec.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/DeclSpec.h?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/DeclSpec.h (original)
+++ cfe/trunk/include/clang/Sema/DeclSpec.h Thu Jan 17 16:16:11 2013
@@ -325,6 +325,7 @@
   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 @@
   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 @@
       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 @@
   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 @@
     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 @@
   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);

Modified: cfe/trunk/lib/Headers/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/CMakeLists.txt?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/CMakeLists.txt (original)
+++ cfe/trunk/lib/Headers/CMakeLists.txt Thu Jan 17 16:16:11 2013
@@ -27,6 +27,7 @@
   stdbool.h
   stddef.h
   stdint.h
+  stdnoreturn.h
   tgmath.h
   tmmintrin.h
   varargs.h

Added: cfe/trunk/lib/Headers/stdnoreturn.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/stdnoreturn.h?rev=172761&view=auto
==============================================================================
--- cfe/trunk/lib/Headers/stdnoreturn.h (added)
+++ cfe/trunk/lib/Headers/stdnoreturn.h Thu Jan 17 16:16:11 2013
@@ -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 */

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Thu Jan 17 16:16:11 2013
@@ -2619,6 +2619,11 @@
     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 @@
   case tok::kw_inline:
   case tok::kw_virtual:
   case tok::kw_explicit:
+  case tok::kw__Noreturn:
 
     // friend keyword.
   case tok::kw_friend:

Modified: cfe/trunk/lib/Sema/DeclSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/DeclSpec.cpp?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/DeclSpec.cpp (original)
+++ cfe/trunk/lib/Sema/DeclSpec.cpp Thu Jan 17 16:16:11 2013
@@ -329,7 +329,8 @@
   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 @@
   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) {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Jan 17 16:16:11 2013
@@ -4097,6 +4097,10 @@
   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 @@
 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 @@
   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());

Added: cfe/trunk/test/Headers/c11.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Headers/c11.c?rev=172761&view=auto
==============================================================================
--- cfe/trunk/test/Headers/c11.c (added)
+++ cfe/trunk/test/Headers/c11.c Thu Jan 17 16:16:11 2013
@@ -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();

Added: cfe/trunk/test/Parser/c11-noreturn.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/c11-noreturn.c?rev=172761&view=auto
==============================================================================
--- cfe/trunk/test/Parser/c11-noreturn.c (added)
+++ cfe/trunk/test/Parser/c11-noreturn.c Thu Jan 17 16:16:11 2013
@@ -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

Modified: cfe/trunk/test/Sema/return-noreturn.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/return-noreturn.c?rev=172761&r1=172760&r2=172761&view=diff
==============================================================================
--- cfe/trunk/test/Sema/return-noreturn.c (original)
+++ cfe/trunk/test/Sema/return-noreturn.c Thu Jan 17 16:16:11 2013
@@ -35,3 +35,8 @@
 test4() {
   test2_positive();
 }
+
+// FIXME: do not warn here.
+_Noreturn void test5() { // expected-warning {{could be declared with attribute 'noreturn'}}
+  test2_positive();
+}





More information about the cfe-commits mailing list