[cfe-commits] r126166 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaType.cpp test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p2.cpp test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p5.cpp test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp

Richard Smith richard-llvm at metafoo.co.uk
Mon Feb 21 16:36:53 PST 2011


Author: rsmith
Date: Mon Feb 21 18:36:53 2011
New Revision: 126166

URL: http://llvm.org/viewvc/llvm-project?rev=126166&view=rev
Log:
Fix a few auto-related issues:

 * 'auto' was being rejected on abstract-declarators with trailing return
types and on typedefs with trailing return types. 'auto' is always
allowed in these cases. This was found while testing the fix for PR 9278.

 * A very poor diagnostic was being issued for auto (f() -> int): "return
type must be 'auto', not 'auto'". This is closely related to PR 9060.

 * Trailing return type handling was happening slightly too late,
resulting in the checks for functions returning arrays and functions
returning functions being missed.

Added:
    cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p2.cpp
    cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp
    cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p5.cpp
    cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=126166&r1=126165&r2=126166&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Feb 21 18:36:53 2011
@@ -918,6 +918,8 @@
   "'auto' return without trailing return type">;
 def err_trailing_return_without_auto : Error<
   "function with trailing return type must specify return type 'auto', not %0">;
+def err_trailing_return_in_parens : Error<
+  "trailing return type may not be nested within parentheses">;
 def err_auto_var_deduction_failure : Error<
   "variable %0 with type %1 has incompatible initializer of type %2">;
 def err_auto_new_deduction_failure : Error<

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=126166&r1=126165&r2=126166&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Mon Feb 21 18:36:53 2011
@@ -1450,6 +1450,7 @@
   if (D.getAttributes())
     distributeTypeAttrsFromDeclarator(state, T);
 
+  // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
   if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
       !D.isFunctionDeclarator()) {
     int Error = -1;
@@ -1495,6 +1496,25 @@
     if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
       Error = 8;
 
+    // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
+    // contains a trailing return type. That is only legal at the outermost
+    // level. Check all declarator chunks (outermost first) anyway, to give
+    // better diagnostics.
+    if (Error != -1) {
+      for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
+        unsigned chunkIndex = e - i - 1;
+        state.setCurrentChunkIndex(chunkIndex);
+        DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
+        if (DeclType.Kind == DeclaratorChunk::Function) {
+          const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
+          if (FTI.TrailingReturnType) {
+            Error = -1;
+            break;
+          }
+        }
+      }
+    }
+
     if (Error != -1) {
       Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
         << Error;
@@ -1502,7 +1522,7 @@
       D.setInvalidType(true);
     }
   }
-
+  
   if (T.isNull())
     return Context.getNullTypeSourceInfo();
 
@@ -1603,21 +1623,6 @@
       // of the type, otherwise the argument list is ().
       const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
 
-      // C99 6.7.5.3p1: The return type may not be a function or array type.
-      // For conversion functions, we'll diagnose this particular error later.
-      if ((T->isArrayType() || T->isFunctionType()) &&
-          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
-        unsigned diagID = diag::err_func_returning_array_function;
-        // Last processing chunk in block context means this function chunk
-        // represents the block.
-        if (chunkIndex == 0 &&
-            D.getContext() == Declarator::BlockLiteralContext)
-          diagID = diag::err_block_returning_array_function;
-        Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
-        T = Context.IntTy;
-        D.setInvalidType(true);
-      }
-
       // Check for auto functions and trailing return type and adjust the
       // return type accordingly.
       if (!D.isInvalidType()) {
@@ -1630,8 +1635,13 @@
           T = Context.IntTy;
           D.setInvalidType(true);
         } else if (FTI.TrailingReturnType) {
-          if (T.hasQualifiers() || !isa<AutoType>(T)) {
-            // T must be exactly 'auto' at this point. See CWG issue 681.
+          // T must be exactly 'auto' at this point. See CWG issue 681.
+          if (isa<ParenType>(T)) {
+            Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
+                 diag::err_trailing_return_in_parens)
+              << T << D.getDeclSpec().getSourceRange();
+            D.setInvalidType(true);
+          } else if (T.hasQualifiers() || !isa<AutoType>(T)) {
             Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
                  diag::err_trailing_return_without_auto)
               << T << D.getDeclSpec().getSourceRange();
@@ -1644,6 +1654,21 @@
         }
       }
 
+      // C99 6.7.5.3p1: The return type may not be a function or array type.
+      // For conversion functions, we'll diagnose this particular error later.
+      if ((T->isArrayType() || T->isFunctionType()) &&
+          (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
+        unsigned diagID = diag::err_func_returning_array_function;
+        // Last processing chunk in block context means this function chunk
+        // represents the block.
+        if (chunkIndex == 0 &&
+            D.getContext() == Declarator::BlockLiteralContext)
+          diagID = diag::err_block_returning_array_function;
+        Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
+        T = Context.IntTy;
+        D.setInvalidType(true);
+      }
+
       // cv-qualifiers on return types are pointless except when the type is a
       // class type in C++.
       if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
@@ -2050,7 +2075,7 @@
       break;
     }
   }
-  
+
   if (T.isNull())
     return Context.getNullTypeSourceInfo();
   else if (D.isInvalidType())

Added: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p2.cpp?rev=126166&view=auto
==============================================================================
--- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p2.cpp (added)
+++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p2.cpp Mon Feb 21 18:36:53 2011
@@ -0,0 +1,49 @@
+// RUN: %clang_cc1 -fexceptions -fsyntax-only -verify %s -std=c++0x
+
+struct S {
+  virtual ~S();
+
+  void g() throw (auto(*)()->int);
+
+  // Note, this is not permitted: conversion-declarator cannot have a trailing return type.
+  // FIXME: don't issue the second diagnostic for this.
+  operator auto(*)()->int(); // expected-error{{'auto' not allowed here}} expected-error {{C++ requires a type specifier}}
+};
+
+typedef auto Fun(int a) -> decltype(a + a);
+typedef auto (*PFun)(int a) -> decltype(a + a);
+
+void g(auto (*f)() -> int) {
+  try { }
+  catch (auto (&f)() -> int) { }
+  catch (auto (*const f[10])() -> int) { }
+}
+
+namespace std {
+  class type_info;
+}
+
+template<typename T> struct U {};
+
+void j() {
+  (void)typeid(auto(*)()->void);
+  (void)sizeof(auto(*)()->void);
+  (void)__alignof(auto(*)()->void);
+
+  U<auto(*)()->void> v;
+
+  int n;
+  (void)static_cast<auto(*)()->void>(&j);
+  auto p = reinterpret_cast<auto(*)()->int>(&j);
+  (void)const_cast<auto(**)()->int>(&p);
+  (void)(auto(*)()->void)(&j);
+}
+
+template <auto (*f)() -> void = &j> class C { };
+struct F : auto(*)()->int {}; // expected-error{{expected class name}}
+template<typename T = auto(*)()->int> struct G { };
+
+int g();
+auto (*h)() -> auto = &g; // expected-error{{'auto' not allowed here}}
+auto (*i)() = &g; // ok; auto deduced as int.
+auto (*k)() -> int = i; // ok; no deduction.

Modified: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p5.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p5.cpp?rev=126166&r1=126165&r2=126166&view=diff
==============================================================================
--- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p5.cpp (original)
+++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p5.cpp Mon Feb 21 18:36:53 2011
@@ -16,6 +16,7 @@
 
 // PR 9278: auto is not allowed in typedefs, except with a trailing return type.
 typedef auto *AutoPtr; // expected-error{{'auto' not allowed in typedef}}
+typedef auto (*PFun)(int a); // expected-error{{'auto' not allowed in typedef}}
 typedef auto Fun(int a) -> decltype(a + a);
 
 void g(auto a) { // expected-error{{'auto' not allowed in function prototype}}
@@ -64,13 +65,5 @@
 
 using A = auto; // expected-error{{expected ';'}} expected-error{{requires a qualified name}}
 
-// Whether this is illegal depends on the interpretation of [decl.spec.auto]p2 and p3,
-// and in particular the "Otherwise, ..." at the start of p3.
-namespace TrailingReturnType {
-  // FIXME: don't issue the second diagnostic for this error.
-  auto f() -> auto; // expected-error{{'auto' not allowed here}} unexpected-error{{without trailing return type}}
-  int g();
-  auto (*h)() -> auto = &g; // expected-error{{'auto' not allowed here}}
-  auto (*i)() = &g; // ok; auto deduced as int.
-  auto (*j)() -> int = i; // ok; no deduction.
-}
+// FIXME: don't issue the second diagnostic for this error.
+auto k() -> auto; // expected-error{{'auto' not allowed here}} unexpected-error{{without trailing return type}}

Modified: cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp?rev=126166&r1=126165&r2=126166&view=diff
==============================================================================
--- cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp (original)
+++ cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp Mon Feb 21 18:36:53 2011
@@ -3,3 +3,5 @@
 auto a() -> int; // ok
 const auto b() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'auto const'}}
 auto *c() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'auto *'}}
+auto (d() -> int); // expected-error {{trailing return type may not be nested within parentheses}}
+auto e() -> auto (*)() -> auto (*)() -> void; // ok: same as void (*(*e())())();

Added: cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp?rev=126166&view=auto
==============================================================================
--- cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp (added)
+++ cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p8-0x.cpp Mon Feb 21 18:36:53 2011
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s
+
+auto f() -> int[32]; // expected-error{{function cannot return array}}
+auto g() -> int(int); // expected-error{{function cannot return function}}
+auto h() -> auto() -> int; // expected-error{{function cannot return function}}
+auto i() -> auto(*)() -> int;

Added: cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp?rev=126166&view=auto
==============================================================================
--- cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp (added)
+++ cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p9-0x.cpp Mon Feb 21 18:36:53 2011
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s
+
+auto j() -> enum { e3 }; // expected-error{{can not be defined in a type specifier}}





More information about the cfe-commits mailing list