[cfe-commits] r148374 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/SemaCXX/constexpr-strlen.cpp
Matthieu Monrocq
matthieu.monrocq at gmail.com
Wed Jan 18 12:36:46 PST 2012
Le 18 janvier 2012 04:06, Richard Smith <richard-llvm at metafoo.co.uk> a
écrit :
> Author: rsmith
> Date: Tue Jan 17 21:06:12 2012
> New Revision: 148374
>
> URL: http://llvm.org/viewvc/llvm-project?rev=148374&view=rev
> Log:
> A call to strlen is not a constant expression, even if we're treating it
> as a
> builtin.
>
> Added:
> cfe/trunk/test/SemaCXX/constexpr-strlen.cpp
> Modified:
> cfe/trunk/lib/AST/ExprConstant.cpp
>
> Modified: cfe/trunk/lib/AST/ExprConstant.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=148374&r1=148373&r2=148374&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/AST/ExprConstant.cpp (original)
> +++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Jan 17 21:06:12 2012
> @@ -3891,8 +3891,15 @@
>
> case Builtin::BI__builtin_expect:
> return Visit(E->getArg(0));
> -
> +
> case Builtin::BIstrlen:
> + // A call to strlen is not a constant expression.
> + if (Info.getLangOpts().CPlusPlus0x)
> + Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function)
> + << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
> + else
> + Info.CCEDiag(E->getExprLoc(),
> diag::note_invalid_subexpr_in_const_expr);
> + // Fall through.
> case Builtin::BI__builtin_strlen:
> // As an extension, we support strlen() and __builtin_strlen() as
> constant
> // expressions when the argument is a string literal.
>
> Added: cfe/trunk/test/SemaCXX/constexpr-strlen.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constexpr-strlen.cpp?rev=148374&view=auto
>
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/constexpr-strlen.cpp (added)
> +++ cfe/trunk/test/SemaCXX/constexpr-strlen.cpp Tue Jan 17 21:06:12 2012
> @@ -0,0 +1,15 @@
> +// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify -pedantic
> +
> +# 1 "/usr/include/string.h" 1 3 4
> +extern "C" {
> + typedef decltype(sizeof(int)) size_t;
> + extern size_t strlen(const char *p);
> +}
> +
> +# 10 "SemaCXX/constexpr-strlen.cpp" 2
> +constexpr int n = __builtin_strlen("hello"); // ok
> +constexpr int m = strlen("hello"); // expected-error {{constant
> expression}} expected-note {{non-constexpr function 'strlen' cannot be used
> in a constant expression}}
> +
> +// Make sure we can evaluate a call to strlen.
> +int arr[3]; // expected-note {{here}}
> +int k = arr[strlen("hello")]; // expected-warning {{array index 5}}
>
>
> Ah bummer.
Do you know if the same apply (I would guess so) to std::strlen ?
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.
-- Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120118/4dd1a3cd/attachment.html>
More information about the cfe-commits
mailing list