<br><br><div class="gmail_quote">Le 18 janvier 2012 04:06, Richard Smith <span dir="ltr"><<a href="mailto:richard-llvm@metafoo.co.uk">richard-llvm@metafoo.co.uk</a>></span> a écrit :<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: rsmith<br>
Date: Tue Jan 17 21:06:12 2012<br>
New Revision: 148374<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=148374&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=148374&view=rev</a><br>
Log:<br>
A call to strlen is not a constant expression, even if we're treating it as a<br>
builtin.<br>
<br>
Added:<br>
    cfe/trunk/test/SemaCXX/constexpr-strlen.cpp<br>
Modified:<br>
    cfe/trunk/lib/AST/ExprConstant.cpp<br>
<br>
Modified: cfe/trunk/lib/AST/ExprConstant.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=148374&r1=148373&r2=148374&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=148374&r1=148373&r2=148374&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)<br>
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Jan 17 21:06:12 2012<br>
@@ -3891,8 +3891,15 @@<br>
<br>
   case Builtin::BI__builtin_expect:<br>
     return Visit(E->getArg(0));<br>
-<br>
+<br>
   case Builtin::BIstrlen:<br>
+    // A call to strlen is not a constant expression.<br>
+    if (Info.getLangOpts().CPlusPlus0x)<br>
+      Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function)<br>
+        << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";<br>
+    else<br>
+      Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr);<br>
+    // Fall through.<br>
   case Builtin::BI__builtin_strlen:<br>
     // As an extension, we support strlen() and __builtin_strlen() as constant<br>
     // expressions when the argument is a string literal.<br>
<br>
Added: cfe/trunk/test/SemaCXX/constexpr-strlen.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constexpr-strlen.cpp?rev=148374&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constexpr-strlen.cpp?rev=148374&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/test/SemaCXX/constexpr-strlen.cpp (added)<br>
+++ cfe/trunk/test/SemaCXX/constexpr-strlen.cpp Tue Jan 17 21:06:12 2012<br>
@@ -0,0 +1,15 @@<br>
+// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify -pedantic<br>
+<br>
+# 1 "/usr/include/string.h" 1 3 4<br>
+extern "C" {<br>
+  typedef decltype(sizeof(int)) size_t;<br>
+  extern size_t strlen(const char *p);<br>
+}<br>
+<br>
+# 10 "SemaCXX/constexpr-strlen.cpp" 2<br>
+constexpr int n = __builtin_strlen("hello"); // ok<br>
+constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}}<br>
+<br>
+// Make sure we can evaluate a call to strlen.<br>
+int arr[3]; // expected-note {{here}}<br>
+int k = arr[strlen("hello")]; // expected-warning {{array index 5}}<br>
<br>
<br></blockquote><div>Ah bummer.<br><br>Do you know if the same apply (I would guess so) to std::strlen ?<br><br>It seems a pity that such a trivial function could not be constexpr as making it constexpr is actually dead simple. I wonder if it would be worth a Defect Report.<br>
<br>-- Matthieu<br> </div></div>