[PATCH] Fix failure in noexcept when the argument is a call to constexpr function

Karthik Bhat kv.bhat at samsung.com
Fri Nov 8 04:41:20 PST 2013


Hi,
The as per standard 5.3.7 point 3.

The result of the noexcept operator is false in case we have a call to any type of function that does not have non-throwing exception specification, unless it is a constant expression.

Hence for a Code  like - 
  constexpr int f(int i) { return i; }
  static_assert(noexcept(f(42)),""); // should not throw error
the result of noexcept(f(42)) should be true as it is a constant expresssion.

Currently clang fails to compile this code. Fixed the same.

Please let me know if this is good to commit.

Thanks!


http://llvm-reviews.chandlerc.com/D2123

Files:
  test/CXX/except/except.spec/p14.cpp
  lib/Sema/SemaExprCXX.cpp
  include/clang/AST/ExprCXX.h

Index: test/CXX/except/except.spec/p14.cpp
===================================================================
--- test/CXX/except/except.spec/p14.cpp
+++ test/CXX/except/except.spec/p14.cpp
@@ -135,3 +135,12 @@
   };
   static_assert(!noexcept(Derived{X<5>{}}), "");
 }
+
+// The result of noexcept is false when we have call to any type of function that does
+// not have non-throwing exception specification, unless it is a constant expression.
+namespace Constexprnoexcept {
+  constexpr int f(int i) { return i; }
+  static_assert(noexcept(f(42)),""); //no error
+  int j;
+  static_assert(!noexcept(f(j)),"");
+}
Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp
+++ lib/Sema/SemaExprCXX.cpp
@@ -5691,8 +5691,11 @@
 ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand,
                                       SourceLocation RParen) {
   CanThrowResult CanThrow = canThrow(Operand);
+  bool isConstExprFn = isa<CallExpr>(Operand) &&
+                       Operand->isCXX11ConstantExpr(Context);
   return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand,
-                                             CanThrow, KeyLoc, RParen));
+                                             CanThrow, KeyLoc, RParen,
+                                             isConstExprFn));
 }
 
 ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation,
Index: include/clang/AST/ExprCXX.h
===================================================================
--- include/clang/AST/ExprCXX.h
+++ include/clang/AST/ExprCXX.h
@@ -3503,13 +3503,15 @@
 
 public:
   CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val,
-                  SourceLocation Keyword, SourceLocation RParen)
+                  SourceLocation Keyword, SourceLocation RParen,
+                  bool isConstExprFn = false)
     : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary,
            /*TypeDependent*/false,
            /*ValueDependent*/Val == CT_Dependent,
            Val == CT_Dependent || Operand->isInstantiationDependent(),
            Operand->containsUnexpandedParameterPack()),
-      Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen)
+           Value((Val == CT_Cannot || isConstExprFn)),
+           Operand(Operand), Range(Keyword, RParen)
   { }
 
   CXXNoexceptExpr(EmptyShell Empty)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2123.1.patch
Type: text/x-patch
Size: 2416 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131108/00806384/attachment.bin>


More information about the cfe-commits mailing list